Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Added size_state trait to Window for maximized information. #29

Merged
merged 1 commit into from Mar 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions pyface/tasks/task_window.py
Expand Up @@ -291,7 +291,8 @@ def reset_layout(self):
def get_window_layout(self):
""" Returns a TaskWindowLayout for the current state of the window.
"""
result = TaskWindowLayout(position=self.position, size=self.size)
result = TaskWindowLayout(position=self.position, size=self.size,
size_state=self.size_state)
for state in self._states:
if state == self._active_state:
result.active_task = state.task.id
Expand All @@ -308,6 +309,7 @@ def set_window_layout(self, window_layout):
# Set window size before laying it out.
self.position = window_layout.position
self.size = window_layout.size
self.size_state = window_layout.size_state

# Store layouts for the tasks, including the active task.
for layout in window_layout.items:
Expand All @@ -318,7 +320,7 @@ def set_window_layout(self, window_layout):
state.layout = layout
else:
logger.warn("Cannot apply layout for task %r: task does not "
"belong to the window." % task)
"belong to the window." % layout.id)

# Attempt to activate the requested task.
state = self._get_state(window_layout.get_active_task())
Expand Down
4 changes: 3 additions & 1 deletion pyface/tasks/task_window_layout.py
@@ -1,5 +1,5 @@
# Enthought library imports.
from traits.api import Either, List, Str, Tuple
from traits.api import Either, List, Str, Tuple, Enum

# Local imports.
from task_layout import LayoutContainer, TaskLayout
Expand All @@ -22,6 +22,8 @@ class TaskWindowLayout(LayoutContainer):
# The size of the window.
size = Tuple(800, 600)

size_state = Enum('normal', 'maximized')

def get_active_task(self):
""" Returns the ID of the active task in the layout, or None if there is
no active task.
Expand Down
9 changes: 1 addition & 8 deletions pyface/ui/qt4/application_window.py
Expand Up @@ -125,18 +125,11 @@ def _create(self):
self._create_trim_widgets(self.control)

def _create_control(self, parent):
control = QtGui.QMainWindow(parent)
control = super(ApplicationWindow, self)._create_control(parent)
control.setObjectName('ApplicationWindow')

if self.position != (-1, -1):
control.move(*self.position)

if self.size != (-1, -1):
control.resize(*self.size)

control.setAnimated(False)
control.setDockNestingEnabled(True)
control.setWindowTitle(self.title)

return control

Expand Down
53 changes: 46 additions & 7 deletions pyface/ui/qt4/window.py
Expand Up @@ -15,7 +15,7 @@
from pyface.qt import QtCore, QtGui

# Enthought library imports.
from traits.api import Any, Event, implements, Property, Unicode
from traits.api import Enum, Event, implements, Property, Unicode
from traits.api import Tuple

# Local imports.
Expand All @@ -38,6 +38,8 @@ class Window(MWindow, Widget):

size = Property(Tuple)

size_state = Enum('normal', 'maximized')

title = Unicode

#### Events #####
Expand Down Expand Up @@ -79,6 +81,23 @@ def show(self, visible):
# Protected 'IWindow' interface.
###########################################################################

def _create_control(self, parent):
""" Create a default QMainWindow. """
control = QtGui.QMainWindow(parent)

if self.size != (-1, -1):
control.resize(*self.size)

if self.position != (-1, -1):
control.move(*self.position)

if self.size_state != 'normal':
self._size_state_changed(self.size_state)

control.setWindowTitle(self.title)

return control

def _add_event_listeners(self):
self._event_filter = _EventFilter(self)

Expand Down Expand Up @@ -137,6 +156,16 @@ def _set_size(self, size):

self.trait_property_changed('size', old, size)

def _size_state_changed(self, state):
control = self.control
if control is None:
return # Nothing to do here

if state == 'maximized':
control.setWindowState(control.windowState() | QtCore.Qt.WindowMaximized)
elif state == 'normal':
control.setWindowState(control.windowState() & ~QtCore.Qt.WindowMaximized)

def _title_changed(self, title):
""" Static trait change handler. """

Expand Down Expand Up @@ -166,7 +195,9 @@ def eventFilter(self, obj, e):
if obj is not window.control:
return False

if e.type() == QtCore.QEvent.Close:
typ = e.type()

if typ == QtCore.QEvent.Close:
# Do not destroy the window during its event handler.
GUI.invoke_later(window.close)

Expand All @@ -175,26 +206,26 @@ def eventFilter(self, obj, e):

return True

if e.type() == QtCore.QEvent.WindowActivate:
if typ == QtCore.QEvent.WindowActivate:
window.activated = window

elif e.type() == QtCore.QEvent.WindowDeactivate:
elif typ == QtCore.QEvent.WindowDeactivate:
window.deactivated = window

elif e.type() == QtCore.QEvent.Resize:
elif typ == QtCore.QEvent.Resize:
# Get the new size and set the shadow trait without performing
# notification.
size = e.size()
window._size = (size.width(), size.height())

elif e.type() == QtCore.QEvent.Move:
elif typ == QtCore.QEvent.Move:
# Get the real position and set the trait without performing
# notification. Don't use event.pos(), as this excludes the window
# frame geometry.
pos = window.control.pos()
window._position = (pos.x(), pos.y())

elif e.type() == QtCore.QEvent.KeyPress:
elif typ == QtCore.QEvent.KeyPress:
# Pyface doesn't seem to be Unicode aware. Only keep the key code
# if it corresponds to a single Latin1 character.
kstr = e.text()
Expand All @@ -214,6 +245,14 @@ def eventFilter(self, obj, e):
key_code = kcode,
event = e)

elif typ == QtCore.QEvent.WindowStateChange:
# set the size_state of the window.
state = obj.windowState()
if state & QtCore.Qt.WindowMaximized:
window.size_state = 'maximized'
else:
window.size_state = 'normal'

return False

#### EOF ######################################################################