Merge lp:~jcsims/pomodoro-timer/rewrite into lp:pomodoro-timer
- rewrite
- Merge into main
Proposed by
Chris Sims
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~jcsims/pomodoro-timer/rewrite |
Merge into: | lp:pomodoro-timer |
Diff against target: |
322 lines (+208/-94) 1 file modified
bin/pomodorotimer (+208/-94) |
To merge this branch: | bzr merge lp:~jcsims/pomodoro-timer/rewrite |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Chris Sims | Approve | ||
Review via email:
|
Commit message
New direction.
Description of the change
A rewrite of the original Quickly base program. Window created and packed using pyGTK. Basic logic for various timer functions, countdown, display of time.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'bin/pomodorotimer' | |||
2 | --- bin/pomodorotimer 2010-04-04 22:06:22 +0000 | |||
3 | +++ bin/pomodorotimer 2010-04-07 04:26:17 +0000 | |||
4 | @@ -15,111 +15,225 @@ | |||
5 | 15 | #with this program. If not, see <http://www.gnu.org/licenses/>. | 15 | #with this program. If not, see <http://www.gnu.org/licenses/>. |
6 | 16 | ### END LICENSE | 16 | ### END LICENSE |
7 | 17 | 17 | ||
28 | 18 | import sys | 18 | import pygtk |
29 | 19 | import os | 19 | pygtk.require('2.0') |
30 | 20 | import gtk | 20 | import gtk, gobject |
31 | 21 | 21 | ||
32 | 22 | # Check if we are working in the source tree or from the installed | 22 | class PomodorotimerWindow(): |
13 | 23 | # package and mangle the python path accordingly | ||
14 | 24 | if os.path.dirname(sys.argv[0]) != ".": | ||
15 | 25 | if sys.argv[0][0] == "/": | ||
16 | 26 | fullPath = os.path.dirname(sys.argv[0]) | ||
17 | 27 | else: | ||
18 | 28 | fullPath = os.getcwd() + "/" + os.path.dirname(sys.argv[0]) | ||
19 | 29 | else: | ||
20 | 30 | fullPath = os.getcwd() | ||
21 | 31 | sys.path.insert(0, os.path.dirname(fullPath)) | ||
22 | 32 | |||
23 | 33 | from pomodorotimer import AboutPomodorotimerDialog, PreferencesPomodorotimerDialog | ||
24 | 34 | from pomodorotimer.pomodorotimerconfig import getdatapath | ||
25 | 35 | |||
26 | 36 | class PomodorotimerWindow(gtk.Window): | ||
27 | 37 | __gtype_name__ = "PomodorotimerWindow" | ||
33 | 38 | 23 | ||
34 | 39 | def __init__(self): | 24 | def __init__(self): |
61 | 40 | """__init__ - This function is typically not called directly. | 25 | # Create the window and set attributes |
62 | 41 | Creation a PomodorotimerWindow requires redeading the associated ui | 26 | self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) |
63 | 42 | file and parsing the ui definition extrenally, | 27 | self.window.set_title("Pomodoro Timer") |
64 | 43 | and then calling PomodorotimerWindow.finish_initializing(). | 28 | self.window.set_border_width(5) |
65 | 44 | 29 | self.window.set_resizable(False) | |
66 | 45 | Use the convenience function NewPomodorotimerWindow to create | 30 | self.window.set_size_request(400, 200) |
67 | 46 | PomodorotimerWindow object. | 31 | |
68 | 47 | 32 | # Connect the delete_event event to the event handler | |
69 | 48 | """ | 33 | self.window.connect("delete_event", self.on_destroy) |
70 | 49 | pass | 34 | |
71 | 50 | 35 | # Create and add the one VBox that we'll need | |
72 | 51 | def finish_initializing(self, builder): | 36 | box1 = gtk.VBox(False, 0) |
73 | 52 | """finish_initalizing should be called after parsing the ui definition | 37 | self.window.add(box1) |
74 | 53 | and creating a PomodorotimerWindow object with it in order to finish | 38 | box1.show() |
75 | 54 | initializing the start of the new PomodorotimerWindow instance. | 39 | |
76 | 55 | 40 | # Create the file menu and pack it | |
77 | 56 | """ | 41 | file_menu = gtk.Menu() |
78 | 57 | #get a reference to the builder and set up the signals | 42 | quit_item = gtk.MenuItem("Quit") |
79 | 58 | self.builder = builder | 43 | |
80 | 59 | self.builder.connect_signals(self) | 44 | file_menu.append(quit_item) |
81 | 60 | 45 | ||
82 | 61 | #uncomment the following code to read in preferences at start up | 46 | # Attach quit menu entry to our exit function |
83 | 62 | #dlg = PreferencesPomodorotimerDialog.NewPreferencesPomodorotimerDialog() | 47 | quit_item.connect("activate", self.on_destroy, None) |
84 | 63 | #self.preferences = dlg.get_preferences() | 48 | |
85 | 64 | 49 | # And show it | |
86 | 65 | #code for other initialization actions should be added here | 50 | quit_item.show() |
87 | 51 | |||
88 | 52 | # Create the menu bar, pack it, show it | ||
89 | 53 | menu_bar = gtk.MenuBar() | ||
90 | 54 | box1.pack_start(menu_bar, False, False, 0) | ||
91 | 55 | menu_bar.show() | ||
92 | 56 | |||
93 | 57 | # Create the visible "File" menu and associate it with our | ||
94 | 58 | # above created menu | ||
95 | 59 | file_item = gtk.MenuItem("File") | ||
96 | 60 | file_item.show() | ||
97 | 61 | file_item.set_submenu(file_menu) | ||
98 | 62 | |||
99 | 63 | # And then append the file menu to our menu bar | ||
100 | 64 | menu_bar.append(file_item) | ||
101 | 65 | |||
102 | 66 | # Create the help menu and pack it | ||
103 | 67 | help_menu = gtk.Menu() | ||
104 | 68 | about_item = gtk.MenuItem("About") | ||
105 | 69 | |||
106 | 70 | help_menu.append(about_item) | ||
107 | 71 | |||
108 | 72 | # Attach about menu entry to our about function | ||
109 | 73 | about_item.connect("activate", self.about, None) | ||
110 | 74 | |||
111 | 75 | # And show it | ||
112 | 76 | about_item.show() | ||
113 | 77 | |||
114 | 78 | # Create the visible "Help" menu and associate it with our | ||
115 | 79 | # above created menu | ||
116 | 80 | help_item = gtk.MenuItem("Help") | ||
117 | 81 | help_item.show() | ||
118 | 82 | help_item.set_submenu(help_menu) | ||
119 | 83 | |||
120 | 84 | # And then append the file menu to our menu bar | ||
121 | 85 | menu_bar.append(help_item) | ||
122 | 86 | |||
123 | 87 | # Create the HBox to hold the buttons and the timer | ||
124 | 88 | box2 = gtk.HBox(True, 0) | ||
125 | 89 | box1.pack_start(box2, False, False, 0) | ||
126 | 90 | box2.show() | ||
127 | 91 | |||
128 | 92 | # Create our VBox for the radio buttons and the start button | ||
129 | 93 | box3 = gtk.VBox(True, 0) | ||
130 | 94 | box3.set_border_width(10) | ||
131 | 95 | box2.pack_start(box3, False, False, 5) | ||
132 | 96 | box3.show() | ||
133 | 97 | |||
134 | 98 | # Create the three radio buttons and pack them | ||
135 | 99 | rbutton1 = gtk.RadioButton(None, "Pomodoro!") | ||
136 | 100 | rbutton1.connect("toggled", self.pomodoro, None) | ||
137 | 101 | rbutton1.set_active(True) | ||
138 | 102 | box3.pack_start(rbutton1, False, False, 5) | ||
139 | 103 | rbutton1.show() | ||
140 | 104 | |||
141 | 105 | rbutton2 = gtk.RadioButton(rbutton1, "Short Break") | ||
142 | 106 | rbutton2.connect("toggled", self.short_break, None) | ||
143 | 107 | box3.pack_start(rbutton2, False, False, 5) | ||
144 | 108 | rbutton2.show() | ||
145 | 109 | |||
146 | 110 | rbutton3 = gtk.RadioButton(rbutton1, "Long Break") | ||
147 | 111 | rbutton3.connect("toggled", self.long_break, None) | ||
148 | 112 | box3.pack_start(rbutton3, False, False, 5) | ||
149 | 113 | rbutton3.show() | ||
150 | 114 | |||
151 | 115 | # Create a small HBox for our start and stop buttons | ||
152 | 116 | box4 = gtk.HBox(True, 0) | ||
153 | 117 | box3.pack_start(box4, False, False, 0) | ||
154 | 118 | box4.show() | ||
155 | 119 | |||
156 | 120 | start_button = gtk.Button("Start!") | ||
157 | 121 | start_button.connect("clicked", self.start, None) | ||
158 | 122 | box4.pack_start(start_button, False, False, 5) | ||
159 | 123 | start_button.show() | ||
160 | 124 | |||
161 | 125 | stop_button = gtk.Button("Stop!") | ||
162 | 126 | stop_button.connect("clicked", self.stop, None) | ||
163 | 127 | box4.pack_start(stop_button, False, False, 5) | ||
164 | 128 | stop_button.show() | ||
165 | 129 | |||
166 | 130 | # Create the label that will become our countdown timer | ||
167 | 131 | self.timer = gtk.Label("25:00") | ||
168 | 132 | box2.pack_start(self.timer, False, False, 5) | ||
169 | 133 | self.timer.show() | ||
170 | 134 | |||
171 | 135 | |||
172 | 136 | self.window.show() | ||
173 | 66 | 137 | ||
174 | 67 | def about(self, widget, data=None): | 138 | def about(self, widget, data=None): |
175 | 68 | """about - display the about box for pomodorotimer """ | 139 | """about - display the about box for pomodorotimer """ |
192 | 69 | about = AboutPomodorotimerDialog.NewAboutPomodorotimerDialog() | 140 | # Borrowed from Jon Staley, from python-snippets package |
193 | 70 | response = about.run() | 141 | # The AboutDialog has good helper methods which |
194 | 71 | about.destroy() | 142 | # setup the dialog and add the content ensuring all |
195 | 72 | 143 | # about dialog are consistant. Below is a small example | |
196 | 73 | def preferences(self, widget, data=None): | 144 | |
197 | 74 | """preferences - display the preferences window for pomodorotimer """ | 145 | # Create AboutDialog object |
198 | 75 | prefs = PreferencesPomodorotimerDialog.NewPreferencesPomodorotimerDialog() | 146 | dialog = gtk.AboutDialog() |
199 | 76 | response = prefs.run() | 147 | |
200 | 77 | if response == gtk.RESPONSE_OK: | 148 | # Add the application name to the dialog |
201 | 78 | #TODO: Save changes made to preferences | 149 | dialog.set_name('About Pomodoro Timer') |
202 | 79 | pass | 150 | |
203 | 80 | prefs.destroy() | 151 | # Set the application version |
204 | 81 | 152 | dialog.set_version('0.1') | |
205 | 82 | def quit(self, widget, data=None): | 153 | |
206 | 83 | """quit - signal handler for closing the PomodorotimerWindow""" | 154 | # Pass a list of authors. This is then connected to the 'Credits' |
207 | 84 | self.destroy() | 155 | # button. When clicked the buttons opens a new window showing |
208 | 156 | # each author on their own line. | ||
209 | 157 | dialog.set_authors(['Chris Sims', 'Steven Nance']) | ||
210 | 158 | |||
211 | 159 | # Add a short comment about the application, this appears below the application | ||
212 | 160 | # name in the dialog | ||
213 | 161 | dialog.set_comments('A simple pomodoro timer.') | ||
214 | 162 | |||
215 | 163 | # Add license information, this is connected to the 'License' button | ||
216 | 164 | # and is displayed in a new window. | ||
217 | 165 | dialog.set_license('Distributed under the GPL v3 license.\nhttp://www.gnu.org/licenses/gpl.html') | ||
218 | 166 | |||
219 | 167 | # Show the dialog | ||
220 | 168 | dialog.run() | ||
221 | 169 | |||
222 | 170 | # The destroy method must be called otherwise the 'Close' button will | ||
223 | 171 | # not work. | ||
224 | 172 | dialog.destroy() | ||
225 | 173 | |||
226 | 174 | # def preferences(self, widget, data=None): | ||
227 | 175 | # """preferences - display the preferences window for pomodorotimer """ | ||
228 | 176 | # prefs = PreferencesPomodorotimerDialog.NewPreferencesPomodorotimerDialog() | ||
229 | 177 | # response = prefs.run() | ||
230 | 178 | # if response == gtk.RESPONSE_OK: | ||
231 | 179 | # #TODO: Save changes made to preferences | ||
232 | 180 | # pass | ||
233 | 181 | # prefs.destroy() | ||
234 | 182 | |||
235 | 183 | # Make sure to default our seconds to zero | ||
236 | 184 | seconds = 0 | ||
237 | 185 | |||
238 | 186 | def pomodoro(self, widget, data=None): | ||
239 | 187 | if widget.get_active(): | ||
240 | 188 | self.minutes = 25 | ||
241 | 189 | self.timer.set_label("25:00") | ||
242 | 190 | |||
243 | 191 | def short_break(self, widget, data=None): | ||
244 | 192 | if widget.get_active(): | ||
245 | 193 | self.minutes = 5 | ||
246 | 194 | self.timer.set_label("5:00") | ||
247 | 195 | |||
248 | 196 | def long_break(self, widget, data=None): | ||
249 | 197 | if widget.get_active(): | ||
250 | 198 | self.minutes = 15 | ||
251 | 199 | self.timer.set_label("15:00") | ||
252 | 200 | |||
253 | 201 | def start(self, widget, data=None): | ||
254 | 202 | gobject.timeout_add(1000, self.countdown) | ||
255 | 203 | self.countdown() | ||
256 | 204 | |||
257 | 205 | def countdown(self): | ||
258 | 206 | if self.seconds>= 0: | ||
259 | 207 | m = "%02d" % self.minutes | ||
260 | 208 | s = "%02d" % self.seconds | ||
261 | 209 | self.timer.set_text(m + ':' + s) | ||
262 | 210 | self.seconds -= 1 | ||
263 | 211 | return True | ||
264 | 212 | elif self.minutes > 0: | ||
265 | 213 | self.minutes -= 1 | ||
266 | 214 | self.seconds = 59 | ||
267 | 215 | return True | ||
268 | 216 | else: | ||
269 | 217 | self.timer.set_text("All done!") | ||
270 | 218 | return False | ||
271 | 219 | |||
272 | 220 | def stop(self, widget, data=None): | ||
273 | 221 | self.minutes = 0 | ||
274 | 222 | self.seconds = 0 | ||
275 | 223 | self.countdown() | ||
276 | 85 | 224 | ||
277 | 86 | def on_destroy(self, widget, data=None): | 225 | def on_destroy(self, widget, data=None): |
279 | 87 | """on_destroy - called when the PomodorotimerWindow is close. """ | 226 | """on_destroy - called when the PomodorotimerWindow is closed, |
280 | 227 | or when "quit" is selected from the menu. """ | ||
281 | 88 | #TODO: Save any changes to historical data before closing | 228 | #TODO: Save any changes to historical data before closing |
282 | 89 | 229 | ||
283 | 90 | gtk.main_quit() | 230 | gtk.main_quit() |
301 | 91 | 231 | ||
302 | 92 | def NewPomodorotimerWindow(): | 232 | def main(self): |
303 | 93 | """NewPomodorotimerWindow - returns a fully instantiated | 233 | gtk.main() |
287 | 94 | PomodorotimerWindow object. Use this function rather than | ||
288 | 95 | creating a PomodorotimerWindow directly. | ||
289 | 96 | """ | ||
290 | 97 | |||
291 | 98 | #look for the ui file that describes the ui | ||
292 | 99 | ui_filename = os.path.join(getdatapath(), 'ui', 'PomodorotimerWindow.ui') | ||
293 | 100 | if not os.path.exists(ui_filename): | ||
294 | 101 | ui_filename = None | ||
295 | 102 | |||
296 | 103 | builder = gtk.Builder() | ||
297 | 104 | builder.add_from_file(ui_filename) | ||
298 | 105 | window = builder.get_object("pomodorotimer_window") | ||
299 | 106 | window.finish_initializing(builder) | ||
300 | 107 | return window | ||
304 | 108 | 234 | ||
305 | 109 | if __name__ == "__main__": | 235 | if __name__ == "__main__": |
306 | 110 | #support for command line options | ||
307 | 111 | import logging, optparse | ||
308 | 112 | parser = optparse.OptionParser(version="%prog %ver") | ||
309 | 113 | parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Show debug messages") | ||
310 | 114 | (options, args) = parser.parse_args() | ||
311 | 115 | |||
312 | 116 | #set the logging level to show debug messages | ||
313 | 117 | if options.verbose: | ||
314 | 118 | logging.basicConfig(level=logging.DEBUG) | ||
315 | 119 | logging.debug('logging enabled') | ||
316 | 120 | |||
317 | 121 | #run the application | 236 | #run the application |
321 | 122 | window = NewPomodorotimerWindow() | 237 | window = PomodorotimerWindow() |
322 | 123 | window.show() | 238 | window.main() |
320 | 124 | gtk.main() | ||
323 | 125 | 239 |
Approving myself...