Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #261 from enthought/feature-window-flags
Browse files Browse the repository at this point in the history
Add send_to_[front|back] and sticky flag to window
  • Loading branch information
sccolbert committed Feb 14, 2013
2 parents 62bbb67 + ae0cd6d commit d6f6181
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 deletions.
47 changes: 47 additions & 0 deletions enaml/qt/qt_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def create(self, tree):
self.set_title(tree['title'])
self.set_initial_size(tree['initial_size'])
self.set_modality(tree['modality'])
self.set_always_on_top(tree['always_on_top'])
self._icon_source = tree['icon_source']
self.widget().closed.connect(self.on_closed)

Expand Down Expand Up @@ -300,6 +301,18 @@ def on_action_restore(self, content):
"""
self.restore()

def on_action_send_to_front(self, content):
""" Handle the 'send_to_front' action from the Enaml widget.
"""
self.send_to_front()

def on_action_send_to_back(self, content):
""" Handle the 'send_to_back' action from the Enaml widget.
"""
self.send_to_back()

def on_action_set_icon_source(self, content):
""" Handle the 'set_icon_source' action from the Enaml widget.
Expand All @@ -318,6 +331,12 @@ def on_action_set_modality(self, content):
"""
self.set_modality(content['modality'])

def on_action_set_always_on_top(self, content):
""" Handle the 'set_always_on_top' action from the Enaml widget.
"""
self.set_always_on_top(content['always_on_top'])

#--------------------------------------------------------------------------
# Widget Update Methods
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -345,6 +364,18 @@ def restore(self):
"""
self.widget().showNormal()

def send_to_front(self):
""" Move the window to the front of all other windows.
"""
self.widget().raise_()

def send_to_back(self):
""" Move the window to the back of all other windows.
"""
self.widget().lower()

def set_icon_source(self, icon_source):
""" Set the window icon source.
Expand Down Expand Up @@ -375,6 +406,22 @@ def set_modality(self, modality):
"""
self.widget().setWindowModality(MODALITY[modality])

def set_always_on_top(self, always_on_top):
""" Set the 'always_on_top' flag on the window
"""
widget = self.widget()
flags = widget.windowFlags()
if always_on_top:
flags |= Qt.WindowStaysOnTopHint
else:
flags &= ~Qt.WindowStaysOnTopHint
if flags != widget.windowFlags():
visible = widget.isVisible()
widget.setWindowFlags(flags)
if visible: # http://qt-project.org/doc/qt-4.8/qwidget.html#windowFlags-prop
widget.show()

def set_visible(self, visible):
""" Set the visibility on the window.
Expand Down
23 changes: 22 additions & 1 deletion enaml/widgets/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Window(Widget):
#: value is 'non_modal'.
modality = Enum('non_modal', 'application_modal', 'window_modal')

#: Whether or not the window remains on top of all others.
always_on_top = Bool(False)

#: If this value is set to True, the window will be destroyed on
#: the completion of the `closed` event.
destroy_on_close = Bool(True)
Expand All @@ -57,6 +60,7 @@ def snapshot(self):
snap['title'] = self.title
snap['initial_size'] = self.initial_size
snap['modality'] = self.modality
snap['always_on_top'] = self.always_on_top
snap['icon_source'] = self.icon_source
return snap

Expand All @@ -66,7 +70,8 @@ def bind(self):
"""
super(Window, self).bind()
self.publish_attributes('title', 'modality', 'icon_source')
attrs = ('title', 'modality', 'always_on_top', 'icon_source')
self.publish_attributes(*attrs)

#--------------------------------------------------------------------------
# Private API
Expand Down Expand Up @@ -126,3 +131,19 @@ def restore(self):
"""
self.send_action('restore', {})

def send_to_front(self):
""" Send the 'send_to_front' action to the client widget.
This moves the window to the front of all the toplevel windows.
"""
self.send_action('send_to_front', {})

def send_to_back(self):
""" Send the 'send_to_back' action to the client widget.
This moves the window to the back of all the toplevel windows.
"""
self.send_action('send_to_back', {})

43 changes: 43 additions & 0 deletions enaml/wx/wx_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def create(self, tree):
self.set_title(tree['title'])
self.set_initial_size(tree['initial_size'])
self.set_modality(tree['modality'])
self.set_always_on_top(tree['always_on_top'])
self.widget().Bind(wx.EVT_CLOSE, self.on_close)

def init_layout(self):
Expand Down Expand Up @@ -220,6 +221,18 @@ def on_action_restore(self, content):
"""
self.restore()

def on_action_send_to_front(self, content):
""" Handle the 'send_to_front' action from the Enaml widget.
"""
self.send_to_front()

def on_action_send_to_back(self, content):
""" Handle the 'send_to_back' action from the Enaml widget.
"""
self.send_to_back()

def on_action_set_icon_source(self, content):
""" Handle the 'set_icon_source' action from the Enaml widget.
Expand All @@ -238,6 +251,12 @@ def on_action_set_modality(self, content):
"""
self.set_modality(content['modality'])

def on_action_set_always_on_top(self, content):
""" Handle the 'set_always_on_top' action from the Enaml widget.
"""
self.set_always_on_top(content['always_on_top'])

#--------------------------------------------------------------------------
# Widget Update Methods
#--------------------------------------------------------------------------
Expand Down Expand Up @@ -265,6 +284,18 @@ def restore(self):
"""
self.widget().Maximize(False)

def send_to_front(self):
""" Move the window to the front of all other windows.
"""
self.widget().Raise()

def send_to_back(self):
""" Move the window to the back of all other windows.
"""
self.widget().Lower()

def set_title(self, title):
""" Set the title of the window.
Expand All @@ -286,6 +317,18 @@ def set_modality(self, modality):
else:
self.widget().MakeModal(True)

def set_always_on_top(self, always_on_top):
""" Set the stickyness of the window.
"""
widget = self.widget()
flags = widget.GetWindowStyleFlag()
if always_on_top:
flags |= wx.STAY_ON_TOP
else:
flags &= ~wx.STAY_ON_TOP
widget.SetWindowStyleFlag(flags)

def set_visible(self, visible):
""" Set the visibility on the window.
Expand Down
26 changes: 20 additions & 6 deletions examples/widgets/popup_windows.enaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,37 @@ done by calling the `add_window` method on the session, and passing it
the newly constructed window instance.

"""
from enaml.widgets.api import Window, Container, PushButton, Field
from enaml.widgets.api import Window, Container, PushButton, Field, CheckBox


enamldef ContentWindow(Window):
enamldef ContentWindow(Window): content:
Container:
Field: pass
Field: pass
CheckBox:
text = 'This window is always on top'
checked := content.always_on_top
Field: pass
PushButton:
text = 'Print object id'
clicked :: print object_id


enamldef Main(Window):
enamldef Main(Window): main:
attr targetwin = None
Container:
PushButton:
text = 'Open Child Window'
clicked :: ContentWindow(root_object(), title='Child Window')
clicked :: ContentWindow(main, title='Child Window')
PushButton:
text = 'Open Target Window'
enabled << not main.targetwin or not main.targetwin.visible
clicked ::
if not main.targetwin:
main.targetwin = ContentWindow(title='Target')
session.add_window(main.targetwin)
PushButton:
text = "Move Target to Front"
enabled << bool(main.targetwin) and main.targetwin.visible
clicked :: main.targetwin.send_to_front()
PushButton:
text = 'Open Modal Child Window'
clicked ::
Expand Down

0 comments on commit d6f6181

Please sign in to comment.