/usr/share/pyshared/bzrlib/plugins/gtk/annotate/spanselector.py is in bzr-gtk 0.103.0+bzr792-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2005 Dan Loda <danloda@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
from gi.repository import GObject
from gi.repository import Gtk
(
SPAN_DAYS_COL,
SPAN_STR_COL,
SPAN_IS_SEPARATOR_COL,
SPAN_IS_CUSTOM_COL
) = range(4)
class SpanSelector(Gtk.HBox):
"""Encapsulates creation and functionality of widgets used for changing
highlight spans.
Note that calling any activate_* methods will emit "span-changed".
"""
max_custom_spans = 4
custom_spans = []
last_selected = None
def __init__(self, homogeneous=False, spacing=6):
super(SpanSelector, self).__init__(
homogeneous=homogeneous, spacing=spacing)
self.model = self._create_model()
self.combo = self._create_combobox(self.model)
self.entry = self._create_custom_entry()
label = Gtk.Label(label="Highlighting spans:")
label.show()
self.pack_start(label, False, True, True, 0)
self.pack_start(self.combo, False, False, True, 0)
self.pack_start(self.entry, False, False, True, 0)
def set_to_oldest_span(self, span):
"""Set the span associated with the "to Oldest Revision" entry."""
self.model.set_value(self.oldest_iter, SPAN_DAYS_COL, span)
def set_newest_to_oldest_span(self, span):
"""Set the span associated with the "Newest to Oldset" entry."""
self.model.set_value(self.newest_iter, SPAN_DAYS_COL, span)
def set_max_custom_spans(self, n):
"""Store up to n custom span entries in the combo box."""
self.max_custom_spans = n
def activate(self, iter):
"""Activate the row pointed to by Gtk.TreeIter iter."""
index = self._get_index_from_iter(iter)
self.combo.set_active(index)
def activate_last_selected(self):
"""Activate the previously selected row.
Expected to be used when cancelling custom entry or revieved bad
input.
"""
if self.last_selected:
self.activate(self.last_selected)
def activate_default(self):
"""Activate the default row."""
# TODO allow setting of default?
self.activate(self.oldest_iter)
def _get_index_from_iter(self, iter):
"""Returns a row index integer from iterator."""
return int(self.model.get_string_from_iter(iter))
def _combo_changed_cb(self, w):
model = w.get_model()
iter = w.get_active_iter()
if model.get_value(iter, SPAN_IS_CUSTOM_COL):
self._request_custom_span()
else:
self.last_selected = iter
self.emit("span-changed", model.get_value(iter, SPAN_DAYS_COL))
def _activate_custom_span_cb(self, w):
self.entry.hide()
self.combo.show()
span = float(w.get_text())
if span == 0:
# FIXME this works as "cancel", returning to the previous span,
# but it emits "span-changed", which isn't necessary.
self.activate_last_selected()
return
self.add_custom_span(span)
self.emit("custom-span-added", span)
self.activate(self.custom_iter)
def add_custom_span(self, span):
if not len(self.custom_spans):
self.custom_iter = self.model.insert_after(self.custom_iter,
self.separator)
self.custom_iter_top = self.custom_iter.copy()
if len(self.custom_spans) == self.max_custom_spans:
self.custom_spans.pop(0)
self.model.remove(self.model.iter_next(self.custom_iter_top))
self.custom_spans.append(span)
self.custom_iter = self.model.insert_after(
self.custom_iter, [span, "%.2f Days" % span, False, False])
def _request_custom_span(self):
self.combo.hide()
self.entry.show_all()
def _create_model(self):
# [span in days, span as string, row is seperator?, row is select
# default?]
m = Gtk.ListStore(GObject.TYPE_FLOAT,
GObject.TYPE_STRING,
GObject.TYPE_BOOLEAN,
GObject.TYPE_BOOLEAN)
self.separator = [0., "", True, False]
self.oldest_iter = m.append([0., "to Oldest Revision", False, False])
self.newest_iter = m.append([0., "Newest to Oldest", False, False])
m.append(self.separator)
m.append([1., "1 Day", False, False])
m.append([7., "1 Week", False, False])
m.append([30., "1 Month", False, False])
self.custom_iter = m.append([365., "1 Year", False, False])
m.append(self.separator)
m.append([0., "Custom...", False, True])
return m
def _create_combobox(self, model):
cb = Gtk.ComboBox(model)
cb.set_row_separator_func(
lambda m, i: m.get_value(i, SPAN_IS_SEPARATOR_COL))
cell = Gtk.CellRendererText()
cb.pack_start(cell, False)
cb.add_attribute(cell, "text", SPAN_STR_COL)
cb.connect("changed", self._combo_changed_cb)
cb.show()
return cb
def _create_custom_entry(self):
entry = Gtk.HBox(False, 6)
spin = Gtk.SpinButton(digits=2)
spin.set_numeric(True)
spin.set_increments(1., 10.)
spin.set_range(0., 100 * 365) # I presume 100 years is sufficient
spin.connect("activate", self._activate_custom_span_cb)
spin.connect("show", lambda w: w.grab_focus())
label = Gtk.Label(label="Days")
entry.pack_start(spin, False, False, True, 0)
entry.pack_start(label, False, False, True, 0)
return entry
"""The "span-changed" signal is emitted when a new span has been selected or
entered.
Callback signature: def callback(SpanSelector, span, [user_param, ...])
"""
GObject.signal_new("span-changed", SpanSelector,
GObject.SignalFlags.RUN_LAST,
None,
(GObject.TYPE_FLOAT,))
"""The "custom-span-added" signal is emitted after a custom span has been
added, but before it has been selected.
Callback signature: def callback(SpanSelector, span, [user_param, ...])
"""
GObject.signal_new("custom-span-added", SpanSelector,
GObject.SignalFlags.RUN_LAST,
None,
(GObject.TYPE_FLOAT,))
|