Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGary Martin <gary@garycmartin.com>2013-05-21 22:41:01 (GMT)
committer Gary Martin <gary@garycmartin.com>2013-05-21 22:41:01 (GMT)
commitd9de13ab65fcfe9139451fbaf99ecf104ab12df8 (patch)
tree1bb54cff10d66e8c47733cd34e4c83cbbb584b0a
parentc7ba6b588396a887dc9bf5f38e3fa1ddcfd25106 (diff)
Allow dragging of hour and minute hands, simulating mechanics of a real clock face.
Simulating a real clock face rather than allowing the hour hand and minute hand to be individually manipulated avoids the clock giving incorrect feedback for invalid times, and avoids rounding issues on hand positions when trying to determine some otherwise valid times.
-rwxr-xr-xclock.py42
1 files changed, 33 insertions, 9 deletions
diff --git a/clock.py b/clock.py
index d3619ed..17bad4d 100755
--- a/clock.py
+++ b/clock.py
@@ -949,7 +949,7 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
# update the time and force a redraw of the clock
self._time = datetime.now()
- self._hand_angles['hour'] = (math.pi / 6 * self._time.hour +
+ self._hand_angles['hour'] = (math.pi / 6 * (self._time.hour % 12) +
math.pi / 360 * self._time.minute)
self._hand_angles['minutes'] = math.pi / 30 * self._time.minute
@@ -976,13 +976,11 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
return self._active and not self.grab_hands_mode
def _get_time_from_hands_angles(self):
- hour = int((self._hand_angles['hour'] * 12) / (math.pi * 2))
+ hour = int(round((self._hand_angles['hour'] * 12) / (math.pi * 2))) % 12
if self._am_pm == 'PM':
hour += 12
- print "hour", hour
-
- minute = int((self._hand_angles['minutes'] * 60) / (math.pi * 2))
+ minute = int(round((self._hand_angles['minutes'] * 60) / (math.pi * 2)))
# Second is not used by speech or to display time in full
# letters, so we avoid that calculation
second = 0
@@ -1023,6 +1021,12 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
active = property(_get_active, _set_active)
+ def toggle_am_pm(self):
+ if self._am_pm == 'AM':
+ self._am_pm = 'PM'
+ else:
+ self._am_pm = 'AM'
+
def change_grab_hands_mode(self, toggle_grab):
"""Connect or disconnect the callbacks for to grab the hands
of the clock.
@@ -1101,10 +1105,7 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
mouse_y > self._center_y + self._radius / 3 - self.am_pm_height and \
mouse_y < self._center_y + self._radius / 3 + self.am_pm_height:
- if self._am_pm == 'AM':
- self._am_pm = 'PM'
- else:
- self._am_pm = 'AM'
+ self.toggle_am_pm()
self.emit("time_minute")
self.queue_draw()
@@ -1135,6 +1136,29 @@ font_desc="Sans Bold 40">%d</span></markup>') % (i + 1)
if pointer_angle < 0:
pointer_angle += math.pi * 2
+ # Auto spin hour hand and snap minute hand when minutes dragged
+ if self._hand_being_grabbed is 'minutes':
+ pointer_angle = int((pointer_angle * 60) / (math.pi * 2)) * (math.pi * 2) / 60.0
+ self._hand_angles['hour'] += (pointer_angle - self._hand_angles['minutes']) / 12.0
+ if pointer_angle - self._hand_angles['minutes'] > math.pi:
+ self._hand_angles['hour'] -= math.pi * 2 / 12.0
+ elif pointer_angle - self._hand_angles['minutes'] < -math.pi:
+ self._hand_angles['hour'] += math.pi * 2 / 12.0
+ # Toggle AM/PM as needed
+ if self._hand_angles['hour'] >= math.pi * 2:
+ self._hand_angles['hour'] -= math.pi * 2
+ self.toggle_am_pm()
+ elif self._hand_angles['hour'] < 0:
+ self._hand_angles['hour'] += math.pi * 2
+ self.toggle_am_pm()
+
+ # Auto spin and snap minute hand when hour hand dragged
+ if self._hand_being_grabbed is 'hour':
+ tmp = self._hand_angles['hour'] * 12.0
+ while tmp >= math.pi * 2:
+ tmp -= math.pi * 2
+ self._hand_angles['minutes'] = int((tmp * 60) / (math.pi * 2)) * (math.pi * 2) / 60.0
+
# Update the angle of the hand being grabbed
self._hand_angles[self._hand_being_grabbed] = pointer_angle