~kklimonda/ubuntu/natty/hamster-applet/lp.697667

« back to all changes in this revision

Viewing changes to src/hamster/widgets/activityentry.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2010-02-10 02:52:31 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100210025231-x0q5h4q7nlvihl09
Tags: 2.29.90-0ubuntu1
* New upstream version
  - workspace tracking - switch activity, when switching desktops
  - chart improvements - theme friendly and less noisier
  - for those without GNOME panel there is now a standalone version, 
    accessible via Applications -> Accessories -> Time Tracker
  - overview window remembers position
  - maintaining cursor on the selected row after edits / refreshes
  - descriptions once again in the main input field, delimited by comma
  - activity suggestion box now sorts items by recency (Patryk Zawadzki)
  - searching
  - simplified save report dialog, thanks to the what you see is what you 
    report revamp
  - overview/stats replaced with activities / totals and stats accessible 
    from totals
  - interactive graphs to drill down in totals
  - miscellaneous performance improvements
  - pixel-perfect graphs
* Updated 01_startup-fix.patch to apply to new source layout
* debian/control:
  - Add build-depend on gnome-doc-utils

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# - coding: utf-8 -
 
2
 
 
3
# Copyright (C) 2008-2009 Toms Bauģis <toms.baugis at gmail.com>
 
4
 
 
5
# This file is part of Project Hamster.
 
6
 
 
7
# Project Hamster 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 3 of the License, or
 
10
# (at your option) any later version.
 
11
 
 
12
# Project Hamster 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 Project Hamster.  If not, see <http://www.gnu.org/licenses/>.
 
19
 
 
20
import gtk, gobject
 
21
import datetime as dt
 
22
 
 
23
from .hamster.configuration import runtime
 
24
 
 
25
from .hamster import stuff
 
26
from .hamster.stuff import format_duration
 
27
 
 
28
class ActivityEntry(gtk.Entry):
 
29
    __gsignals__ = {
 
30
        'value-entered': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
 
31
    }
 
32
 
 
33
 
 
34
    def __init__(self):
 
35
        gtk.Entry.__init__(self)
 
36
        self.news = False
 
37
        self.activities = None
 
38
        self.categories = None
 
39
        self.filter = None
 
40
        self.max_results = 10 # limit popup size to 10 results
 
41
 
 
42
        self.popup = gtk.Window(type = gtk.WINDOW_POPUP)
 
43
 
 
44
        box = gtk.ScrolledWindow()
 
45
        box.set_shadow_type(gtk.SHADOW_IN)
 
46
        box.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
 
47
 
 
48
        self.tree = gtk.TreeView()
 
49
        self.tree.set_headers_visible(False)
 
50
        self.tree.set_hover_selection(True)
 
51
 
 
52
        bgcolor = gtk.Style().bg[gtk.STATE_NORMAL].to_string()
 
53
        time_cell = gtk.CellRendererPixbuf()
 
54
        time_cell.set_property("icon-name", "appointment-new")
 
55
        time_cell.set_property("cell-background", bgcolor)
 
56
 
 
57
        self.time_icon_column = gtk.TreeViewColumn("",
 
58
                                              time_cell)
 
59
        self.tree.append_column(self.time_icon_column)
 
60
 
 
61
        time_cell = gtk.CellRendererText()
 
62
        time_cell.set_property("scale", 0.8)
 
63
        time_cell.set_property("cell-background", bgcolor)
 
64
 
 
65
        self.time_column = gtk.TreeViewColumn("Time",
 
66
                                              time_cell,
 
67
                                              text = 3)
 
68
        self.tree.append_column(self.time_column)
 
69
 
 
70
 
 
71
        self.activity_column = gtk.TreeViewColumn("Activity",
 
72
                                                  gtk.CellRendererText(),
 
73
                                                  text=1)
 
74
        self.activity_column.set_expand(True)
 
75
        self.tree.append_column(self.activity_column)
 
76
 
 
77
        self.category_column = gtk.TreeViewColumn("Category",
 
78
                                                  stuff.CategoryCell(),
 
79
                                                  text=2)
 
80
        self.tree.append_column(self.category_column)
 
81
 
 
82
 
 
83
 
 
84
        self.tree.connect("button-press-event", self._on_tree_button_press_event)
 
85
 
 
86
        box.add(self.tree)
 
87
        self.popup.add(box)
 
88
 
 
89
        self.connect("button-press-event", self._on_button_press_event)
 
90
        self.connect("key-press-event", self._on_key_press_event)
 
91
        self.connect("key-release-event", self._on_key_release_event)
 
92
        self.connect("focus-out-event", self._on_focus_out_event)
 
93
        self.connect("changed", self._on_text_changed)
 
94
        self.connect("parent-set", self._on_parent_set)
 
95
 
 
96
        runtime.dispatcher.add_handler('activity_updated', self.after_activity_update)
 
97
 
 
98
        self.show()
 
99
        self.populate_suggestions()
 
100
 
 
101
    def hide_popup(self):
 
102
        self.popup.hide()
 
103
 
 
104
    def show_popup(self):
 
105
        result_count = self.tree.get_model().iter_n_children(None)
 
106
        if result_count <= 1:
 
107
            self.hide_popup()
 
108
            return
 
109
 
 
110
        activity = stuff.parse_activity_input(self.filter)
 
111
        time = ''
 
112
        if activity.start_time:
 
113
            time = activity.start_time.strftime("%H:%M")
 
114
            if activity.end_time:
 
115
                time += "-%s" % activity.end_time.strftime("%H:%M")
 
116
 
 
117
        self.time_icon_column.set_visible(activity.start_time != None and self.filter.find("@") == -1)
 
118
        self.time_column.set_visible(activity.start_time != None and self.filter.find("@") == -1)
 
119
 
 
120
 
 
121
        self.category_column.set_visible(self.filter.find("@") == -1)
 
122
 
 
123
 
 
124
        #move popup under the widget
 
125
        alloc = self.get_allocation()
 
126
 
 
127
        #TODO - this is clearly unreliable as we calculate tree row size based on our gtk entry
 
128
        popup_height = (alloc.height-6) * min([result_count, self.max_results])
 
129
        self.tree.parent.set_size_request(alloc.width, popup_height)
 
130
        self.popup.resize(alloc.width, popup_height)
 
131
 
 
132
        x, y = self.get_parent_window().get_origin()
 
133
        y = y + alloc.y
 
134
 
 
135
        if y + alloc.height + popup_height < self.get_screen().get_height():
 
136
            y = y + alloc.height
 
137
        else:
 
138
            y = y - popup_height
 
139
 
 
140
        self.popup.move(x + alloc.x, y)
 
141
 
 
142
        self.popup.show_all()
 
143
 
 
144
    def complete_inline(self):
 
145
        model = self.tree.get_model()
 
146
        activity = stuff.parse_activity_input(self.filter)
 
147
        subject = self.get_text()
 
148
 
 
149
        if not subject or model.iter_n_children(None) == 0:
 
150
            return
 
151
 
 
152
        prefix_length = 0
 
153
 
 
154
        labels = [row[0] for row in model]
 
155
        shortest = min([len(label) for label in labels])
 
156
        first = labels[0] #since we are looking for common prefix, we do not care which label we use for comparisons
 
157
 
 
158
        for i in range(len(subject), shortest):
 
159
            letter_matching = all([label[i]==first[i] for label in labels])
 
160
 
 
161
            if not letter_matching:
 
162
                break
 
163
 
 
164
            prefix_length +=1
 
165
 
 
166
        if prefix_length:
 
167
            prefix = first[len(subject):len(subject)+prefix_length]
 
168
            self.set_text("%s%s" % (self.filter, prefix))
 
169
            self.select_region(len(self.filter), len(self.filter) + prefix_length)
 
170
 
 
171
    def refresh_activities(self):
 
172
        # scratch category cache so it gets repopulated on demand
 
173
        self.categories = None
 
174
 
 
175
    def populate_suggestions(self):
 
176
        if self.get_selection_bounds():
 
177
            cursor = self.get_selection_bounds()[0]
 
178
        else:
 
179
            cursor = self.get_position()
 
180
 
 
181
        if self.activities and self.categories and self.filter == self.get_text().decode('utf8', 'replace')[:cursor]:
 
182
            return #same thing, no need to repopulate
 
183
 
 
184
        self.filter = self.get_text().decode('utf8', 'replace')[:cursor]
 
185
        input_activity = stuff.parse_activity_input(self.filter)
 
186
 
 
187
        # do not cache as ordering and available options change over time
 
188
        self.activities = runtime.storage.get_autocomplete_activities(input_activity.activity_name)
 
189
        self.categories = self.categories or runtime.storage.get_category_list()
 
190
 
 
191
 
 
192
        time = ''
 
193
        if input_activity.start_time:
 
194
            time = input_activity.start_time.strftime("%H:%M")
 
195
            if input_activity.end_time:
 
196
                time += "-%s" % input_activity.end_time.strftime("%H:%M")
 
197
 
 
198
 
 
199
        store = self.tree.get_model()
 
200
        if not store:
 
201
            store = gtk.ListStore(str, str, str, str)
 
202
            self.tree.set_model(store)
 
203
        store.clear()
 
204
 
 
205
        if self.filter.find("@") > 0:
 
206
            key = self.filter[self.filter.find("@")+1:].decode('utf8', 'replace').lower()
 
207
            for category in self.categories:
 
208
                if key in category['name'].decode('utf8', 'replace').lower():
 
209
                    fillable = (self.filter[:self.filter.find("@") + 1] + category['name'])
 
210
                    store.append([fillable, category['name'], fillable, time])
 
211
        else:
 
212
            key = input_activity.activity_name.decode('utf8', 'replace').lower()
 
213
            for activity in self.activities:
 
214
                fillable = activity['name']
 
215
                if activity['category']:
 
216
                    fillable += "@%s" % activity['category']
 
217
 
 
218
                if time: #as we also support deltas, for the time we will grab anything up to first space
 
219
                    fillable = "%s %s" % (self.filter.split(" ", 1)[0], fillable)
 
220
 
 
221
                store.append([fillable, activity['name'], activity['category'], time])
 
222
 
 
223
    def after_activity_update(self, widget, event):
 
224
        self.refresh_activities()
 
225
 
 
226
    def _on_focus_out_event(self, widget, event):
 
227
        self.hide_popup()
 
228
 
 
229
    def _on_text_changed(self, widget):
 
230
        self.news = True
 
231
 
 
232
    def _on_button_press_event(self, button, event):
 
233
        self.populate_suggestions()
 
234
        self.show_popup()
 
235
 
 
236
    def _on_key_release_event(self, entry, event):
 
237
        if (event.keyval in (gtk.keysyms.Return, gtk.keysyms.KP_Enter)):
 
238
            if self.popup.get_property("visible"):
 
239
                if self.tree.get_cursor()[0]:
 
240
                    self.set_text(self.tree.get_model()[self.tree.get_cursor()[0][0]][0])
 
241
                self.hide_popup()
 
242
                self.set_position(len(self.get_text()))
 
243
            else:
 
244
                self._on_selected()
 
245
 
 
246
        elif (event.keyval == gtk.keysyms.Escape):
 
247
            if self.popup.get_property("visible"):
 
248
                self.hide_popup()
 
249
                return True
 
250
            else:
 
251
                return False
 
252
        elif event.keyval in (gtk.keysyms.Up, gtk.keysyms.Down):
 
253
            return False
 
254
        else:
 
255
            self.populate_suggestions()
 
256
            self.show_popup()
 
257
 
 
258
            if event.keyval not in (gtk.keysyms.Delete, gtk.keysyms.BackSpace):
 
259
                self.complete_inline()
 
260
 
 
261
 
 
262
 
 
263
 
 
264
    def _on_key_press_event(self, entry, event):
 
265
 
 
266
        if event.keyval in (gtk.keysyms.Up, gtk.keysyms.Down):
 
267
            cursor = self.tree.get_cursor()
 
268
 
 
269
            if not cursor or not cursor[0]:
 
270
                self.tree.set_cursor(0)
 
271
                return True
 
272
 
 
273
            i = cursor[0][0]
 
274
 
 
275
            if event.keyval == gtk.keysyms.Up:
 
276
                i-=1
 
277
            elif event.keyval == gtk.keysyms.Down:
 
278
                i+=1
 
279
 
 
280
            # keep it in the sane borders
 
281
            i = min(max(i, 0), len(self.tree.get_model()) - 1)
 
282
 
 
283
            self.tree.set_cursor(i)
 
284
            self.tree.scroll_to_cell(i, use_align = True, row_align = 0.4)
 
285
            return True
 
286
        else:
 
287
            return False
 
288
 
 
289
    def _on_tree_button_press_event(self, tree, event):
 
290
        model, iter = tree.get_selection().get_selected()
 
291
        value = model.get_value(iter, 0)
 
292
        self.set_text(value)
 
293
        self.hide_popup()
 
294
        self.set_position(len(self.get_text()))
 
295
 
 
296
    def _on_selected(self):
 
297
        if self.news and self.get_text():
 
298
            self.set_position(len(self.get_text()))
 
299
            self.emit("value-entered")
 
300
 
 
301
        self.news = False
 
302
 
 
303
 
 
304
    def _on_parent_set(self, old_parent, user_data):
 
305
        # when parent changes to itself, that means that it has been actually deleted
 
306
        if old_parent and old_parent == self.get_toplevel():
 
307
            runtime.dispatcher.del_handler('activity_updated', self.after_activity_update)