File: style.py

package info (click to toggle)
python-qtpynodeeditor 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 604 kB
  • sloc: python: 5,038; makefile: 14; sh: 1
file content (272 lines) | stat: -rw-r--r-- 8,114 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json
import logging
import random

from qtpy.QtGui import QColor

logger = logging.getLogger(__name__)


def _get_qcolor(style_dict, key):
    if key not in style_dict:
        return QColor()

    name_or_list = style_dict[key]
    if isinstance(name_or_list, list):
        color = QColor(*name_or_list)
    else:
        color = QColor(name_or_list)
    logger.debug('Loaded color %s = %s -> %d %d %d %d', key, name_or_list,
                 *color.getRgb())
    return color


class Style:
    default_style = {
        "FlowViewStyle": {
            "BackgroundColor": [53, 53, 53],
            "FineGridColor": [60, 60, 60],
            "CoarseGridColor": [25, 25, 25]
        },
        "NodeStyle": {
            "NormalBoundaryColor": [255, 255, 255],
            "SelectedBoundaryColor": [255, 165, 0],
            "GradientColor0": "gray",
            "GradientColor1": [80, 80, 80],
            "GradientColor2": [64, 64, 64],
            "GradientColor3": [58, 58, 58],
            "ShadowColor": [20, 20, 20],
            "FontColor": "white",
            "FontColorFaded": "gray",
            "ConnectionPointColor": [169, 169, 169],
            "FilledConnectionPointColor": "cyan",
            "ErrorColor": "red",
            "WarningColor": [128, 128, 0],

            "PenWidth": 1.0,
            "HoveredPenWidth": 1.5,

            "ConnectionPointDiameter": 8.0,

            "Opacity": 0.8
        },
        "ConnectionStyle": {
            "ConstructionColor": "gray",
            "NormalColor": "darkcyan",
            "SelectedColor": [100, 100, 100],
            "SelectedHaloColor": "orange",
            "HoveredColor": "lightcyan",
            "LineWidth": 3.0,
            "ConstructionLineWidth": 2.0,
            "PointDiameter": 10.0,
            "UseDataDefinedColors": False
        }
    }

    def __init__(self, json_style=None):
        if json_style is None:
            json_style = self.default_style

        self.load_from_json(json_style)

    def load_from_json(self, json_style: str):
        """
        Load from json

        Parameters
        ----------
        json_style : str or dict
        """
        if isinstance(json_style, dict):
            return json_style
        else:
            return json.loads(json_style)


class FlowViewStyle(Style):
    def __init__(self, json_style=None):
        self.background_color = QColor()
        self.fine_grid_color = QColor()
        self.coarse_grid_color = QColor()
        super().__init__(json_style=json_style)

    def load_from_json(self, json_style: str):
        """
        Load from json

        Parameters
        ----------
        json_style : str or dict
        """
        doc = super().load_from_json(json_style)
        style = doc["FlowViewStyle"]
        self.background_color = _get_qcolor(style, 'BackgroundColor')
        self.fine_grid_color = _get_qcolor(style, 'FineGridColor')
        self.coarse_grid_color = _get_qcolor(style, 'CoarseGridColor')


class ConnectionStyle(Style):
    '''
    Style for connections

    Attributes
    ----------
    construction_color : QColor
    normal_color : QColor
    selected_color : QColor
    selected_halo_color : QColor
    hovered_color : QColor
    line_width : float
    construction_line_width : float
    point_diameter : float
    use_data_defined_colors : bool
    '''

    def __init__(self, json_style=None):
        self.construction_color = QColor()
        self.normal_color = QColor()
        self.selected_color = QColor()
        self.selected_halo_color = QColor()
        self.hovered_color = QColor()

        self.line_width = 0.0
        self.construction_line_width = 0.0
        self.point_diameter = 0.0

        self.use_data_defined_colors = True

        super().__init__(json_style=json_style)

    def load_from_json(self, json_style: str):
        """
        Load from json

        Parameters
        ----------
        json_style : str
        """
        doc = super().load_from_json(json_style)
        style = doc["ConnectionStyle"]
        self.construction_color = _get_qcolor(style, 'ConstructionColor')
        self.normal_color = _get_qcolor(style, 'NormalColor')
        self.selected_color = _get_qcolor(style, 'SelectedColor')
        self.selected_halo_color = _get_qcolor(style, 'SelectedHaloColor')
        self.hovered_color = _get_qcolor(style, 'HoveredColor')

        self.line_width = float(style['LineWidth'])
        self.construction_line_width = float(style['ConstructionLineWidth'])
        self.point_diameter = float(style['PointDiameter'])
        self.use_data_defined_colors = bool(style['UseDataDefinedColors'])

    def get_normal_color(self, type_id: str = None) -> QColor:
        """
        Normal color

        Parameters
        ----------
        type_id : str

        Returns
        -------
        value : QColor
        """
        if type_id is None:
            return self.normal_color

        hue_range = 0xFF
        random.seed(type_id)
        hue = random.randint(0, hue_range)
        sat = 120 + id(type_id) % 129
        return QColor.fromHsl(hue, sat, 160)


class NodeStyle(Style):
    def __init__(self, json_style=None):
        self.normal_boundary_color = QColor()
        self.selected_boundary_color = QColor()
        self.gradient_colors = ((0, QColor()), )
        self.shadow_color = QColor()
        self.font_color = QColor()
        self.font_color_faded = QColor()

        self.connection_point_color = QColor()
        self.filled_connection_point_color = QColor()

        self.warning_color = QColor()
        self.error_color = QColor()

        self.pen_width = 1.0
        self.hovered_pen_width = 2.0
        self.connection_point_diameter = 5.0
        self.opacity = 1.0

        super().__init__(json_style=json_style)

    def load_from_json(self, json_style: str):
        """
        Load from json

        Parameters
        ----------
        json_style : str
        """
        doc = super().load_from_json(json_style)
        style = doc["NodeStyle"]

        self.normal_boundary_color = _get_qcolor(style, 'NormalBoundaryColor')
        self.selected_boundary_color = _get_qcolor(
            style, 'SelectedBoundaryColor')
        self.gradient_colors = (
            (0.0, _get_qcolor(style, 'GradientColor0')),
            (0.03, _get_qcolor(style, 'GradientColor1')),
            (0.97, _get_qcolor(style, 'GradientColor2')),
            (1.0, _get_qcolor(style, 'GradientColor3')),
        )
        self.shadow_color = _get_qcolor(style, 'ShadowColor')
        self.font_color = _get_qcolor(style, 'FontColor')
        self.font_color_faded = _get_qcolor(style, 'FontColorFaded')
        self.connection_point_color = _get_qcolor(
            style, 'ConnectionPointColor')
        self.filled_connection_point_color = _get_qcolor(
            style, 'FilledConnectionPointColor')
        self.warning_color = _get_qcolor(style, 'WarningColor')
        self.error_color = _get_qcolor(style, 'ErrorColor')

        self.pen_width = float(style['PenWidth'])
        self.hovered_pen_width = float(style['HoveredPenWidth'])
        self.connection_point_diameter = float(
            style['ConnectionPointDiameter'])
        self.opacity = float(style['Opacity'])


class StyleCollection:
    'Container for all styles'

    def __init__(self, *, node=None, connection=None, flow_view=None):
        if node is None:
            node = NodeStyle()
        self.node = node

        if connection is None:
            connection = ConnectionStyle()
        self.connection = connection

        if flow_view is None:
            flow_view = FlowViewStyle()
        self.flow_view = flow_view

    @classmethod
    def from_json(cls, json_doc):
        if isinstance(json_doc, dict):
            json_style = json_doc
        else:
            json_style = json.loads(json_doc)

        return StyleCollection(
            node=NodeStyle(json_style),
            connection=ConnectionStyle(json_style),
            flow_view=FlowViewStyle(json_style),
        )


default_style = StyleCollection()