~gtg-user/gtg/documenters-credits

« back to all changes in this revision

Viewing changes to GTG/gtk/browser/tag_context_menu.py

  • Committer: Izidor Matušov
  • Date: 2012-05-17 19:05:15 UTC
  • mfrom: (1175.2.25 new-tag-editor)
  • Revision ID: izidor.matusov@gmail.com-20120517190515-wbxxgcah6ogxp9an
New awesome tag editor made by Bertrand!

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# this program.  If not, see <http://www.gnu.org/licenses/>.
19
19
# -----------------------------------------------------------------------------
20
20
 
 
21
"""
 
22
tag_context_menu:
 
23
Implements a context (pop-up) menu for the tag item in the sidebar.
 
24
Right now it is just a void shell It is supposed to become a more generic 
 
25
sidebar context for all kind of item displayed there.
 
26
Also, it is supposed to handle more complex menus (with non-std widgets,
 
27
like a color picker)
 
28
"""
 
29
 
21
30
import pygtk
22
31
pygtk.require('2.0')
23
 
import gobject
24
32
import gtk
25
33
 
26
34
from GTG import _
27
 
from GTG.gtk.browser import GnomeConfig
28
 
 
29
 
class TagContextMenu(gtk.Menu):
30
 
 
31
 
    def __init__(self, req, tag=None):
 
35
 
 
36
class TagContextMenu(gtk.Menu): # pylint: disable-msg=R0904
 
37
    """Context menu fo the tag i the sidebar"""
 
38
 
 
39
    def __init__(self, req, vmanager, tag=None):
32
40
        self.__gobject_init__()
 
41
        gtk.Menu.__init__(self)
33
42
        self.req = req
 
43
        self.vmanager = vmanager
34
44
        self.tag = tag
35
45
        # Build up the menu
36
 
        self.__build_menu()
37
46
        self.set_tag(tag)
 
47
        self.__build_menu()
 
48
 
 
49
    def __build_menu(self):
 
50
        """Build up the widget"""
 
51
        # Reset the widget
 
52
        for i in self:
 
53
            self.remove(i)
 
54
            i.destroy()
 
55
        if self.tag is not None:
 
56
            # Color chooser FIXME: SHOULD BECOME A COLOR PICKER
 
57
            self.mi_cc = gtk.MenuItem()
 
58
            self.mi_cc.set_label(_("Edit Tag..."))
 
59
            self.append(self.mi_cc)
 
60
            self.mi_cc.connect('activate', self.on_mi_cc_activate)
 
61
            if self.tag.is_search_tag():
 
62
                self.mi_del = gtk.MenuItem()
 
63
                self.mi_del.set_label(_("Delete"))
 
64
                self.append(self.mi_del)
 
65
                self.mi_del.connect('activate', self.on_mi_del_activate)
38
66
        # Make it visible
39
67
        self.show_all()
40
68
 
41
 
    def __build_menu(self):
42
 
        # Color chooser FIXME: SHOULD BECOME A COLOR PICKER
43
 
        self.mi_cc = gtk.MenuItem()
44
 
        self.mi_cc.set_label(_("Set color..."))
45
 
        self.append(self.mi_cc)
46
 
        # Reset color
47
 
        self.mi_rc = gtk.MenuItem()
48
 
        self.mi_rc.set_label(_("Reset color"))
49
 
        self.append(self.mi_rc)
50
 
        # Don't display in work view mode
51
 
        self.mi_wv = gtk.CheckMenuItem()
52
 
        self.mi_wv.set_label(GnomeConfig.TAG_IN_WORKVIEW_TOGG)
53
 
        self.append(self.mi_wv)
54
 
        # Set the callbacks
55
 
        self.mi_cc.connect('activate', self.on_mi_cc_activate)
56
 
        self.mi_rc.connect('activate', self.on_mi_rc_activate)
57
 
        self.mi_wv_toggle_hid = self.mi_wv.connect('activate', self.on_mi_wv_activate)
58
 
 
59
 
    def __set_default_values(self):
60
 
        # Don't set "Hide in workview" as active
61
 
        self.mi_wv.set_active(False)
62
 
 
63
 
    def __disable_all(self):
64
 
        pass
65
 
 
66
 
    def __enable_all(self):
67
 
        pass
68
 
 
69
69
    ### PUBLIC API ###
70
70
 
71
71
    def set_tag(self, tag):
72
72
        """Update the context menu items using the tag attributes."""
73
 
        # set_active emit the 'toggle' signal, so we have to disable the handler
74
 
        # when we update programmatically
75
 
        self.mi_wv.handler_block(self.mi_wv_toggle_hid)
76
 
        if tag is None:
77
 
            self.tag = None
78
 
            self.__set_default_values()
79
 
            self.__disable_all()
80
 
        else:
81
 
            self.tag = tag
82
 
            self.__enable_all()
83
 
            is_hidden_in_wv = (self.tag.get_attribute("nonworkview") == "True")
84
 
            self.mi_wv.set_active(is_hidden_in_wv)
85
 
        self.mi_wv.handler_unblock(self.mi_wv_toggle_hid)
 
73
        self.tag = tag
 
74
        self.__build_menu()
86
75
 
87
76
    ### CALLBACKS ###
88
77
 
89
 
    def on_mi_wv_activate(self, widget):
90
 
        """Toggle the nonworkview attribute of the tag, update the view"""
91
 
        is_hidden_in_wv = not (self.tag.get_attribute("nonworkview") == "True")
92
 
        self.tag.set_attribute("nonworkview", str(is_hidden_in_wv))
93
 
 
94
 
    def on_mi_cc_activate(self, widget):
95
 
        color_dialog = gtk.ColorSelectionDialog('Choose color')
96
 
        colorsel = color_dialog.colorsel
97
 
 
98
 
        # Get previous color
99
 
        color = self.tag.get_attribute("color")
100
 
        if color is not None:
101
 
            colorspec = gtk.gdk.color_parse(color)
102
 
            colorsel.set_previous_color(colorspec)
103
 
            colorsel.set_current_color(colorspec)
104
 
        response = color_dialog.run()
105
 
        new_color = colorsel.get_current_color()
106
 
        
107
 
        # Check response_id and set color if required
108
 
        if response == gtk.RESPONSE_OK and new_color:
109
 
            strcolor = gtk.color_selection_palette_to_string([new_color])
110
 
            self.tag.set_attribute("color", strcolor)
111
 
        color_dialog.destroy()
112
 
 
113
 
    def on_mi_rc_activate(self, widget):
114
 
        """
115
 
        handler for the right click popup menu item from tag tree, when its a @tag
116
 
        """
117
 
        self.tag.del_attribute("color")
118
 
 
 
78
    def on_mi_cc_activate(self, widget): # pylint: disable-msg=W0613
 
79
        """Callback: show the tag editor upon request"""
 
80
        self.vmanager.open_tag_editor(self.tag)
 
81
 
 
82
    def on_mi_del_activate(self, widget): # pylint: disable-msg=W0613
 
83
        """ delete a selected search """
 
84
        self.req.remove_tag(self.tag.get_name())