Package dtk ::
Package ui ::
Module color_selection
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from button import Button
24 from dialog import DialogBox, DIALOG_MASK_SINGLE_PAGE
25 from draw import draw_vlinear, draw_line, draw_pixbuf
26 from entry import TextEntry
27 from iconview import IconView
28 from label import Label
29 from locales import _
30 from scrolled_window import ScrolledWindow
31 from spin import SpinBox
32 from theme import ui_theme
33 import gobject
34 import gtk
35 from utils import (gdkcolor_to_string, color_hex_to_cairo,
36 propagate_expose, alpha_color_hex_to_cairo,
37 color_hex_to_rgb, color_rgb_to_hex, cairo_disable_antialias,
38 is_hex_color, place_center)
39
40 -class HSV(gtk.ColorSelection):
41 '''
42 HSV widget that use deepin-ui widget instead Gtk.ColorSelection's child widget.
43 '''
44
46 '''
47 Initialize HSV class.
48 '''
49 gtk.ColorSelection.__init__(self)
50
51
52 self.get_children()[0].remove(self.get_children()[0].get_children()[1])
53
54
55 self.get_children()[0].get_children()[0].remove(self.get_children()[0].get_children()[0].get_children()[1])
56
62
68
70 '''
71 Get RGB color.
72 '''
73 gdk_color = self.get_current_color()
74 return (gdk_color.red / 256, gdk_color.green / 256, gdk_color.blue / 256)
75
76 gobject.type_register(HSV)
77
79 '''
80 ColorSelectionDialog widget.
81
82 @undocumented: click_confirm_button
83 @undocumented: click_cancel_button
84 @undocumented: click_rgb_spin
85 @undocumented: press_return_color_entry
86 @undocumented: expose_display_button
87 '''
88
89 DEFAULT_COLOR_LIST = ["#000000", "#808080", "#E20417", "#F29300", "#FFEC00", "#95BE0D", "#008F35", "#00968F", "#FFFFFF", "#C0C0C0", "#E2004E", "#E2007A", "#920A7E", "#162883", "#0069B2", "#009DE0"]
90
91 - def __init__(self,
92 confirm_callback=None,
93 cancel_callback=None):
94 '''
95 Initialize ColorSelectDialog class.
96
97 @param confirm_callback: Callback when user click OK, this callback accept one argument, color string.
98 @param cancel_callback: Callback when user click cancel, this callback don't accept any argument.
99 '''
100 DialogBox.__init__(self, _("Select color"), mask_type=DIALOG_MASK_SINGLE_PAGE)
101 self.confirm_callback = confirm_callback
102 self.cancel_callback = cancel_callback
103
104 self.color_box = gtk.HBox()
105 self.color_align = gtk.Alignment()
106 self.color_align.set(0.5, 0.5, 0.0, 0.0)
107 self.color_align.set_padding(10, 0, 8, 8)
108 self.color_align.add(self.color_box)
109 self.color_hsv = HSV()
110 self.color_string = self.color_hsv.get_color_string()
111 (self.color_r, self.color_g, self.color_b) = self.color_hsv.get_rgb_color()
112 self.color_hsv.get_hsv_widget().connect(
113 "button-release-event",
114 lambda w, e: self.update_color_info(self.color_hsv.get_color_string()))
115 self.color_box.pack_start(self.color_hsv, False, False)
116
117 self.color_right_box = gtk.VBox()
118 self.color_right_align = gtk.Alignment()
119 self.color_right_align.set(0.5, 0.5, 0.0, 0.0)
120 self.color_right_align.set_padding(8, 0, 0, 0)
121 self.color_right_align.add(self.color_right_box)
122 self.color_box.pack_start(self.color_right_align)
123
124 self.color_info_box = gtk.HBox()
125 self.color_right_box.pack_start(self.color_info_box, False, False)
126
127 self.color_display_box = gtk.VBox()
128 self.color_display_button = gtk.Button()
129 self.color_display_button.connect("expose-event", self.expose_display_button)
130 self.color_display_button.set_size_request(70, 49)
131 self.color_display_align = gtk.Alignment()
132 self.color_display_align.set(0.5, 0.5, 1.0, 1.0)
133 self.color_display_align.set_padding(5, 5, 5, 5)
134 self.color_display_align.add(self.color_display_button)
135 self.color_display_box.pack_start(self.color_display_align, False, False, 5)
136
137 self.color_hex_box = gtk.HBox()
138 self.color_hex_label = Label(_("Color value"))
139 self.color_hex_box.pack_start(self.color_hex_label, False, False, 5)
140 self.color_hex_entry = TextEntry(self.color_string)
141 self.color_hex_entry.entry.check_text = is_hex_color
142 self.color_hex_entry.entry.connect("press-return", self.press_return_color_entry)
143 self.color_hex_entry.set_size(70, 24)
144 self.color_hex_box.pack_start(self.color_hex_entry, False, False, 5)
145 self.color_display_box.pack_start(self.color_hex_box, False, False, 5)
146
147 self.color_info_box.pack_start(self.color_display_box, False, False, 5)
148
149 self.color_rgb_box = gtk.VBox()
150 self.color_r_box = gtk.HBox()
151 self.color_r_label = Label(_("Red: "))
152 self.color_r_spin = SpinBox(self.color_r, 0, 255, 1)
153 self.color_r_spin.connect("value-changed", lambda s, v: self.click_rgb_spin())
154 self.color_r_box.pack_start(self.color_r_label, False, False)
155 self.color_r_box.pack_start(self.color_r_spin, False, False)
156 self.color_g_box = gtk.HBox()
157 self.color_g_label = Label(_("Green: "))
158 self.color_g_spin = SpinBox(self.color_g, 0, 255, 1)
159 self.color_g_spin.connect("value-changed", lambda s, v: self.click_rgb_spin())
160 self.color_g_box.pack_start(self.color_g_label, False, False)
161 self.color_g_box.pack_start(self.color_g_spin, False, False)
162 self.color_b_box = gtk.HBox()
163 self.color_b_label = Label(_("Blue: "))
164 self.color_b_spin = SpinBox(self.color_b, 0, 255, 1)
165 self.color_b_spin.connect("value-changed", lambda s, v: self.click_rgb_spin())
166 self.color_b_box.pack_start(self.color_b_label, False, False)
167 self.color_b_box.pack_start(self.color_b_spin, False, False)
168
169 self.color_rgb_box.pack_start(self.color_r_box, False, False, 8)
170 self.color_rgb_box.pack_start(self.color_g_box, False, False, 8)
171 self.color_rgb_box.pack_start(self.color_b_box, False, False, 8)
172 self.color_info_box.pack_start(self.color_rgb_box, False, False, 5)
173
174 self.color_select_view = IconView()
175 self.color_select_view.set_size_request(250, 60)
176 self.color_select_view.connect("button-press-item", lambda view, item, x, y: self.update_color_info(item.color, False))
177 self.color_select_view.draw_mask = self.get_mask_func(self.color_select_view)
178 self.color_select_scrolled_window = ScrolledWindow()
179 for color in self.DEFAULT_COLOR_LIST:
180 self.color_select_view.add_items([ColorItem(color)])
181
182 self.color_select_align = gtk.Alignment()
183 self.color_select_align.set(0.5, 0.5, 1.0, 1.0)
184 self.color_select_align.set_padding(10, 5, 6, 5)
185
186 self.color_select_scrolled_window.add_child(self.color_select_view)
187 self.color_select_scrolled_window.set_size_request(-1, 60)
188 self.color_select_align.add(self.color_select_scrolled_window)
189 self.color_right_box.pack_start(self.color_select_align, True, True)
190
191 self.confirm_button = Button(_("OK"))
192 self.cancel_button = Button(_("Cancel"))
193
194 self.confirm_button.connect("clicked", lambda w: self.click_confirm_button())
195 self.cancel_button.connect("clicked", lambda w: self.click_cancel_button())
196
197 self.right_button_box.set_buttons([self.confirm_button, self.cancel_button])
198 self.body_box.pack_start(self.color_align, True, True)
199
200 self.update_color_info(self.color_string)
201
210
219
227
229 '''
230 Callback when user press `return` key on entry.
231
232 @param entry: Color input entry.
233 '''
234 self.update_color_info(entry.get_text())
235 entry.select_all()
236
257
259 '''
260 Update color information.
261
262 @param color_string: Hex color string.
263 @param clear_highlight: Whether clear color select view's highlight status, default is True.
264 '''
265 self.color_string = color_string
266 (self.color_r, self.color_g, self.color_b) = color_hex_to_rgb(self.color_string)
267 self.color_r_spin.update(self.color_r)
268 self.color_g_spin.update(self.color_g)
269 self.color_b_spin.update(self.color_b)
270 self.color_hex_entry.set_text(self.color_string)
271 if not color_string.startswith("#"):
272 color_string = "#" + color_string
273 self.color_hsv.set_current_color(gtk.gdk.color_parse(color_string))
274
275 if clear_highlight:
276 self.color_select_view.clear_highlight()
277
278 self.color_display_button.queue_draw()
279
280 gobject.type_register(ColorSelectDialog)
281
283 '''
284 ColorItem class for use in L{ I{ColorSelectDialog} <ColorSelectDialog>}.
285 '''
286
287 __gsignals__ = {
288 "redraw-request" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
289 }
290
292 '''
293 Initialize ColorItem class.
294
295 @param color: Hex color string.
296 '''
297 gobject.GObject.__init__(self)
298 self.color = color
299 self.width = 20
300 self.height = 16
301 self.padding_x = 4
302 self.padding_y = 4
303 self.hover_flag = False
304 self.highlight_flag = False
305
307 '''
308 IconView interface function.
309
310 Emit `redraw-request` signal.
311 '''
312 self.emit("redraw-request")
313
315 '''
316 IconView interface function.
317
318 Get item width.
319 @return: Return item width, in pixel.
320 '''
321 return self.width + self.padding_x * 2
322
324 '''
325 IconView interface function.
326
327 Get item height.
328 @return: Return item height, in pixel.
329 '''
330 return self.height + self.padding_y * 2
331
333 '''
334 IconView interface function.
335
336 Render item.
337
338 @param cr: Cairo context.
339 @param rect: Render rectangle area.
340 '''
341
342 draw_x = rect.x + self.padding_x
343 draw_y = rect.y + self.padding_y
344
345
346 cr.set_source_rgb(*color_hex_to_cairo(self.color))
347 cr.rectangle(draw_x, draw_y, self.width, self.height)
348 cr.fill()
349
350 if self.hover_flag:
351 cr.set_source_rgb(*color_hex_to_cairo(ui_theme.get_color("color_item_hover").get_color()))
352 cr.rectangle(draw_x, draw_y, self.width, self.height)
353 cr.stroke()
354 elif self.highlight_flag:
355 cr.set_source_rgb(*color_hex_to_cairo(ui_theme.get_color("color_item_highlight").get_color()))
356 cr.rectangle(draw_x, draw_y, self.width, self.height)
357 cr.stroke()
358
359
360 with cairo_disable_antialias(cr):
361 cr.set_line_width(1)
362 cr.set_source_rgb(*color_hex_to_cairo(ui_theme.get_color("color_item_frame").get_color()))
363 cr.rectangle(draw_x, draw_y, self.width, self.height)
364 cr.stroke()
365
367 '''
368 IconView interface function.
369
370 Handle `motion-notify-event` signal.
371
372 @param x: X coordinate that user motion on item.
373 @param y: Y coordinate that user motion on item.
374 '''
375 self.hover_flag = True
376
377 self.emit_redraw_request()
378
380 '''
381 IconView interface function.
382
383 Handle `lost-focus` signal.
384 '''
385 self.hover_flag = False
386
387 self.emit_redraw_request()
388
390 '''
391 IconView interface function.
392
393 Handle `highlight` signal.
394 '''
395 self.highlight_flag = True
396
397 self.emit_redraw_request()
398
400 '''
401 Normal icon item.
402 '''
403 self.highlight_flag = False
404
405 self.emit_redraw_request()
406
414
422
424 '''
425 IconView interface function.
426
427 Handle `click` signal.
428 '''
429 pass
430
432 '''
433 IconView interface function.
434
435 Handle `double-click` signal.
436 '''
437 pass
438
439 gobject.type_register(ColorItem)
440
593
594 gobject.type_register(ColorButton)
595