Skip to content

Commit c71cf7b

Browse files
authored
Merge pull request #291 from ales-erjavec/application-palette-change
[FIX] Reconfigure stylesheet on application palette change
2 parents af0fac5 + 76e5981 commit c71cf7b

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

orangecanvas/application/application.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def maybe_match_prefix(prefix: str, path: str) -> Optional[str]:
110110

111111
class CanvasApplication(QApplication):
112112
fileOpenRequest = Signal(QUrl)
113+
applicationPaletteChanged = Signal()
113114

114115
__args = None
115116

@@ -160,6 +161,8 @@ def event(self, event):
160161
self.fileOpenRequest.emit(event.url())
161162
elif event.type() == QEvent.PolishRequest:
162163
self.configureStyle()
164+
elif event.type() == QEvent.Type.ApplicationPaletteChange:
165+
self.applicationPaletteChanged.emit()
163166
return super().event(event)
164167

165168
def exec(self) -> int:

orangecanvas/application/tests/test_application.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
import time
44
import unittest
55

6+
from AnyQt.QtGui import QPalette
7+
from AnyQt.QtTest import QSignalSpy
8+
69
from orangecanvas.utils import shtools as sh
710
from orangecanvas.application import application as appmod
811
from orangecanvas.utils.shtools import temp_named_file
912

1013

1114
def application_test_helper():
1215
app = appmod.CanvasApplication([])
16+
p = app.palette()
17+
spy = QSignalSpy(app.applicationPaletteChanged)
18+
p.setColor(QPalette.Base, p.color(QPalette.Text))
19+
app.setPalette(p)
20+
assert list(spy) == [[]]
1321
app.quit()
1422
return
1523

orangecanvas/application/tests/test_main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from typing import Iterable
66
from unittest.mock import patch, Mock
77

8+
from AnyQt.QtCore import Qt
9+
from AnyQt.QtGui import QPalette
10+
811
from orangecanvas import config
912
from orangecanvas.application.canvasmain import CanvasMainWindow
1013
from orangecanvas.config import Config
@@ -135,3 +138,19 @@ def test_run_with_file(self):
135138
res = m.run(["-", "--no-welcome", "--no-splash", fname])
136139
CanvasMainWindow.open_scheme_file.assert_called_with(fname)
137140
self.assertEqual(res, 42)
141+
142+
@with_patched_main_application
143+
def test_run_stylesheet_reconfigure(self):
144+
m = Main()
145+
m.parse_arguments(["-", "--config", f"{__name__}.TestConfig"])
146+
m.window = m.create_main_window()
147+
m.application = self.app
148+
149+
def setpalette(color):
150+
self.app.setPalette(QPalette(color))
151+
m._Main__reconfigure_stylesheet()
152+
153+
setpalette(Qt.white)
154+
sheet = m.window.styleSheet()
155+
setpalette(Qt.black)
156+
self.assertNotEqual(sheet, m.window.styleSheet())

orangecanvas/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""
22
"""
33
import argparse
4-
54
import os
65
import sys
76
import gc
87
import logging
98
import pickle
109
import shlex
1110
import warnings
12-
from typing import List, Optional, IO, Any
11+
from typing import List, Optional, IO, Any, Iterable
1312

1413
from urllib.request import getproxies
1514
from contextlib import ExitStack, closing
@@ -176,6 +175,7 @@ def setup_application(self):
176175
# sys.argv[0] must be in QApplication's argv list.
177176
self.application = CanvasApplication(sys.argv[:1] + self.arguments)
178177
self.application.setWindowIcon(self.config.application_icon())
178+
self.application.applicationPaletteChanged.connect(self.__reconfigure_stylesheet)
179179
# Update the arguments
180180
self.arguments = self.application.arguments()[1:]
181181
fix_set_proxy_env()
@@ -289,7 +289,7 @@ def create_main_window(self) -> CanvasMainWindow:
289289
"""Create the (initial) main window."""
290290
return CanvasMainWindow()
291291

292-
window: CanvasMainWindow
292+
window: Optional[CanvasMainWindow] = None
293293

294294
def setup_main_window(self):
295295
stylesheet = self.main_window_stylesheet()
@@ -377,6 +377,16 @@ def tear_down_sys_redirections(self):
377377
if self.__stdout__ is not None:
378378
sys.stdout = self.__stdout__
379379

380+
def _main_windows(self) -> Iterable[CanvasMainWindow]:
381+
first = self.window
382+
return (*((first,) if first else ()), *CanvasMainWindow._instances)
383+
384+
def __reconfigure_stylesheet(self) -> None:
385+
ssheet = self.main_window_stylesheet()
386+
for inst in self._main_windows():
387+
if inst.styleSheet() != ssheet:
388+
inst.setStyleSheet(ssheet)
389+
380390

381391
def fix_win_pythonw_std_stream():
382392
"""

0 commit comments

Comments
 (0)