5 |
5 |
6 """ |
6 """ |
7 Module implementing a toolbar manager class. |
7 Module implementing a toolbar manager class. |
8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import QByteArray, QDataStream, QIODevice, QObject |
10 import contextlib |
11 from PyQt6.QtWidgets import QToolBar |
11 |
12 |
12 from PyQt6.QtCore import ( |
13 from eric7 import Utilities |
13 QByteArray, |
|
14 QCoreApplication, |
|
15 QDataStream, |
|
16 QIODevice, |
|
17 QObject, |
|
18 QSize, |
|
19 pyqtSlot, |
|
20 ) |
|
21 from PyQt6.QtWidgets import QStyle, QToolBar |
|
22 |
|
23 from eric7 import Preferences, Utilities |
14 |
24 |
15 |
25 |
16 class EricToolBarManager(QObject): |
26 class EricToolBarManager(QObject): |
17 """ |
27 """ |
18 Class implementing a toolbar manager. |
28 Class implementing a toolbar manager. |
19 """ |
29 """ |
20 |
30 |
21 VersionMarker = 0xFFFF |
31 VersionMarker = 0xFFFF |
22 ToolBarMarker = 0xFEFE |
32 ToolBarMarker = 0xFEFE |
23 CustomToolBarMarker = 0xFDFD |
33 CustomToolBarMarker = 0xFDFD |
|
34 |
|
35 IconSizes = { |
|
36 # tuples with (icon size, translated size string) |
|
37 "": ( |
|
38 0, |
|
39 QCoreApplication.translate( |
|
40 "EricToolBarManager", "default (style dependent)" |
|
41 ), |
|
42 ), |
|
43 "xs": ( |
|
44 16, |
|
45 QCoreApplication.translate("EricToolBarManager", "extra small (16 px)"), |
|
46 ), |
|
47 "sm": (22, QCoreApplication.translate("EricToolBarManager", "small (22 px)")), |
|
48 "md": (32, QCoreApplication.translate("EricToolBarManager", "medium (32 px)")), |
|
49 "lg": (48, QCoreApplication.translate("EricToolBarManager", "large (48 px)")), |
|
50 "xl": ( |
|
51 64, |
|
52 QCoreApplication.translate("EricToolBarManager", "extra large (64 px)"), |
|
53 ), |
|
54 } |
24 |
55 |
25 def __init__(self, ui=None, parent=None): |
56 def __init__(self, ui=None, parent=None): |
26 """ |
57 """ |
27 Constructor |
58 Constructor |
28 |
59 |
142 @param toolBar reference to the toolbar to be managed (QToolBar) |
173 @param toolBar reference to the toolbar to be managed (QToolBar) |
143 @param category category for the toolbar (string) |
174 @param category category for the toolbar (string) |
144 """ |
175 """ |
145 if toolBar is None: |
176 if toolBar is None: |
146 return |
177 return |
|
178 |
|
179 iconSizeStr = Preferences.getIcons("IconSize") |
|
180 if iconSizeStr: |
|
181 with contextlib.suppress(KeyError): |
|
182 iconSize = EricToolBarManager.IconSizes[iconSizeStr][0] |
|
183 toolBar.setIconSize(QSize(iconSize, iconSize)) |
147 |
184 |
148 newActions = [] |
185 newActions = [] |
149 newActionsWithSeparators = [] |
186 newActionsWithSeparators = [] |
150 actions = toolBar.actions() |
187 actions = toolBar.actions() |
151 for action in actions: |
188 for action in actions: |
715 @return list of actions (list of QAction) |
752 @return list of actions (list of QAction) |
716 """ |
753 """ |
717 if tbID not in self.__defaultToolBars: |
754 if tbID not in self.__defaultToolBars: |
718 return [] |
755 return [] |
719 return self.__defaultToolBars[tbID][:] |
756 return self.__defaultToolBars[tbID][:] |
|
757 |
|
758 @pyqtSlot() |
|
759 def preferencesChanged(self): |
|
760 """ |
|
761 Public slot to handle a change of preferences. |
|
762 """ |
|
763 iconSizeStr = Preferences.getIcons("IconSize") |
|
764 if iconSizeStr: |
|
765 try: |
|
766 iconSize = EricToolBarManager.IconSizes[iconSizeStr][0] |
|
767 except KeyError: |
|
768 # determine icon size through the style |
|
769 iconSize = ( |
|
770 list(self.__allToolBars.items()[0]) |
|
771 .style() |
|
772 .pixelMetric(QStyle.PixelMetric.PM_ToolBarIconSize) |
|
773 ) |
|
774 else: |
|
775 # determine icon size through the style |
|
776 iconSize = ( |
|
777 list(self.__allToolBars.values())[0] |
|
778 .style() |
|
779 .pixelMetric(QStyle.PixelMetric.PM_ToolBarIconSize) |
|
780 ) |
|
781 |
|
782 for tbID in self.__allToolBars: |
|
783 self.__allToolBars[tbID].setIconSize(QSize(iconSize, iconSize)) |