annotate python/other/qtpropertyviewer.py @ 303:be7f60545368

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