-
Notifications
You must be signed in to change notification settings - Fork 2
/
higrichlists.py
465 lines (343 loc) · 12.4 KB
/
higrichlists.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2008 Adriano Monteiro Marques
#
# Author: Francesco Piccinno <stack.box@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
This module contains:
- HIGRichList like firefox one
- HIGRichRow a row for HIGRichList
- PluginRow a custom HIGRichRow for HIGRichList
"""
import gtk
import pango
import gobject
from higwidgets.higbuttons import HIGButton
from umit.core.I18N import _
class HIGRichRow(gtk.EventBox):
"""
Represent a single row for HIGRichList
"""
__gtype_name__ = "HIGRichRow"
__gsignals__ = {
# Emitted when the user activate (selected)
'activate' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
# Emitted when the user click with mouse
'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
# Emitted when the user click with the right button
'popup' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, \
(gtk.gdk.Event,))
}
def __init__(self, tree):
"""
Create a HIGRichRow
@param tree a PluginRichList object to use as parent
"""
assert isinstance(tree, HIGRichList), "must be a HIGRichList object"
gtk.EventBox.__init__(self)
self.tree = tree
self.__create_widgets()
self.__pack_widgets()
self._vbox.show()
def __create_widgets(self):
self._vbox = gtk.VBox()
def __pack_widgets(self):
self.add(self._vbox)
def do_expose_event(self, evt):
"Override this function"
gtk.EventBox.do_expose_event(self, evt)
alloc = self.allocation
cr = self.window.cairo_create()
# Only draw an end-line
cr.set_line_width(0.5)
cr.set_dash([1, 1], 1)
cr.move_to(0, alloc.height)
cr.line_to(alloc.width, alloc.height)
cr.stroke()
return True
def do_realize(self):
gtk.EventBox.do_realize(self)
self.active = False
def do_button_press_event(self, evt):
if (evt.button == 1) and \
(evt.type == gtk.gdk._2BUTTON_PRESS) and \
(self.tree.change_selection(self)):
self.active = True
self.emit('clicked')
elif (evt.type == gtk.gdk.BUTTON_PRESS) and \
(self.tree.change_selection(self)):
self.active = True
if evt.button == 3:
self.emit('popup', evt)
def get_active(self):
return self._active
def set_active(self, value):
self._active = value
self.emit('activate')
if self.flags() & gtk.REALIZED:
if value:
self.modify_bg(gtk.STATE_NORMAL, \
self.style.base[gtk.STATE_PRELIGHT])
else:
self.modify_bg(gtk.STATE_NORMAL, self.style.white)
def get_vbox(self):
return self._vbox
active = property(get_active, set_active)
vbox = property(get_vbox)
class PluginRow(HIGRichRow):
"""
A custom HIGRichRow to contains Plugin informations
"""
__gtype_name__ = "PluginRow"
def __init__(self, tree, reader):
"""
Create a PluginRow
@param tree a PluginRichList object to use as parent
@param reader a PluginrReader object to be represented
"""
HIGRichRow.__init__(self, tree)
self._reader = reader
self._enabled = False
self._message = reader.description
self._show_progress = False
self._show_include = False
self._activatable = True
self._saturate = False
self.__create_widgets()
self.__pack_widgets()
self.enabled = self._reader.enabled
self.connect('activate', self.__on_activate)
self.show_all()
self.progressbar.hide()
self.box_act.hide()
self.versions_button.hide()
def __create_widgets(self):
self.image = gtk.image_new_from_pixbuf(self._reader.get_logo())
self.label = gtk.Label('')
self.label.set_ellipsize(pango.ELLIPSIZE_END)
self.versions_model = gtk.ListStore(str, str)
self.versions_button = gtk.ComboBox(self.versions_model)
rend = gtk.CellRendererPixbuf()
self.versions_button.pack_start(rend, False)
self.versions_button.add_attribute(rend, 'stock-id', 0)
rend = gtk.CellRendererText()
self.versions_button.pack_end(rend)
self.versions_button.add_attribute(rend, 'text', 1)
self.img_play = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PLAY, \
gtk.ICON_SIZE_BUTTON)
self.img_stop = gtk.image_new_from_stock(gtk.STOCK_MEDIA_STOP, \
gtk.ICON_SIZE_BUTTON)
self.action_btn = HIGButton('')
self.uninstall_btn = HIGButton(_("Uninstall"), gtk.STOCK_CLEAR)
self.preference_btn = HIGButton(stock=gtk.STOCK_PREFERENCES)
self.progressbar = gtk.ProgressBar()
def __pack_widgets(self):
# Visible part
hbox = gtk.HBox(False, 4)
hbox.set_border_width(4)
hbox.pack_start(self.image, False, False, 0)
vbox = gtk.VBox(False, 2)
mhbox = gtk.HBox(False, 2)
self.label.set_alignment(0, 0.5)
mhbox.pack_start(self.label)
minibox = gtk.VBox()
minibox.pack_start(self.versions_button, False, False, 0)
mhbox.pack_start(minibox, False, False)
vbox.pack_start(mhbox)
vbox.pack_start(self.progressbar, False, False, 0)
hbox.pack_start(vbox)
self.vbox.pack_start(hbox, False, False, 0)
# Buttons part
align = gtk.Alignment(0, 0.5)
align.add(self.preference_btn)
self.box_act = gtk.HBox(False, 2)
self.box_act.pack_start(align, True, True, 0)
self.box_act.pack_start(self.uninstall_btn, False, False, 0)
self.box_act.pack_start(self.action_btn, False, False, 0)
self.box_act.set_border_width(4)
self.vbox.pack_start(self.box_act, False, False, 0)
def __on_activate(self, widget):
if not self._activatable:
self.box_act.hide()
return
if self._show_progress:
self.box_act.hide()
self.progressbar.show()
else:
self.progressbar.hide()
if self.active:
self.box_act.show()
else:
self.box_act.hide()
def get_enabled(self):
return self._enabled
def set_enabled(self, val):
self._enabled = val
# We need more testing on color/saturate on enabled
if self._enabled:
self.action_btn.set_label(_("Disable"))
self.action_btn.set_image(self.img_stop)
#
color = self.style.text[gtk.STATE_NORMAL]
self.saturate = False
else:
self.action_btn.set_label(_("Enable"))
self.action_btn.set_image(self.img_play)
#
color = self.style.text[gtk.STATE_INSENSITIVE]
self.saturate = True
self.label.set_text( \
"<span color=\"%s\">"
"<span size=\"x-large\" weight=\"bold\">%s</span>" \
" %s" \
"\n<tt>%s</tt>" \
"</span>" % \
( \
color.to_string(), \
self._reader.name, \
self._reader.version, \
self._message \
) \
)
self.label.set_use_markup(True)
def get_reader(self):
return self._reader
enabled = property(get_enabled, set_enabled)
reader = property(get_reader)
def get_message(self):
return self._message
def set_message(self, value):
"""
If not defined don't update
"""
if value is not None:
self._message = value
# Used to update the label
self.enabled = self.enabled
def get_progress(self):
if self._show_progress:
return self.progressbar.get_fraction()
return None
def set_progress(self, val):
self.box_act.hide()
if not val or val < .0:
self._show_progress = False
self.progressbar.set_fraction(0)
self.progressbar.hide()
else:
self._show_progress = True
self.progressbar.set_fraction(val)
self.progressbar.set_text('%d %%' % int(val * 100))
self.progressbar.show()
def get_include(self):
if self._show_include:
id = self.versions_button.get_active() -1
if id >= 0:
return True
else:
return False
def set_include(self, value):
self._show_include = value
if value:
self.versions_button.show()
else:
self.versions_button.hide()
def get_activatable(self):
return self._activatable
def set_activatable(self, value):
self._activatable = value
self.__on_activate(self)
def get_saturate(self):
return self._saturate
def set_saturate(self, val):
self._saturate = val
if self._saturate:
logo = self._reader.get_logo()
logo.saturate_and_pixelate(logo, 0.3, False)
self.image.set_from_pixbuf(logo)
else:
self.image.set_from_pixbuf(self._reader.get_logo())
message = property(get_message, set_message)
progress = property(get_progress, set_progress)
show_include = property(get_include, set_include)
activatable = property(get_activatable, set_activatable)
saturate = property(get_saturate, set_saturate)
class HIGRichList(gtk.ScrolledWindow):
"""
A simil-treeview widget like firefox RichList
"""
def __init__(self):
"""
Create a HIGRichList object
"""
gtk.ScrolledWindow.__init__(self)
self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
self.set_shadow_type(gtk.SHADOW_ETCHED_IN)
self.vbox = gtk.VBox()
self.add_with_viewport(self.vbox)
# We set the background of viewport to white
self.get_child().modify_bg(gtk.STATE_NORMAL, \
self.get_child().style.white)
self.prev_sel = None
self.show_all()
def append_row(self, widget):
"""
Append a row to the tree
@param widget a HIGRichRow type object
"""
assert isinstance(widget, HIGRichRow), "must be a HIGRichRow object"
self.vbox.pack_start(widget, False, False, 0)
def remove_row(self, widget):
"""
Remove a row from the tree
@param widget a HIGRichRow type object
"""
assert isinstance(widget, HIGRichRow), "must be a HIGRichRow object"
self.vbox.remove(widget)
def clear(self):
"""
Remove all the row
"""
def remove(widget, parent):
#
widget.hide()
parent.remove(widget)
self.vbox.foreach(remove, self.vbox)
def get_rows(self):
return len(self.vbox)
def foreach(self, callback, userdata):
"Foreach in any widgets"
self.vbox.foreach(callback, userdata)
def change_selection(self, row):
"""
Change the selected item in the tree
@return True if selection was changed
"""
assert row is not None
if self.prev_sel:
self.prev_sel.active = False
self.prev_sel = row
# Grab the focus!
self.grab_focus()
# Scroll to active item
adj = self.get_vadjustment()
alloc = row.get_allocation()
if alloc.y < adj.value:
adj.set_value(alloc.y)
elif alloc.y + alloc.height > adj.value + adj.page_size:
adj.set_value(alloc.y)
return True