Skip to content

Commit 75e3295

Browse files
committed
avoid circular imports
1 parent dbc9b8e commit 75e3295

File tree

4 files changed

+81
-73
lines changed

4 files changed

+81
-73
lines changed

orangecanvas/canvas/items/nodeitem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
from .utils import (
3838
radial_gradient, linspace, qpainterpath_sub_path, clip
3939
)
40-
from ...gui.utils import create_palette, default_palette, disconnected
40+
from ...gui.utils import disconnected
41+
from ...gui.palette_utils import create_palette, default_palette
4142
from ...scheme.node import UserMessage
4243
from ...registry import WidgetDescription, CategoryDescription, InputSignal, \
4344
OutputSignal

orangecanvas/gui/palette_utils.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from AnyQt.QtWidgets import (
2+
QApplication
3+
)
4+
from AnyQt.QtGui import (
5+
QColor, QPalette
6+
)
7+
8+
from orangecanvas.registry import NAMED_COLORS, DEFAULT_COLOR
9+
from orangecanvas.gui.utils import saturated, foreground_for_background
10+
11+
12+
def create_palette(background):
13+
# type: (Any) -> QPalette
14+
"""
15+
Return a new :class:`QPalette` from for the :class:`NodeBodyItem`.
16+
"""
17+
app = QApplication.instance()
18+
darkMode = app.property('darkMode')
19+
20+
defaults = {
21+
# Canvas widget background radial gradient
22+
QPalette.Light:
23+
lambda color: saturated(color, 50),
24+
QPalette.Midlight:
25+
lambda color: saturated(color, 90),
26+
QPalette.Button:
27+
lambda color: color,
28+
# Canvas widget shadow
29+
QPalette.Shadow:
30+
lambda color: saturated(color, 125) if darkMode else
31+
saturated(color, 150),
32+
33+
# Category tab color
34+
QPalette.Highlight:
35+
lambda color: color,
36+
37+
QPalette.HighlightedText:
38+
lambda color: foreground_for_background(color),
39+
}
40+
41+
palette = QPalette()
42+
43+
if isinstance(background, dict):
44+
if app.property('darkMode'):
45+
background = background.get('dark', next(iter(background.values())))
46+
else:
47+
background = background.get('light', next(iter(background.values())))
48+
base_color = background[QPalette.Button]
49+
base_color = QColor(base_color)
50+
else:
51+
color = NAMED_COLORS.get(background, background)
52+
color = QColor(background)
53+
if color.isValid():
54+
base_color = color
55+
else:
56+
color = NAMED_COLORS[DEFAULT_COLOR]
57+
base_color = QColor(color)
58+
59+
for role, default in defaults.items():
60+
if isinstance(background, dict) and role in background:
61+
v = background[role]
62+
color = NAMED_COLORS.get(v, v)
63+
color = QColor(color)
64+
if color.isValid():
65+
palette.setColor(role, color)
66+
continue
67+
color = default(base_color)
68+
palette.setColor(role, color)
69+
70+
return palette
71+
72+
73+
def default_palette():
74+
# type: () -> QPalette
75+
"""
76+
Create and return a default palette for a node.
77+
"""
78+
return create_palette(DEFAULT_COLOR)

orangecanvas/gui/utils.py

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
from AnyQt.QtCore import Qt, QPointF, QPoint, QRect, QRectF, Signal, QEvent
2727
from AnyQt import sip
2828

29-
from orangecanvas.registry import NAMED_COLORS, DEFAULT_COLOR
30-
3129

3230
@contextmanager
3331
def updates_disabled(widget):
@@ -354,75 +352,6 @@ def saturated(color, factor=150):
354352
return QColor.fromHsvF(h, s, v, a).convertTo(color.spec())
355353

356354

357-
def create_palette(background):
358-
# type: (Any) -> QPalette
359-
"""
360-
Return a new :class:`QPalette` from for the :class:`NodeBodyItem`.
361-
"""
362-
app = QApplication.instance()
363-
darkMode = app.property('darkMode')
364-
365-
defaults = {
366-
# Canvas widget background radial gradient
367-
QPalette.Light:
368-
lambda color: saturated(color, 50),
369-
QPalette.Midlight:
370-
lambda color: saturated(color, 90),
371-
QPalette.Button:
372-
lambda color: color,
373-
# Canvas widget shadow
374-
QPalette.Shadow:
375-
lambda color: saturated(color, 125) if darkMode else
376-
saturated(color, 150),
377-
378-
# Category tab color
379-
QPalette.Highlight:
380-
lambda color: color,
381-
382-
QPalette.HighlightedText:
383-
lambda color: foreground_for_background(color),
384-
}
385-
386-
palette = QPalette()
387-
388-
if isinstance(background, dict):
389-
if app.property('darkMode'):
390-
background = background.get('dark', next(iter(background.values())))
391-
else:
392-
background = background.get('light', next(iter(background.values())))
393-
base_color = background[QPalette.Button]
394-
base_color = QColor(base_color)
395-
else:
396-
color = NAMED_COLORS.get(background, background)
397-
color = QColor(background)
398-
if color.isValid():
399-
base_color = color
400-
else:
401-
color = NAMED_COLORS[DEFAULT_COLOR]
402-
base_color = QColor(color)
403-
404-
for role, default in defaults.items():
405-
if isinstance(background, dict) and role in background:
406-
v = background[role]
407-
color = NAMED_COLORS.get(v, v)
408-
color = QColor(color)
409-
if color.isValid():
410-
palette.setColor(role, color)
411-
continue
412-
color = default(base_color)
413-
palette.setColor(role, color)
414-
415-
return palette
416-
417-
418-
def default_palette():
419-
# type: () -> QPalette
420-
"""
421-
Create and return a default palette for a node.
422-
"""
423-
return create_palette(DEFAULT_COLOR)
424-
425-
426355
def create_gradient(base_color: QColor, stop=QPointF(0, 0),
427356
finalStop=QPointF(0, 1)) -> QLinearGradient:
428357
"""

orangecanvas/registry/qt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from AnyQt.QtCore import QObject, Qt
1616
from AnyQt.QtCore import pyqtSignal as Signal
1717

18-
from ..gui.utils import create_palette, default_palette
18+
from ..gui.palette_utils import create_palette, default_palette
1919
from ..utils import type_str
2020
from .discovery import WidgetDiscovery
2121
from .description import WidgetDescription, CategoryDescription

0 commit comments

Comments
 (0)