eric7/Unittest/UTTestResultsTree.py

branch
unittest
changeset 9059
e7fd342f8bfc
child 9062
7f27bf3b50c3
equal deleted inserted replaced
9057:ddc46e93ccc4 9059:e7fd342f8bfc
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a tree view and associated model to show the test result
8 data.
9 """
10
11 from PyQt6.QtCore import (
12 pyqtSignal, pyqtSlot, Qt, QAbstractItemModel, QCoreApplication, QModelIndex
13 )
14 from PyQt6.QtWidgets import QTreeView
15
16 TopLevelId = 2 ** 32 - 1
17
18
19 class TestResultsModel(QAbstractItemModel):
20 """
21 Class implementing the item model containing the test data.
22 """
23 Headers = [
24 QCoreApplication.translate("TestResultsModel", "Status"),
25 QCoreApplication.translate("TestResultsModel", "Name"),
26 QCoreApplication.translate("TestResultsModel", "Message"),
27 QCoreApplication.translate("TestResultsModel", "Duration (ms)"),
28 ]
29
30 def __init__(self, parent=None):
31 """
32 Constructor
33
34 @param parent reference to the parent object (defaults to None)
35 @type QObject (optional)
36 """
37 super().__init__(parent)
38
39 self.__testResults = []
40
41 def headerData(self, section, orientation,
42 role=Qt.ItemDataRole.DisplayRole):
43 """
44 Public method to get the header string for the various sections.
45
46 @param section section number
47 @type int
48 @param orientation orientation of the header
49 @type Qt.Orientation
50 @param role data role (defaults to Qt.ItemDataRole.DisplayRole)
51 @type Qt.ItemDataRole (optional)
52 @return header string of the section
53 @rtype str
54 """
55 if (
56 orientation == Qt.Orientation.Horizontal and
57 role == Qt.ItemDataRole.DisplayRole
58 ):
59 return TestResultsModel.Headers[section]
60 else:
61 return None
62
63 def rowCount(self, parent=QModelIndex()):
64 """
65 Public method to get the number of row for a given parent index.
66
67 @param parent index of the parent item (defaults to QModelIndex())
68 @type QModelIndex (optional)
69 @return number of rows
70 @rtype int
71 """
72 if not parent.isValid():
73 return len(self.__testResults)
74
75 if parent.internalId() == TopLevelId and parent.column() == 0:
76 return len(self.__testResults[parent.row()].extra)
77
78 return 0
79
80 def columnCount(self, parent=QModelIndex()):
81 """
82 Public method to get the number of columns.
83
84 @param parent index of the parent item (defaults to QModelIndex())
85 @type QModelIndex (optional)
86 @return number of columns
87 @rtype int
88 """
89 if not parent.isValid():
90 return len(TestResultsModel.Headers)
91 else:
92 return 1
93
94 def clear(self):
95 """
96 Public method to clear the model data.
97 """
98 self.beginResetModel()
99 self.__testResults.clear()
100 self.endResetModel()
101
102
103 class TestResultsTreeView(QTreeView):
104 """
105 Class implementing a tree view to show the test result data.
106
107 @signal goto(str, int) emitted to go to the position given by file name
108 and line number
109 """
110 goto = pyqtSignal(str, int)
111
112 def __init__(self, parent=None):
113 """
114 Constructor
115
116 @param parent reference to the parent widget (defaults to None)
117 @type QWidget (optional)
118 """
119 super().__init__(parent)
120
121 self.setItemsExpandable(True)
122 self.setExpandsOnDoubleClick(False)
123 self.setSortingEnabled(True)
124
125 self.header().setDefaultAlignment(Qt.AlignmentFlag.AlignCenter)
126 self.header().setSortIndicatorShown(False)
127
128 # connect signals and slots
129 self.doubleClicked.connect(self.__gotoTestDefinition)
130
131 self.header().sortIndicatorChanged.connect(self.sortByColumn)
132 self.header().sortIndicatorChanged.connect(
133 lambda column, order: self.header().setSortIndicatorShown(True))
134
135 @pyqtSlot(QModelIndex)
136 def __gotoTestDefinition(self, index):
137 """
138 Private slot to show the test definition.
139
140 @param index index for the double-clicked item
141 @type QModelIndex
142 """
143 # TODO: not implemented yet
144 pass
145
146 #
147 # eflag: noqa = M822

eric ide

mercurial