~ubuntu-branches/ubuntu/natty/gnome-activity-journal/natty

« back to all changes in this revision

Viewing changes to src/view.py

  • Committer: Bazaar Package Importer
  • Author(s): Siegfried-Angel Gevatter Pujals
  • Date: 2010-01-19 19:02:56 UTC
  • Revision ID: james.westby@ubuntu.com-20100119190256-x0iws5zymbi2oitq
Tags: upstream-0.3.2
Import upstream version 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -.- coding: utf-8 -.-
 
2
#
 
3
# GNOME Activity Journal
 
4
#
 
5
# Copyright © 2009-2010 Seif Lotfy <seif@lotfy.com>
 
6
# Copyright © 2010 Siegfried Gevatter <siegfried@gevatter.com>
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU Lesser General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU Lesser General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
import time
 
22
import datetime
 
23
 
 
24
from widgets import *
 
25
from daywidgets import *
 
26
from histogramwidget import histogramdata
 
27
from config import settings
 
28
 
 
29
class ActivityView(gtk.VBox):
 
30
 
 
31
    def __init__(self, cal):
 
32
 
 
33
        gtk.VBox.__init__(self)
 
34
        
 
35
        self.cal = cal
 
36
        
 
37
        self.days = {}
 
38
 
 
39
        self.daysbox = None
 
40
        self.__first_run = True
 
41
        self.set_num_days(3)
 
42
 
 
43
        self._set_searchbox()
 
44
        self._set_today_timestamp()
 
45
        self._set_view_type()
 
46
        self._set_timeline()
 
47
 
 
48
        self.set_views()
 
49
 
 
50
    def set_num_days(self, dayrange):
 
51
        self.dayrange = dayrange
 
52
        self.cal.histogram.set_dayrange(dayrange)
 
53
        self.set_views()
 
54
 
 
55
    def _set_searchbox(self):
 
56
        self.searchbox = searchbox
 
57
        self.pack_start(self.searchbox, False, False)
 
58
        self.searchbox.connect("search", self._handle_search_results)
 
59
        self.searchbox.connect("clear", self._clear_search_results)
 
60
    
 
61
    def _clear_search_results(self, widget):
 
62
        self.cal.histogram.clear_highlighted()
 
63
        for item in ITEMS:
 
64
            item.highlight()
 
65
    
 
66
    def _handle_search_results(self, widget, results):
 
67
        datastore = self.cal.histogram.datastore
 
68
        keys = []
 
69
        t = time.time()
 
70
        offset =time.mktime(time.gmtime(t)) - time.mktime(time.localtime(t))
 
71
 
 
72
        for r in results:
 
73
            timestamp = int(time.mktime(time.localtime(r[0]))) / 86400
 
74
            keys.append(offset + timestamp*86400)
 
75
 
 
76
        dates = []
 
77
        for i, (date, nitems) in enumerate(datastore):
 
78
            if int(date) in keys: 
 
79
                dates.append(i)
 
80
        self.cal.histogram.set_highlighted(dates)
 
81
        for item in ITEMS:
 
82
            item.highlight()
 
83
 
 
84
    def _set_timeline(self):
 
85
        def selection_callback(widget, datastore, i):
 
86
            if i < len(datastore):
 
87
                selection_date = datastore[i][0]
 
88
                end = selection_date  + 86399
 
89
                start = selection_date - (self.dayrange - 1) * 86400
 
90
                self.set_dayrange(start, end)
 
91
        
 
92
        histogramdata.datelist(90, self.cal.histogram.set_data)
 
93
        self.cal.histogram.add_selection_callback(selection_callback)
 
94
 
 
95
    def _set_view_type(self, refresh=False):
 
96
 
 
97
        for w in self:
 
98
            if w != self.searchbox:
 
99
                self.remove(w)
 
100
 
 
101
        if settings.get("view", "Journal") == "Journal":
 
102
            self.daysbox = gtk.HBox(True)
 
103
        else:
 
104
            self.daysbox = gtk.VBox()
 
105
 
 
106
        self.pack_start(self.daysbox, True, True, 6)
 
107
        if refresh:
 
108
            self.set_views()
 
109
        self.daysbox.show_all()
 
110
 
 
111
    def jump(self, offset):
 
112
        self.start = self.start + offset
 
113
        if time.time() > self.start:
 
114
            diff = self.start - self.cal.histogram.datastore[0][0]
 
115
            self.cal.histogram.set_selected(diff / 86400)
 
116
            self.set_dayrange(self.start, self.end+offset)
 
117
 
 
118
    def set_dayrange(self, start, end):
 
119
        self.start = start
 
120
        self.end = end
 
121
        self.dayrange = int(int((end - start)) / 86400) + 1
 
122
        self.set_views()
 
123
 
 
124
    def _set_today_timestamp(self, dayinfocus=None):
 
125
        """
 
126
        Return the dayrange of seconds between the min_timestamp and max_timestamp
 
127
        """
 
128
        # For the local timezone
 
129
        if not dayinfocus:
 
130
            dayinfocus = int(time.mktime(time.strptime(
 
131
                time.strftime("%d %B %Y") , "%d %B %Y")))
 
132
        self.end = dayinfocus + 86399
 
133
        self.start = dayinfocus - (self.dayrange - 1) * 86400
 
134
        self.set_views()
 
135
 
 
136
    def set_views(self):
 
137
        if not self.daysbox:
 
138
            return # nothing to do - TODO: should this be allowed to happen?
 
139
        for w in self.daysbox:
 
140
            self.daysbox.remove(w)
 
141
        for i in xrange(self.dayrange):
 
142
            if not settings.get("view", "Journal") == "Journal":
 
143
                i = (self.dayrange - 1) - i
 
144
            ptime =  datetime.datetime.fromtimestamp(
 
145
                self.start + i*86400).strftime("%A, %d %B %Y")
 
146
            if not self.days.has_key(ptime):
 
147
                dayview = DayWidget(self.start + i*86400,
 
148
                    self.start + i*86400 + 86400)
 
149
                self.days[ptime] = dayview
 
150
            self.daysbox.pack_start(self.days[ptime], True, True, 3)
 
151
            self.days[ptime].refresh()