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