Repository navigation
Expand file tree
/
Copy pathIconThemesLegacy.py
More file actions
443 lines (362 loc) · 12.5 KB
/
Copy pathIconThemesLegacy.py
File metadata and controls
443 lines (362 loc) · 12.5 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Icon themes for FreeCAD
# Copyright (C) 2016, 2017, 2018, 2019 triplus @ FreeCAD
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Icon themes for FreeCAD."""
def iconThemes():
"""Icon themes for FreeCAD."""
import os
import FreeCADGui as Gui
import FreeCAD as App
from PySide import QtGui
from PySide import QtCore
mw = Gui.getMainWindow()
appliedIcons = []
defaultIcons = {}
noneIcon = QtGui.QIcon(":/icons/freecad")
paramGet = App.ParamGet("User parameter:BaseApp/IconThemes")
def actionList():
"""
Create a dictionary of unique actions.
"""
actions = {}
duplicates = []
for i in mw.findChildren(QtGui.QAction):
if i.objectName() and i.text():
if i.objectName() in actions:
if i.objectName() not in duplicates:
duplicates.append(i.objectName())
else:
pass
else:
actions[i.objectName()] = i
else:
pass
for d in duplicates:
del actions[d]
return actions
def themeFolders():
"""
Create a list of icon theme folders.
"""
folders = []
path = (App.getUserAppDataDir() +
"Gui" +
os.path.sep +
"Icons" +
os.path.sep)
if os.path.isdir(path):
for i in os.listdir(path):
if os.path.isdir(path + i):
folders.append(i.encode("UTF-8"))
else:
pass
else:
pass
return folders
def currentFolder():
"""
Current icon theme folder.
"""
themeFolder = paramGet.GetString("ThemeFolder")
if themeFolder:
try:
folder = themeFolder.decode("UTF-8")
except AttributeError:
folder = themeFolder
path = (App.getUserAppDataDir() +
"Gui" +
os.path.sep +
"Icons" +
os.path.sep +
folder +
os.path.sep)
if os.path.isdir(path):
return path
else:
return False
else:
return False
def themeIcons():
"""
Create a list of theme icons.
"""
icons = []
path = currentFolder()
if path:
for i in os.listdir(path):
if os.path.isfile(path + i) and i.endswith(".svg"):
icons.append(path + i)
else:
pass
else:
pass
return icons
def resetIcons():
"""
Set default theme icons.
"""
actions = actionList()
for i in actions:
if i in defaultIcons:
actions[i].setIcon(defaultIcons[i])
else:
pass
def applyIcons():
"""
Apply the icons from the currently set icon theme.
"""
path = currentFolder()
actions = actionList()
if path:
for i in actions:
name = actions[i].objectName()
if name not in defaultIcons:
defaultIcons[name] = actions[i].icon()
else:
pass
if name not in appliedIcons:
icon = path + name + ".svg"
if os.path.isfile(icon):
actions[i].setIcon(QtGui.QIcon(icon))
else:
pass
appliedIcons.append(name)
else:
pass
else:
pass
def onPreferences():
"""
Open the preferences dialog.
"""
dialog = prefDialog()
dialog.show()
def prefDialog():
"""
Preferences dialog.
"""
def updateComboBox():
"""
Update theme folders combobox.
"""
default = True
folders = themeFolders()
try:
current = paramGet.GetString("ThemeFolder").decode("UTF-8")
except AttributeError:
current = paramGet.GetString("ThemeFolder")
comboBox.blockSignals(True)
comboBox.clear()
for i in folders:
try:
comboBox.addItem(i.decode("UTF-8"))
except AttributeError:
comboBox.addItem(i)
for count in range(comboBox.count()):
if comboBox.itemText(count) == current:
comboBox.setCurrentIndex(count)
default = False
else:
pass
comboBox.insertSeparator(0)
comboBox.insertItem(0, "Default")
if default:
comboBox.setCurrentIndex(0)
else:
pass
comboBox.blockSignals(False)
def onTheme(index):
"""
Apply the selected theme.
"""
if index != 0:
text = comboBox.currentText()
try:
paramGet.SetString("ThemeFolder", text.encode("UTF-8"))
except TypeError:
paramGet.SetString("ThemeFolder", text)
else:
paramGet.RemString("ThemeFolder")
del appliedIcons[:]
resetIcons()
applyIcons()
updateIconArea()
def updateIconArea():
"""
Update icons in the icon preview area.
"""
iconArea.blockSignals(True)
iconArea.clear()
if paramGet.GetBool("DesignerMode"):
actions = actionList()
for i in actions:
item = QtGui.QListWidgetItem()
item.setToolTip(actions[i].toolTip())
item.setData(33, actions[i].objectName())
item.setText(actions[i].text().replace("&", ""))
if actions[i].icon():
item.setIcon(actions[i].icon())
else:
item.setIcon(noneIcon)
iconArea.addItem(item)
else:
icons = themeIcons()
for i in icons:
item = QtGui.QListWidgetItem(iconArea)
item.setIcon(QtGui.QIcon(i))
iconArea.sortItems(QtCore.Qt.AscendingOrder)
iconArea.blockSignals(False)
def onSelected():
"""
Update icon file name on selection.
"""
if paramGet.GetBool("DesignerMode"):
item = iconArea.currentItem()
if item:
labelIconName.setText(item.data(33) + ".svg")
else:
pass
else:
pass
def onDesignerMode():
"""
Enable or disable the designer mode.
"""
if paramGet.GetBool("DesignerMode"):
paramGet.SetBool("DesignerMode", 0)
labelIconName.setVisible(False)
buttonDesignerMode.setChecked(False)
else:
paramGet.SetBool("DesignerMode", 1)
labelIconName.setVisible(True)
buttonDesignerMode.setChecked(True)
updateIconArea()
def onAccepted():
"""
Close dialog on button close.
"""
dialog.done(1)
def onFinished():
"""
Delete dialog on close.
"""
dialog.deleteLater()
def prefDefaults():
"""
Set preferences default values.
"""
if paramGet.GetBool("DesignerMode"):
labelIconName.setVisible(True)
buttonDesignerMode.setChecked(True)
else:
labelIconName.setVisible(False)
buttonDesignerMode.setChecked(False)
updateComboBox()
updateIconArea()
dialog = QtGui.QDialog(mw)
dialog.resize(800, 450)
dialog.setWindowTitle("IconThemes")
dialog.setObjectName("IconThemes")
layout = QtGui.QVBoxLayout()
dialog.setLayout(layout)
comboBox = QtGui.QComboBox(dialog)
comboBox.setMinimumWidth(220)
labelIconName = QtGui.QLabel(dialog)
labelIconName.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse)
iconArea = QtGui.QListWidget(dialog)
iconArea.setParent(dialog)
iconArea.setIconSize(QtCore.QSize(48, 48))
iconArea.setGridSize(QtCore.QSize(108, 96))
iconArea.sortItems(QtCore.Qt.AscendingOrder)
iconArea.setViewMode(QtGui.QListView.IconMode)
iconArea.setResizeMode(QtGui.QListView.Adjust)
buttonDesignerMode = QtGui.QPushButton("D", dialog)
buttonDesignerMode.setToolTip("Designer mode")
buttonDesignerMode.setMaximumWidth(40)
buttonDesignerMode.setCheckable(True)
buttonClose = QtGui.QPushButton("Close", dialog)
buttonClose.setDefault(True)
layoutTop = QtGui.QHBoxLayout()
layoutTop.addWidget(comboBox)
layoutTop.addStretch(1)
layoutTop.addWidget(labelIconName)
layoutBottom = QtGui.QHBoxLayout()
layoutBottom.addWidget(buttonDesignerMode)
layoutBottom.addStretch(1)
layoutBottom.addWidget(buttonClose)
layout.insertLayout(0, layoutTop)
layout.insertWidget(1, iconArea)
layout.insertLayout(2, layoutBottom)
prefDefaults()
dialog.finished.connect(onFinished)
buttonClose.clicked.connect(onAccepted)
comboBox.currentIndexChanged.connect(onTheme)
iconArea.itemSelectionChanged.connect(onSelected)
buttonDesignerMode.clicked.connect(onDesignerMode)
return dialog
def accessoriesMenu():
"""Add icon themes preferences to accessories menu."""
pref = QtGui.QAction(mw)
pref.setText("Icon themes legacy")
pref.setObjectName("IconThemesLegacy")
pref.triggered.connect(onPreferences)
try:
import AccessoriesMenu
AccessoriesMenu.addItem("IconThemesLegacy")
except ImportError:
a = mw.findChild(QtGui.QAction, "AccessoriesMenu")
if a:
a.menu().addAction(pref)
else:
mb = mw.menuBar()
actionAccessories = QtGui.QAction(mw)
actionAccessories.setObjectName("AccessoriesMenu")
actionAccessories.setIconText("Accessories")
menu = QtGui.QMenu(mw)
actionAccessories.setMenu(menu)
menu.addAction(pref)
def addMenu():
"""Add accessories menu to the menubar."""
mb.addAction(actionAccessories)
actionAccessories.setVisible(True)
addMenu()
mw.workbenchActivated.connect(addMenu)
def onStart():
"""Start icon themes."""
start = False
try:
mw.workbenchActivated
start = True
except AttributeError:
pass
if start:
timer.stop()
timer.deleteLater()
accessoriesMenu()
applyIcons()
mw.workbenchActivated.connect(applyIcons)
def onPreStart():
"""Improve start reliability and maintain FreeCAD 0.16 support."""
if App.Version()[1] < "17":
onStart()
else:
if mw.property("eventLoop"):
onStart()
timer = QtCore.QTimer()
timer.timeout.connect(onPreStart)
timer.start(500)
iconThemes()