[clocks] timer.py added



commit 59a8011df05848312784c0029c47ea747651be8f
Author: Eslam Mostafa <cseslam gmail com>
Date:   Thu Jun 7 23:09:36 2012 +0200

    timer.py added
    
    Signed-off-by: Seif Lotfy <seif lotfy collabora co uk>

 clocks.py     |   78 ++++++++++++++++++++-
 gtk-style.css |    1 +
 timer.py      |  216 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 291 insertions(+), 4 deletions(-)
---
diff --git a/clocks.py b/clocks.py
index 102930e..81b3001 100644
--- a/clocks.py
+++ b/clocks.py
@@ -26,11 +26,17 @@ from storage import worldclockstorage
 
 from datetime import datetime, timedelta
 from pytz import timezone
+from timer import TimerWelcomeScreen, TimerScreen, Spinner
 import pytz, time, os
 
+
 STOPWATCH_LABEL_MARKUP = "<span font_desc=\"64.0\">%02i:%04.1f</span>"
 STOPWATCH_BUTTON_MARKUP = "<span font_desc=\"24.0\">%s</span>"
 
+TIMER_LABEL_MARKUP = "<span font_desc=\"64.0\">%02i:%02i</span>"
+TIMER = "<span font_desc=\"64.0\">%02i</span>"
+TIMER_BUTTON_MARKUP = "<span font_desc=\"24.0\">%s</span>"
+
 GFILE = Gio.File.new_for_uri ('widgets.css')
 CSS_PROVIDER = Gtk.CssProvider()
 #CSS_PROVIDER.load_from_file(GFILE)
@@ -268,10 +274,74 @@ class Stopwatch (Clock):
             elapsed_seconds))
         
         return True
+        
 
 
 class Timer (Clock):
-    def __init__ (self):
-        Clock.__init__ (self, "Timer")
-        self.button.set_sensitive (False)
-
+	
+	#State
+	#Zero: 0
+	#Running: 1
+	#Paused: 2
+	
+	def __init__ (self):
+		Clock.__init__ (self, "Timer")
+		self.state = 0
+		self.g_id = 0
+		#
+		self.vbox = Gtk.Box (orientation = Gtk.Orientation.VERTICAL)
+		box = Gtk.Box ()
+		self.add (box)
+		box.pack_start (Gtk.Box(), True, True, 0)
+		box.pack_start (self.vbox, False, False, 0)
+		box.pack_end (Gtk.Box(), True, True, 0)
+		self.timer_welcome_screen = TimerWelcomeScreen(self)
+		self.timer_screen = TimerScreen(self)
+		self.show_timer_welcome_screen()
+		
+	def show_timer_welcome_screen(self):
+		self.vbox.pack_start(self.timer_welcome_screen, True, True, 0)
+		
+	def start_timer_screen(self):
+		self.vbox.remove(self.timer_welcome_screen)
+		self.vbox.pack_start(self.timer_screen, True, True, 0)
+		self.vbox.show_all()
+	
+	def end_timer_screen(self):
+		self.timer_screen.rightButton.get_style_context ().add_class ("clocks-lap")
+		self.timer_screen.rightLabel.set_markup (TIMER_BUTTON_MARKUP%("Pause"))
+		self.vbox.remove(self.timer_screen)
+		self.show_timer_welcome_screen()
+			
+	def start(self):
+		if self.g_id == 0: 
+			hours = self.timer_welcome_screen.hours.get_value()
+			minutes = self.timer_welcome_screen.minutes.get_value()
+			self.time = (hours * 60 * 60) + (minutes * 60) 
+			self.state = 1
+			self.g_id = GObject.timeout_add(1000, self.count)
+		
+	def cancel(self):
+		self.state = 0
+		self.end_timer_screen()
+		if self.g_id != 0:
+			GObject.source_remove(self.g_id)
+			self.g_id = 0
+		
+	def pause(self):
+		GObject.source_remove(self.g_id)
+		self.g_id = 0
+		
+	def cont(self):
+		self.g_id = GObject.timeout_add(1000, self.count)
+	
+	def count(self):
+		self.time -= 1
+		minutes, sec = divmod(self.time, 60)
+		hours, minutes = divmod(minutes, 60)
+
+		self.timer_screen.timerLabel.set_markup (TIMER_LABEL_MARKUP%(hours, minutes))
+		if hours == 00 and minutes == 00 and sec == 00:
+			return False
+		else:
+			return True
diff --git a/gtk-style.css b/gtk-style.css
index 33436a3..dddbe63 100644
--- a/gtk-style.css
+++ b/gtk-style.css
@@ -53,3 +53,4 @@
     border-image: none;
     border-width: 0;
 }
+
diff --git a/timer.py b/timer.py
new file mode 100644
index 0000000..d37b24c
--- /dev/null
+++ b/timer.py
@@ -0,0 +1,216 @@
+"""
+ Copyright (c) 2011-2012 Collabora, Ltd.
+
+ Gnome Clocks 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.
+
+ Gnome Clocks 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 Gnome Documents; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ 
+ Author: Eslam Mostafa <cseslam gmail com>
+"""
+
+
+from gi.repository import Gtk, Gio
+
+TIMER = "<span font_desc=\"64.0\">%02i</span>"
+TIMER_LABEL_MARKUP = "<span font_desc=\"64.0\">%02i:%02i</span>"
+TIMER = "<span font_desc=\"64.0\">%02i</span>"
+TIMER_BUTTON_MARKUP = "<span font_desc=\"24.0\">%s</span>"
+
+class Spinner(Gtk.Box):
+	def __init__(self, value_type, timer_welcome_screen):
+		super(Spinner, self).__init__()
+		self.vType = value_type
+		self.timer_welcome_screen = timer_welcome_screen
+		self.set_orientation(Gtk.Orientation.VERTICAL)
+		iconUp = Gio.ThemedIcon.new_with_default_fallbacks ("go-up-symbolic")
+		iconDown = Gio.ThemedIcon.new_with_default_fallbacks ("go-down-symbolic")
+		imageUp = Gtk.Image.new_from_gicon(iconUp, Gtk.IconSize.BUTTON)
+		imageDown = Gtk.Image.new_from_gicon(iconDown, Gtk.IconSize.BUTTON)
+		#Up Button
+		self.up = Gtk.Button()
+		self.up.set_image(imageUp)
+		self.up.set_relief(Gtk.ReliefStyle.NONE)
+		#Value
+		self.value = Gtk.Label('')
+		self.value.set_markup(TIMER%(0))
+		#Down Button
+		self.down = Gtk.Button()
+		self.down.set_image(imageDown)
+		self.down.set_relief(Gtk.ReliefStyle.NONE)
+		#
+		self.pack_start(self.up, False, False, 5)
+		self.pack_start(self.value, False, False, 5)
+		self.pack_start(self.down, False, False, 5)
+		#Signals
+		self.up.connect('clicked', self._increase)
+		self.down.connect('clicked', self._decrease)
+		
+	def get_value(self):
+		return int(self.value.get_text())
+		
+	def set_value(self, newValue):
+		self.value.set_markup(TIMER%(newValue))
+		
+	def _increase(self, widget):
+		value = self.get_value()
+		if self.vType == 'hours':
+			if value == 23:
+				value = 0
+			else:
+				value += 1
+			self.set_value(value)
+		elif self.vType == 'minutes':
+			if value == 59:
+				value = 0
+			else:
+				value += 1
+			self.set_value(value)
+		self.timer_welcome_screen.update_start_button_status()
+			
+	def _decrease(self, widget):
+		value = self.get_value()
+		if self.vType == 'hours':
+			if value == 0:
+				value = 23
+			else:
+				value -= 1
+			self.set_value(value)
+		elif self.vType == 'minutes':
+			if value == 0:
+				value = 59
+			else:
+				value -= 1
+			self.set_value(value)	
+		self.timer_welcome_screen.update_start_button_status()
+			
+class TimerScreen (Gtk.Box):
+	def __init__(self, timer):
+		super(TimerScreen, self).__init__()
+		self.set_orientation(Gtk.Orientation.VERTICAL)
+		self.timer = timer
+		top_spacer = Gtk.Label("")
+		
+		contents = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
+		self.timerLabel = Gtk.Label ()
+		self.timerLabel.set_alignment (0.5, 0.5)
+		self.timerLabel.set_markup (TIMER_LABEL_MARKUP%(0,0))
+		contents.pack_start (self.timerLabel, False, False, 0)
+
+		hbox = Gtk.Box()		
+		self.leftButton = Gtk.Button ()
+		self.leftButton.set_size_request(200, -1)
+		self.leftLabel = Gtk.Label ()
+		self.leftButton.add (self.leftLabel)
+		self.rightButton = Gtk.Button ()
+		self.rightButton.set_size_request(200, -1)
+		self.rightLabel = Gtk.Label ()
+		self.rightButton.add (self.rightLabel)
+
+		self.leftButton.get_style_context ().add_class ("clocks-stop")
+		self.rightButton.get_style_context ().add_class ("clocks-lap")
+
+		hbox.pack_start (self.leftButton, True, True, 0)
+		hbox.pack_start (Gtk.Box(), True, True, 24)
+		hbox.pack_start (self.rightButton, True, True, 0)
+
+		box = Gtk.Box ()
+		box.pack_start (Gtk.Box (), True, True, 0)
+		box.pack_start (hbox, True, True, 0)
+		box.pack_start (Gtk.Box (), True, True, 0)
+
+		contents.pack_start (box, False, False, 32)
+		contents.pack_start (Gtk.Box(), True, True, 0)
+		contents.pack_start (Gtk.Box(), True, True, 0)
+
+		self.leftLabel.set_markup (TIMER_BUTTON_MARKUP%("Cancel"))
+		self.leftLabel.set_padding (6, 0)
+		self.rightLabel.set_markup (TIMER_BUTTON_MARKUP%("Pause"))
+		self.rightLabel.set_padding (6, 0)
+		
+		bottom_spacer = Gtk.Label("")
+		
+		self.leftButton.connect('clicked', self._on_left_button_clicked)
+		self.rightButton.connect('clicked', self._on_right_button_clicked)
+		
+		self.pack_start(top_spacer, True, True, 0)
+		self.pack_start(contents, False, False, 0)		
+		self.pack_start(bottom_spacer, True, True, 0)
+	
+		
+	def _on_left_button_clicked(self, data):
+		self.timer.cancel()
+		
+	def _on_right_button_clicked(self, widget):
+		if self.timer.state == 1: #Pause
+			self.timer.state = 2
+			self.timer.pause()
+			self.rightButton.get_style_context ().remove_class ("clocks-lap")
+			self.rightButton.get_style_context ().add_class ("clocks-start")
+			self.rightLabel.set_markup(TIMER_BUTTON_MARKUP%("Continue"))
+			
+		elif self.timer.state == 2: #Continue
+			self.timer.state = 1
+			self.timer.cont()
+			self.rightButton.get_style_context ().remove_class ("clocks-start")
+			self.rightButton.get_style_context ().add_class ("clocks-lap")
+			self.rightLabel.set_markup(TIMER_BUTTON_MARKUP%("Pause"))	
+			
+class TimerWelcomeScreen (Gtk.Box):
+	def __init__ (self, timer):
+		super(TimerWelcomeScreen, self).__init__ ()
+		self.timer = timer
+		self.set_orientation(Gtk.Orientation.VERTICAL)
+		
+		top_spacer = Gtk.Label('')
+		spinner = Gtk.Box () #Containes 3 columns to set the time
+		
+		self.hours = Spinner('hours', self)
+		self.minutes = Spinner('minutes', self)
+		colon = Gtk.Label('')
+		colon.set_markup('<span font_desc=\"64.0\">:</span>')
+		
+		spinner.pack_start(self.hours, False, True, 20)
+		spinner.pack_start(colon, False, True, 20)
+		spinner.pack_start(self.minutes, False, True, 20)
+			
+		#Start Button
+		self.startButton = Gtk.Button()
+		self.startButton.set_sensitive(False)
+		self.startButton.set_size_request(200, -1)
+		self.startButton.get_style_context ().add_class ("clocks-start")
+		self.startLabel = Gtk.Label()
+		self.startLabel.set_markup (TIMER_BUTTON_MARKUP%("Start"))
+		self.startLabel.set_padding (6, 0)
+		self.startButton.add(self.startLabel)
+		self.startButton.connect('clicked', self._on_start_clicked)
+		#
+		bottom_spacer = Gtk.Label("")
+		#
+		self.pack_start(top_spacer, True, True, 0)
+		self.pack_start(spinner, False, True, 5)
+		self.pack_start(self.startButton, False, True, 5)
+		self.pack_start(bottom_spacer, True, True, 0)
+		
+	def update_start_button_status(self):
+		hours = self.hours.get_value()
+		minutes = self.minutes.get_value()
+		if hours == 0 and minutes == 0:
+			self.startButton.set_sensitive(False)
+		else:
+			self.startButton.set_sensitive(True)
+		
+
+	def _on_start_clicked(self, data):
+		if self.timer.state == 0:
+			self.timer.start_timer_screen()
+			self.timer.start()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]