-
Notifications
You must be signed in to change notification settings - Fork 4
/
widgetinfo.py
251 lines (189 loc) · 7.71 KB
/
widgetinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from ts3plugin import ts3plugin
from PythonQt.QtGui import QApplication, QCursor, QDialog, QSplitter, QTreeView, QTableView, QHBoxLayout, QVBoxLayout, QCheckBox, QWidget, QItemSelectionModel, QMenu
from PythonQt.QtCore import Qt, QAbstractItemModel, QModelIndex
import ts3defines
import traceback
class PropertyModel(QAbstractItemModel):
def __init__(self, parent=None):
super(QAbstractItemModel, self).__init__(parent)
self.widget = None
def setWidget(self, widg):
self.layoutAboutToBeChanged()
self.widget = widg
self.layoutChanged()
def index(self, row, column, parent):
if parent.isValid():
return QAbstractItemModel.index(self, row, column, parent)
return self.createIndex(row, column)
def parent(self, index):
return QModelIndex()
def rowCount(self, parent):
if parent.isValid() or self.widget is None:
return 0
else:
return self.widget.metaObject().propertyCount()
def columnCount(self, parent):
if parent.isValid():
return 0
else:
return 2
def data(self, index, role):
if not index.isValid() or role != Qt.DisplayRole:
return None
if index.column() == 0:
return self.widget.metaObject().property(index.row()).name()
else:
return self.widget.metaObject().property(index.row()).read(self.widget)
def headerData(self, section, orientation, role):
if orientation != Qt.Horizontal or role != Qt.DisplayRole:
return None
if section == 0:
return "Property"
else:
return "Value"
class WidgetModel(QAbstractItemModel):
def __init__(self, parent=None):
super(QAbstractItemModel, self).__init__(parent)
def index(self, row, column, parent):
if not parent.isValid():
if len(QApplication.topLevelWidgets()) <= row:
return QModelIndex()
else:
return self.createIndex(row, column, QApplication.topLevelWidgets()[row])
else:
if len(parent.internalPointer().children()) <= row:
return QModelIndex()
else:
return self.createIndex(row, column, parent.internalPointer().children()[row])
def parent(self, index):
if not index.isValid():
return QModelIndex()
obj = index.internalPointer()
tlw = QApplication.topLevelWidgets()
if obj in tlw:
return QModelIndex()
else:
if obj.parent() in tlw:
return self.createIndex(tlw.index(obj.parent()), 0, obj.parent())
else:
return self.createIndex(obj.parent().children().index(obj), 0, obj.parent())
def rowCount(self, parent):
if not parent.isValid():
return len(QApplication.topLevelWidgets())
else:
return len(parent.internalPointer().children())
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid() or role != Qt.DisplayRole:
return None
obj = index.internalPointer()
if obj.objectName == "":
return "Class: %s" % obj.className()
else:
return "%s (Class: %s)" % (obj.objectName, obj.className())
class InfoDialog(QDialog):
def __init__(self):
super(QDialog, self).__init__(None)
self.setAttribute(Qt.WA_DeleteOnClose)
self.splitter = QSplitter(Qt.Horizontal, self)
self.leftwidg = QWidget(self)
self.vlayout = QVBoxLayout(self.leftwidg)
self.checkbox = QCheckBox("Mark selection", self)
self.tree = QTreeView(self)
self.tree.header().hide()
self.vlayout.addWidget(self.checkbox)
self.vlayout.addWidget(self.tree)
self.table = QTableView(self)
self.table.horizontalHeader().setStretchLastSection(True)
self.splitter.addWidget(self.leftwidg)
self.splitter.addWidget(self.table)
self.hlayout = QHBoxLayout(self)
self.hlayout.addWidget(self.splitter)
self.treemodel = WidgetModel(self.tree)
self.tree.setModel(self.treemodel)
self.tablemodel = PropertyModel(self.table)
self.table.setModel(self.tablemodel)
self.stylesheet = None
self.connect("finished(int)", self.onClosed)
self.tree.selectionModel().connect("currentChanged(const QModelIndex&, const QModelIndex&)", self.onTreeSelectionChanged)
self.tree.connect("doubleClicked(const QModelIndex&)", self.onTreeDoubleClicked)
self.checkbox.connect("toggled(bool)", self.onCheckBoxClicked)
self.resize(800, 500)
def onClosed(self):
if self.checkbox.isChecked() and self.stylesheet is not None:
index = self.tree.selectionModel().currentIndex
if index.isValid():
obj = index.internalPointer()
if hasattr(obj, "setStyleSheet"):
obj.setStyleSheet(self.stylesheet)
def setWidget(self, widg):
self.widget = widg
tlw = QApplication.topLevelWidgets()
if widg in tlw:
index = self.treemodel.createIndex(tlw.index(widg), 0, widg)
else:
index = self.treemodel.createIndex(widg.parent().children().index(widg), 0, widg)
self.tree.selectionModel().select(index, QItemSelectionModel.ClearAndSelect)
self.tree.scrollTo(index)
while index.isValid():
self.tree.expand(index)
index = index.parent()
self.tablemodel.setWidget(widg)
def onTreeDoubleClicked(self, index):
obj = index.internalPointer()
if obj.inherits("QMenu"):
obj.popup(QCursor.pos())
def onTreeSelectionChanged(self, cur, prev):
obj = cur.internalPointer()
self.tablemodel.setWidget(obj)
if self.checkbox.isChecked():
if prev.isValid() and self.stylesheet is not None:
oldobj = prev.internalPointer()
if hasattr(oldobj, "setStyleSheet"):
oldobj.setStyleSheet(self.stylesheet)
if hasattr(obj, "setStyleSheet"):
self.stylesheet = obj.styleSheet
obj.setStyleSheet("background: red;")
def onCheckBoxClicked(self, act):
index = self.tree.selectionModel().currentIndex
if not index.isValid():
return
obj = index.internalPointer()
if not hasattr(obj, "setStyleSheet"):
return
if act:
self.stylesheet = obj.styleSheet
obj.setStyleSheet("background: red;")
elif self.stylesheet is not None:
obj.setStyleSheet(self.stylesheet)
class widgetinfo(ts3plugin):
name = "widgetinfo"
requestAutoload = False
version = "1.0.1"
apiVersion = 21
author = "Thomas \"PLuS\" Pathmann"
description = "Show information of the client's ui elements"
offersConfigure = False
commandKeyword = ""
infoTitle = None
menuItems = [(ts3defines.PluginMenuType.PLUGIN_MENU_TYPE_GLOBAL, 0, "Widget info", "icon.png")]
hotkeys = [("info", "Show widget info")]
def __init__(self):
self.dlg = None
def stop(self):
pass
def showInfo(self, widg=None):
if not self.dlg:
self.dlg = InfoDialog()
if widg is not None:
self.dlg.setWidget(widg)
self.dlg.show()
self.dlg.raise_()
self.dlg.activateWindow()
def onHotkeyEvent(self, keyword):
if keyword == "info":
self.showInfo(QApplication.instance().widgetAt(QCursor.pos()))
def onMenuItemEvent(self, schid, atype, menuItemID, selectedItemID):
if menuItemID == 0:
self.showInfo(None)