1from __future__ import absolute_import
2
3import sys
4
5import pytest
6from qtpy import PYSIDE, PYSIDE2, PYQT4
7from qtpy.QtWidgets import QApplication
8from qtpy.QtWidgets import QHeaderView
9from qtpy.QtCore import Qt
10from qtpy.QtCore import QAbstractListModel
11
12
13PY3 = sys.version[0] == "3"
14
15
16def get_qapp(icon_path=None):
17    qapp = QApplication.instance()
18    if qapp is None:
19        qapp = QApplication([''])
20    return qapp
21
22
23@pytest.mark.skipif(PY3 or PYSIDE2, reason="It fails on Python 3 and PySide2")
24def test_patched_qheaderview():
25    """
26    This will test whether QHeaderView has the new methods introduced in Qt5.
27    It will then create an instance of QHeaderView and test that no exceptions
28    are raised and that some basic behaviour works.
29    """
30    assert QHeaderView.sectionsClickable is not None
31    assert QHeaderView.sectionsMovable is not None
32    assert QHeaderView.sectionResizeMode is not None
33    assert QHeaderView.setSectionsClickable is not None
34    assert QHeaderView.setSectionsMovable is not None
35    assert QHeaderView.setSectionResizeMode is not None
36
37    # setup a model and add it to a headerview
38    qapp = get_qapp()
39    headerview = QHeaderView(Qt.Horizontal)
40    class Model(QAbstractListModel):
41        pass
42    model = Model()
43    headerview.setModel(model)
44    assert headerview.count() == 1
45
46    # test it
47    assert isinstance(headerview.sectionsClickable(), bool)
48    assert isinstance(headerview.sectionsMovable(), bool)
49    if PYSIDE:
50        assert isinstance(headerview.sectionResizeMode(0),
51                          QHeaderView.ResizeMode)
52    else:
53        assert isinstance(headerview.sectionResizeMode(0), int)
54
55    headerview.setSectionsClickable(True)
56    assert headerview.sectionsClickable() == True
57    headerview.setSectionsClickable(False)
58    assert headerview.sectionsClickable() == False
59
60    headerview.setSectionsMovable(True)
61    assert headerview.sectionsMovable() == True
62    headerview.setSectionsMovable(False)
63    assert headerview.sectionsMovable() == False
64
65    headerview.setSectionResizeMode(QHeaderView.Interactive)
66    assert headerview.sectionResizeMode(0) == QHeaderView.Interactive
67    headerview.setSectionResizeMode(QHeaderView.Fixed)
68    assert headerview.sectionResizeMode(0) == QHeaderView.Fixed
69    headerview.setSectionResizeMode(QHeaderView.Stretch)
70    assert headerview.sectionResizeMode(0) == QHeaderView.Stretch
71    headerview.setSectionResizeMode(QHeaderView.ResizeToContents)
72    assert headerview.sectionResizeMode(0) == QHeaderView.ResizeToContents
73
74    headerview.setSectionResizeMode(0, QHeaderView.Interactive)
75    assert headerview.sectionResizeMode(0) == QHeaderView.Interactive
76    headerview.setSectionResizeMode(0, QHeaderView.Fixed)
77    assert headerview.sectionResizeMode(0) == QHeaderView.Fixed
78    headerview.setSectionResizeMode(0, QHeaderView.Stretch)
79    assert headerview.sectionResizeMode(0) == QHeaderView.Stretch
80    headerview.setSectionResizeMode(0, QHeaderView.ResizeToContents)
81    assert headerview.sectionResizeMode(0) == QHeaderView.ResizeToContents
82
83    # test that the old methods in Qt4 raise exceptions
84    if PYQT4 or PYSIDE:
85        with pytest.warns(UserWarning):
86            headerview.isClickable()
87        with pytest.warns(UserWarning):
88            headerview.isMovable()
89        with pytest.warns(UserWarning):
90            headerview.resizeMode(0)
91        with pytest.warns(UserWarning):
92            headerview.setClickable(True)
93        with pytest.warns(UserWarning):
94            headerview.setMovable(True)
95        with pytest.warns(UserWarning):
96            headerview.setResizeMode(0, QHeaderView.Interactive)
97
98
99