[gnome-clocks] Clock class changes
- From: Paolo Borelli <pborelli src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-clocks] Clock class changes
- Date: Sun, 6 Jan 2013 17:47:51 +0000 (UTC)
commit 3ebfe8346424b7d91e963179318efbf165e3e43c
Author: Volker Sobek <reklov live com>
Date: Sat Jan 5 19:07:45 2013 +0100
Clock class changes
Get rid of Clock.Mode and base the Clock class on Gtk.Notebook. Each
Clock has now at least one page and the toolbar buttons are set by
each Clock depending on its current page.
gnomeclocks/alarm.py | 80 ++++++++++++++++++++--------------------------
gnomeclocks/app.py | 1 +
gnomeclocks/clocks.py | 52 ++++++++++++++----------------
gnomeclocks/stopwatch.py | 11 +++++-
gnomeclocks/timer.py | 36 +++++++++-----------
gnomeclocks/utils.py | 2 +-
gnomeclocks/world.py | 72 ++++++++++++++++++-----------------------
7 files changed, 118 insertions(+), 136 deletions(-)
---
diff --git a/gnomeclocks/alarm.py b/gnomeclocks/alarm.py
index 8ec0785..a1967c6 100644
--- a/gnomeclocks/alarm.py
+++ b/gnomeclocks/alarm.py
@@ -447,6 +447,10 @@ class AlarmStandalone(Gtk.EventBox):
class Alarm(Clock):
+ class Page:
+ OVERVIEW = 0
+ STANDALONE = 1
+
def __init__(self, toolbar, embed):
Clock.__init__(self, _("Alarm"), toolbar, embed)
@@ -470,11 +474,6 @@ class Alarm(Clock):
self.delete_button = Gtk.Button(_("Delete"))
self.delete_button.connect('clicked', self._on_delete_clicked)
- self.notebook = Gtk.Notebook()
- self.notebook.set_show_tabs(False)
- self.notebook.set_show_border(False)
- self.add(self.notebook)
-
self.liststore = Gtk.ListStore(bool, str, object)
self.iconview = SelectableIconView(self.liststore, 0, 1, self._thumb_data_func)
self.iconview.connect("item-activated", self._on_item_activated)
@@ -483,14 +482,14 @@ class Alarm(Clock):
contentview = ContentView(self.iconview,
"alarm-symbolic",
_("Select <b>New</b> to add an alarm"))
- self.notebook.append_page(contentview, None)
+ self.standalone = AlarmStandalone(self)
+
+ self.insert_page(contentview, Alarm.Page.OVERVIEW)
+ self.insert_page(self.standalone, Alarm.Page.STANDALONE)
+ self.set_current_page(Alarm.Page.OVERVIEW)
self.storage = AlarmsStorage()
self.load_alarms()
- self.show_all()
-
- self.standalone = AlarmStandalone(self)
- self.notebook.append_page(self.standalone, None)
wallclock.connect("time-changed", self._tick_alarms)
@@ -498,13 +497,16 @@ class Alarm(Clock):
self.activate_new()
def _on_select_clicked(self, button):
- self.set_mode(Clock.Mode.SELECTION)
+ self.iconview.set_selection_mode(True)
+ self.update_toolbar()
def _on_done_clicked(self, button):
- self.set_mode(Clock.Mode.NORMAL)
+ self.iconview.set_selection_mode(False)
+ self.update_toolbar()
+ self._embed.hide_floatingbar()
def _on_back_clicked(self, button):
- self.embed.spotlight(lambda: self.set_mode(Clock.Mode.NORMAL))
+ self.change_page_spotlight(Alarm.Page.OVERVIEW)
def _on_edit_clicked(self, button):
self.standalone.open_edit_dialog()
@@ -524,23 +526,9 @@ class Alarm(Clock):
else:
cell.css_class = "inactive"
- def set_mode(self, mode):
- self.mode = mode
- if mode is Clock.Mode.NORMAL:
- if self.standalone.alarm and \
- self.standalone.alarm.state == AlarmItem.State.RINGING:
- self.standalone.alarm.stop()
- self.notebook.set_current_page(0)
- self.iconview.set_selection_mode(False)
- elif mode is Clock.Mode.SELECTION:
- self.iconview.set_selection_mode(True)
- elif mode is Clock.Mode.STANDALONE:
- self.notebook.set_current_page(1)
- self.update_toolbar()
-
@GObject.Signal
def alarm_ringing(self):
- self.set_mode(Clock.Mode.STANDALONE)
+ self.set_current_page(Alarm.Page.STANDALONE)
def _tick_alarms(self, *args):
for a in self.alarms:
@@ -557,16 +545,16 @@ class Alarm(Clock):
def _on_item_activated(self, iconview, path):
alarm = self.liststore[path][2]
self.standalone.set_alarm(alarm)
- self.embed.spotlight(lambda: self.set_mode(Clock.Mode.STANDALONE))
+ self.change_page_spotlight(Alarm.Page.STANDALONE)
def _on_selection_changed(self, iconview):
selection = iconview.get_selection()
n_selected = len(selection)
- self.toolbar.set_selection(n_selected)
+ self._toolbar.set_selection(n_selected)
if n_selected > 0:
- self.embed.show_floatingbar(self.delete_button)
+ self._embed.show_floatingbar(self.delete_button)
else:
- self.embed.hide_floatingbar()
+ self._embed.hide_floatingbar()
def load_alarms(self):
self.alarms = self.storage.load()
@@ -603,19 +591,21 @@ class Alarm(Clock):
self.save_alarms()
def update_toolbar(self):
- self.toolbar.clear()
- if self.mode is Clock.Mode.NORMAL:
- self.toolbar.set_mode(Toolbar.Mode.NORMAL)
- self.toolbar.add_widget(self.new_button)
- self.toolbar.add_widget(self.select_button, Gtk.PackType.END)
- elif self.mode is Clock.Mode.SELECTION:
- self.toolbar.set_mode(Toolbar.Mode.SELECTION)
- self.toolbar.add_widget(self.done_button, Gtk.PackType.END)
- elif self.mode is Clock.Mode.STANDALONE:
- self.toolbar.set_mode(Toolbar.Mode.STANDALONE)
- self.toolbar.add_widget(self.back_button)
- self.toolbar.add_widget(self.edit_button, Gtk.PackType.END)
- self.toolbar.set_title(GLib.markup_escape_text(self.standalone.alarm.name))
+ self._toolbar.clear()
+ if self.get_current_page() == Alarm.Page.OVERVIEW:
+ if self.iconview.selection_mode:
+ self._toolbar.set_mode(Toolbar.Mode.SELECTION)
+ self._toolbar.add_widget(self.done_button, Gtk.PackType.END)
+ else:
+ self._toolbar.set_mode(Toolbar.Mode.NORMAL)
+ self._toolbar.add_widget(self.new_button)
+ self._toolbar.add_widget(self.select_button, Gtk.PackType.END)
+ elif self.get_current_page() == Alarm.Page.STANDALONE:
+ self._toolbar.set_mode(Toolbar.Mode.STANDALONE)
+ self._toolbar.add_widget(self.back_button)
+ self._toolbar.add_widget(self.edit_button, Gtk.PackType.END)
+ self._toolbar.set_title(GLib.markup_escape_text(
+ self.standalone.alarm.name))
def activate_new(self):
window = AlarmDialog(self.get_toplevel())
diff --git a/gnomeclocks/app.py b/gnomeclocks/app.py
index a8356d8..a08c6d8 100644
--- a/gnomeclocks/app.py
+++ b/gnomeclocks/app.py
@@ -90,6 +90,7 @@ class Window(Gtk.ApplicationWindow):
def _on_alarm_ringing(self, view):
self.notebook.set_current_page(self.views.index(view))
+ view.update_toolbar()
def _on_page_changed(self, toolbar, page):
self.notebook.set_current_page(page)
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index 5ef70d5..f43dd61 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -19,47 +19,43 @@
from gi.repository import Gtk
-class Clock(Gtk.EventBox):
- class Mode:
- NORMAL = 0
- STANDALONE = 1
- SELECTION = 2
-
+class Clock(Gtk.Notebook):
def __init__(self, label, toolbar, embed):
- Gtk.EventBox.__init__(self)
-
- self.toolbar = toolbar
- self.embed = embed
+ Gtk.Notebook.__init__(self, show_tabs=False, show_border=False)
+ self.show()
+ self.label = label
+ self._embed = embed
+ self._toolbar = toolbar
self.connect('map', self._ui_thaw)
self.connect('unmap', self._ui_freeze)
- self.label = label
- self.mode = Clock.Mode.NORMAL
- self.get_style_context().add_class('view')
- self.get_style_context().add_class('content-view')
+ def insert_page(self, page, page_number):
+ page.show_all()
+ Gtk.Notebook.insert_page(self, page, None, page_number)
- def update_toolbar(self):
- self.toolbar.clear()
+ def change_page(self, page_number):
+ self.set_current_page(page_number)
+ self.update_toolbar()
- def activate_new(self):
- pass
+ def change_page_spotlight(self, page_number):
+ self._embed.spotlight(lambda: self.change_page(page_number))
+
+ def update_toolbar(self):
+ """Updates the toolbar depending on the current clock page."""
+ raise NotImplementedError
def _ui_freeze(self, widget):
- """
- Called when the clock widget is unmapped.
+ """Called when the Clock widget is unmapped.
Derived classes can implement this method to remove timeouts
- in order to save CPU time while the clock widget is not
- visible.
- """
+ in order to save CPU time while the Clock widget is not
+ visible."""
pass
def _ui_thaw(self, widget):
- """
- Called when the clock widget is mapped.
+ """Called when the clock widget is mapped.
- Derived clock classes can implement this method to re-add
- timeouts when the clock widget becomes visible again.
- """
+ Derived Clock classes can implement this method to re-add
+ timeouts when the Clock widget becomes visible again."""
pass
diff --git a/gnomeclocks/stopwatch.py b/gnomeclocks/stopwatch.py
index 6527c30..d08395a 100644
--- a/gnomeclocks/stopwatch.py
+++ b/gnomeclocks/stopwatch.py
@@ -19,6 +19,7 @@
import time
from gi.repository import GLib, Gtk
from clocks import Clock
+from widgets import Toolbar
class Stopwatch(Clock):
@@ -26,6 +27,9 @@ class Stopwatch(Clock):
LABEL_MARKUP_LONG = "<span font_desc=\"64.0\">%i:%02i:%04.1f</span>"
BUTTON_MARKUP = "<span font_desc=\"18.0\">%s</span>"
+ class Page:
+ STANDALONE = 0
+
class State:
RESET = 0
RUNNING = 1
@@ -46,7 +50,6 @@ class Stopwatch(Clock):
self.lap_time_diff = 0
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
- self.add(vbox)
grid = Gtk.Grid()
grid.set_margin_top(36)
@@ -111,7 +114,7 @@ class Stopwatch(Clock):
scroll.set_vexpand(True)
scroll.add(self.laps_view)
vbox.pack_start(scroll, True, True, 0)
-
+ self.insert_page(vbox, self.Page.STANDALONE)
self.show_all()
def _on_left_button_clicked(self, widget):
@@ -208,6 +211,10 @@ class Stopwatch(Clock):
self.set_time_label(h, m, s)
return True
+ def update_toolbar(self):
+ self._toolbar.clear()
+ self._toolbar.set_mode(Toolbar.Mode.NORMAL)
+
def _ui_freeze(self, widget):
if self.state == Stopwatch.State.RUNNING:
self._remove_timeout()
diff --git a/gnomeclocks/timer.py b/gnomeclocks/timer.py
index be732de..add002c 100644
--- a/gnomeclocks/timer.py
+++ b/gnomeclocks/timer.py
@@ -21,7 +21,7 @@ import math
from gi.repository import GLib, GObject, Gtk
from clocks import Clock
from utils import Alert
-from widgets import Spinner
+from widgets import Spinner, Toolbar
class TimerScreen(Gtk.Grid):
@@ -152,6 +152,10 @@ class Timer(Clock):
RUNNING = 1
PAUSED = 2
+ class Page:
+ SETUP = 0
+ TIMER = 1
+
def __init__(self, toolbar, embed):
Clock.__init__(self, _("Timer"), toolbar, embed)
self.state = Timer.State.STOPPED
@@ -159,21 +163,16 @@ class Timer(Clock):
self._last_set_time = None
self._ui_is_frozen = False
- self.notebook = Gtk.Notebook()
- self.notebook.set_show_tabs(False)
- self.notebook.set_show_border(False)
- self.add(self.notebook)
-
# force the time label and the spinner to the same size
size_group = Gtk.SizeGroup(Gtk.SizeGroupMode.VERTICAL)
self.setup_screen = TimerSetupScreen(self, size_group)
- self.notebook.append_page(self.setup_screen, None)
+ self.insert_page(self.setup_screen, self.Page.SETUP)
self.timer_screen = TimerScreen(self, size_group)
- self.notebook.append_page(self.timer_screen, None)
+ self.insert_page(self.timer_screen, self.Page.TIMER)
- self.show_all()
+ self.set_current_page(self.Page.SETUP)
self.alert = Alert("complete",
_("Time is up!"),
@@ -183,14 +182,6 @@ class Timer(Clock):
def alarm_ringing(self):
self.alert.show()
- def show_setup_screen(self, reset):
- self.notebook.set_current_page(0)
- if reset:
- self.setup_screen.set_values(0, 0, 0)
-
- def show_timer_screen(self):
- self.notebook.set_current_page(1)
-
def _add_timeout(self):
if self.timeout_id == 0:
self.timeout_id = GLib.timeout_add(250, self.count)
@@ -208,12 +199,13 @@ class Timer(Clock):
self.deadline = time.time() + self.duration
self.state = Timer.State.RUNNING
self._add_timeout()
- self.show_timer_screen()
+ self.change_page(self.Page.TIMER)
def reset(self):
self.state = Timer.State.STOPPED
self._remove_timeout()
- self.show_setup_screen(True)
+ self.setup_screen.set_values(0, 0, 0)
+ self.change_page(Timer.Page.SETUP)
def pause(self):
self.duration = self.deadline - time.time()
@@ -232,7 +224,7 @@ class Timer(Clock):
self.state = Timer.State.STOPPED
self._remove_timeout()
self.timer_screen.set_time(0, 0, 0)
- self.show_setup_screen(False)
+ self.change_page(self.Page.SETUP)
return False
elif not self._ui_is_frozen:
# math.ceil() is needed because we count backwards. It assures the
@@ -246,6 +238,10 @@ class Timer(Clock):
self._last_set_time = (h, m, s)
return True
+ def update_toolbar(self):
+ self._toolbar.clear()
+ self._toolbar.set_mode(Toolbar.Mode.NORMAL)
+
def _ui_freeze(self, widget):
self._ui_is_frozen = True
diff --git a/gnomeclocks/utils.py b/gnomeclocks/utils.py
index 267aed6..41cce68 100644
--- a/gnomeclocks/utils.py
+++ b/gnomeclocks/utils.py
@@ -176,7 +176,7 @@ class Alert:
self.notification = None
if Notify.is_initted() or Notify.init("GNOME Clocks"):
self.notification = Notify.Notification.new(title, msg, "gnome-clocks")
- self.notification.set_hint_string("desktop-entry", "gnome-clocks");
+ self.notification.set_hint_string("desktop-entry", "gnome-clocks")
else:
print "Error: Could not trigger Alert"
diff --git a/gnomeclocks/world.py b/gnomeclocks/world.py
index 2579bcf..11dfbca 100644
--- a/gnomeclocks/world.py
+++ b/gnomeclocks/world.py
@@ -290,6 +290,10 @@ class WorldStandalone(Gtk.EventBox):
class World(Clock):
+ class Page:
+ OVERVIEW = 0
+ STANDALONE = 1
+
def __init__(self, toolbar, embed):
Clock.__init__(self, _("World"), toolbar, embed)
@@ -310,11 +314,6 @@ class World(Clock):
self.delete_button = Gtk.Button(_("Delete"))
self.delete_button.connect('clicked', self._on_delete_clicked)
- self.notebook = Gtk.Notebook()
- self.notebook.set_show_tabs(False)
- self.notebook.set_show_border(False)
- self.add(self.notebook)
-
f = os.path.join(Dirs.get_images_dir(), "day.png")
self.daypixbuf = GdkPixbuf.Pixbuf.new_from_file(f)
f = os.path.join(Dirs.get_images_dir(), "night.png")
@@ -328,14 +327,14 @@ class World(Clock):
contentview = ContentView(self.iconview,
"document-open-recent-symbolic",
_("Select <b>New</b> to add a world clock"))
- self.notebook.append_page(contentview, None)
+ self.standalone = WorldStandalone()
+
+ self.insert_page(contentview, World.Page.OVERVIEW)
+ self.insert_page(self.standalone, World.Page.STANDALONE)
+ self.set_current_page(World.Page.OVERVIEW)
self.storage = WorldClockStorage()
self.load_clocks()
- self.show_all()
-
- self.standalone = WorldStandalone()
- self.notebook.append_page(self.standalone, None)
wallclock.connect("time-changed", self._tick_clocks)
@@ -343,13 +342,16 @@ class World(Clock):
self.activate_new()
def _on_select_clicked(self, button):
- self.set_mode(Clock.Mode.SELECTION)
+ self.iconview.set_selection_mode(True)
+ self.update_toolbar()
def _on_done_clicked(self, button):
- self.set_mode(Clock.Mode.NORMAL)
+ self.iconview.set_selection_mode(False)
+ self.update_toolbar()
+ self._embed.hide_floatingbar()
def _on_back_clicked(self, button):
- self.embed.spotlight(lambda: self.set_mode(Clock.Mode.NORMAL))
+ self.change_page_spotlight(World.Page.OVERVIEW)
def _on_delete_clicked(self, button):
selection = self.iconview.get_selection()
@@ -368,17 +370,6 @@ class World(Clock):
cell.props.pixbuf = self.nightpixbuf
cell.css_class = "dark"
- def set_mode(self, mode):
- self.mode = mode
- if mode is Clock.Mode.NORMAL:
- self.notebook.set_current_page(0)
- self.iconview.set_selection_mode(False)
- elif mode is Clock.Mode.STANDALONE:
- self.notebook.set_current_page(1)
- elif mode is Clock.Mode.SELECTION:
- self.iconview.set_selection_mode(True)
- self.update_toolbar()
-
def _tick_clocks(self, *args):
for c in self.clocks:
c.tick()
@@ -389,16 +380,16 @@ class World(Clock):
def _on_item_activated(self, iconview, path):
clock = self.liststore[path][2]
self.standalone.set_clock(clock)
- self.embed.spotlight(lambda: self.set_mode(Clock.Mode.STANDALONE))
+ self.change_page_spotlight(World.Page.STANDALONE)
def _on_selection_changed(self, iconview):
selection = iconview.get_selection()
n_selected = len(selection)
- self.toolbar.set_selection(n_selected)
+ self._toolbar.set_selection(n_selected)
if n_selected > 0:
- self.embed.show_floatingbar(self.delete_button)
+ self._embed.show_floatingbar(self.delete_button)
else:
- self.embed.hide_floatingbar()
+ self._embed.hide_floatingbar()
def load_clocks(self):
self.clocks = self.storage.load()
@@ -428,18 +419,19 @@ class World(Clock):
self.load_clocks()
def update_toolbar(self):
- self.toolbar.clear()
- if self.mode is Clock.Mode.NORMAL:
- self.toolbar.set_mode(Toolbar.Mode.NORMAL)
- self.toolbar.add_widget(self.new_button)
- self.toolbar.add_widget(self.select_button, Gtk.PackType.END)
- elif self.mode is Clock.Mode.SELECTION:
- self.toolbar.set_mode(Toolbar.Mode.SELECTION)
- self.toolbar.add_widget(self.done_button, Gtk.PackType.END)
- elif self.mode is Clock.Mode.STANDALONE:
- self.toolbar.set_mode(Toolbar.Mode.STANDALONE)
- self.toolbar.add_widget(self.back_button)
- self.toolbar.set_title(GLib.markup_escape_text(self.standalone.clock.name))
+ self._toolbar.clear()
+ if self.get_current_page() == World.Page.OVERVIEW:
+ if self.iconview.selection_mode:
+ self._toolbar.set_mode(Toolbar.Mode.SELECTION)
+ self._toolbar.add_widget(self.done_button, Gtk.PackType.END)
+ else:
+ self._toolbar.set_mode(Toolbar.Mode.NORMAL)
+ self._toolbar.add_widget(self.new_button)
+ self._toolbar.add_widget(self.select_button, Gtk.PackType.END)
+ elif self.get_current_page() == World.Page.STANDALONE:
+ self._toolbar.set_mode(Toolbar.Mode.STANDALONE)
+ self._toolbar.add_widget(self.back_button)
+ self._toolbar.set_title(GLib.markup_escape_text(self.standalone.clock.name))
def activate_new(self):
window = NewWorldClockDialog(self.get_toplevel())
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]