Skip to content

Commit

Permalink
avoid circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Mar 7, 2023
1 parent dbc9b8e commit 75e3295
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 73 deletions.
3 changes: 2 additions & 1 deletion orangecanvas/canvas/items/nodeitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from .utils import (
radial_gradient, linspace, qpainterpath_sub_path, clip
)
from ...gui.utils import create_palette, default_palette, disconnected
from ...gui.utils import disconnected
from ...gui.palette_utils import create_palette, default_palette
from ...scheme.node import UserMessage
from ...registry import WidgetDescription, CategoryDescription, InputSignal, \
OutputSignal
Expand Down
78 changes: 78 additions & 0 deletions orangecanvas/gui/palette_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from AnyQt.QtWidgets import (
QApplication
)
from AnyQt.QtGui import (
QColor, QPalette
)

from orangecanvas.registry import NAMED_COLORS, DEFAULT_COLOR
from orangecanvas.gui.utils import saturated, foreground_for_background


def create_palette(background):
# type: (Any) -> QPalette
"""
Return a new :class:`QPalette` from for the :class:`NodeBodyItem`.
"""
app = QApplication.instance()
darkMode = app.property('darkMode')

defaults = {
# Canvas widget background radial gradient
QPalette.Light:
lambda color: saturated(color, 50),
QPalette.Midlight:
lambda color: saturated(color, 90),
QPalette.Button:
lambda color: color,
# Canvas widget shadow
QPalette.Shadow:
lambda color: saturated(color, 125) if darkMode else
saturated(color, 150),

# Category tab color
QPalette.Highlight:
lambda color: color,

QPalette.HighlightedText:
lambda color: foreground_for_background(color),
}

palette = QPalette()

if isinstance(background, dict):
if app.property('darkMode'):
background = background.get('dark', next(iter(background.values())))
else:
background = background.get('light', next(iter(background.values())))
base_color = background[QPalette.Button]
base_color = QColor(base_color)
else:
color = NAMED_COLORS.get(background, background)
color = QColor(background)
if color.isValid():
base_color = color
else:
color = NAMED_COLORS[DEFAULT_COLOR]
base_color = QColor(color)

for role, default in defaults.items():
if isinstance(background, dict) and role in background:
v = background[role]
color = NAMED_COLORS.get(v, v)
color = QColor(color)
if color.isValid():
palette.setColor(role, color)
continue
color = default(base_color)
palette.setColor(role, color)

return palette


def default_palette():
# type: () -> QPalette
"""
Create and return a default palette for a node.
"""
return create_palette(DEFAULT_COLOR)
71 changes: 0 additions & 71 deletions orangecanvas/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
from AnyQt.QtCore import Qt, QPointF, QPoint, QRect, QRectF, Signal, QEvent
from AnyQt import sip

from orangecanvas.registry import NAMED_COLORS, DEFAULT_COLOR


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


def create_palette(background):
# type: (Any) -> QPalette
"""
Return a new :class:`QPalette` from for the :class:`NodeBodyItem`.
"""
app = QApplication.instance()
darkMode = app.property('darkMode')

defaults = {
# Canvas widget background radial gradient
QPalette.Light:
lambda color: saturated(color, 50),
QPalette.Midlight:
lambda color: saturated(color, 90),
QPalette.Button:
lambda color: color,
# Canvas widget shadow
QPalette.Shadow:
lambda color: saturated(color, 125) if darkMode else
saturated(color, 150),

# Category tab color
QPalette.Highlight:
lambda color: color,

QPalette.HighlightedText:
lambda color: foreground_for_background(color),
}

palette = QPalette()

if isinstance(background, dict):
if app.property('darkMode'):
background = background.get('dark', next(iter(background.values())))
else:
background = background.get('light', next(iter(background.values())))
base_color = background[QPalette.Button]
base_color = QColor(base_color)
else:
color = NAMED_COLORS.get(background, background)
color = QColor(background)
if color.isValid():
base_color = color
else:
color = NAMED_COLORS[DEFAULT_COLOR]
base_color = QColor(color)

for role, default in defaults.items():
if isinstance(background, dict) and role in background:
v = background[role]
color = NAMED_COLORS.get(v, v)
color = QColor(color)
if color.isValid():
palette.setColor(role, color)
continue
color = default(base_color)
palette.setColor(role, color)

return palette


def default_palette():
# type: () -> QPalette
"""
Create and return a default palette for a node.
"""
return create_palette(DEFAULT_COLOR)


def create_gradient(base_color: QColor, stop=QPointF(0, 0),
finalStop=QPointF(0, 1)) -> QLinearGradient:
"""
Expand Down
2 changes: 1 addition & 1 deletion orangecanvas/registry/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from AnyQt.QtCore import QObject, Qt
from AnyQt.QtCore import pyqtSignal as Signal

from ..gui.utils import create_palette, default_palette
from ..gui.palette_utils import create_palette, default_palette
from ..utils import type_str
from .discovery import WidgetDiscovery
from .description import WidgetDescription, CategoryDescription
Expand Down

0 comments on commit 75e3295

Please sign in to comment.