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