|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a menu populated from a QAbstractItemModel. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSignal, Qt, QModelIndex, QPoint |
|
11 from PyQt5.QtGui import QFontMetrics, QDrag |
|
12 from PyQt5.QtWidgets import QMenu, QAction, QApplication |
|
13 |
|
14 import UI.PixmapCache |
|
15 |
|
16 |
|
17 class E5ModelMenu(QMenu): |
|
18 """ |
|
19 Class implementing a menu populated from a QAbstractItemModel. |
|
20 |
|
21 @signal activated(QModelIndex) emitted when an action has been triggered |
|
22 """ |
|
23 activated = pyqtSignal(QModelIndex) |
|
24 |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent widget (QWidget) |
|
30 """ |
|
31 super().__init__(parent) |
|
32 |
|
33 self.__maxRows = -1 |
|
34 self.__firstSeparator = -1 |
|
35 self.__maxWidth = -1 |
|
36 self.__statusBarTextRole = 0 |
|
37 self.__separatorRole = 0 |
|
38 self.__model = None |
|
39 self.__root = QModelIndex() |
|
40 self.__dragStartPosition = QPoint() |
|
41 |
|
42 self.setAcceptDrops(True) |
|
43 |
|
44 self._mouseButton = Qt.MouseButton.NoButton |
|
45 self._keyboardModifiers = Qt.KeyboardModifiers( |
|
46 Qt.KeyboardModifier.NoModifier) |
|
47 self.__dropRow = -1 |
|
48 self.__dropIndex = None |
|
49 |
|
50 # This is to ensure it will be shown on Mac OS X |
|
51 self.addAction("--not populated--") |
|
52 |
|
53 self.aboutToShow.connect(self.__aboutToShow) |
|
54 self.triggered.connect(self.__actionTriggered) |
|
55 |
|
56 def prePopulated(self): |
|
57 """ |
|
58 Public method to add any actions before the tree. |
|
59 |
|
60 @return flag indicating if any actions were added |
|
61 """ |
|
62 return False |
|
63 |
|
64 def postPopulated(self): |
|
65 """ |
|
66 Public method to add any actions after the tree. |
|
67 """ |
|
68 pass |
|
69 |
|
70 def setModel(self, model): |
|
71 """ |
|
72 Public method to set the model for the menu. |
|
73 |
|
74 @param model reference to the model (QAbstractItemModel) |
|
75 """ |
|
76 self.__model = model |
|
77 |
|
78 def model(self): |
|
79 """ |
|
80 Public method to get a reference to the model. |
|
81 |
|
82 @return reference to the model (QAbstractItemModel) |
|
83 """ |
|
84 return self.__model |
|
85 |
|
86 def setMaxRows(self, rows): |
|
87 """ |
|
88 Public method to set the maximum number of entries to show. |
|
89 |
|
90 @param rows maximum number of entries to show (integer) |
|
91 """ |
|
92 self.__maxRows = rows |
|
93 |
|
94 def maxRows(self): |
|
95 """ |
|
96 Public method to get the maximum number of entries to show. |
|
97 |
|
98 @return maximum number of entries to show (integer) |
|
99 """ |
|
100 return self.__maxRows |
|
101 |
|
102 def setFirstSeparator(self, offset): |
|
103 """ |
|
104 Public method to set the first separator. |
|
105 |
|
106 @param offset row number of the first separator (integer) |
|
107 """ |
|
108 self.__firstSeparator = offset |
|
109 |
|
110 def firstSeparator(self): |
|
111 """ |
|
112 Public method to get the first separator. |
|
113 |
|
114 @return row number of the first separator (integer) |
|
115 """ |
|
116 return self.__firstSeparator |
|
117 |
|
118 def setRootIndex(self, index): |
|
119 """ |
|
120 Public method to set the index of the root item. |
|
121 |
|
122 @param index index of the root item (QModelIndex) |
|
123 """ |
|
124 self.__root = index |
|
125 |
|
126 def rootIndex(self): |
|
127 """ |
|
128 Public method to get the index of the root item. |
|
129 |
|
130 @return index of the root item (QModelIndex) |
|
131 """ |
|
132 return self.__root |
|
133 |
|
134 def setStatusBarTextRole(self, role): |
|
135 """ |
|
136 Public method to set the role of the status bar text. |
|
137 |
|
138 @param role role of the status bar text (integer) |
|
139 """ |
|
140 self.__statusBarTextRole = role |
|
141 |
|
142 def statusBarTextRole(self): |
|
143 """ |
|
144 Public method to get the role of the status bar text. |
|
145 |
|
146 @return role of the status bar text (integer) |
|
147 """ |
|
148 return self.__statusBarTextRole |
|
149 |
|
150 def setSeparatorRole(self, role): |
|
151 """ |
|
152 Public method to set the role of the separator. |
|
153 |
|
154 @param role role of the separator (integer) |
|
155 """ |
|
156 self.__separatorRole = role |
|
157 |
|
158 def separatorRole(self): |
|
159 """ |
|
160 Public method to get the role of the separator. |
|
161 |
|
162 @return role of the separator (integer) |
|
163 """ |
|
164 return self.__separatorRole |
|
165 |
|
166 def __aboutToShow(self): |
|
167 """ |
|
168 Private slot to show the menu. |
|
169 """ |
|
170 self.clear() |
|
171 |
|
172 if self.prePopulated(): |
|
173 self.addSeparator() |
|
174 max_ = self.__maxRows |
|
175 if max_ != -1: |
|
176 max_ += self.__firstSeparator |
|
177 self.createMenu(self.__root, max_, self, self) |
|
178 self.postPopulated() |
|
179 |
|
180 def createBaseMenu(self): |
|
181 """ |
|
182 Public method to get the menu that is used to populate sub menu's. |
|
183 |
|
184 @return reference to the menu (E5ModelMenu) |
|
185 """ |
|
186 return E5ModelMenu(self) |
|
187 |
|
188 def createMenu(self, parent, max_, parentMenu=None, menu=None): |
|
189 """ |
|
190 Public method to put all the children of a parent into a menu of a |
|
191 given length. |
|
192 |
|
193 @param parent index of the parent item (QModelIndex) |
|
194 @param max_ maximum number of entries (integer) |
|
195 @param parentMenu reference to the parent menu (QMenu) |
|
196 @param menu reference to the menu to be populated (QMenu) |
|
197 """ |
|
198 if menu is None: |
|
199 v = parent |
|
200 |
|
201 title = parent.data() |
|
202 modelMenu = self.createBaseMenu() |
|
203 # triggered goes all the way up the menu structure |
|
204 modelMenu.triggered.disconnect(modelMenu.__actionTriggered) |
|
205 modelMenu.setTitle(title) |
|
206 |
|
207 icon = parent.data(Qt.ItemDataRole.DecorationRole) |
|
208 if icon == NotImplemented or icon is None: |
|
209 icon = UI.PixmapCache.getIcon("defaultIcon") |
|
210 modelMenu.setIcon(icon) |
|
211 if parentMenu is not None: |
|
212 parentMenu.addMenu(modelMenu).setData(v) |
|
213 modelMenu.setRootIndex(parent) |
|
214 modelMenu.setModel(self.__model) |
|
215 return |
|
216 |
|
217 if self.__model is None: |
|
218 return |
|
219 |
|
220 end = self.__model.rowCount(parent) |
|
221 if max_ != -1: |
|
222 end = min(max_, end) |
|
223 |
|
224 for i in range(end): |
|
225 idx = self.__model.index(i, 0, parent) |
|
226 if self.__model.hasChildren(idx): |
|
227 self.createMenu(idx, -1, menu) |
|
228 else: |
|
229 if ( |
|
230 self.__separatorRole != 0 and |
|
231 idx.data(self.__separatorRole) |
|
232 ): |
|
233 self.addSeparator() |
|
234 else: |
|
235 menu.addAction(self.__makeAction(idx)) |
|
236 |
|
237 if menu == self and i == self.__firstSeparator - 1: |
|
238 self.addSeparator() |
|
239 |
|
240 def __makeAction(self, idx): |
|
241 """ |
|
242 Private method to create an action. |
|
243 |
|
244 @param idx index of the item to create an action for (QModelIndex) |
|
245 @return reference to the created action (QAction) |
|
246 """ |
|
247 icon = idx.data(Qt.ItemDataRole.DecorationRole) |
|
248 if icon == NotImplemented or icon is None: |
|
249 icon = UI.PixmapCache.getIcon("defaultIcon") |
|
250 action = self.makeAction(icon, idx.data(), self) |
|
251 action.setStatusTip(idx.data(self.__statusBarTextRole)) |
|
252 |
|
253 v = idx |
|
254 action.setData(v) |
|
255 |
|
256 return action |
|
257 |
|
258 def makeAction(self, icon, text, parent): |
|
259 """ |
|
260 Public method to create an action. |
|
261 |
|
262 @param icon icon of the action (QIcon) |
|
263 @param text text of the action (string) |
|
264 @param parent reference to the parent object (QObject) |
|
265 @return reference to the created action (QAction) |
|
266 """ |
|
267 fm = QFontMetrics(self.font()) |
|
268 if self.__maxWidth == -1: |
|
269 try: |
|
270 self.__maxWidth = fm.horizontalAdvance("m") * 30 |
|
271 except AttributeError: |
|
272 self.__maxWidth = fm.width('m') * 30 |
|
273 smallText = fm.elidedText(text, Qt.TextElideMode.ElideMiddle, |
|
274 self.__maxWidth) |
|
275 |
|
276 return QAction(icon, smallText, parent) |
|
277 |
|
278 def __actionTriggered(self, action): |
|
279 """ |
|
280 Private slot to handle the triggering of an action. |
|
281 |
|
282 @param action reference to the action that was triggered (QAction) |
|
283 """ |
|
284 idx = self.index(action) |
|
285 if idx.isValid(): |
|
286 self._keyboardModifiers = QApplication.keyboardModifiers() |
|
287 self.activated[QModelIndex].emit(idx) |
|
288 |
|
289 def index(self, action): |
|
290 """ |
|
291 Public method to get the index of an action. |
|
292 |
|
293 @param action reference to the action to get the index for (QAction) |
|
294 @return index of the action (QModelIndex) |
|
295 """ |
|
296 if action is None: |
|
297 return QModelIndex() |
|
298 |
|
299 idx = action.data() |
|
300 if idx is None: |
|
301 return QModelIndex() |
|
302 |
|
303 if not isinstance(idx, QModelIndex): |
|
304 return QModelIndex() |
|
305 |
|
306 return idx |
|
307 |
|
308 def dragEnterEvent(self, evt): |
|
309 """ |
|
310 Protected method to handle drag enter events. |
|
311 |
|
312 @param evt reference to the event (QDragEnterEvent) |
|
313 """ |
|
314 if self.__model is not None: |
|
315 mimeTypes = self.__model.mimeTypes() |
|
316 for mimeType in mimeTypes: |
|
317 if evt.mimeData().hasFormat(mimeType): |
|
318 evt.acceptProposedAction() |
|
319 |
|
320 super().dragEnterEvent(evt) |
|
321 |
|
322 def dropEvent(self, evt): |
|
323 """ |
|
324 Protected method to handle drop events. |
|
325 |
|
326 @param evt reference to the event (QDropEvent) |
|
327 """ |
|
328 if self.__model is not None: |
|
329 act = self.actionAt(evt.pos()) |
|
330 parentIndex = self.__root |
|
331 if act is None: |
|
332 row = self.__model.rowCount(self.__root) |
|
333 else: |
|
334 idx = self.index(act) |
|
335 if not idx.isValid(): |
|
336 super().dropEvent(evt) |
|
337 return |
|
338 |
|
339 row = idx.row() |
|
340 if self.__model.hasChildren(idx): |
|
341 parentIndex = idx |
|
342 row = self.__model.rowCount(idx) |
|
343 |
|
344 self.__dropRow = row |
|
345 self.__dropIndex = parentIndex |
|
346 evt.acceptProposedAction() |
|
347 self.__model.dropMimeData(evt.mimeData(), evt.dropAction(), |
|
348 row, 0, parentIndex) |
|
349 self.close() |
|
350 |
|
351 super().dropEvent(evt) |
|
352 |
|
353 def mousePressEvent(self, evt): |
|
354 """ |
|
355 Protected method handling mouse press events. |
|
356 |
|
357 @param evt reference to the event object (QMouseEvent) |
|
358 """ |
|
359 if evt.button() == Qt.MouseButton.LeftButton: |
|
360 self.__dragStartPosition = evt.pos() |
|
361 super().mousePressEvent(evt) |
|
362 |
|
363 def mouseMoveEvent(self, evt): |
|
364 """ |
|
365 Protected method to handle mouse move events. |
|
366 |
|
367 @param evt reference to the event (QMouseEvent) |
|
368 """ |
|
369 if self.__model is None: |
|
370 super().mouseMoveEvent(evt) |
|
371 return |
|
372 |
|
373 if not (evt.buttons() & Qt.MouseButton.LeftButton): |
|
374 super().mouseMoveEvent(evt) |
|
375 return |
|
376 |
|
377 if self.__dragStartPosition.isNull(): |
|
378 super().mouseMoveEvent(evt) |
|
379 return |
|
380 |
|
381 manhattanLength = (evt.pos() - |
|
382 self.__dragStartPosition).manhattanLength() |
|
383 if manhattanLength <= QApplication.startDragDistance(): |
|
384 super().mouseMoveEvent(evt) |
|
385 return |
|
386 |
|
387 act = self.actionAt(self.__dragStartPosition) |
|
388 if act is None: |
|
389 super().mouseMoveEvent(evt) |
|
390 return |
|
391 |
|
392 idx = self.index(act) |
|
393 if not idx.isValid(): |
|
394 super().mouseMoveEvent(evt) |
|
395 return |
|
396 |
|
397 drag = QDrag(self) |
|
398 drag.setMimeData(self.__model.mimeData([idx])) |
|
399 actionRect = self.actionGeometry(act) |
|
400 drag.setPixmap(self.grab(actionRect)) |
|
401 |
|
402 if drag.exec() == Qt.DropAction.MoveAction: |
|
403 row = idx.row() |
|
404 if self.__dropIndex == idx.parent() and self.__dropRow <= row: |
|
405 row += 1 |
|
406 self.__model.removeRow(row, self.__root) |
|
407 |
|
408 if not self.isAncestorOf(drag.target()): |
|
409 self.close() |
|
410 else: |
|
411 self.aboutToShow.emit() |
|
412 |
|
413 def mouseReleaseEvent(self, evt): |
|
414 """ |
|
415 Protected method handling mouse release events. |
|
416 |
|
417 @param evt reference to the event object (QMouseEvent) |
|
418 """ |
|
419 self._mouseButton = evt.button() |
|
420 self._keyboardModifiers = evt.modifiers() |
|
421 |
|
422 if evt.button() == Qt.MouseButton.LeftButton: |
|
423 self.__dragStartPosition = QPoint() |
|
424 |
|
425 super().mouseReleaseEvent(evt) |
|
426 |
|
427 def resetFlags(self): |
|
428 """ |
|
429 Public method to reset the saved internal state. |
|
430 """ |
|
431 self._mouseButton = Qt.MouseButton.NoButton |
|
432 self._keyboardModifiers = Qt.KeyboardModifiers( |
|
433 Qt.KeyboardModifier.NoModifier) |
|
434 |
|
435 def removeEntry(self, idx): |
|
436 """ |
|
437 Public method to remove a menu entry. |
|
438 |
|
439 @param idx index of the entry to be removed (QModelIndex) |
|
440 """ |
|
441 row = idx.row() |
|
442 self.__model.removeRow(row, self.__root) |
|
443 self.aboutToShow.emit() |