| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import cairo |
| 19 |
from gi.repository import Gtk |
| 20 |
from .utilities_colors import utilities_get_rgba_name |
| 21 |
|
| 22 |
PREFIX = '/com/github/maoschanz/drawing/' |
| 23 |
|
| 24 |
CAIRO_OP_VALUES = { |
| 25 |
'over': cairo.Operator.OVER, |
| 26 |
'clear': cairo.Operator.CLEAR, |
| 27 |
|
| 28 |
'source': cairo.Operator.SOURCE, |
| 29 |
'difference': cairo.Operator.DIFFERENCE, |
| 30 |
|
| 31 |
|
| 32 |
'multiply': cairo.Operator.MULTIPLY, |
| 33 |
'screen': cairo.Operator.SCREEN, |
| 34 |
|
| 35 |
'hsl-hue': cairo.Operator.HSL_HUE, |
| 36 |
'hsl-saturation': cairo.Operator.HSL_SATURATION, |
| 37 |
'hsl-color': cairo.Operator.HSL_COLOR, |
| 38 |
'hsl-luminosity': cairo.Operator.HSL_LUMINOSITY, |
| 39 |
} |
| 40 |
|
| 41 |
CAIRO_OP_LABELS = { |
| 42 |
'over': _("Normal"), |
| 43 |
'clear': _("Erase"), |
| 44 |
|
| 45 |
'source': _("Raw source color"), |
| 46 |
'difference': _("Difference"), |
| 47 |
|
| 48 |
'multiply': _("Highlight"), |
| 49 |
|
| 50 |
|
| 51 |
'screen': _("Highlight (dark)"), |
| 52 |
|
| 53 |
'hsl-hue': _("Hue only"), |
| 54 |
'hsl-saturation': _("Saturation only"), |
| 55 |
'hsl-color': _("Hue and saturation"), |
| 56 |
'hsl-luminosity': _("Luminosity only"), |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
class OptionsBarClassicColorPopover(Gtk.Popover): |
| 62 |
__gtype_name__ = 'OptionsBarClassicColorPopover' |
| 63 |
|
| 64 |
def __init__(self, btn, thumbn, is_left_color, options_manager): |
| 65 |
super().__init__() |
| 66 |
|
| 67 |
suffix = 'optionsbars/classic/optionsbar-color-popover.ui' |
| 68 |
builder = Gtk.Builder.new_from_resource(PREFIX + suffix) |
| 69 |
|
| 70 |
main_box = builder.get_object('main-box') |
| 71 |
self.add(main_box) |
| 72 |
self._button = btn |
| 73 |
self._button.set_popover(self) |
| 74 |
self._thumbnail_image = thumbn |
| 75 |
self._options_manager = options_manager |
| 76 |
|
| 77 |
|
| 78 |
self._operator_enum = cairo.Operator.OVER |
| 79 |
self._operator_label = _("Normal") |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if is_left_color: |
| 85 |
title_label = _("Main color") |
| 86 |
else: |
| 87 |
title_label = _("Secondary color") |
| 88 |
self._popover_title = builder.get_object('popover-title') |
| 89 |
self._popover_title.set_label(title_label) |
| 90 |
|
| 91 |
self._operator_box_1 = builder.get_object('operator-box-start') |
| 92 |
self._operator_box_2 = builder.get_object('operator-box-end') |
| 93 |
|
| 94 |
operators_radio_group = builder.get_object('op-group') |
| 95 |
self._build_all_operators_groups(operators_radio_group) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
self.color_widget = builder.get_object('color-widget') |
| 101 |
initial_rgba = self._options_manager.get_persisted_color(is_left_color) |
| 102 |
self.color_widget.set_rgba(initial_rgba) |
| 103 |
self.color_widget.connect('notify::rgba', self._on_color_changed) |
| 104 |
self.color_widget.connect('notify::show-editor', self._update_nav_box) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
back_btn = builder.get_object('back-btn') |
| 110 |
self.editor_box = builder.get_object('editor-box') |
| 111 |
back_btn.connect('clicked', self._close_color_editor) |
| 112 |
|
| 113 |
self._update_nav_box() |
| 114 |
self._on_color_changed() |
| 115 |
|
| 116 |
def _build_all_operators_groups(self, radio_group): |
| 117 |
self._operators_menus = [] |
| 118 |
self._operators_hideable_radiobtns = { |
| 119 |
'over': None, |
| 120 |
'erase': None, |
| 121 |
} |
| 122 |
suffix = 'optionsbars/classic/optionsbar-operator-menus.ui' |
| 123 |
builder = Gtk.Builder.new_from_resource(PREFIX + suffix) |
| 124 |
|
| 125 |
box = self._build_radios(radio_group, ['multiply', 'screen']) |
| 126 |
menu_model = builder.get_object('highlight-operators-menu') |
| 127 |
btn = self._build_op_menu(menu_model, 'tool-highlight-symbolic') |
| 128 |
btn.set_tooltip_text(_("Highlight")) |
| 129 |
box.add(btn) |
| 130 |
self._operator_box_1.add(box) |
| 131 |
|
| 132 |
box = self._build_radios(radio_group, ['hsl-hue', 'hsl-saturation', |
| 133 |
'hsl-color', 'hsl-luminosity']) |
| 134 |
menu_model = builder.get_object('hsl-operators-menu') |
| 135 |
btn = self._build_op_menu(menu_model, 'display-brightness-symbolic') |
| 136 |
btn.set_tooltip_text(_("HSL modes")) |
| 137 |
box.add(btn) |
| 138 |
self._operator_box_1.add(box) |
| 139 |
|
| 140 |
box = self._build_radios(radio_group, ['source', 'difference']) |
| 141 |
menu_model = builder.get_object('other-operators-menu') |
| 142 |
btn = self._build_op_menu(menu_model, 'view-more-symbolic') |
| 143 |
btn.set_tooltip_text(_("Other modes")) |
| 144 |
box.add(btn) |
| 145 |
self._operator_box_1.add(box) |
| 146 |
|
| 147 |
def _build_radios(self, radio_group, ops_list): |
| 148 |
groupbox = Gtk.Box(visible=True) |
| 149 |
groupbox.get_style_context().add_class('linked') |
| 150 |
for operator_id in ops_list: |
| 151 |
button = Gtk.RadioButton(visible=False, draw_indicator=False) |
| 152 |
button.join_group(radio_group) |
| 153 |
button.set_detailed_action_name ('win.cairo_op_mirror::' + operator_id) |
| 154 |
|
| 155 |
|
| 156 |
button_label = CAIRO_OP_LABELS[operator_id] |
| 157 |
if len(button_label) > 16: |
| 158 |
button.set_tooltip_text(button_label) |
| 159 |
button_label = button_label[:14] + "…" |
| 160 |
button.set_label(button_label) |
| 161 |
|
| 162 |
self._operators_hideable_radiobtns[operator_id] = button |
| 163 |
groupbox.add(button) |
| 164 |
return groupbox |
| 165 |
|
| 166 |
def _build_op_menu(self, menu_model, icon_name): |
| 167 |
menu_btn = Gtk.MenuButton(visible=True) |
| 168 |
image = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.BUTTON) |
| 169 |
menu_btn.set_image(image) |
| 170 |
menu_btn.set_menu_model(menu_model) |
| 171 |
self._operators_menus.append(menu_btn.get_popover()) |
| 172 |
return menu_btn |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
def set_operators_available(self, tool_use_operator, op_as_string): |
| 177 |
"""Adapts the availability of operators to the tool's property.""" |
| 178 |
self._operator_box_1.set_visible(tool_use_operator) |
| 179 |
self._operator_box_2.set_visible(tool_use_operator) |
| 180 |
self._popover_title.set_visible(not tool_use_operator) |
| 181 |
self.adapt_to_operator(op_as_string) |
| 182 |
|
| 183 |
def _operator_supports_color(self, op_as_string): |
| 184 |
return not op_as_string == 'clear' |
| 185 |
|
| 186 |
def adapt_to_operator(self, op_as_string): |
| 187 |
self._operator_enum = CAIRO_OP_VALUES[op_as_string] |
| 188 |
self._operator_label = CAIRO_OP_LABELS[op_as_string] |
| 189 |
|
| 190 |
supports_colors = self._operator_supports_color(op_as_string) |
| 191 |
|
| 192 |
|
| 193 |
for op_submenu in self._operators_menus: |
| 194 |
op_submenu.popdown() |
| 195 |
|
| 196 |
|
| 197 |
for operator_id in self._operators_hideable_radiobtns: |
| 198 |
op_radiobtn = self._operators_hideable_radiobtns[operator_id] |
| 199 |
if op_radiobtn is not None: |
| 200 |
op_radiobtn.set_visible(operator_id == op_as_string) |
| 201 |
|
| 202 |
self.color_widget.set_sensitive(supports_colors) |
| 203 |
self._set_thumbnail_color(op_as_string) |
| 204 |
|
| 205 |
def _on_color_changed(self, *args): |
| 206 |
"""When the use clicks on a color in the palette""" |
| 207 |
op_as_string = self._options_manager.get_value('cairo_operator') |
| 208 |
self._set_thumbnail_color(op_as_string) |
| 209 |
|
| 210 |
def _set_thumbnail_color(self, op_as_string): |
| 211 |
"""Sets the icon and the tooltip of the popover's button. The icon can |
| 212 |
be an actual icon, or a rectangle of color. The tooltip shows the active |
| 213 |
operator (if pertinent), and an approximation of the color name (if |
| 214 |
pertinent).""" |
| 215 |
rgba = self.color_widget.get_rgba() |
| 216 |
red = rgba.red |
| 217 |
green = rgba.green |
| 218 |
blue = rgba.blue |
| 219 |
alpha = rgba.alpha |
| 220 |
|
| 221 |
|
| 222 |
if op_as_string == 'clear': |
| 223 |
self._thumbnail_image.set_from_icon_name('tool-eraser-symbolic', \ |
| 224 |
Gtk.IconSize.SMALL_TOOLBAR) |
| 225 |
else: |
| 226 |
surface = cairo.ImageSurface(cairo.Format.ARGB32, 16, 16) |
| 227 |
cairo_context = cairo.Context(surface) |
| 228 |
cairo_context.set_source_rgba(red, green, blue, alpha) |
| 229 |
cairo_context.paint() |
| 230 |
|
| 231 |
self._thumbnail_image.set_from_surface(surface) |
| 232 |
|
| 233 |
|
| 234 |
tooltip_string = utilities_get_rgba_name(rgba) |
| 235 |
if not self._operator_supports_color(op_as_string): |
| 236 |
tooltip_string = self._operator_label |
| 237 |
elif op_as_string == 'over': |
| 238 |
pass |
| 239 |
else: |
| 240 |
tooltip_string = self._operator_label + " - " + tooltip_string |
| 241 |
self._button.set_tooltip_text(tooltip_string) |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
def editor_setting_changed(self, show_editor): |
| 246 |
self.color_widget.props.show_editor = show_editor |
| 247 |
self._update_nav_box() |
| 248 |
|
| 249 |
def _close_color_editor(self, *args): |
| 250 |
self.color_widget.props.show_editor = False |
| 251 |
|
| 252 |
def open(self, *args): |
| 253 |
self._button.activate() |
| 254 |
|
| 255 |
def _update_nav_box(self, *args): |
| 256 |
"""Update the visibility of the 'back to the palette' button.""" |
| 257 |
self.editor_box.set_visible(self.color_widget.props.show_editor) |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|