-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a declarative Timer toolkit object
- Loading branch information
Showing
4 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2013, Nucleic Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#------------------------------------------------------------------------------ | ||
from atom.api import Typed | ||
|
||
from enaml.widgets.timer import ProxyTimer | ||
|
||
from .QtCore import QTimer | ||
|
||
from .qt_toolkit_object import QtToolkitObject | ||
|
||
|
||
class QtTimer(QtToolkitObject, ProxyTimer): | ||
""" A Qt implementation of an Enaml ProxyTimer. | ||
""" | ||
#: A reference to the widget created by the proxy. | ||
widget = Typed(QTimer) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Initialization | ||
#-------------------------------------------------------------------------- | ||
def create_widget(self): | ||
""" Create the calender widget. | ||
""" | ||
self.widget = QTimer() | ||
|
||
def init_widget(self): | ||
""" Initialize the widget. | ||
""" | ||
super(QtTimer, self).init_widget() | ||
d = self.declaration | ||
self.set_interval(d.interval) | ||
self.set_single_shot(d.single_shot) | ||
self.widget.timeout.connect(self.on_timeout) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Signal Handlers | ||
#-------------------------------------------------------------------------- | ||
def on_timeout(self): | ||
""" Handle the timeout signal for the timer. | ||
""" | ||
d = self.declaration | ||
if d is not None: | ||
d.timeout() | ||
|
||
#-------------------------------------------------------------------------- | ||
# ProxyTimer API | ||
#-------------------------------------------------------------------------- | ||
def set_interval(self, interval): | ||
""" Set the interval on the timer. | ||
""" | ||
self.widget.setInterval(interval) | ||
|
||
def set_single_shot(self, single_shot): | ||
""" Set the single shot flag on the timer. | ||
""" | ||
self.widget.setSingleShot(single_shot) | ||
|
||
def start(self): | ||
""" Start or restart the timer. | ||
""" | ||
self.widget.start() | ||
|
||
def stop(self): | ||
""" Stop the timer. | ||
""" | ||
self.widget.stop() | ||
|
||
def is_active(self): | ||
""" Get whether or not the timer is running. | ||
""" | ||
return self.widget.isActive() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#------------------------------------------------------------------------------ | ||
# Copyright (c) 2013, Nucleic Development Team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file COPYING.txt, distributed with this software. | ||
#------------------------------------------------------------------------------ | ||
from atom.api import Bool, Event, Int, ForwardTyped, Typed, observe | ||
|
||
from enaml.core.declarative import d_ | ||
from enaml.widgets.toolkit_object import ToolkitObject, ProxyToolkitObject | ||
|
||
|
||
class ProxyTimer(ProxyToolkitObject): | ||
""" The abstract definition of a proxy Timer object. | ||
""" | ||
#: A reference to the Timer declaration. | ||
declaration = ForwardTyped(lambda: Timer) | ||
|
||
def set_interval(self, interval): | ||
raise NotImplementedError | ||
|
||
def set_single_shot(self, single_shot): | ||
raise NotImplementedError | ||
|
||
def start(self): | ||
raise NotImplementedError | ||
|
||
def stop(self): | ||
raise NotImplementedError | ||
|
||
def is_active(self): | ||
raise NotImplementedError | ||
|
||
|
||
class Timer(ToolkitObject): | ||
""" An object which represents a toolkit independent timer. | ||
""" | ||
#: The interval of the timer, in milliseconds. The default is 0 and | ||
#: indicates that the timer will fire as soon as the event queue is | ||
#: emptied of all pending events. | ||
interval = d_(Int(0)) | ||
|
||
#: Whether the timer fires only once, or repeatedly until stopped. | ||
single_shot = d_(Bool(False)) | ||
|
||
#: An event fired when the timer times out. | ||
timeout = d_(Event(), writable=False) | ||
|
||
#: A reference to the ProxyTimer object. | ||
proxy = Typed(ProxyTimer) | ||
|
||
#-------------------------------------------------------------------------- | ||
# Observers | ||
#-------------------------------------------------------------------------- | ||
@observe(('single_shot', 'interval')) | ||
def _update_proxy(self, change): | ||
""" An observer which updates the proxy when the state changes. | ||
""" | ||
# The superclass implementation is sufficient. | ||
super(Timer, self)._update_proxy(change) | ||
|
||
def start(self): | ||
""" Start or restart the timer. | ||
If the timer is already started, it will be stopped and | ||
restarted. | ||
""" | ||
if self.proxy_is_active: | ||
self.proxy.start() | ||
|
||
def stop(self): | ||
""" Stop the timer. | ||
If the timer is already stopped, this is a no-op. | ||
""" | ||
if self.proxy_is_active: | ||
self.proxy.stop() | ||
|
||
def is_active(self): | ||
""" Returns True if the timer is running, False otherwise. | ||
""" | ||
if self.proxy_is_active: | ||
return self.proxy.is_active() | ||
return False |