Skip to content

Commit

Permalink
add a declarative Timer toolkit object
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Jul 29, 2013
1 parent 3e1b4db commit 1325925
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 5 deletions.
14 changes: 10 additions & 4 deletions enaml/qt/qt_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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,
Expand Down
85 changes: 85 additions & 0 deletions enaml/qt/qt_timer.py
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()
3 changes: 2 additions & 1 deletion enaml/widgets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
91 changes: 91 additions & 0 deletions enaml/widgets/timer.py
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

0 comments on commit 1325925

Please sign in to comment.