| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # Copyright (C) 2008 Adriano Monteiro Marques |
|---|
| 4 | # |
|---|
| 5 | # Author: Francesco Piccinno <stack.box@gmail.com> |
|---|
| 6 | # |
|---|
| 7 | # This program is free software; you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | # This program is distributed in the hope that it will be useful, |
|---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | # GNU General Public License for more details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License |
|---|
| 18 | # along with this program; if not, write to the Free Software |
|---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | """ |
|---|
| 22 | This module contains: |
|---|
| 23 | - HIGRichList like firefox one |
|---|
| 24 | - HIGRichRow a row for HIGRichList |
|---|
| 25 | - PluginRow a custom HIGRichRow for HIGRichList |
|---|
| 26 | """ |
|---|
| 27 | |
|---|
| 28 | import gtk |
|---|
| 29 | import pango |
|---|
| 30 | import gobject |
|---|
| 31 | from higwidgets.higbuttons import HIGButton |
|---|
| 32 | from umit.core.I18N import _ |
|---|
| 33 | |
|---|
| 34 | class HIGRichRow(gtk.EventBox): |
|---|
| 35 | """ |
|---|
| 36 | Represent a single row for HIGRichList |
|---|
| 37 | """ |
|---|
| 38 | |
|---|
| 39 | __gtype_name__ = "HIGRichRow" |
|---|
| 40 | __gsignals__ = { |
|---|
| 41 | # Emitted when the user activate (selected) |
|---|
| 42 | 'activate' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()), |
|---|
| 43 | |
|---|
| 44 | # Emitted when the user click with mouse |
|---|
| 45 | 'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()), |
|---|
| 46 | |
|---|
| 47 | # Emitted when the user click with the right button |
|---|
| 48 | 'popup' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, \ |
|---|
| 49 | (gtk.gdk.Event,)) |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | def __init__(self, tree): |
|---|
| 53 | """ |
|---|
| 54 | Create a HIGRichRow |
|---|
| 55 | |
|---|
| 56 | @param tree a PluginRichList object to use as parent |
|---|
| 57 | """ |
|---|
| 58 | |
|---|
| 59 | assert isinstance(tree, HIGRichList), "must be a HIGRichList object" |
|---|
| 60 | |
|---|
| 61 | gtk.EventBox.__init__(self) |
|---|
| 62 | |
|---|
| 63 | self.tree = tree |
|---|
| 64 | |
|---|
| 65 | self.__create_widgets() |
|---|
| 66 | self.__pack_widgets() |
|---|
| 67 | |
|---|
| 68 | self._vbox.show() |
|---|
| 69 | |
|---|
| 70 | def __create_widgets(self): |
|---|
| 71 | self._vbox = gtk.VBox() |
|---|
| 72 | |
|---|
| 73 | def __pack_widgets(self): |
|---|
| 74 | self.add(self._vbox) |
|---|
| 75 | |
|---|
| 76 | def do_expose_event(self, evt): |
|---|
| 77 | "Override this function" |
|---|
| 78 | |
|---|
| 79 | gtk.EventBox.do_expose_event(self, evt) |
|---|
| 80 | |
|---|
| 81 | alloc = self.allocation |
|---|
| 82 | cr = self.window.cairo_create() |
|---|
| 83 | |
|---|
| 84 | # Only draw an end-line |
|---|
| 85 | cr.set_line_width(0.5) |
|---|
| 86 | cr.set_dash([1, 1], 1) |
|---|
| 87 | cr.move_to(0, alloc.height) |
|---|
| 88 | cr.line_to(alloc.width, alloc.height) |
|---|
| 89 | cr.stroke() |
|---|
| 90 | |
|---|
| 91 | return True |
|---|
| 92 | |
|---|
| 93 | def do_realize(self): |
|---|
| 94 | gtk.EventBox.do_realize(self) |
|---|
| 95 | self.active = False |
|---|
| 96 | |
|---|
| 97 | def do_button_press_event(self, evt): |
|---|
| 98 | if (evt.button == 1) and \ |
|---|
| 99 | (evt.type == gtk.gdk._2BUTTON_PRESS) and \ |
|---|
| 100 | (self.tree.change_selection(self)): |
|---|
| 101 | |
|---|
| 102 | self.active = True |
|---|
| 103 | self.emit('clicked') |
|---|
| 104 | |
|---|
| 105 | elif (evt.type == gtk.gdk.BUTTON_PRESS) and \ |
|---|
| 106 | (self.tree.change_selection(self)): |
|---|
| 107 | |
|---|
| 108 | self.active = True |
|---|
| 109 | |
|---|
| 110 | if evt.button == 3: |
|---|
| 111 | self.emit('popup', evt) |
|---|
| 112 | |
|---|
| 113 | def get_active(self): |
|---|
| 114 | return self._active |
|---|
| 115 | |
|---|
| 116 | def set_active(self, value): |
|---|
| 117 | self._active = value |
|---|
| 118 | self.emit('activate') |
|---|
| 119 | |
|---|
| 120 | if self.flags() & gtk.REALIZED: |
|---|
| 121 | if value: |
|---|
| 122 | self.modify_bg(gtk.STATE_NORMAL, \ |
|---|
| 123 | self.style.base[gtk.STATE_PRELIGHT]) |
|---|
| 124 | else: |
|---|
| 125 | self.modify_bg(gtk.STATE_NORMAL, self.style.white) |
|---|
| 126 | |
|---|
| 127 | def get_vbox(self): |
|---|
| 128 | return self._vbox |
|---|
| 129 | |
|---|
| 130 | active = property(get_active, set_active) |
|---|
| 131 | vbox = property(get_vbox) |
|---|
| 132 | |
|---|
| 133 | class PluginRow(HIGRichRow): |
|---|
| 134 | """ |
|---|
| 135 | A custom HIGRichRow to contains Plugin informations |
|---|
| 136 | """ |
|---|
| 137 | |
|---|
| 138 | __gtype_name__ = "PluginRow" |
|---|
| 139 | |
|---|
| 140 | def __init__(self, tree, reader): |
|---|
| 141 | """ |
|---|
| 142 | Create a PluginRow |
|---|
| 143 | |
|---|
| 144 | @param tree a PluginRichList object to use as parent |
|---|
| 145 | @param reader a PluginrReader object to be represented |
|---|
| 146 | """ |
|---|
| 147 | |
|---|
| 148 | HIGRichRow.__init__(self, tree) |
|---|
| 149 | |
|---|
| 150 | self._reader = reader |
|---|
| 151 | self._enabled = False |
|---|
| 152 | |
|---|
| 153 | self._message = reader.description |
|---|
| 154 | self._show_progress = False |
|---|
| 155 | self._show_include = False |
|---|
| 156 | self._activatable = True |
|---|
| 157 | self._saturate = False |
|---|
| 158 | |
|---|
| 159 | self.__create_widgets() |
|---|
| 160 | self.__pack_widgets() |
|---|
| 161 | |
|---|
| 162 | self.enabled = self._reader.enabled |
|---|
| 163 | self.connect('activate', self.__on_activate) |
|---|
| 164 | |
|---|
| 165 | self.show_all() |
|---|
| 166 | |
|---|
| 167 | self.progressbar.hide() |
|---|
| 168 | self.box_act.hide() |
|---|
| 169 | self.versions_button.hide() |
|---|
| 170 | |
|---|
| 171 | def __create_widgets(self): |
|---|
| 172 | self.image = gtk.image_new_from_pixbuf(self._reader.get_logo()) |
|---|
| 173 | |
|---|
| 174 | self.label = gtk.Label('') |
|---|
| 175 | self.label.set_ellipsize(pango.ELLIPSIZE_END) |
|---|
| 176 | |
|---|
| 177 | self.versions_model = gtk.ListStore(str, str) |
|---|
| 178 | self.versions_button = gtk.ComboBox(self.versions_model) |
|---|
| 179 | |
|---|
| 180 | rend = gtk.CellRendererPixbuf() |
|---|
| 181 | self.versions_button.pack_start(rend, False) |
|---|
| 182 | self.versions_button.add_attribute(rend, 'stock-id', 0) |
|---|
| 183 | |
|---|
| 184 | rend = gtk.CellRendererText() |
|---|
| 185 | self.versions_button.pack_end(rend) |
|---|
| 186 | self.versions_button.add_attribute(rend, 'text', 1) |
|---|
| 187 | |
|---|
| 188 | self.img_play = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PLAY, \ |
|---|
| 189 | gtk.ICON_SIZE_BUTTON) |
|---|
| 190 | self.img_stop = gtk.image_new_from_stock(gtk.STOCK_MEDIA_STOP, \ |
|---|
| 191 | gtk.ICON_SIZE_BUTTON) |
|---|
| 192 | |
|---|
| 193 | self.action_btn = HIGButton('') |
|---|
| 194 | self.uninstall_btn = HIGButton(_("Uninstall"), gtk.STOCK_CLEAR) |
|---|
| 195 | self.preference_btn = HIGButton(stock=gtk.STOCK_PREFERENCES) |
|---|
| 196 | |
|---|
| 197 | self.progressbar = gtk.ProgressBar() |
|---|
| 198 | |
|---|
| 199 | def __pack_widgets(self): |
|---|
| 200 | |
|---|
| 201 | # Visible part |
|---|
| 202 | hbox = gtk.HBox(False, 4) |
|---|
| 203 | hbox.set_border_width(4) |
|---|
| 204 | |
|---|
| 205 | hbox.pack_start(self.image, False, False, 0) |
|---|
| 206 | |
|---|
| 207 | vbox = gtk.VBox(False, 2) |
|---|
| 208 | |
|---|
| 209 | mhbox = gtk.HBox(False, 2) |
|---|
| 210 | self.label.set_alignment(0, 0.5) |
|---|
| 211 | |
|---|
| 212 | mhbox.pack_start(self.label) |
|---|
| 213 | |
|---|
| 214 | minibox = gtk.VBox() |
|---|
| 215 | minibox.pack_start(self.versions_button, False, False, 0) |
|---|
| 216 | |
|---|
| 217 | mhbox.pack_start(minibox, False, False) |
|---|
| 218 | |
|---|
| 219 | vbox.pack_start(mhbox) |
|---|
| 220 | vbox.pack_start(self.progressbar, False, False, 0) |
|---|
| 221 | |
|---|
| 222 | hbox.pack_start(vbox) |
|---|
| 223 | |
|---|
| 224 | self.vbox.pack_start(hbox, False, False, 0) |
|---|
| 225 | |
|---|
| 226 | # Buttons part |
|---|
| 227 | align = gtk.Alignment(0, 0.5) |
|---|
| 228 | align.add(self.preference_btn) |
|---|
| 229 | |
|---|
| 230 | self.box_act = gtk.HBox(False, 2) |
|---|
| 231 | self.box_act.pack_start(align, True, True, 0) |
|---|
| 232 | self.box_act.pack_start(self.uninstall_btn, False, False, 0) |
|---|
| 233 | self.box_act.pack_start(self.action_btn, False, False, 0) |
|---|
| 234 | |
|---|
| 235 | self.box_act.set_border_width(4) |
|---|
| 236 | self.vbox.pack_start(self.box_act, False, False, 0) |
|---|
| 237 | |
|---|
| 238 | def __on_activate(self, widget): |
|---|
| 239 | if not self._activatable: |
|---|
| 240 | self.box_act.hide() |
|---|
| 241 | return |
|---|
| 242 | |
|---|
| 243 | if self._show_progress: |
|---|
| 244 | self.box_act.hide() |
|---|
| 245 | self.progressbar.show() |
|---|
| 246 | else: |
|---|
| 247 | self.progressbar.hide() |
|---|
| 248 | |
|---|
| 249 | if self.active: |
|---|
| 250 | self.box_act.show() |
|---|
| 251 | else: |
|---|
| 252 | self.box_act.hide() |
|---|
| 253 | |
|---|
| 254 | def get_enabled(self): |
|---|
| 255 | return self._enabled |
|---|
| 256 | |
|---|
| 257 | def set_enabled(self, val): |
|---|
| 258 | self._enabled = val |
|---|
| 259 | |
|---|
| 260 | # We need more testing on color/saturate on enabled |
|---|
| 261 | |
|---|
| 262 | if self._enabled: |
|---|
| 263 | self.action_btn.set_label(_("Disable")) |
|---|
| 264 | self.action_btn.set_image(self.img_stop) |
|---|
| 265 | |
|---|
| 266 | # |
|---|
| 267 | color = self.style.text[gtk.STATE_NORMAL] |
|---|
| 268 | self.saturate = False |
|---|
| 269 | else: |
|---|
| 270 | self.action_btn.set_label(_("Enable")) |
|---|
| 271 | self.action_btn.set_image(self.img_play) |
|---|
| 272 | |
|---|
| 273 | # |
|---|
| 274 | color = self.style.text[gtk.STATE_INSENSITIVE] |
|---|
| 275 | self.saturate = True |
|---|
| 276 | |
|---|
| 277 | self.label.set_text( \ |
|---|
| 278 | "<span color=\"%s\">" |
|---|
| 279 | "<span size=\"x-large\" weight=\"bold\">%s</span>" \ |
|---|
| 280 | " %s" \ |
|---|
| 281 | "\n<tt>%s</tt>" \ |
|---|
| 282 | "</span>" % \ |
|---|
| 283 | ( \ |
|---|
| 284 | color.to_string(), \ |
|---|
| 285 | self._reader.name, \ |
|---|
| 286 | self._reader.version, \ |
|---|
| 287 | self._message \ |
|---|
| 288 | ) \ |
|---|
| 289 | ) |
|---|
| 290 | self.label.set_use_markup(True) |
|---|
| 291 | |
|---|
| 292 | def get_reader(self): |
|---|
| 293 | return self._reader |
|---|
| 294 | |
|---|
| 295 | enabled = property(get_enabled, set_enabled) |
|---|
| 296 | reader = property(get_reader) |
|---|
| 297 | |
|---|
| 298 | def get_message(self): |
|---|
| 299 | return self._message |
|---|
| 300 | |
|---|
| 301 | def set_message(self, value): |
|---|
| 302 | """ |
|---|
| 303 | If not defined don't update |
|---|
| 304 | """ |
|---|
| 305 | |
|---|
| 306 | if value is not None: |
|---|
| 307 | self._message = value |
|---|
| 308 | |
|---|
| 309 | # Used to update the label |
|---|
| 310 | self.enabled = self.enabled |
|---|
| 311 | |
|---|
| 312 | def get_progress(self): |
|---|
| 313 | if self._show_progress: |
|---|
| 314 | return self.progressbar.get_fraction() |
|---|
| 315 | return None |
|---|
| 316 | |
|---|
| 317 | def set_progress(self, val): |
|---|
| 318 | self.box_act.hide() |
|---|
| 319 | |
|---|
| 320 | if not val or val < .0: |
|---|
| 321 | self._show_progress = False |
|---|
| 322 | self.progressbar.set_fraction(0) |
|---|
| 323 | self.progressbar.hide() |
|---|
| 324 | else: |
|---|
| 325 | self._show_progress = True |
|---|
| 326 | self.progressbar.set_fraction(val) |
|---|
| 327 | self.progressbar.set_text('%d %%' % int(val * 100)) |
|---|
| 328 | self.progressbar.show() |
|---|
| 329 | |
|---|
| 330 | def get_include(self): |
|---|
| 331 | if self._show_include: |
|---|
| 332 | id = self.versions_button.get_active() -1 |
|---|
| 333 | |
|---|
| 334 | if id >= 0: |
|---|
| 335 | return True |
|---|
| 336 | else: |
|---|
| 337 | return False |
|---|
| 338 | |
|---|
| 339 | def set_include(self, value): |
|---|
| 340 | self._show_include = value |
|---|
| 341 | |
|---|
| 342 | if value: |
|---|
| 343 | self.versions_button.show() |
|---|
| 344 | else: |
|---|
| 345 | self.versions_button.hide() |
|---|
| 346 | |
|---|
| 347 | def get_activatable(self): |
|---|
| 348 | return self._activatable |
|---|
| 349 | def set_activatable(self, value): |
|---|
| 350 | self._activatable = value |
|---|
| 351 | self.__on_activate(self) |
|---|
| 352 | |
|---|
| 353 | def get_saturate(self): |
|---|
| 354 | return self._saturate |
|---|
| 355 | |
|---|
| 356 | def set_saturate(self, val): |
|---|
| 357 | self._saturate = val |
|---|
| 358 | |
|---|
| 359 | if self._saturate: |
|---|
| 360 | logo = self._reader.get_logo() |
|---|
| 361 | logo.saturate_and_pixelate(logo, 0.3, False) |
|---|
| 362 | self.image.set_from_pixbuf(logo) |
|---|
| 363 | else: |
|---|
| 364 | self.image.set_from_pixbuf(self._reader.get_logo()) |
|---|
| 365 | |
|---|
| 366 | message = property(get_message, set_message) |
|---|
| 367 | progress = property(get_progress, set_progress) |
|---|
| 368 | show_include = property(get_include, set_include) |
|---|
| 369 | activatable = property(get_activatable, set_activatable) |
|---|
| 370 | saturate = property(get_saturate, set_saturate) |
|---|
| 371 | |
|---|
| 372 | class HIGRichList(gtk.ScrolledWindow): |
|---|
| 373 | """ |
|---|
| 374 | A simil-treeview widget like firefox RichList |
|---|
| 375 | """ |
|---|
| 376 | |
|---|
| 377 | def __init__(self): |
|---|
| 378 | """ |
|---|
| 379 | Create a HIGRichList object |
|---|
| 380 | """ |
|---|
| 381 | |
|---|
| 382 | gtk.ScrolledWindow.__init__(self) |
|---|
| 383 | |
|---|
| 384 | self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) |
|---|
| 385 | self.set_shadow_type(gtk.SHADOW_ETCHED_IN) |
|---|
| 386 | |
|---|
| 387 | self.vbox = gtk.VBox() |
|---|
| 388 | |
|---|
| 389 | self.add_with_viewport(self.vbox) |
|---|
| 390 | |
|---|
| 391 | # We set the background of viewport to white |
|---|
| 392 | self.get_child().modify_bg(gtk.STATE_NORMAL, \ |
|---|
| 393 | self.get_child().style.white) |
|---|
| 394 | |
|---|
| 395 | self.prev_sel = None |
|---|
| 396 | |
|---|
| 397 | self.show_all() |
|---|
| 398 | |
|---|
| 399 | def append_row(self, widget): |
|---|
| 400 | """ |
|---|
| 401 | Append a row to the tree |
|---|
| 402 | |
|---|
| 403 | @param widget a HIGRichRow type object |
|---|
| 404 | """ |
|---|
| 405 | |
|---|
| 406 | assert isinstance(widget, HIGRichRow), "must be a HIGRichRow object" |
|---|
| 407 | |
|---|
| 408 | self.vbox.pack_start(widget, False, False, 0) |
|---|
| 409 | |
|---|
| 410 | def remove_row(self, widget): |
|---|
| 411 | """ |
|---|
| 412 | Remove a row from the tree |
|---|
| 413 | |
|---|
| 414 | @param widget a HIGRichRow type object |
|---|
| 415 | """ |
|---|
| 416 | assert isinstance(widget, HIGRichRow), "must be a HIGRichRow object" |
|---|
| 417 | |
|---|
| 418 | self.vbox.remove(widget) |
|---|
| 419 | |
|---|
| 420 | def clear(self): |
|---|
| 421 | """ |
|---|
| 422 | Remove all the row |
|---|
| 423 | """ |
|---|
| 424 | |
|---|
| 425 | def remove(widget, parent): |
|---|
| 426 | # |
|---|
| 427 | widget.hide() |
|---|
| 428 | parent.remove(widget) |
|---|
| 429 | |
|---|
| 430 | self.vbox.foreach(remove, self.vbox) |
|---|
| 431 | |
|---|
| 432 | def get_rows(self): |
|---|
| 433 | return len(self.vbox) |
|---|
| 434 | |
|---|
| 435 | def foreach(self, callback, userdata): |
|---|
| 436 | "Foreach in any widgets" |
|---|
| 437 | |
|---|
| 438 | self.vbox.foreach(callback, userdata) |
|---|
| 439 | |
|---|
| 440 | def change_selection(self, row): |
|---|
| 441 | """ |
|---|
| 442 | Change the selected item in the tree |
|---|
| 443 | |
|---|
| 444 | @return True if selection was changed |
|---|
| 445 | """ |
|---|
| 446 | assert row is not None |
|---|
| 447 | |
|---|
| 448 | if self.prev_sel: |
|---|
| 449 | self.prev_sel.active = False |
|---|
| 450 | |
|---|
| 451 | self.prev_sel = row |
|---|
| 452 | |
|---|
| 453 | # Grab the focus! |
|---|
| 454 | self.grab_focus() |
|---|
| 455 | |
|---|
| 456 | # Scroll to active item |
|---|
| 457 | adj = self.get_vadjustment() |
|---|
| 458 | alloc = row.get_allocation() |
|---|
| 459 | |
|---|
| 460 | if alloc.y < adj.value: |
|---|
| 461 | adj.set_value(alloc.y) |
|---|
| 462 | elif alloc.y + alloc.height > adj.value + adj.page_size: |
|---|
| 463 | adj.set_value(alloc.y) |
|---|
| 464 | |
|---|
| 465 | return True |
|---|