diff --git a/enaml/qt/qt_factories.py b/enaml/qt/qt_factories.py index 6fa22e0ef..0f1f98cb2 100644 --- a/enaml/qt/qt_factories.py +++ b/enaml/qt/qt_factories.py @@ -250,14 +250,19 @@ def status_item_factory(): return QtStatusItem +def text_editor_factory(): + from .qt_text_editor import QtTextEditor + return QtTextEditor + + def time_selector_factory(): from .qt_time_selector import QtTimeSelector return QtTimeSelector -def text_editor_factory(): - from .qt_text_editor import QtTextEditor - return QtTextEditor +def timer_factory(): + from .qt_timer import QtTimer + return QtTimer def tool_bar_factory(): @@ -324,8 +329,9 @@ def window_factory(): 'StackItem': stack_item_factory, 'StatusBar': status_bar_factory, 'StatusItem': status_item_factory, - 'TimeSelector': time_selector_factory, 'TextEditor': text_editor_factory, + 'TimeSelector': time_selector_factory, + 'Timer': timer_factory, 'ToolBar': tool_bar_factory, 'WebView': web_view_factory, 'Window': window_factory, diff --git a/enaml/qt/qt_timer.py b/enaml/qt/qt_timer.py new file mode 100644 index 000000000..bd9b86b17 --- /dev/null +++ b/enaml/qt/qt_timer.py @@ -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() diff --git a/enaml/widgets/api.py b/enaml/widgets/api.py index 1db877583..71ce73844 100644 --- a/enaml/widgets/api.py +++ b/enaml/widgets/api.py @@ -53,8 +53,9 @@ from .stack_item import StackItem from .status_bar import StatusBar from .status_item import StatusItem -from .time_selector import TimeSelector from .text_editor import TextEditor, TextDocument +from .time_selector import TimeSelector +from .timer import Timer from .tool_bar import ToolBar from .web_view import WebView from .window import Window diff --git a/enaml/widgets/timer.py b/enaml/widgets/timer.py new file mode 100644 index 000000000..65a96b853 --- /dev/null +++ b/enaml/widgets/timer.py @@ -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