1
 
# Copyright (C) 2005 Dan Loda <danloda@gmail.com>
 
3
 
# This program is free software; you can redistribute it and/or modify
 
4
 
# it under the terms of the GNU General Public License as published by
 
5
 
# the Free Software Foundation; either version 2 of the License, or
 
6
 
# (at your option) any later version.
 
8
 
# This program is distributed in the hope that it will be useful,
 
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 
# GNU General Public License for more details.
 
13
 
# You should have received a copy of the GNU General Public License
 
14
 
# along with this program; if not, write to the Free Software
 
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
27
 
    SPAN_IS_SEPARATOR_COL,
 
32
 
class SpanSelector(gtk.HBox):
 
33
 
    """Encapsulates creation and functionality of widgets used for changing
 
36
 
    Note that calling any activate_* methods will emit "span-changed".
 
43
 
    def __init__(self, homogeneous=False, spacing=6):
 
44
 
        gtk.HBox.__init__(self, homogeneous, spacing)
 
46
 
        self.model = self._create_model()
 
47
 
        self.combo = self._create_combobox(self.model)
 
48
 
        self.entry = self._create_custom_entry()
 
50
 
        label = gtk.Label("Highlighting spans:")
 
53
 
        self.pack_start(label, expand=False, fill=True)
 
54
 
        self.pack_start(self.combo, expand=False, fill=False)
 
55
 
        self.pack_start(self.entry, expand=False, fill=False)
 
57
 
    def set_to_oldest_span(self, span):
 
58
 
        """Set the span associated with the "to Oldest Revision" entry."""
 
59
 
        self.model.set_value(self.oldest_iter, SPAN_DAYS_COL, span)
 
61
 
    def set_newest_to_oldest_span(self, span):
 
62
 
        """Set the span associated with the "Newest to Oldset" entry."""
 
63
 
        self.model.set_value(self.newest_iter, SPAN_DAYS_COL, span)
 
65
 
    def set_max_custom_spans(self, n):
 
66
 
        """Store up to n custom span entries in the combo box."""
 
67
 
        self.max_custom_spans = n
 
69
 
    def activate(self, iter):
 
70
 
        """Activate the row pointed to by gtk.TreeIter iter."""
 
71
 
        index = self._get_index_from_iter(iter)
 
72
 
        self.combo.set_active(index)
 
74
 
    def activate_last_selected(self):
 
75
 
        """Activate the previously selected row.
 
77
 
        Expected to be used when cancelling custom entry or revieved bad
 
80
 
        if self.last_selected:
 
81
 
            self.activate(self.last_selected)
 
83
 
    def activate_default(self):
 
84
 
        """Activate the default row."""
 
85
 
        # TODO allow setting of default?
 
86
 
        self.activate(self.oldest_iter)
 
88
 
    def _get_index_from_iter(self, iter):
 
89
 
        """Returns a row index integer from iterator."""
 
90
 
        return int(self.model.get_string_from_iter(iter))
 
92
 
    def _combo_changed_cb(self, w):
 
94
 
        iter = w.get_active_iter()
 
96
 
        if model.get_value(iter, SPAN_IS_CUSTOM_COL):
 
97
 
            self._request_custom_span()
 
99
 
            self.last_selected = iter
 
100
 
            self.emit("span-changed", model.get_value(iter, SPAN_DAYS_COL))
 
102
 
    def _activate_custom_span_cb(self, w):
 
103
 
        self.entry.hide_all()
 
106
 
        span = float(w.get_text())
 
109
 
            # FIXME this works as "cancel", returning to the previous span,
 
110
 
            # but it emits "span-changed", which isn't necessary.
 
111
 
            self.activate_last_selected()
 
114
 
        self.add_custom_span(span)
 
115
 
        self.emit("custom-span-added", span)
 
117
 
        self.activate(self.custom_iter)
 
119
 
    def add_custom_span(self, span):
 
120
 
        if not len(self.custom_spans):
 
121
 
            self.custom_iter = self.model.insert_after(self.custom_iter,
 
123
 
            self.custom_iter_top = self.custom_iter.copy()
 
125
 
        if len(self.custom_spans) == self.max_custom_spans:
 
126
 
            self.custom_spans.pop(0)
 
127
 
            self.model.remove(self.model.iter_next(self.custom_iter_top))
 
129
 
        self.custom_spans.append(span)
 
130
 
        self.custom_iter = self.model.insert_after(
 
131
 
            self.custom_iter, [span, "%.2f Days" % span, False, False])
 
133
 
    def _request_custom_span(self):
 
135
 
        self.entry.show_all()
 
137
 
    def _create_model(self):
 
138
 
        # [span in days, span as string, row is seperator?, row is select
 
140
 
        m = gtk.ListStore(gobject.TYPE_FLOAT,
 
142
 
                          gobject.TYPE_BOOLEAN,
 
143
 
                          gobject.TYPE_BOOLEAN)
 
145
 
        self.separator = [0., "", True, False]
 
147
 
        self.oldest_iter = m.append([0., "to Oldest Revision", False, False])
 
148
 
        self.newest_iter = m.append([0., "Newest to Oldest", False, False])
 
149
 
        m.append(self.separator)
 
150
 
        m.append([1., "1 Day", False, False])
 
151
 
        m.append([7., "1 Week", False, False])
 
152
 
        m.append([30., "1 Month", False, False])
 
153
 
        self.custom_iter = m.append([365., "1 Year", False, False])
 
154
 
        m.append(self.separator)
 
155
 
        m.append([0., "Custom...", False, True])
 
159
 
    def _create_combobox(self, model):
 
160
 
        cb = gtk.ComboBox(model)
 
161
 
        cb.set_row_separator_func(
 
162
 
            lambda m, i: m.get_value(i, SPAN_IS_SEPARATOR_COL))
 
163
 
        cell = gtk.CellRendererText()
 
164
 
        cb.pack_start(cell, False)
 
165
 
        cb.add_attribute(cell, "text", SPAN_STR_COL)
 
166
 
        cb.connect("changed", self._combo_changed_cb)
 
171
 
    def _create_custom_entry(self):
 
172
 
        entry = gtk.HBox(False, 6)
 
174
 
        spin = gtk.SpinButton(digits=2)
 
175
 
        spin.set_numeric(True)
 
176
 
        spin.set_increments(1., 10.)
 
177
 
        spin.set_range(0., 100 * 365) # I presume 100 years is sufficient
 
178
 
        spin.connect("activate", self._activate_custom_span_cb)
 
179
 
        spin.connect("show", lambda w: w.grab_focus())
 
181
 
        label = gtk.Label("Days")
 
183
 
        entry.pack_start(spin, expand=False, fill=False)
 
184
 
        entry.pack_start(label, expand=False, fill=False)
 
189
 
"""The "span-changed" signal is emitted when a new span has been selected or
 
192
 
Callback signature: def callback(SpanSelector, span, [user_param, ...])
 
194
 
gobject.signal_new("span-changed", SpanSelector,
 
195
 
                   gobject.SIGNAL_RUN_LAST,
 
197
 
                   (gobject.TYPE_FLOAT,))
 
199
 
"""The "custom-span-added" signal is emitted after a custom span has been
 
200
 
added, but before it has been selected.
 
202
 
Callback signature: def callback(SpanSelector, span, [user_param, ...])
 
204
 
gobject.signal_new("custom-span-added", SpanSelector,
 
205
 
                   gobject.SIGNAL_RUN_LAST,
 
207
 
                   (gobject.TYPE_FLOAT,))