Mercurial > lcfOS
comparison python/other/qtpropertyviewer.py @ 290:7b38782ed496
File moves
author | Windel Bouwman |
---|---|
date | Sun, 24 Nov 2013 11:24:15 +0100 |
parents | python/qtpropertyviewer.py@ad14c7c52589 |
children |
comparison
equal
deleted
inserted
replaced
289:bd2593de3ff8 | 290:7b38782ed496 |
---|---|
1 from PyQt4.QtCore import * | |
2 from PyQt4.QtGui import * | |
3 import sys | |
4 | |
5 class QtPropertyModel(QAbstractTableModel): | |
6 def __init__(self): | |
7 super().__init__() | |
8 self.InspectedWidget = None | |
9 def getInspectedWidget(self): | |
10 return self.inspectedWidget | |
11 def setInspectedWidget(self, w): | |
12 self.inspectedWidget = w | |
13 if isinstance(self.inspectedWidget, QObject): | |
14 mo = self.inspectedWidget.metaObject() | |
15 self.metaprops = [mo.property(i) for i in range(mo.propertyCount())] | |
16 self.metaprops = sorted(self.metaprops, key=lambda mp: mp.name()) | |
17 else: | |
18 self.metaprops = [] | |
19 self.modelReset.emit() | |
20 InspectedWidget = property(getInspectedWidget, setInspectedWidget) | |
21 def rowCount(self, parent): | |
22 return len(self.metaprops) | |
23 def columnCount(self, parent): | |
24 return 2 | |
25 def headerData(self, section, orientation, role): | |
26 if orientation == Qt.Horizontal and role == Qt.DisplayRole: | |
27 if section == 0: | |
28 return 'Property' | |
29 elif section == 1: | |
30 return 'Value' | |
31 def data(self, index, role): | |
32 if index.isValid(): | |
33 row = index.row() | |
34 col = index.column() | |
35 metaprop = self.metaprops[row] | |
36 if role == Qt.DisplayRole: | |
37 propname = metaprop.name() | |
38 if col == 0: | |
39 return propname | |
40 elif col == 1: | |
41 if metaprop.isReadable(): | |
42 propval = metaprop.read(self.inspectedWidget) | |
43 return propval | |
44 return 'NOT READABLE' | |
45 return '{0}, {1}'.format(row, col) | |
46 elif role == Qt.BackgroundRole: | |
47 if col == 1: | |
48 if metaprop.isWritable(): | |
49 return QBrush(Qt.green) | |
50 else: | |
51 return QBrush(Qt.red) | |
52 elif role == Qt.CheckStateRole: | |
53 if col == 1: | |
54 val = metaprop.read(self.inspectedWidget) | |
55 if type(val) is bool: | |
56 val = Qt.Checked if val else Qt.Unchecked | |
57 return val | |
58 | |
59 def flags(self, index): | |
60 if index.isValid(): | |
61 row = index.row() | |
62 col = index.column() | |
63 metaprop = self.metaprops[row] | |
64 if col == 1 and metaprop.isWritable(): | |
65 val = metaprop.read(self.inspectedWidget) | |
66 if type(val) == bool: | |
67 return super().flags(index) | Qt.ItemIsEditable | Qt.ItemIsUserCheckable | |
68 else: | |
69 return super().flags(index) | Qt.ItemIsEditable | |
70 else: | |
71 return super().flags(index) | |
72 def setData(self, index, value, role): | |
73 if index.isValid(): | |
74 row = index.row() | |
75 col = index.column() | |
76 metaprop = self.metaprops[row] | |
77 if role == Qt.EditRole: | |
78 metaprop.write(self.inspectedWidget, value) | |
79 return True | |
80 elif role == Qt.CheckStateRole: | |
81 value = True if value == Qt.Checked else False | |
82 metaprop.write(self.inspectedWidget, value) | |
83 return True | |
84 else: | |
85 print('unknown role:', role) | |
86 return False | |
87 return False | |
88 | |
89 class QtPropertyViewer(QWidget): | |
90 """ | |
91 The binviewer consists of a hex view, a ascii view | |
92 and perhaps others.. | |
93 """ | |
94 def __init__(self): | |
95 super().__init__() | |
96 tableView = QTableView() | |
97 l = QVBoxLayout(self) | |
98 l.addWidget(tableView) | |
99 header = tableView.horizontalHeader() | |
100 header.setResizeMode(QHeaderView.ResizeToContents) | |
101 self.propertyModel = QtPropertyModel() | |
102 tableView.setModel(self.propertyModel) | |
103 |