src/eric7/EricWidgets/EricModelMenu.py

branch
eric7
changeset 10423
299802979277
parent 9653
e67609152c5e
child 10439
21c28b0f9e41
equal deleted inserted replaced
10422:e28b89693f37 10423:299802979277
25 25
26 def __init__(self, parent=None): 26 def __init__(self, parent=None):
27 """ 27 """
28 Constructor 28 Constructor
29 29
30 @param parent reference to the parent widget (QWidget) 30 @param parent reference to the parent widget
31 @type QWidget
31 """ 32 """
32 super().__init__(parent) 33 super().__init__(parent)
33 34
34 self.__maxRows = -1 35 self.__maxRows = -1
35 self.__firstSeparator = -1 36 self.__firstSeparator = -1
65 def prePopulated(self): 66 def prePopulated(self):
66 """ 67 """
67 Public method to add any actions before the tree. 68 Public method to add any actions before the tree.
68 69
69 @return flag indicating if any actions were added 70 @return flag indicating if any actions were added
71 @rtype bool
70 """ 72 """
71 return False 73 return False
72 74
73 def postPopulated(self): 75 def postPopulated(self):
74 """ 76 """
78 80
79 def setModel(self, model): 81 def setModel(self, model):
80 """ 82 """
81 Public method to set the model for the menu. 83 Public method to set the model for the menu.
82 84
83 @param model reference to the model (QAbstractItemModel) 85 @param model reference to the model
86 @type QAbstractItemModel
84 """ 87 """
85 self.__model = model 88 self.__model = model
86 89
87 def model(self): 90 def model(self):
88 """ 91 """
89 Public method to get a reference to the model. 92 Public method to get a reference to the model.
90 93
91 @return reference to the model (QAbstractItemModel) 94 @return reference to the model
95 @rtype QAbstractItemModel
92 """ 96 """
93 return self.__model 97 return self.__model
94 98
95 def setMaxRows(self, rows): 99 def setMaxRows(self, rows):
96 """ 100 """
97 Public method to set the maximum number of entries to show. 101 Public method to set the maximum number of entries to show.
98 102
99 @param rows maximum number of entries to show (integer) 103 @param rows maximum number of entries to show
104 @type int
100 """ 105 """
101 self.__maxRows = rows 106 self.__maxRows = rows
102 107
103 def maxRows(self): 108 def maxRows(self):
104 """ 109 """
105 Public method to get the maximum number of entries to show. 110 Public method to get the maximum number of entries to show.
106 111
107 @return maximum number of entries to show (integer) 112 @return maximum number of entries to show
113 @rtype int
108 """ 114 """
109 return self.__maxRows 115 return self.__maxRows
110 116
111 def setFirstSeparator(self, offset): 117 def setFirstSeparator(self, offset):
112 """ 118 """
113 Public method to set the first separator. 119 Public method to set the first separator.
114 120
115 @param offset row number of the first separator (integer) 121 @param offset row number of the first separator
122 @type int
116 """ 123 """
117 self.__firstSeparator = offset 124 self.__firstSeparator = offset
118 125
119 def firstSeparator(self): 126 def firstSeparator(self):
120 """ 127 """
121 Public method to get the first separator. 128 Public method to get the first separator.
122 129
123 @return row number of the first separator (integer) 130 @return row number of the first separator
131 @rtype int
124 """ 132 """
125 return self.__firstSeparator 133 return self.__firstSeparator
126 134
127 def setRootIndex(self, index): 135 def setRootIndex(self, index):
128 """ 136 """
129 Public method to set the index of the root item. 137 Public method to set the index of the root item.
130 138
131 @param index index of the root item (QModelIndex) 139 @param index index of the root item
140 @type QModelIndex
132 """ 141 """
133 self.__root = index 142 self.__root = index
134 143
135 def rootIndex(self): 144 def rootIndex(self):
136 """ 145 """
137 Public method to get the index of the root item. 146 Public method to get the index of the root item.
138 147
139 @return index of the root item (QModelIndex) 148 @return index of the root item
149 @rtype QModelIndex
140 """ 150 """
141 return self.__root 151 return self.__root
142 152
143 def setStatusBarTextRole(self, role): 153 def setStatusBarTextRole(self, role):
144 """ 154 """
145 Public method to set the role of the status bar text. 155 Public method to set the role of the status bar text.
146 156
147 @param role role of the status bar text (integer) 157 @param role role of the status bar text
158 @type int
148 """ 159 """
149 self.__statusBarTextRole = role 160 self.__statusBarTextRole = role
150 161
151 def statusBarTextRole(self): 162 def statusBarTextRole(self):
152 """ 163 """
153 Public method to get the role of the status bar text. 164 Public method to get the role of the status bar text.
154 165
155 @return role of the status bar text (integer) 166 @return role of the status bar text
167 @rtype int
156 """ 168 """
157 return self.__statusBarTextRole 169 return self.__statusBarTextRole
158 170
159 def setSeparatorRole(self, role): 171 def setSeparatorRole(self, role):
160 """ 172 """
161 Public method to set the role of the separator. 173 Public method to set the role of the separator.
162 174
163 @param role role of the separator (integer) 175 @param role role of the separator
176 @type int
164 """ 177 """
165 self.__separatorRole = role 178 self.__separatorRole = role
166 179
167 def separatorRole(self): 180 def separatorRole(self):
168 """ 181 """
169 Public method to get the role of the separator. 182 Public method to get the role of the separator.
170 183
171 @return role of the separator (integer) 184 @return role of the separator
185 @rtype int
172 """ 186 """
173 return self.__separatorRole 187 return self.__separatorRole
174 188
175 def __aboutToShow(self): 189 def __aboutToShow(self):
176 """ 190 """
188 202
189 def createBaseMenu(self): 203 def createBaseMenu(self):
190 """ 204 """
191 Public method to get the menu that is used to populate sub menu's. 205 Public method to get the menu that is used to populate sub menu's.
192 206
193 @return reference to the menu (EricModelMenu) 207 @return reference to the menu
208 @rtype EricModelMenu
194 """ 209 """
195 return EricModelMenu(self) 210 return EricModelMenu(self)
196 211
197 def createMenu(self, parent, max_, parentMenu=None, menu=None): 212 def createMenu(self, parent, max_, parentMenu=None, menu=None):
198 """ 213 """
199 Public method to put all the children of a parent into a menu of a 214 Public method to put all the children of a parent into a menu of a
200 given length. 215 given length.
201 216
202 @param parent index of the parent item (QModelIndex) 217 @param parent index of the parent item
203 @param max_ maximum number of entries (integer) 218 @type QModelIndex
204 @param parentMenu reference to the parent menu (QMenu) 219 @param max_ maximum number of entries
205 @param menu reference to the menu to be populated (QMenu) 220 @type int
221 @param parentMenu reference to the parent menu
222 @type QMenu
223 @param menu reference to the menu to be populated
224 @type QMenu
206 """ 225 """
207 if menu is None: 226 if menu is None:
208 v = parent 227 v = parent
209 228
210 title = parent.data() 229 title = parent.data()
245 264
246 def __makeAction(self, idx): 265 def __makeAction(self, idx):
247 """ 266 """
248 Private method to create an action. 267 Private method to create an action.
249 268
250 @param idx index of the item to create an action for (QModelIndex) 269 @param idx index of the item to create an action for
251 @return reference to the created action (QAction) 270 @type QModelIndex
271 @return reference to the created action
272 @rtype QAction
252 """ 273 """
253 icon = idx.data(Qt.ItemDataRole.DecorationRole) 274 icon = idx.data(Qt.ItemDataRole.DecorationRole)
254 if icon == NotImplemented or icon is None: 275 if icon == NotImplemented or icon is None:
255 icon = EricPixmapCache.getIcon("defaultIcon") 276 icon = EricPixmapCache.getIcon("defaultIcon")
256 action = self.makeAction(icon, idx.data(), self) 277 action = self.makeAction(icon, idx.data(), self)
263 284
264 def makeAction(self, icon, text, parent): 285 def makeAction(self, icon, text, parent):
265 """ 286 """
266 Public method to create an action. 287 Public method to create an action.
267 288
268 @param icon icon of the action (QIcon) 289 @param icon icon of the action
269 @param text text of the action (string) 290 @type QIcon
270 @param parent reference to the parent object (QObject) 291 @param text text of the action
271 @return reference to the created action (QAction) 292 @type str
293 @param parent reference to the parent object
294 @type QObject
295 @return reference to the created action
296 @rtype QAction
272 """ 297 """
273 fm = QFontMetrics(self.font()) 298 fm = QFontMetrics(self.font())
274 if self.__maxWidth == -1: 299 if self.__maxWidth == -1:
275 self.__maxWidth = fm.horizontalAdvance("m") * 30 300 self.__maxWidth = fm.horizontalAdvance("m") * 30
276 smallText = fm.elidedText(text, Qt.TextElideMode.ElideMiddle, self.__maxWidth) 301 smallText = fm.elidedText(text, Qt.TextElideMode.ElideMiddle, self.__maxWidth)
279 304
280 def __actionTriggered(self, action): 305 def __actionTriggered(self, action):
281 """ 306 """
282 Private slot to handle the triggering of an action. 307 Private slot to handle the triggering of an action.
283 308
284 @param action reference to the action that was triggered (QAction) 309 @param action reference to the action that was triggered
310 @type QAction
285 """ 311 """
286 idx = self.index(action) 312 idx = self.index(action)
287 if idx.isValid(): 313 if idx.isValid():
288 self._keyboardModifiers = QApplication.keyboardModifiers() 314 self._keyboardModifiers = QApplication.keyboardModifiers()
289 self.activated[QModelIndex].emit(idx) 315 self.activated[QModelIndex].emit(idx)
290 316
291 def index(self, action): 317 def index(self, action):
292 """ 318 """
293 Public method to get the index of an action. 319 Public method to get the index of an action.
294 320
295 @param action reference to the action to get the index for (QAction) 321 @param action reference to the action to get the index for
296 @return index of the action (QModelIndex) 322 @type QAction
323 @return index of the action
324 @rtype QModelIndex
297 """ 325 """
298 if action is None: 326 if action is None:
299 return QModelIndex() 327 return QModelIndex()
300 328
301 idx = action.data() 329 idx = action.data()
309 337
310 def dragEnterEvent(self, evt): 338 def dragEnterEvent(self, evt):
311 """ 339 """
312 Protected method to handle drag enter events. 340 Protected method to handle drag enter events.
313 341
314 @param evt reference to the event (QDragEnterEvent) 342 @param evt reference to the event
343 @type QDragEnterEvent
315 """ 344 """
316 if self.__model is not None: 345 if self.__model is not None:
317 mimeTypes = self.__model.mimeTypes() 346 mimeTypes = self.__model.mimeTypes()
318 for mimeType in mimeTypes: 347 for mimeType in mimeTypes:
319 if evt.mimeData().hasFormat(mimeType): 348 if evt.mimeData().hasFormat(mimeType):
323 352
324 def dropEvent(self, evt): 353 def dropEvent(self, evt):
325 """ 354 """
326 Protected method to handle drop events. 355 Protected method to handle drop events.
327 356
328 @param evt reference to the event (QDropEvent) 357 @param evt reference to the event
358 @type QDropEvent
329 """ 359 """
330 if self.__model is not None: 360 if self.__model is not None:
331 act = self.actionAt(evt.position().toPoint()) 361 act = self.actionAt(evt.position().toPoint())
332 parentIndex = self.__root 362 parentIndex = self.__root
333 if act is None: 363 if act is None:
355 385
356 def mousePressEvent(self, evt): 386 def mousePressEvent(self, evt):
357 """ 387 """
358 Protected method handling mouse press events. 388 Protected method handling mouse press events.
359 389
360 @param evt reference to the event object (QMouseEvent) 390 @param evt reference to the event object
391 @type QMouseEvent
361 """ 392 """
362 if evt.button() == Qt.MouseButton.LeftButton: 393 if evt.button() == Qt.MouseButton.LeftButton:
363 self.__dragStartPosition = evt.position().toPoint() 394 self.__dragStartPosition = evt.position().toPoint()
364 super().mousePressEvent(evt) 395 super().mousePressEvent(evt)
365 396
366 def mouseMoveEvent(self, evt): 397 def mouseMoveEvent(self, evt):
367 """ 398 """
368 Protected method to handle mouse move events. 399 Protected method to handle mouse move events.
369 400
370 @param evt reference to the event (QMouseEvent) 401 @param evt reference to the event
402 @type QMouseEvent
371 """ 403 """
372 if self.__model is None: 404 if self.__model is None:
373 super().mouseMoveEvent(evt) 405 super().mouseMoveEvent(evt)
374 return 406 return
375 407
416 448
417 def mouseReleaseEvent(self, evt): 449 def mouseReleaseEvent(self, evt):
418 """ 450 """
419 Protected method handling mouse release events. 451 Protected method handling mouse release events.
420 452
421 @param evt reference to the event object (QMouseEvent) 453 @param evt reference to the event object
454 @type QMouseEvent
422 """ 455 """
423 self._mouseButton = evt.button() 456 self._mouseButton = evt.button()
424 self._keyboardModifiers = evt.modifiers() 457 self._keyboardModifiers = evt.modifiers()
425 458
426 if evt.button() == Qt.MouseButton.LeftButton: 459 if evt.button() == Qt.MouseButton.LeftButton:
437 470
438 def removeEntry(self, idx): 471 def removeEntry(self, idx):
439 """ 472 """
440 Public method to remove a menu entry. 473 Public method to remove a menu entry.
441 474
442 @param idx index of the entry to be removed (QModelIndex) 475 @param idx index of the entry to be removed
476 @type QModelIndex
443 """ 477 """
444 row = idx.row() 478 row = idx.row()
445 self.__model.removeRow(row, self.__root) 479 self.__model.removeRow(row, self.__root)
446 self.aboutToShow.emit() 480 self.aboutToShow.emit()

eric ide

mercurial