-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInitGui.py
189 lines (153 loc) · 5.19 KB
/
InitGui.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
# Launcher widget for FreeCAD
# Copyright (C) 2016, 2017, 2018 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
def singleInstance():
"""
Only have one instance of Launcher running.
"""
import FreeCADGui as Gui
from PySide import QtGui
mw = Gui.getMainWindow()
if mw:
for i in mw.findChildren(QtGui.QDockWidget):
if i.objectName() == "Launcher":
i.deleteLater()
else:
pass
else:
pass
singleInstance()
def dockWidget():
"""
Launcher widget for FreeCAD
"""
import FreeCADGui as Gui
from PySide import QtGui
from PySide import QtCore
mw = Gui.getMainWindow()
icon = """<svg xmlns="http://www.w3.org/2000/svg" height="64" width="64">
<rect height="64" width="64" fill="none" />
</svg>"""
iconPixmap = QtGui.QPixmap()
iconPixmap.loadFromData(str.encode(icon))
class LauncherEdit(QtGui.QLineEdit):
"""
Define completer show/hide behavior.
"""
def __init__(self, parent=None):
super(LauncherEdit, self).__init__(parent)
def focusInEvent(self, e):
"""
Prevent updating model data after closing completer.
"""
if e.reason() == QtCore.Qt.PopupFocusReason:
pass
else:
modelData()
def keyPressEvent(self, e):
"""
Show completer after down key is pressed.
"""
if e.key() == QtCore.Qt.Key_Down:
edit.clear()
completer.setCompletionPrefix("")
completer.complete()
else:
QtGui.QLineEdit.keyPressEvent(self, e)
index = model.index(0, 0)
completer.popup().setCurrentIndex(index)
completer = QtGui.QCompleter()
completer.setMaxVisibleItems(16)
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
try:
# Qt 5.2 and up.
completer.setFilterMode(QtCore.Qt.MatchContains)
except AttributeError:
pass
edit = LauncherEdit()
edit.setCompleter(completer)
model = QtGui.QStandardItemModel()
completer.setModel(model)
widget = QtGui.QDockWidget()
widget.setWindowTitle("Launcher")
widget.setObjectName("Launcher")
widget.setWidget(edit)
if mw:
mw.addDockWidget(QtCore.Qt.LeftDockWidgetArea, widget)
else:
pass
def modelData():
"""
Fill the model with model items.
"""
actions = {}
duplicates = []
for i in mw.findChildren(QtGui.QAction):
if i.objectName():
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]
rows = len(actions)
model.clear()
model.setRowCount(rows)
model.setColumnCount(1)
row = 0
for i in actions:
item = QtGui.QStandardItem()
item.setText((actions[i].text()).replace("&", ""))
if actions[i].icon():
item.setIcon(actions[i].icon())
else:
item.setIcon(QtGui.QIcon(QtGui.QIcon(iconPixmap)))
item.setToolTip(actions[i].toolTip())
item.setEnabled(actions[i].isEnabled())
item.setData(actions[i].objectName(), QtCore.Qt.UserRole)
model.setItem(row, 0, item)
row += 1
def onCompleter(modelIndex):
"""
When command is selected and triggered run it and update model data.
"""
actions = {}
for i in mw.findChildren(QtGui.QAction):
actions[i.objectName()] = i
index = completer.completionModel().mapToSource(modelIndex)
item = model.itemFromIndex(index)
data = item.data(QtCore.Qt.UserRole)
if data in actions:
actions[data].trigger()
else:
pass
edit.clear()
edit.clearFocus()
edit.setFocus()
completer.activated[QtCore.QModelIndex].connect(onCompleter)
a = QtGui.QAction(mw)
mw.addAction(a)
a.setText("Launcher focus")
a.setObjectName("SetLauncherFocus")
a.setShortcut(QtGui.QKeySequence("Ctrl+Shift+Q"))
a.triggered.connect(edit.setFocus)
dockWidget()