Python-FastUI-Widgets 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. fastuiwidgets/__init__.py +12 -0
  2. fastuiwidgets/_rc/__init__.py +0 -0
  3. fastuiwidgets/_rc/resource.py +98835 -0
  4. fastuiwidgets/common/__init__.py +12 -0
  5. fastuiwidgets/common/animation.py +530 -0
  6. fastuiwidgets/common/auto_wrap.py +164 -0
  7. fastuiwidgets/common/color.py +95 -0
  8. fastuiwidgets/common/config.py +423 -0
  9. fastuiwidgets/common/exception_handler.py +31 -0
  10. fastuiwidgets/common/font.py +38 -0
  11. fastuiwidgets/common/icon.py +703 -0
  12. fastuiwidgets/common/image_utils.py +198 -0
  13. fastuiwidgets/common/overload.py +47 -0
  14. fastuiwidgets/common/router.py +133 -0
  15. fastuiwidgets/common/screen.py +25 -0
  16. fastuiwidgets/common/smooth_scroll.py +141 -0
  17. fastuiwidgets/common/style_sheet.py +512 -0
  18. fastuiwidgets/common/theme_listener.py +27 -0
  19. fastuiwidgets/common/translator.py +14 -0
  20. fastuiwidgets/components/__init__.py +6 -0
  21. fastuiwidgets/components/date_time/__init__.py +4 -0
  22. fastuiwidgets/components/date_time/calendar_picker.py +121 -0
  23. fastuiwidgets/components/date_time/calendar_view.py +671 -0
  24. fastuiwidgets/components/date_time/date_picker.py +245 -0
  25. fastuiwidgets/components/date_time/fast_calendar_view.py +487 -0
  26. fastuiwidgets/components/date_time/picker_base.py +632 -0
  27. fastuiwidgets/components/date_time/time_picker.py +223 -0
  28. fastuiwidgets/components/dialog_box/__init__.py +6 -0
  29. fastuiwidgets/components/dialog_box/color_dialog.py +414 -0
  30. fastuiwidgets/components/dialog_box/dialog.py +167 -0
  31. fastuiwidgets/components/dialog_box/folder_list_dialog.py +307 -0
  32. fastuiwidgets/components/dialog_box/mask_dialog_base.py +120 -0
  33. fastuiwidgets/components/dialog_box/message_box_base.py +92 -0
  34. fastuiwidgets/components/dialog_box/message_dialog.py +65 -0
  35. fastuiwidgets/components/layout/__init__.py +3 -0
  36. fastuiwidgets/components/layout/expand_layout.py +96 -0
  37. fastuiwidgets/components/layout/flow_layout.py +236 -0
  38. fastuiwidgets/components/layout/v_box_layout.py +41 -0
  39. fastuiwidgets/components/material/__init__.py +6 -0
  40. fastuiwidgets/components/material/acrylic_combo_box.py +96 -0
  41. fastuiwidgets/components/material/acrylic_flyout.py +105 -0
  42. fastuiwidgets/components/material/acrylic_line_edit.py +27 -0
  43. fastuiwidgets/components/material/acrylic_menu.py +204 -0
  44. fastuiwidgets/components/material/acrylic_tool_tip.py +39 -0
  45. fastuiwidgets/components/material/acrylic_widget.py +42 -0
  46. fastuiwidgets/components/navigation/__init__.py +9 -0
  47. fastuiwidgets/components/navigation/breadcrumb.py +350 -0
  48. fastuiwidgets/components/navigation/navigation_bar.py +416 -0
  49. fastuiwidgets/components/navigation/navigation_interface.py +268 -0
  50. fastuiwidgets/components/navigation/navigation_panel.py +657 -0
  51. fastuiwidgets/components/navigation/navigation_widget.py +686 -0
  52. fastuiwidgets/components/navigation/pivot.py +272 -0
  53. fastuiwidgets/components/navigation/segmented_widget.py +174 -0
  54. fastuiwidgets/components/settings/__init__.py +8 -0
  55. fastuiwidgets/components/settings/custom_color_setting_card.py +139 -0
  56. fastuiwidgets/components/settings/expand_setting_card.py +390 -0
  57. fastuiwidgets/components/settings/folder_list_setting_card.py +134 -0
  58. fastuiwidgets/components/settings/options_setting_card.py +86 -0
  59. fastuiwidgets/components/settings/setting_card.py +449 -0
  60. fastuiwidgets/components/settings/setting_card_group.py +48 -0
  61. fastuiwidgets/components/widgets/__init__.py +41 -0
  62. fastuiwidgets/components/widgets/acrylic_label.py +261 -0
  63. fastuiwidgets/components/widgets/button.py +1059 -0
  64. fastuiwidgets/components/widgets/card_widget.py +369 -0
  65. fastuiwidgets/components/widgets/check_box.py +203 -0
  66. fastuiwidgets/components/widgets/combo_box.py +556 -0
  67. fastuiwidgets/components/widgets/command_bar.py +636 -0
  68. fastuiwidgets/components/widgets/cycle_list_widget.py +251 -0
  69. fastuiwidgets/components/widgets/flip_view.py +430 -0
  70. fastuiwidgets/components/widgets/flyout.py +521 -0
  71. fastuiwidgets/components/widgets/frameless_window.py +49 -0
  72. fastuiwidgets/components/widgets/icon_widget.py +53 -0
  73. fastuiwidgets/components/widgets/info_badge.py +483 -0
  74. fastuiwidgets/components/widgets/info_bar.py +596 -0
  75. fastuiwidgets/components/widgets/label.py +553 -0
  76. fastuiwidgets/components/widgets/line_edit.py +551 -0
  77. fastuiwidgets/components/widgets/list_view.py +158 -0
  78. fastuiwidgets/components/widgets/menu.py +1318 -0
  79. fastuiwidgets/components/widgets/pips_pager.py +331 -0
  80. fastuiwidgets/components/widgets/progress_bar.py +311 -0
  81. fastuiwidgets/components/widgets/progress_ring.py +212 -0
  82. fastuiwidgets/components/widgets/scroll_area.py +125 -0
  83. fastuiwidgets/components/widgets/scroll_bar.py +673 -0
  84. fastuiwidgets/components/widgets/separator.py +43 -0
  85. fastuiwidgets/components/widgets/slider.py +307 -0
  86. fastuiwidgets/components/widgets/spin_box.py +306 -0
  87. fastuiwidgets/components/widgets/stacked_widget.py +211 -0
  88. fastuiwidgets/components/widgets/state_tool_tip.py +188 -0
  89. fastuiwidgets/components/widgets/switch_button.py +312 -0
  90. fastuiwidgets/components/widgets/tab_view.py +804 -0
  91. fastuiwidgets/components/widgets/table_view.py +360 -0
  92. fastuiwidgets/components/widgets/teaching_tip.py +657 -0
  93. fastuiwidgets/components/widgets/tool_tip.py +460 -0
  94. fastuiwidgets/components/widgets/tree_view.py +216 -0
  95. fastuiwidgets/multimedia/__init__.py +3 -0
  96. fastuiwidgets/multimedia/media_play_bar.py +319 -0
  97. fastuiwidgets/multimedia/media_player.py +124 -0
  98. fastuiwidgets/multimedia/video_widget.py +93 -0
  99. fastuiwidgets/window/__init__.py +2 -0
  100. fastuiwidgets/window/fluent_window.py +413 -0
  101. fastuiwidgets/window/splash_screen.py +92 -0
  102. fastuiwidgets/window/stacked_widget.py +66 -0
  103. python_fastui_widgets-1.0.0.dist-info/METADATA +30 -0
  104. python_fastui_widgets-1.0.0.dist-info/RECORD +107 -0
  105. python_fastui_widgets-1.0.0.dist-info/WHEEL +5 -0
  106. python_fastui_widgets-1.0.0.dist-info/licenses/LICENSE +674 -0
  107. python_fastui_widgets-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,272 @@
1
+ # coding:utf-8
2
+ from typing import Dict
3
+
4
+ from PySide6.QtCore import Qt, Signal, QRectF
5
+ from PySide6.QtGui import QPainter, QFont, QColor
6
+ from PySide6.QtWidgets import QApplication, QPushButton, QWidget, QHBoxLayout, QSizePolicy
7
+
8
+ from ...common.font import setFont
9
+ from ...common.router import qrouter
10
+ from ...common.style_sheet import themeColor, FluentStyleSheet
11
+ from ...common.color import autoFallbackThemeColor
12
+ from ...common.animation import FluentAnimation, FluentAnimationType, FluentAnimationProperty
13
+ from ..widgets.button import PushButton
14
+ from .navigation_panel import RouteKeyError
15
+
16
+
17
+ class PivotItem(PushButton):
18
+ """ Pivot item """
19
+
20
+ itemClicked = Signal(bool)
21
+
22
+ def _postInit(self):
23
+ self.isSelected = False
24
+ self.setProperty('isSelected', False)
25
+ self.clicked.connect(lambda: self.itemClicked.emit(True))
26
+ self.setAttribute(Qt.WA_LayoutUsesWidgetRect)
27
+
28
+ FluentStyleSheet.PIVOT.apply(self)
29
+ setFont(self, 18)
30
+
31
+ def setSelected(self, isSelected: bool):
32
+ if self.isSelected == isSelected:
33
+ return
34
+
35
+ self.isSelected = isSelected
36
+ self.setProperty('isSelected', isSelected)
37
+ self.setStyle(QApplication.style())
38
+ self.update()
39
+
40
+
41
+ class Pivot(QWidget):
42
+ """ Pivot """
43
+
44
+ currentItemChanged = Signal(str)
45
+
46
+ def __init__(self, parent=None):
47
+ super().__init__(parent)
48
+ self.items = {} # type: Dict[str, PivotItem]
49
+ self._currentRouteKey = None
50
+
51
+ self.lightIndicatorColor = QColor()
52
+ self.darkIndicatorColor = QColor()
53
+
54
+ self.hBoxLayout = QHBoxLayout(self)
55
+ self.slideAni = FluentAnimation.create(
56
+ FluentAnimationType.POINT_TO_POINT, FluentAnimationProperty.SCALE, value=0, parent=self)
57
+
58
+ FluentStyleSheet.PIVOT.apply(self)
59
+
60
+ self.hBoxLayout.setSpacing(0)
61
+ self.hBoxLayout.setAlignment(Qt.AlignLeft)
62
+ self.hBoxLayout.setContentsMargins(0, 0, 0, 0)
63
+ self.hBoxLayout.setSizeConstraint(QHBoxLayout.SetMinimumSize)
64
+
65
+ self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
66
+
67
+ def addItem(self, routeKey: str, text: str, onClick=None, icon=None):
68
+ """ add item
69
+
70
+ Parameters
71
+ ----------
72
+ routeKey: str
73
+ the unique name of item
74
+
75
+ text: str
76
+ the text of navigation item
77
+
78
+ onClick: callable
79
+ the slot connected to item clicked signal
80
+
81
+ icon: str
82
+ the icon of navigation item
83
+ """
84
+ return self.insertItem(-1, routeKey, text, onClick, icon)
85
+
86
+ def addWidget(self, routeKey: str, widget: PivotItem, onClick=None):
87
+ """ add widget
88
+
89
+ Parameters
90
+ ----------
91
+ routeKey: str
92
+ the unique name of item
93
+
94
+ widget: PivotItem
95
+ navigation widget
96
+
97
+ onClick: callable
98
+ the slot connected to item clicked signal
99
+ """
100
+ self.insertWidget(-1, routeKey, widget, onClick)
101
+
102
+ def insertItem(self, index: int, routeKey: str, text: str, onClick=None, icon=None):
103
+ """ insert item
104
+
105
+ Parameters
106
+ ----------
107
+ index: int
108
+ insert position
109
+
110
+ routeKey: str
111
+ the unique name of item
112
+
113
+ text: str
114
+ the text of navigation item
115
+
116
+ onClick: callable
117
+ the slot connected to item clicked signal
118
+
119
+ icon: str
120
+ the icon of navigation item
121
+ """
122
+ if routeKey in self.items:
123
+ return
124
+
125
+ item = PivotItem(text, self)
126
+ if icon:
127
+ item.setIcon(icon)
128
+
129
+ self.insertWidget(index, routeKey, item, onClick)
130
+ return item
131
+
132
+ def insertWidget(self, index: int, routeKey: str, widget: PivotItem, onClick=None):
133
+ """ insert item
134
+
135
+ Parameters
136
+ ----------
137
+ index: int
138
+ insert position
139
+
140
+ routeKey: str
141
+ the unique name of item
142
+
143
+ widget: PivotItem
144
+ navigation widget
145
+
146
+ onClick: callable
147
+ the slot connected to item clicked signal
148
+ """
149
+ if routeKey in self.items:
150
+ return
151
+
152
+ widget.setProperty('routeKey', routeKey)
153
+ widget.itemClicked.connect(self._onItemClicked)
154
+ if onClick:
155
+ widget.itemClicked.connect(onClick)
156
+
157
+ self.items[routeKey] = widget
158
+ self.hBoxLayout.insertWidget(index, widget, 1)
159
+
160
+ def removeWidget(self, routeKey: str):
161
+ """ remove widget
162
+
163
+ Parameters
164
+ ----------
165
+ routeKey: str
166
+ the unique name of item
167
+ """
168
+ if routeKey not in self.items:
169
+ return
170
+
171
+ item = self.items.pop(routeKey)
172
+ self.hBoxLayout.removeWidget(item)
173
+ qrouter.remove(routeKey)
174
+ item.deleteLater()
175
+
176
+ if not self.items:
177
+ self._currentRouteKey = None
178
+
179
+ def clear(self):
180
+ """ clear all navigation items """
181
+ for k, w in self.items.items():
182
+ self.hBoxLayout.removeWidget(w)
183
+ qrouter.remove(k)
184
+ w.deleteLater()
185
+
186
+ self.items.clear()
187
+ self._currentRouteKey = None
188
+
189
+ def currentItem(self):
190
+ """ Returns the current selected item """
191
+ if self._currentRouteKey is None:
192
+ return None
193
+
194
+ return self.widget(self._currentRouteKey)
195
+
196
+ def currentRouteKey(self):
197
+ return self._currentRouteKey
198
+
199
+ def setCurrentItem(self, routeKey: str):
200
+ """ set current selected item
201
+
202
+ Parameters
203
+ ----------
204
+ routeKey: str
205
+ the unique name of item
206
+ """
207
+ if routeKey not in self.items or routeKey == self.currentRouteKey():
208
+ return
209
+
210
+ self._currentRouteKey = routeKey
211
+ self.slideAni.startAnimation(self.widget(routeKey).x())
212
+
213
+ for k, item in self.items.items():
214
+ item.setSelected(k == routeKey)
215
+
216
+ self.currentItemChanged.emit(routeKey)
217
+
218
+ def showEvent(self, e):
219
+ super().showEvent(e)
220
+ self._adjustIndicatorPos()
221
+
222
+ def setItemFontSize(self, size: int):
223
+ """ set the pixel font size of items """
224
+ for item in self.items.values():
225
+ font = item.font()
226
+ font.setPixelSize(size)
227
+ item.setFont(font)
228
+ item.adjustSize()
229
+
230
+ def setItemText(self, routeKey: str, text: str):
231
+ """ set the text of item """
232
+ item = self.widget(routeKey)
233
+ item.setText(text)
234
+
235
+ def setIndicatorColor(self, light, dark):
236
+ self.lightIndicatorColor = QColor(light)
237
+ self.darkIndicatorColor = QColor(dark)
238
+ self.update()
239
+
240
+ def _onItemClicked(self):
241
+ item = self.sender() # type: PivotItem
242
+ self.setCurrentItem(item.property('routeKey'))
243
+
244
+ def widget(self, routeKey: str):
245
+ if routeKey not in self.items:
246
+ raise RouteKeyError(f"`{routeKey}` is illegal.")
247
+
248
+ return self.items[routeKey]
249
+
250
+ def resizeEvent(self, e) -> None:
251
+ super().resizeEvent(e)
252
+ self._adjustIndicatorPos()
253
+
254
+ def _adjustIndicatorPos(self):
255
+ item = self.currentItem()
256
+ if item:
257
+ self.slideAni.stop()
258
+ self.slideAni.setValue(item.x())
259
+
260
+ def paintEvent(self, e):
261
+ super().paintEvent(e)
262
+
263
+ if not self.currentItem():
264
+ return
265
+
266
+ painter = QPainter(self)
267
+ painter.setRenderHints(QPainter.Antialiasing)
268
+ painter.setPen(Qt.NoPen)
269
+ painter.setBrush(autoFallbackThemeColor(self.lightIndicatorColor, self.darkIndicatorColor))
270
+
271
+ x = int(self.currentItem().width() / 2 - 8 + self.slideAni.value())
272
+ painter.drawRoundedRect(x, self.height() - 3, 16, 3, 1.5, 1.5)
@@ -0,0 +1,174 @@
1
+ # coding:utf-8
2
+ from typing import Union
3
+ from PySide6.QtCore import Qt, Signal, QRectF
4
+ from PySide6.QtGui import QPainter, QIcon, QColor
5
+ from PySide6.QtWidgets import QApplication, QWidget
6
+
7
+ from ...common.font import setFont
8
+ from ...common.icon import FluentIconBase, drawIcon, Theme
9
+ from ...common.color import autoFallbackThemeColor
10
+ from ...common.style_sheet import themeColor, FluentStyleSheet, isDarkTheme
11
+ from ..widgets.button import PushButton, ToolButton, TransparentToolButton
12
+ from .pivot import Pivot, PivotItem
13
+
14
+
15
+ class SegmentedItem(PivotItem):
16
+ """ Segmented item """
17
+
18
+ def _postInit(self):
19
+ super()._postInit()
20
+ setFont(self, 14)
21
+
22
+
23
+ class SegmentedToolItem(ToolButton):
24
+ """ Pivot item """
25
+
26
+ itemClicked = Signal(bool)
27
+
28
+ def _postInit(self):
29
+ self.isSelected = False
30
+ self.setProperty('isSelected', False)
31
+ self.clicked.connect(lambda: self.itemClicked.emit(True))
32
+
33
+ self.setFixedSize(38, 33)
34
+ FluentStyleSheet.PIVOT.apply(self)
35
+
36
+ def setSelected(self, isSelected: bool):
37
+ if self.isSelected == isSelected:
38
+ return
39
+
40
+ self.isSelected = isSelected
41
+ self.setProperty('isSelected', isSelected)
42
+ self.setStyle(QApplication.style())
43
+ self.update()
44
+
45
+
46
+ class SegmentedToggleToolItem(TransparentToolButton):
47
+
48
+ itemClicked = Signal(bool)
49
+
50
+ def _postInit(self):
51
+ super()._postInit()
52
+ self.isSelected = False
53
+
54
+ self.setFixedSize(50, 32)
55
+ self.clicked.connect(lambda: self.itemClicked.emit(True))
56
+
57
+ def setSelected(self, isSelected: bool):
58
+ if self.isSelected == isSelected:
59
+ return
60
+
61
+ self.isSelected = isSelected
62
+ self.setChecked(isSelected)
63
+
64
+ def _drawIcon(self, icon, painter: QPainter, rect: QRectF, state=QIcon.State.Off):
65
+ if self.isSelected and isinstance(icon, FluentIconBase):
66
+ theme = Theme.DARK if not isDarkTheme() else Theme.LIGHT
67
+ icon = icon.icon(theme)
68
+
69
+ return drawIcon(icon, painter, rect, state)
70
+
71
+
72
+ class SegmentedWidget(Pivot):
73
+ """ Segmented widget """
74
+
75
+ def __init__(self, parent=None):
76
+ super().__init__(parent)
77
+ self.setAttribute(Qt.WA_StyledBackground)
78
+
79
+ def insertItem(self, index: int, routeKey: str, text: str, onClick=None, icon=None):
80
+ if routeKey in self.items:
81
+ return
82
+
83
+ item = SegmentedItem(text, self)
84
+ if icon:
85
+ item.setIcon(icon)
86
+
87
+ self.insertWidget(index, routeKey, item, onClick)
88
+ return item
89
+
90
+ def paintEvent(self, e):
91
+ QWidget.paintEvent(self, e)
92
+
93
+ if not self.currentItem():
94
+ return
95
+
96
+ painter = QPainter(self)
97
+ painter.setRenderHints(QPainter.Antialiasing)
98
+
99
+ # draw background
100
+ if isDarkTheme():
101
+ painter.setPen(QColor(255, 255, 255, 14))
102
+ painter.setBrush(QColor(255, 255, 255, 15))
103
+ else:
104
+ painter.setPen(QColor(0, 0, 0, 19))
105
+ painter.setBrush(QColor(255, 255, 255, 179))
106
+
107
+ item = self.currentItem()
108
+ rect = item.rect().adjusted(1, 1, -1, -1).translated(int(self.slideAni.value()), 0)
109
+ painter.drawRoundedRect(rect, 5, 5)
110
+
111
+ # draw indicator
112
+ painter.setPen(Qt.PenStyle.NoPen)
113
+ painter.setBrush(autoFallbackThemeColor(self.lightIndicatorColor, self.darkIndicatorColor))
114
+
115
+ x = int(self.currentItem().width() / 2 - 8 + self.slideAni.value())
116
+ painter.drawRoundedRect(QRectF(x, self.height() - 3.5, 16, 3), 1.5, 1.5)
117
+
118
+
119
+ class SegmentedToolWidget(SegmentedWidget):
120
+ """ Segmented tool widget """
121
+
122
+ def __init__(self, parent=None):
123
+ super().__init__(parent)
124
+ self.setAttribute(Qt.WA_StyledBackground)
125
+
126
+ def addItem(self, routeKey: str, icon: Union[str, QIcon, FluentIconBase], onClick=None):
127
+ """ add item
128
+
129
+ Parameters
130
+ ----------
131
+ routeKey: str
132
+ the unique name of item
133
+
134
+ icon: str | QIcon | FluentIconBase
135
+ the icon of navigation item
136
+
137
+ onClick: callable
138
+ the slot connected to item clicked signal
139
+ """
140
+ return self.insertItem(-1, routeKey, icon, onClick)
141
+
142
+ def insertItem(self, index: int, routeKey: str, icon: Union[str, QIcon, FluentIconBase], onClick=None):
143
+ if routeKey in self.items:
144
+ return
145
+
146
+ item = self._createItem(icon)
147
+ self.insertWidget(index, routeKey, item, onClick)
148
+ return item
149
+
150
+ def _createItem(self, icon):
151
+ return SegmentedToolItem(icon)
152
+
153
+
154
+ class SegmentedToggleToolWidget(SegmentedToolWidget):
155
+ """ Segmented toggle tool widget """
156
+
157
+ def _createItem(self, icon):
158
+ return SegmentedToggleToolItem(icon)
159
+
160
+ def paintEvent(self, e):
161
+ QWidget.paintEvent(self, e)
162
+
163
+ if not self.currentItem():
164
+ return
165
+
166
+ painter = QPainter(self)
167
+ painter.setRenderHints(QPainter.RenderHint.Antialiasing)
168
+
169
+ painter.setPen(Qt.PenStyle.NoPen)
170
+ painter.setBrush(autoFallbackThemeColor(self.lightIndicatorColor, self.darkIndicatorColor))
171
+
172
+ item = self.currentItem()
173
+ painter.drawRoundedRect(
174
+ QRectF(self.slideAni.value(), 0, item.width(), item.height()), 4, 4)
@@ -0,0 +1,8 @@
1
+ from .setting_card import (SettingCard, SwitchSettingCard, RangeSettingCard,
2
+ PushSettingCard, ColorSettingCard, HyperlinkCard,
3
+ PrimaryPushSettingCard, ColorPickerButton, ComboBoxSettingCard)
4
+ from .expand_setting_card import ExpandSettingCard, ExpandGroupSettingCard, SimpleExpandGroupSettingCard
5
+ from .folder_list_setting_card import FolderListSettingCard
6
+ from .options_setting_card import OptionsSettingCard
7
+ from .custom_color_setting_card import CustomColorSettingCard
8
+ from .setting_card_group import SettingCardGroup
@@ -0,0 +1,139 @@
1
+ # coding:utf-8
2
+ from typing import Union
3
+ from PySide6.QtCore import Qt, Signal
4
+ from PySide6.QtGui import QIcon, QColor
5
+ from PySide6.QtWidgets import QWidget, QLabel, QButtonGroup, QVBoxLayout, QPushButton, QHBoxLayout
6
+
7
+ from ..dialog_box import ColorDialog
8
+ from .expand_setting_card import ExpandGroupSettingCard
9
+ from ..widgets.button import RadioButton
10
+ from ...common.config import qconfig, ColorConfigItem
11
+ from ...common.icon import FluentIconBase
12
+
13
+
14
+ class CustomColorSettingCard(ExpandGroupSettingCard):
15
+ """ Custom color setting card """
16
+
17
+ colorChanged = Signal(QColor)
18
+
19
+ def __init__(self, configItem: ColorConfigItem, icon: Union[str, QIcon, FluentIconBase], title: str,
20
+ content=None, parent=None, enableAlpha=False):
21
+ """
22
+ Parameters
23
+ ----------
24
+ configItem: ColorConfigItem
25
+ options config item
26
+
27
+ icon: str | QIcon | FluentIconBase
28
+ the icon to be drawn
29
+
30
+ title: str
31
+ the title of setting card
32
+
33
+ content: str
34
+ the content of setting card
35
+
36
+ parent: QWidget
37
+ parent window
38
+
39
+ enableAlpha: bool
40
+ whether to enable the alpha channel
41
+ """
42
+ super().__init__(icon, title, content, parent=parent)
43
+ self.enableAlpha = enableAlpha
44
+ self.configItem = configItem
45
+ self.defaultColor = QColor(configItem.defaultValue)
46
+ self.customColor = QColor(qconfig.get(configItem))
47
+
48
+ self.choiceLabel = QLabel(self)
49
+
50
+ self.radioWidget = QWidget(self.view)
51
+ self.radioLayout = QVBoxLayout(self.radioWidget)
52
+ self.defaultRadioButton = RadioButton(
53
+ self.tr('Default color'), self.radioWidget)
54
+ self.customRadioButton = RadioButton(
55
+ self.tr('Custom color'), self.radioWidget)
56
+ self.buttonGroup = QButtonGroup(self)
57
+
58
+ self.customColorWidget = QWidget(self.view)
59
+ self.customColorLayout = QHBoxLayout(self.customColorWidget)
60
+ self.customLabel = QLabel(
61
+ self.tr('Custom color'), self.customColorWidget)
62
+ self.chooseColorButton = QPushButton(
63
+ self.tr('Choose color'), self.customColorWidget)
64
+
65
+ self.__initWidget()
66
+
67
+ def __initWidget(self):
68
+ self.__initLayout()
69
+
70
+ if self.defaultColor != self.customColor:
71
+ self.customRadioButton.setChecked(True)
72
+ self.chooseColorButton.setEnabled(True)
73
+ else:
74
+ self.defaultRadioButton.setChecked(True)
75
+ self.chooseColorButton.setEnabled(False)
76
+
77
+ self.choiceLabel.setText(self.buttonGroup.checkedButton().text())
78
+ self.choiceLabel.adjustSize()
79
+
80
+ self.choiceLabel.setObjectName("titleLabel")
81
+ self.customLabel.setObjectName("titleLabel")
82
+ self.chooseColorButton.setObjectName('chooseColorButton')
83
+
84
+ self.buttonGroup.buttonClicked.connect(self.__onRadioButtonClicked)
85
+ self.chooseColorButton.clicked.connect(self.__showColorDialog)
86
+
87
+ def __initLayout(self):
88
+ self.addWidget(self.choiceLabel)
89
+
90
+ self.radioLayout.setSpacing(19)
91
+ self.radioLayout.setAlignment(Qt.AlignTop)
92
+ self.radioLayout.setContentsMargins(48, 18, 0, 18)
93
+ self.buttonGroup.addButton(self.customRadioButton)
94
+ self.buttonGroup.addButton(self.defaultRadioButton)
95
+ self.radioLayout.addWidget(self.customRadioButton)
96
+ self.radioLayout.addWidget(self.defaultRadioButton)
97
+ self.radioLayout.setSizeConstraint(QVBoxLayout.SetMinimumSize)
98
+
99
+ self.customColorLayout.setContentsMargins(48, 18, 44, 18)
100
+ self.customColorLayout.addWidget(self.customLabel, 0, Qt.AlignLeft)
101
+ self.customColorLayout.addWidget(self.chooseColorButton, 0, Qt.AlignRight)
102
+ self.customColorLayout.setSizeConstraint(QHBoxLayout.SetMinimumSize)
103
+
104
+ self.viewLayout.setSpacing(0)
105
+ self.viewLayout.setContentsMargins(0, 0, 0, 0)
106
+ self.addGroupWidget(self.radioWidget)
107
+ self.addGroupWidget(self.customColorWidget)
108
+
109
+ def __onRadioButtonClicked(self, button: RadioButton):
110
+ """ radio button clicked slot """
111
+ if button.text() == self.choiceLabel.text():
112
+ return
113
+
114
+ self.choiceLabel.setText(button.text())
115
+ self.choiceLabel.adjustSize()
116
+
117
+ if button is self.defaultRadioButton:
118
+ self.chooseColorButton.setDisabled(True)
119
+ qconfig.set(self.configItem, self.defaultColor)
120
+ if self.defaultColor != self.customColor:
121
+ self.colorChanged.emit(self.defaultColor)
122
+ else:
123
+ self.chooseColorButton.setDisabled(False)
124
+ qconfig.set(self.configItem, self.customColor)
125
+ if self.defaultColor != self.customColor:
126
+ self.colorChanged.emit(self.customColor)
127
+
128
+ def __showColorDialog(self):
129
+ """ show color dialog """
130
+ w = ColorDialog(
131
+ qconfig.get(self.configItem), self.tr('Choose color'), self.window(), self.enableAlpha)
132
+ w.colorChanged.connect(self.__onCustomColorChanged)
133
+ w.exec()
134
+
135
+ def __onCustomColorChanged(self, color):
136
+ """ custom color changed slot """
137
+ qconfig.set(self.configItem, color)
138
+ self.customColor = QColor(color)
139
+ self.colorChanged.emit(color)