Skip to content

Commit 8e34d7e

Browse files
authored
Avoid duplicate use of overridden standard icons in setup_theme() (#213)
* Separate definition that override Qt standard icons * Refactor * Fix JSONDecodeError * Fix JSONDecodeError
1 parent ed6310b commit 8e34d7e

File tree

8 files changed

+230
-146
lines changed

8 files changed

+230
-146
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ build-backend = "poetry.core.masonry.api"
8282

8383
[tool.flake8]
8484
max-line-length = 104
85-
max-complexity = 6
85+
max-complexity = 9
8686
docstring-convention = "google"
8787
per-file-ignores = ["**/__init__.py:F401", "examples/**:D103,D100"]
8888

qdarktheme/_main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ def setup_theme(
160160
raise Exception("setup_theme() must be called after instantiation of QApplication.")
161161
if theme != "auto":
162162
stop_sync()
163+
app.setProperty("_qdarktheme_use_setup_style", True)
163164

164165
def callback():
165166
_apply_style(

qdarktheme/_resources/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
from qdarktheme._resources._color_values import COLOR_VALUES
1111
from qdarktheme._resources._palette import mk_q_palette
1212
from qdarktheme._resources._svg import SVG_RESOURCES
13-
from qdarktheme._resources._template_stylesheet import TEMPLATE_STYLESHEET
13+
from qdarktheme._resources._template_stylesheet import (
14+
TEMPLATE_STANDARD_ICONS_STYLESHEET,
15+
TEMPLATE_STYLESHEET,
16+
)
1417

1518
THEMES = ("dark", "light", "auto")

qdarktheme/_resources/_template_stylesheet.py

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,6 @@
2424
selection-background-color: {{ foreground|color(state="disabledSelectionBackground") }};
2525
selection-color: {{ foreground|color(state="disabled") }};
2626
}
27-
QWidget {
28-
backward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
29-
forward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
30-
leftarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
31-
rightarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
32-
downarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=180) }};
33-
uparrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward") }};
34-
dockwidget-close-icon: {{ foreground|color(state="icon")|url(id="close") }};
35-
{{ foreground|color(state="icon")|url(id="close")|env(value="lineedit-clear-button-icon: ${};", version=">=6.0.0") }}
36-
home-icon: {{ foreground|color(state="icon")|url(id="home") }};
37-
trash-icon: {{ foreground|color(state="icon")|url(id="delete") }};
38-
filedialog-parent-directory-icon: {{ foreground|color(state="icon")|url(id="arrow_upward") }};
39-
filedialog-new-directory-icon: {{ foreground|color(state="icon")|url(id="create_new_folder") }};
40-
filedialog-detailedview-icon: {{ foreground|color(state="icon")|url(id="list") }};
41-
filedialog-listview-icon: {{ foreground|color(state="icon")|url(id="grid_view") }};
42-
filedialog-infoview-icon: {{ foreground|color(state="icon")|url(id="info") }};
43-
filedialog-start-icon: {{ foreground|color(state="icon")|url(id="drive_file_move") }};
44-
filedialog-end-icon: {{ foreground|color(state="icon")|url(id="drive_file_move_rtl") }};
45-
filedialog-contentsview-icon: {{ foreground|color(state="icon")|url(id="search") }};
46-
titlebar-close-icon: {{ foreground|color(state="icon")|url(id="close") }};
47-
titlebar-normal-icon: {{ foreground|color(state="icon")|url(id="flip_to_front") }};
48-
titlebar-maximize-icon: {{ foreground|color(state="icon")|url(id="fullscreen") }};
49-
titlebar-minimize-icon: {{ foreground|color(state="icon")|url(id="minimize") }};
50-
titlebar-contexthelp-icon: {{ foreground|color(state="icon")|url(id="question_mark") }};
51-
titlebar-shade-icon: {{ foreground|color(state="icon")|url(id="chevron_right", rotate=270) }};
52-
titlebar-unshade-icon: {{ foreground|color(state="icon")|url(id="chevron_right", rotate=90) }};
53-
}
54-
QCommandLinkButton {
55-
qproperty-icon: {{ primary|color|url(id="east") }};
56-
}
5727
QCheckBox:!window,
5828
QRadioButton:!window,
5929
QPushButton:!window,
@@ -201,12 +171,6 @@
201171
QToolBar > QToolButton:checked:enabled {
202172
background: {{ toolbar.activeBackground|color }};
203173
}
204-
QToolBar > QToolBarExtension {
205-
qproperty-icon: {{ foreground|color(state="icon")|url(id="double_arrow") }};
206-
}
207-
QToolBar > QToolBarExtension:disabled {
208-
qproperty-icon: {{ foreground|color(state="disabled")|url(id="double_arrow") }};
209-
}
210174
QToolBar > QWidget {
211175
background: transparent;
212176
}
@@ -998,12 +962,6 @@
998962
{{ corner-shape|corner(size=4)|env(value="border-radius: ${}px;", version="<6.0.0", os="Darwin") }}
999963
{{ primary|color(state="table.selectionBackground")|env(value="selection-background-color: ${};", version="<6.0.0") }}
1000964
}
1001-
QCalendarWidget > .QWidget > QToolButton#qt_calendar_prevmonth {
1002-
qproperty-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
1003-
}
1004-
QCalendarWidget > .QWidget > QToolButton#qt_calendar_nextmonth {
1005-
qproperty-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
1006-
}
1007965
QLineEdit,
1008966
QAbstractSpinBox {
1009967
padding: 3px 4px;
@@ -1168,3 +1126,35 @@
11681126
}
11691127
11701128
""" # noqa: E501
1129+
TEMPLATE_STANDARD_ICONS_STYLESHEET = """
1130+
QCalendarWidget {
1131+
leftarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
1132+
rightarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
1133+
}
1134+
QCommandLinkButton {
1135+
qproperty-icon: {{ foreground|color(state="icon")|url(id="east") }};
1136+
}
1137+
QDockWidget,
1138+
QMdiSubWindow {
1139+
titlebar-close-icon: {{ foreground|color(state="icon")|url(id="close") }};
1140+
titlebar-normal-icon: {{ foreground|color(state="icon")|url(id="flip_to_front") }};
1141+
}
1142+
QFileDialog {
1143+
backward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
1144+
filedialog-detailedview-icon: {{ foreground|color(state="icon")|url(id="list") }};
1145+
filedialog-listview-icon: {{ foreground|color(state="icon")|url(id="grid_view") }};
1146+
filedialog-new-directory-icon: {{ foreground|color(state="icon")|url(id="create_new_folder") }};
1147+
filedialog-parent-directory-icon: {{ foreground|color(state="icon")|url(id="arrow_upward") }};
1148+
forward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
1149+
}
1150+
QLineEdit {
1151+
{{ foreground|color(state="icon")|url(id="close")|env(value="lineedit-clear-button-icon:${};",version=">=6.0.0") }};
1152+
}
1153+
QMdiSubWindow {
1154+
titlebar-maximize-icon: {{ foreground|color(state="icon")|url(id="fullscreen") }};
1155+
titlebar-minimize-icon: {{ foreground|color(state="icon")|url(id="minimize") }};
1156+
}
1157+
QToolBarExtension {
1158+
qproperty-icon: {{ foreground|color(state="icon")|url(id="double_arrow") }};
1159+
}
1160+
""" # noqa: E501

qdarktheme/_style_loader.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,19 @@ def load_stylesheet(
160160

161161
get_cash_root_path(__version__).mkdir(parents=True, exist_ok=True)
162162

163+
stylesheet = _resources.TEMPLATE_STYLESHEET
164+
try:
165+
from qdarktheme.qtpy.QtCore import QCoreApplication
166+
167+
app = QCoreApplication.instance()
168+
if app is not None and not app.property("_qdarktheme_use_setup_style"):
169+
stylesheet += _resources.TEMPLATE_STANDARD_ICONS_STYLESHEET
170+
except Exception: # noqa: PIE786
171+
pass
172+
163173
# Build stylesheet
164174
template = Template(
165-
_resources.TEMPLATE_STYLESHEET,
175+
stylesheet,
166176
{"color": filter.color, "corner": filter.corner, "env": filter.env, "url": filter.url},
167177
)
168178
replacements = dict(color_values, **{"corner-shape": corner_shape})

style/base.qss

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,6 @@ QWidget:disabled {
2626
selection-color: {{ foreground|color(state="disabled") }};
2727
}
2828

29-
/* Override default icons -------------------------------------------------
30-
31-
document: https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-icons
32-
33-
--------------------------------------------------------------------------- */
34-
QWidget {
35-
backward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
36-
forward-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
37-
leftarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
38-
rightarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
39-
downarrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=180) }};
40-
uparrow-icon: {{ foreground|color(state="icon")|url(id="arrow_upward") }};
41-
42-
dockwidget-close-icon: {{ foreground|color(state="icon")|url(id="close") }};
43-
44-
/* lineedit-clear-button-icon is supported only Qt6. */
45-
{{ foreground|color(state="icon")|url(id="close")|env(value="lineedit-clear-button-icon: ${};", version=">=6.0.0") }}
46-
47-
home-icon: {{ foreground|color(state="icon")|url(id="home") }};
48-
trash-icon: {{ foreground|color(state="icon")|url(id="delete") }};
49-
50-
filedialog-parent-directory-icon: {{ foreground|color(state="icon")|url(id="arrow_upward") }};
51-
filedialog-new-directory-icon: {{ foreground|color(state="icon")|url(id="create_new_folder") }};
52-
filedialog-detailedview-icon: {{ foreground|color(state="icon")|url(id="list") }};
53-
filedialog-listview-icon: {{ foreground|color(state="icon")|url(id="grid_view") }};
54-
filedialog-infoview-icon: {{ foreground|color(state="icon")|url(id="info") }};
55-
filedialog-start-icon: {{ foreground|color(state="icon")|url(id="drive_file_move") }};
56-
filedialog-end-icon: {{ foreground|color(state="icon")|url(id="drive_file_move_rtl") }};
57-
filedialog-contentsview-icon: {{ foreground|color(state="icon")|url(id="search") }};
58-
59-
titlebar-close-icon: {{ foreground|color(state="icon")|url(id="close") }};
60-
titlebar-normal-icon: {{ foreground|color(state="icon")|url(id="flip_to_front") }};
61-
titlebar-maximize-icon: {{ foreground|color(state="icon")|url(id="fullscreen") }};
62-
titlebar-minimize-icon: {{ foreground|color(state="icon")|url(id="minimize") }};
63-
titlebar-contexthelp-icon: {{ foreground|color(state="icon")|url(id="question_mark") }};
64-
titlebar-shade-icon: {{ foreground|color(state="icon")|url(id="chevron_right", rotate=270) }};
65-
titlebar-unshade-icon: {{ foreground|color(state="icon")|url(id="chevron_right", rotate=90) }};
66-
}
67-
68-
QCommandLinkButton {
69-
qproperty-icon: {{ primary|color|url(id="east") }};
70-
}
71-
7229
/* Top level widget -------------------------------------------------------
7330

7431
--------------------------------------------------------------------------- */
@@ -270,12 +227,6 @@ QToolBar > QToolButton::menu-button:pressed:enabled,
270227
QToolBar > QToolButton:checked:enabled {
271228
background: {{ toolbar.activeBackground|color }};
272229
}
273-
QToolBar > QToolBarExtension {
274-
qproperty-icon: {{ foreground|color(state="icon")|url(id="double_arrow") }};
275-
}
276-
QToolBar > QToolBarExtension:disabled {
277-
qproperty-icon: {{ foreground|color(state="disabled")|url(id="double_arrow") }};
278-
}
279230

280231
QToolBar > QWidget {
281232
background: transparent;
@@ -1238,17 +1189,6 @@ QCalendarWidget > QTableView {
12381189
{{ primary|color(state="table.selectionBackground")|env(value="selection-background-color: ${};", version="<6.0.0") }}
12391190
}
12401191

1241-
/*
1242-
The following setting is need when displaying QCalendarWidget as part of the layout.
1243-
This is not necessary when displaying QCalendar as a popup of QDateTimeEdit.
1244-
*/
1245-
QCalendarWidget > .QWidget > QToolButton#qt_calendar_prevmonth {
1246-
qproperty-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=270) }};
1247-
}
1248-
QCalendarWidget > .QWidget > QToolButton#qt_calendar_nextmonth {
1249-
qproperty-icon: {{ foreground|color(state="icon")|url(id="arrow_upward", rotate=90) }};
1250-
}
1251-
12521192
/* QAbstractSpinBox QLineEdit----------------------------------------------
12531193

12541194
examples: https://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qlineedit

0 commit comments

Comments
 (0)