src/eric7/EricWidgets/EricToolBarManager.py

branch
eric7
changeset 10423
299802979277
parent 10373
093dcebe5ecb
child 10439
21c28b0f9e41
equal deleted inserted replaced
10422:e28b89693f37 10423:299802979277
55 55
56 def __init__(self, ui=None, parent=None): 56 def __init__(self, ui=None, parent=None):
57 """ 57 """
58 Constructor 58 Constructor
59 59
60 @param ui reference to the user interface object (UI.UserInterface) 60 @param ui reference to the user interface object
61 @param parent reference to the parent object (QObject) 61 @type UI.UserInterface
62 @param parent reference to the parent object
63 @type QObject
62 """ 64 """
63 super().__init__(parent) 65 super().__init__(parent)
64 66
65 self.__mainWindow = None 67 self.__mainWindow = None
66 self.__ui = ui 68 self.__ui = ui
96 98
97 def __toolBarByName(self, name): 99 def __toolBarByName(self, name):
98 """ 100 """
99 Private slot to get a toolbar by its object name. 101 Private slot to get a toolbar by its object name.
100 102
101 @param name object name of the toolbar (string) 103 @param name object name of the toolbar
102 @return reference to the toolbar (QToolBar) 104 @type str
105 @return reference to the toolbar
106 @rtype QToolBar
103 """ 107 """
104 for toolBar in self.__allToolBars.values(): 108 for toolBar in self.__allToolBars.values():
105 if toolBar.objectName() == name: 109 if toolBar.objectName() == name:
106 return toolBar 110 return toolBar
107 return None 111 return None
108 112
109 def __findAction(self, name): 113 def __findAction(self, name):
110 """ 114 """
111 Private method to find an action by name. 115 Private method to find an action by name.
112 116
113 @param name name of the action to search for (string) 117 @param name name of the action to search for
114 @return reference to the action (QAction) 118 @type str
119 @return reference to the action
120 @rtype QAction
115 """ 121 """
116 # check objectName() first 122 # check objectName() first
117 for action in self.__allActions.values(): 123 for action in self.__allActions.values():
118 if action.objectName() == name: 124 if action.objectName() == name:
119 return action 125 return action
127 133
128 def __findDefaultToolBar(self, name): 134 def __findDefaultToolBar(self, name):
129 """ 135 """
130 Private method to find a default toolbar by name. 136 Private method to find a default toolbar by name.
131 137
132 @param name name of the default toolbar to search for (string) 138 @param name name of the default toolbar to search for
133 @return reference to the default toolbar (QToolBar) 139 @type str
140 @return reference to the default toolbar
141 @rtype QToolBar
134 """ 142 """
135 # check objectName() first 143 # check objectName() first
136 for tbID in self.__defaultToolBars: 144 for tbID in self.__defaultToolBars:
137 tb = self.__allToolBars[tbID] 145 tb = self.__allToolBars[tbID]
138 if tb.objectName() == name: 146 if tb.objectName() == name:
152 160
153 def setMainWindow(self, mainWindow): 161 def setMainWindow(self, mainWindow):
154 """ 162 """
155 Public method to set the reference to the main window. 163 Public method to set the reference to the main window.
156 164
157 @param mainWindow reference to the main window (QMainWindow) 165 @param mainWindow reference to the main window
166 @type QMainWindow
158 """ 167 """
159 self.__mainWindow = mainWindow 168 self.__mainWindow = mainWindow
160 169
161 def mainWindow(self): 170 def mainWindow(self):
162 """ 171 """
163 Public method to get the reference to the main window. 172 Public method to get the reference to the main window.
164 173
165 @return reference to the main window (QMainWindow) 174 @return reference to the main window
175 @rtype QMainWindow
166 """ 176 """
167 return self.__mainWindow 177 return self.__mainWindow
168 178
169 def addToolBar(self, toolBar, category): 179 def addToolBar(self, toolBar, category):
170 """ 180 """
171 Public method to add a toolbar to be managed. 181 Public method to add a toolbar to be managed.
172 182
173 @param toolBar reference to the toolbar to be managed (QToolBar) 183 @param toolBar reference to the toolbar to be managed
174 @param category category for the toolbar (string) 184 @type QToolBar
185 @param category category for the toolbar
186 @type str
175 """ 187 """
176 if toolBar is None: 188 if toolBar is None:
177 return 189 return
178 190
179 iconSizeStr = Preferences.getIcons("IconSize") 191 iconSizeStr = Preferences.getIcons("IconSize")
205 217
206 def removeToolBar(self, toolBar): 218 def removeToolBar(self, toolBar):
207 """ 219 """
208 Public method to remove a toolbar added with addToolBar(). 220 Public method to remove a toolbar added with addToolBar().
209 221
210 @param toolBar reference to the toolbar to be removed (QToolBar) 222 @param toolBar reference to the toolbar to be removed
223 @type QToolBar
211 """ 224 """
212 if toolBar is None: 225 if toolBar is None:
213 return 226 return
214 227
215 tbID = id(toolBar) 228 tbID = id(toolBar)
237 """ 250 """
238 Public method to set the actions of several toolbars. 251 Public method to set the actions of several toolbars.
239 252
240 @param toolBars dictionary with toolbar id as key and 253 @param toolBars dictionary with toolbar id as key and
241 a list of actions as value 254 a list of actions as value
255 @type dict
242 """ 256 """
243 for key, actions in toolBars.items(): 257 for key, actions in toolBars.items():
244 tb = self.__allToolBars[key] 258 tb = self.__allToolBars[key]
245 self.setToolBar(tb, actions) 259 self.setToolBar(tb, actions)
246 260
247 def setToolBar(self, toolBar, actions): 261 def setToolBar(self, toolBar, actions):
248 """ 262 """
249 Public method to set the actions of a toolbar. 263 Public method to set the actions of a toolbar.
250 264
251 @param toolBar reference to the toolbar to configure (QToolBar) 265 @param toolBar reference to the toolbar to configure
252 @param actions list of actions to be set (list of QAction) 266 @type QToolBar
267 @param actions list of actions to be set
268 @type list of QAction
253 """ 269 """
254 if toolBar is None: 270 if toolBar is None:
255 return 271 return
256 272
257 tbID = id(toolBar) 273 tbID = id(toolBar)
309 325
310 def resetToolBar(self, toolBar): 326 def resetToolBar(self, toolBar):
311 """ 327 """
312 Public method to reset a toolbar to its default state. 328 Public method to reset a toolbar to its default state.
313 329
314 @param toolBar reference to the toolbar to configure (QToolBar) 330 @param toolBar reference to the toolbar to configure
331 @type QToolBar
315 """ 332 """
316 if not self.isDefaultToolBar(): 333 if not self.isDefaultToolBar():
317 return 334 return
318 self.setToolBar(toolBar, self.__defaultToolBars[id(toolBar)]) 335 self.setToolBar(toolBar, self.__defaultToolBars[id(toolBar)])
319 336
327 344
328 def defaultToolBars(self): 345 def defaultToolBars(self):
329 """ 346 """
330 Public method to get all toolbars added with addToolBar(). 347 Public method to get all toolbars added with addToolBar().
331 348
332 @return list of all default toolbars (list of QToolBar) 349 @return list of all default toolbars
350 @rtype list of QToolBar
333 """ 351 """
334 return list(self.__defaultToolBars.values()) 352 return list(self.__defaultToolBars.values())
335 353
336 def isDefaultToolBar(self, toolBar): 354 def isDefaultToolBar(self, toolBar):
337 """ 355 """
338 Public method to check, if a toolbar was added with addToolBar(). 356 Public method to check, if a toolbar was added with addToolBar().
339 357
340 @param toolBar reference to the toolbar to be checked (QToolBar) 358 @param toolBar reference to the toolbar to be checked
341 @return flag indicating an added toolbar (boolean) 359 @type QToolBar
360 @return flag indicating an added toolbar
361 @rtype bool
342 """ 362 """
343 return toolBar is not None and id(toolBar) in self.__defaultToolBars 363 return toolBar is not None and id(toolBar) in self.__defaultToolBars
344 364
345 def createToolBar(self, title, name=""): 365 def createToolBar(self, title, name=""):
346 """ 366 """
347 Public method to create a custom toolbar. 367 Public method to create a custom toolbar.
348 368
349 @param title title to be used for the toolbar (string) 369 @param title title to be used for the toolbar
350 @param name optional name for the new toolbar (string) 370 @type str
351 @return reference to the created toolbar (QToolBar) 371 @param name optional name for the new toolbar
372 @type str
373 @return reference to the created toolbar
374 @rtype QToolBar
352 """ 375 """
353 if self.__mainWindow is None: 376 if self.__mainWindow is None:
354 return None 377 return None
355 378
356 toolBar = QToolBar(title, self.__mainWindow) 379 toolBar = QToolBar(title, self.__mainWindow)
378 401
379 def deleteToolBar(self, toolBar): 402 def deleteToolBar(self, toolBar):
380 """ 403 """
381 Public method to remove a custom toolbar created with createToolBar(). 404 Public method to remove a custom toolbar created with createToolBar().
382 405
383 @param toolBar reference to the toolbar to be managed (QToolBar) 406 @param toolBar reference to the toolbar to be managed
407 @type QToolBar
384 """ 408 """
385 if toolBar is None: 409 if toolBar is None:
386 return 410 return
387 411
388 tbID = id(toolBar) 412 tbID = id(toolBar)
405 429
406 def renameToolBar(self, toolBar, title): 430 def renameToolBar(self, toolBar, title):
407 """ 431 """
408 Public method to give a toolbar a new title. 432 Public method to give a toolbar a new title.
409 433
410 @param toolBar reference to the toolbar to be managed (QToolBar) 434 @param toolBar reference to the toolbar to be managed
411 @param title title to be used for the toolbar (string) 435 @type QToolBar
436 @param title title to be used for the toolbar
437 @type str
412 """ 438 """
413 if toolBar is None: 439 if toolBar is None:
414 return 440 return
415 441
416 toolBar.setWindowTitle(title) 442 toolBar.setWindowTitle(title)
420 446
421 def toolBars(self): 447 def toolBars(self):
422 """ 448 """
423 Public method to get all toolbars. 449 Public method to get all toolbars.
424 450
425 @return list of all toolbars (list of QToolBar) 451 @return list of all toolbars
452 @rtype list of QToolBar
426 """ 453 """
427 return list(self.__allToolBars.values()) 454 return list(self.__allToolBars.values())
428 455
429 def addAction(self, action, category): 456 def addAction(self, action, category):
430 """ 457 """
431 Public method to add an action to be managed. 458 Public method to add an action to be managed.
432 459
433 @param action reference to the action to be managed (QAction) 460 @param action reference to the action to be managed
434 @param category category for the toolbar (string) 461 @type QAction
462 @param category category for the toolbar
463 @type str
435 """ 464 """
436 if action is None: 465 if action is None:
437 return 466 return
438 if action.isSeparator(): 467 if action.isSeparator():
439 return 468 return
464 493
465 def removeAction(self, action): 494 def removeAction(self, action):
466 """ 495 """
467 Public method to remove an action from the manager. 496 Public method to remove an action from the manager.
468 497
469 @param action reference to the action to be removed (QAction) 498 @param action reference to the action to be removed
499 @type QAction
470 """ 500 """
471 aID = id(action) 501 aID = id(action)
472 502
473 if aID not in self.__allActions: 503 if aID not in self.__allActions:
474 return 504 return
500 530
501 def removeCategoryActions(self, category): 531 def removeCategoryActions(self, category):
502 """ 532 """
503 Public method to remove the actions belonging to a category. 533 Public method to remove the actions belonging to a category.
504 534
505 @param category category for the actions (string) 535 @param category category for the actions
536 @type str
506 """ 537 """
507 for action in self.categoryActions(category): 538 for action in self.categoryActions(category):
508 self.removeAction(action) 539 self.removeAction(action)
509 540
510 def saveState(self, version=0): 541 def saveState(self, version=0):
511 """ 542 """
512 Public method to save the state of the toolbar manager. 543 Public method to save the state of the toolbar manager.
513 544
514 @param version version number stored with the data (integer) 545 @param version version number stored with the data
515 @return saved state as a byte array (QByteArray) 546 @type int
547 @return saved state as a byte array
548 @rtype QByteArray
516 """ 549 """
517 data = QByteArray() 550 data = QByteArray()
518 stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly) 551 stream = QDataStream(data, QIODevice.OpenModeFlag.WriteOnly)
519 stream.setVersion(QDataStream.Version.Qt_4_6) 552 stream.setVersion(QDataStream.Version.Qt_4_6)
520 stream.writeUInt16(EricToolBarManager.VersionMarker) 553 stream.writeUInt16(EricToolBarManager.VersionMarker)
561 594
562 def restoreState(self, state, version=0): 595 def restoreState(self, state, version=0):
563 """ 596 """
564 Public method to restore the state of the toolbar manager. 597 Public method to restore the state of the toolbar manager.
565 598
566 @param state byte array containing the saved state (QByteArray) 599 @param state byte array containing the saved state
567 @param version version number stored with the data (integer) 600 @type QByteArray
568 @return flag indicating success (boolean) 601 @param version version number stored with the data
602 @type int
603 @return flag indicating success
604 @rtype bool
569 """ 605 """
570 if state.isEmpty(): 606 if state.isEmpty():
571 return False 607 return False
572 608
573 data = QByteArray(state) 609 data = QByteArray(state)
636 672
637 def toolBarWidgetAction(self, action): 673 def toolBarWidgetAction(self, action):
638 """ 674 """
639 Public method to get the toolbar for a widget action. 675 Public method to get the toolbar for a widget action.
640 676
641 @param action widget action to check for (QAction) 677 @param action widget action to check for
642 @return reference to the toolbar containing action (QToolBar) 678 @type QAction
679 @return reference to the toolbar containing action
680 @rtype QToolBar
643 """ 681 """
644 aID = id(action) 682 aID = id(action)
645 if aID in self.__widgetActions: 683 if aID in self.__widgetActions:
646 return self.__widgetActions[aID] 684 return self.__widgetActions[aID]
647 return None 685 return None
650 """ 688 """
651 Public method to remove widget actions. 689 Public method to remove widget actions.
652 690
653 @param actions dictionary with toolbar id as key and 691 @param actions dictionary with toolbar id as key and
654 a list of widget actions as value 692 a list of widget actions as value
693 @type dict
655 """ 694 """
656 for tbID in list(actions): 695 for tbID in list(actions):
657 toolBar = self.__allToolBars[tbID] 696 toolBar = self.__allToolBars[tbID]
658 newActions = self.__toolBars[tbID][:] 697 newActions = self.__toolBars[tbID][:]
659 newActionsWithSeparators = self.__toolBarsWithSeparators[tbID][:] 698 newActionsWithSeparators = self.__toolBarsWithSeparators[tbID][:]
675 714
676 def isWidgetAction(self, action): 715 def isWidgetAction(self, action):
677 """ 716 """
678 Public method to check, if action is a widget action. 717 Public method to check, if action is a widget action.
679 718
680 @param action reference to the action to be checked (QAction) 719 @param action reference to the action to be checked
681 @return flag indicating a widget action (boolean) 720 @type QAction
721 @return flag indicating a widget action
722 @rtype bool
682 """ 723 """
683 return id(action) in self.__allWidgetActions 724 return id(action) in self.__allWidgetActions
684 725
685 def categories(self): 726 def categories(self):
686 """ 727 """
687 Public method to get the list of categories. 728 Public method to get the list of categories.
688 729
689 @return list of categories (list of string) 730 @return list of categories
731 @rtype list of str
690 """ 732 """
691 return list(self.__categoryToActions) 733 return list(self.__categoryToActions)
692 734
693 def categoryActions(self, category): 735 def categoryActions(self, category):
694 """ 736 """
695 Public method to get the actions belonging to a category. 737 Public method to get the actions belonging to a category.
696 738
697 @param category category for the actions (string) 739 @param category category for the actions
698 @return list of actions (list of QAction) 740 @type str
741 @return list of actions
742 @rtype list of QAction
699 """ 743 """
700 if category not in self.__categoryToActions: 744 if category not in self.__categoryToActions:
701 return [] 745 return []
702 746
703 return self.__categoryToActions[category][:] 747 return self.__categoryToActions[category][:]
704 748
705 def actionById(self, aID): 749 def actionById(self, aID):
706 """ 750 """
707 Public method to get an action given its id. 751 Public method to get an action given its id.
708 752
709 @param aID id of the action object (integer) 753 @param aID id of the action object
710 @return reference to the action (QAction) 754 @type int
755 @return reference to the action
756 @rtype QAction
711 """ 757 """
712 if aID not in self.__allActions: 758 if aID not in self.__allActions:
713 return None 759 return None
714 return self.__allActions[aID] 760 return self.__allActions[aID]
715 761
716 def toolBarById(self, tbID): 762 def toolBarById(self, tbID):
717 """ 763 """
718 Public method to get a toolbar given its id. 764 Public method to get a toolbar given its id.
719 765
720 @param tbID id of the toolbar object (integer) 766 @param tbID id of the toolbar object
721 @return reference to the toolbar (QToolBar) 767 @type int
768 @return reference to the toolbar
769 @rtype QToolBar
722 """ 770 """
723 if tbID not in self.__allToolBars: 771 if tbID not in self.__allToolBars:
724 return None 772 return None
725 return self.__allToolBars[tbID] 773 return self.__allToolBars[tbID]
726 774
727 def toolBarActions(self, tbID): 775 def toolBarActions(self, tbID):
728 """ 776 """
729 Public method to get a toolbar's actions given its id. 777 Public method to get a toolbar's actions given its id.
730 778
731 @param tbID id of the toolbar object (integer) 779 @param tbID id of the toolbar object
732 @return list of actions (list of QAction) 780 @type int
781 @return list of actions
782 @rtype list of QAction
733 """ 783 """
734 if tbID not in self.__toolBars: 784 if tbID not in self.__toolBars:
735 return [] 785 return []
736 return self.__toolBars[tbID][:] 786 return self.__toolBars[tbID][:]
737 787
739 """ 789 """
740 Public method to get all toolbars and their actions. 790 Public method to get all toolbars and their actions.
741 791
742 @return reference to dictionary of toolbar IDs as key and list 792 @return reference to dictionary of toolbar IDs as key and list
743 of actions as values 793 of actions as values
794 @rtype dict
744 """ 795 """
745 return self.__toolBars 796 return self.__toolBars
746 797
747 def defaultToolBarActions(self, tbID): 798 def defaultToolBarActions(self, tbID):
748 """ 799 """
749 Public method to get a default toolbar's actions given its id. 800 Public method to get a default toolbar's actions given its id.
750 801
751 @param tbID id of the default toolbar object (integer) 802 @param tbID id of the default toolbar object
752 @return list of actions (list of QAction) 803 @type int
804 @return list of actions
805 @rtype list of QAction
753 """ 806 """
754 if tbID not in self.__defaultToolBars: 807 if tbID not in self.__defaultToolBars:
755 return [] 808 return []
756 return self.__defaultToolBars[tbID][:] 809 return self.__defaultToolBars[tbID][:]
757 810

eric ide

mercurial