Debian Codegraph @

src/tools/classic_tools/abstract_classic_tool.py

raw symbols SCIP
1 # abstract_classic_tool.py
2 #
3 # Copyright 2018-2023 Romain F. T.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import cairo
19 from .abstract_tool import AbstractAbstractTool
20 from .optionsbar_classic import OptionsBarClassic
21 from .utilities_colors import utilities_gdk_rgba_to_normalized_array
22
23 class AbstractClassicTool(AbstractAbstractTool):
24 __gtype_name__ = 'AbstractClassicTool'
25
26 def __init__(self, tool_id, label, icon_name, window, **kwargs):
27 super().__init__(tool_id, label, icon_name, window)
28 self.menu_id = 0
29 self.use_color = True
30 self.use_size = True
31 self.accept_selection = False
32
33 self.tool_width = 10
34 self.main_color = None
35 self.secondary_color = None
36 self._fallback_operator = 'over'
37 self._operator = cairo.Operator.OVER
38 self.x_press = 0
39 self.y_press = 0
40 self._use_antialias = self.load_tool_action_boolean('antialias', \
41 'use-antialiasing')
42 # XXX honteusement sous-performant ^
43
44 ############################################################################
45 # UI implementations #######################################################
46
47 def try_build_pane(self):
48 self.pane_id = 'classic'
49 self.window.options_manager.try_add_bottom_pane(self.pane_id, self)
50
51 def build_bottom_pane(self):
52 return OptionsBarClassic(self.window)
53
54 ############################################################################
55 # Options ##################################################################
56
57 def set_common_values(self, event_btn, event_x, event_y):
58 self._use_antialias = self.get_option_value('antialias')
59 self.tool_width = self.window.options_manager.get_tool_width()
60 left_c = self.window.options_manager.get_left_color()
61 right_c = self.window.options_manager.get_right_color()
62 if event_btn == 1:
63 self.main_color = utilities_gdk_rgba_to_normalized_array(left_c)
64 self.secondary_color = utilities_gdk_rgba_to_normalized_array(right_c)
65 if event_btn == 3:
66 self.main_color = utilities_gdk_rgba_to_normalized_array(right_c)
67 self.secondary_color = utilities_gdk_rgba_to_normalized_array(left_c)
68 self.x_press = event_x
69 self.y_press = event_y
70 self._operator = self.window.options_manager.get_operator()[0]
71
72 ############################################################################
73 # Operations common methods ################################################
74
75 def start_tool_operation(self, operation):
76 super().start_tool_operation(operation)
77 context = self.get_context()
78 if 'antialias' not in operation:
79 antialias = cairo.Antialias.DEFAULT
80 elif operation['antialias']:
81 antialias = cairo.Antialias.DEFAULT
82 else:
83 antialias = cairo.Antialias.NONE
84 context.set_antialias(antialias)
85 return context
86
87 def set_dashes_and_cap(self, cairo_context, lw, dashes_type, line_cap):
88 """Set a cairo dash pattern to the given context. The pattern we want
89 is described by its name as a string (`dashes_type`), but the actual
90 lengths that cairo wants depend also on the line width (`lw`) and the
91 `line_cap`. The line cap is also set here."""
92 cairo_context.set_line_cap(line_cap)
93 dashes_descriptor = []
94 if dashes_type == 'regular':
95 dashes_descriptor = [2, 2]
96 elif dashes_type == 'long':
97 dashes_descriptor = [3, 1]
98 elif dashes_type == 'dots':
99 dashes_descriptor = [1, 1]
100 elif dashes_type == 'alt':
101 dashes_descriptor = [3, 1, 1, 1]
102
103 i = 0
104 for length in dashes_descriptor:
105 if line_cap != cairo.LineCap.BUTT:
106 if i % 2 == 0:
107 length = length - 1
108 else:
109 length = length + 1
110 actual_length = length * lw
111 if length == 0:
112 actual_length = actual_length + 1
113 dashes_descriptor[i] = actual_length
114 i = i + 1
115 cairo_context.set_dash(dashes_descriptor)
116
117 ############################################################################
118 ################################################################################