~computer-janitor-hackers/systemcleaner/trunk

« back to all changes in this revision

Viewing changes to computerjanitorapp/ui_gtk.py

  • Committer: Lars Wirzenius
  • Date: 2009-08-17 18:10:26 UTC
  • mfrom: (170.1.2 popup-menu)
  • Revision ID: liw@gytha-20090817181026-lhckz680cc5kcyg6
Merged popup menu for lists of cruft.

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
        self.find_and_bind_widgets(builder)
106
106
        
107
107
        self.store = self.create_cruft_store()
 
108
        self.name_cols = set()
 
109
        self.popup_menus = dict()
108
110
        self.create_column('unused_treeview', self.unused_filter)
109
111
        self.create_column('recommended_treeview', self.recommended_filter)
110
112
        self.create_column('optimize_treeview', self.optimize_filter)
171
173
        name_col.pack_start(name_cr)
172
174
        name_col.add_attribute(name_cr, 'markup', NAME_COL)
173
175
        treeview.append_column(name_col)
 
176
        self.name_cols.add(name_col)
174
177
        
175
178
        filter_store = self.store.filter_new()
176
179
        filter_store.set_visible_func(filterfunc)
177
180
        treeview.set_model(filter_store)
 
181
        
 
182
        self.create_popup_menu_for_treeview(treeview)
 
183
 
 
184
    def create_popup_menu_for_treeview(self, treeview):
 
185
        select_all = self.gtk.MenuItem(label='Select all')
 
186
        select_all.connect('activate', self.popup_menu_select_all, treeview)
 
187
 
 
188
        unselect_all = self.gtk.MenuItem(label='Unselect all')
 
189
        unselect_all.connect('activate', self.popup_menu_unselect_all, treeview)
 
190
 
 
191
        menu = self.gtk.Menu()
 
192
        menu.append(select_all)
 
193
        menu.append(unselect_all)
 
194
        menu.show_all()
 
195
        
 
196
        self.popup_menus[treeview] = menu
178
197
 
179
198
    def unused_filter(self, store, iter):
180
199
        cruft = store.get_value(iter, CRUFT_COL)
248
267
        self.app.state.save(self.options.state_file)
249
268
        self.store.set_value(iter, STATE_COL, enabled)
250
269
 
 
270
    def foreach_set_state(self, treeview, enabled):
 
271
        def set_state(model, path, iter, user_data):
 
272
            iter2 = model.convert_iter_to_child_iter(iter)
 
273
            cruft = self.store.get_value(iter2, CRUFT_COL)
 
274
            cruft_name = cruft.get_name()
 
275
            if enabled:
 
276
                self.app.state.enable(cruft_name)
 
277
            else:
 
278
                self.app.state.disable(cruft_name)
 
279
            self.store.set_value(iter2, STATE_COL, enabled)
 
280
        treeview.get_model().foreach(set_state, None)
 
281
        self.app.state.save(self.options.state_file)
 
282
 
251
283
    def format_name(self, cruft):
252
284
        return self.gobject.markup_escape_text(cruft.get_shortname())
253
285
 
338
370
                     not self.app.state.was_previously_ignored(name))
339
371
            self.store.set_value(iter, SHOW_COL, shown)
340
372
            iter = self.store.iter_next(iter)
341
 
 
342
 
    def treeview_cursor_changed(self, treeview):
343
 
        self.toggle_long_description(treeview)
344
 
    
345
 
    on_unused_treeview_cursor_changed = treeview_cursor_changed
346
 
    on_recommended_treeview_cursor_changed = treeview_cursor_changed
347
 
    on_optimize_treeview_cursor_changed = treeview_cursor_changed
348
373
    
349
374
    def treeview_size_allocate(self, treeview, *args):
350
375
        column = treeview.get_column(NAME_COL)
369
394
            self.require_working_apt_cache()
370
395
            self.show_cruft()
371
396
 
 
397
    def treeview_button_press_event(self, treeview, event):
 
398
        # We handle mouse button presses ourselves so that we can either
 
399
        # toggle the long description (button 1, typically left) or 
 
400
        # pop up a menu (button 3, typically right).
 
401
        #
 
402
        # This is slightly tricky and probably a source of bugs.
 
403
        # Oh well.
 
404
        
 
405
        if event.button == 1:
 
406
            # Select row being clicked on. Also show/hide its long 
 
407
            # description. But only if click is on the name
 
408
            # portion of the column, not the toggle button.
 
409
            x = int(event.x)
 
410
            y = int(event.y)
 
411
            time = event.time
 
412
            pathinfo = treeview.get_path_at_pos(x, y)
 
413
            if pathinfo:
 
414
                path, col, cellx, celly = pathinfo
 
415
                if col in self.name_cols:
 
416
                    treeview.set_cursor(path, col, False)
 
417
                    self.toggle_long_description(treeview)
 
418
                else:
 
419
                    return False
 
420
            return True
 
421
        if event.button == 3:
 
422
            # Popup a menu
 
423
            x = int(event.x)
 
424
            y = int(event.y)
 
425
            time = event.time
 
426
            pathinfo = treeview.get_path_at_pos(x, y)
 
427
            if pathinfo:
 
428
                path, col, cellx, celly = pathinfo
 
429
                treeview.grab_focus()
 
430
                treeview.set_cursor(path, col, False)
 
431
                menu = self.popup_menus[treeview]
 
432
                menu.popup(None, None, None, event.button, time)
 
433
            return True
 
434
 
 
435
    on_unused_treeview_button_press_event = treeview_button_press_event
 
436
    on_recommended_treeview_button_press_event = treeview_button_press_event
 
437
    on_optimize_treeview_button_press_event = treeview_button_press_event
 
438
 
 
439
    def popup_menu_select_all(self, menuitem, treeview):
 
440
        self.foreach_set_state(treeview, True)
 
441
 
 
442
    def popup_menu_unselect_all(self, menuitem, treeview):
 
443
        self.foreach_set_state(treeview, False)
 
444