From d328b32038487b5056c0f3b4e19c7f1e9159958d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 18 Apr 2011 21:04:31 -0500 Subject: [PATCH] Switching to make rotation more configurable4 --- src/dialcentral_qt.py | 32 +++++++++++++++++-------- src/dialogs.py | 8 +++---- src/util/qui_utils.py | 21 +++++++++------- src/util/qwrappers.py | 64 ++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 94 insertions(+), 31 deletions(-) diff --git a/src/dialcentral_qt.py b/src/dialcentral_qt.py index 4bf128a..4a789f4 100755 --- a/src/dialcentral_qt.py +++ b/src/dialcentral_qt.py @@ -256,6 +256,7 @@ class MainWindow(qwrappers.WindowWrapper): def __init__(self, parent, app): qwrappers.WindowWrapper.__init__(self, parent, app) self._window.setWindowTitle("%s" % constants.__pretty_app_name__) + self._window.resized.connect(self._on_window_resized) self._errorLog = self._app.errorLog self._session = session.Session(self._errorLog, constants._data_path_) @@ -350,7 +351,7 @@ class MainWindow(qwrappers.WindowWrapper): self._initialize_tab(self._tabWidget.currentIndex()) self.set_fullscreen(self._app.fullscreenAction.isChecked()) - self.set_orientation(self._app.orientationAction.isChecked()) + self.update_orientation(self._app.orientation) def set_default_credentials(self, username, password): self._defaultCredentials = username, password @@ -401,7 +402,7 @@ class MainWindow(qwrappers.WindowWrapper): def load_settings(self, config): blobs = "", "" isFullscreen = False - isPortrait = qui_utils.screen_orientation() == QtCore.Qt.Vertical + orientation = self._app.orientation tabIndex = 0 try: blobs = [ @@ -410,7 +411,7 @@ class MainWindow(qwrappers.WindowWrapper): ] isFullscreen = config.getboolean(constants.__pretty_app_name__, "fullscreen") tabIndex = config.getint(constants.__pretty_app_name__, "tab") - isPortrait = config.getboolean(constants.__pretty_app_name__, "portrait") + orientation = config.get(constants.__pretty_app_name__, "orientation") except ConfigParser.NoOptionError, e: _moduleLogger.info( "Settings file %s is missing option %s" % ( @@ -457,7 +458,7 @@ class MainWindow(qwrappers.WindowWrapper): ) self.set_default_credentials(*creds) self._app.fullscreenAction.setChecked(isFullscreen) - self._app.orientationAction.setChecked(isPortrait) + self.update_orientation(orientation) self.set_current_tab(tabIndex) backendId = 2 # For backwards compatibility @@ -493,7 +494,7 @@ class MainWindow(qwrappers.WindowWrapper): config.add_section(constants.__pretty_app_name__) config.set(constants.__pretty_app_name__, "tab", str(self.get_current_tab())) config.set(constants.__pretty_app_name__, "fullscreen", str(self._app.fullscreenAction.isChecked())) - config.set(constants.__pretty_app_name__, "portrait", str(self._app.orientationAction.isChecked())) + config.set(constants.__pretty_app_name__, "orientation", str(self._app.orientation)) for i, value in enumerate(self.get_default_credentials()): blob = base64.b64encode(value) config.set(constants.__pretty_app_name__, "bin_blob_%i" % i, blob) @@ -514,12 +515,13 @@ class MainWindow(qwrappers.WindowWrapper): for settingName, settingValue in tabSettings.iteritems(): config.set(sectionName, settingName, settingValue) - def set_orientation(self, isPortrait): - qwrappers.WindowWrapper.set_orientation(self, isPortrait) - if isPortrait: - self._tabWidget.setTabPosition(QtGui.QTabWidget.South) - else: + def update_orientation(self, orientation): + qwrappers.WindowWrapper.update_orientation(self, orientation) + windowOrientation = self.idealWindowOrientation + if windowOrientation == QtCore.Qt.Horizontal: self._tabWidget.setTabPosition(QtGui.QTabWidget.West) + else: + self._tabWidget.setTabPosition(QtGui.QTabWidget.South) def _initialize_tab(self, index): assert index < self.MAX_TABS, "Invalid tab" @@ -606,6 +608,16 @@ class MainWindow(qwrappers.WindowWrapper): @qt_compat.Slot() @misc_utils.log_exception(_moduleLogger) + def _on_window_resized(self): + with qui_utils.notify_error(self._app.errorLog): + windowOrientation = self.idealWindowOrientation + if windowOrientation == QtCore.Qt.Horizontal: + self._tabWidget.setTabPosition(QtGui.QTabWidget.West) + else: + self._tabWidget.setTabPosition(QtGui.QTabWidget.South) + + @qt_compat.Slot() + @misc_utils.log_exception(_moduleLogger) def _on_new_message_alert(self): with qui_utils.notify_error(self._errorLog): if self._app.alarmHandler.alarmType == self._app.alarmHandler.ALARM_APPLICATION: diff --git a/src/dialogs.py b/src/dialogs.py index 417b8b8..0e1a92e 100644 --- a/src/dialogs.py +++ b/src/dialogs.py @@ -869,7 +869,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper): self._update_letter_count() self._update_target_fields() self.set_fullscreen(self._app.fullscreenAction.isChecked()) - self.set_orientation(self._app.orientationAction.isChecked()) + self.update_orientation(self._app.orientation) def close(self): if self._window is None: @@ -908,8 +908,8 @@ class SMSEntryWindow(qwrappers.WindowWrapper): except RuntimeError: _moduleLogger.exception("Oh well") - def set_orientation(self, isPortrait): - qwrappers.WindowWrapper.set_orientation(self, isPortrait) + def update_orientation(self, orientation): + qwrappers.WindowWrapper.update_orientation(self, orientation) self._scroll_to_bottom() def _update_letter_count(self): @@ -1117,7 +1117,7 @@ class SMSEntryWindow(qwrappers.WindowWrapper): @qt_compat.Slot() @misc_utils.log_exception(_moduleLogger) - def _on_window_resized(self, checked = True): + def _on_window_resized(self): with qui_utils.notify_error(self._app.errorLog): self._scroll_to_bottom() diff --git a/src/util/qui_utils.py b/src/util/qui_utils.py index 0d1fd6a..325dc6e 100644 --- a/src/util/qui_utils.py +++ b/src/util/qui_utils.py @@ -309,12 +309,12 @@ except AttributeError: set_stackable = _null_set_stackable -def _null_set_autorient(window, isStackable): +def _null_set_autorient(window, doAutoOrient): pass -def _maemo_set_autorient(window, isStackable): - window.setAttribute(QtCore.Qt.WA_Maemo5AutoOrientation, isStackable) +def _maemo_set_autorient(window, doAutoOrient): + window.setAttribute(QtCore.Qt.WA_Maemo5AutoOrientation, doAutoOrient) try: @@ -338,13 +338,16 @@ def _null_set_window_orientation(window, orientation): def _maemo_set_window_orientation(window, orientation): if orientation == QtCore.Qt.Vertical: - oldHint = QtCore.Qt.WA_Maemo5LandscapeOrientation - newHint = QtCore.Qt.WA_Maemo5PortraitOrientation + window.setAttribute(QtCore.Qt.WA_Maemo5LandscapeOrientation, False) + window.setAttribute(QtCore.Qt.WA_Maemo5PortraitOrientation, True) elif orientation == QtCore.Qt.Horizontal: - oldHint = QtCore.Qt.WA_Maemo5PortraitOrientation - newHint = QtCore.Qt.WA_Maemo5LandscapeOrientation - window.setAttribute(oldHint, False) - window.setAttribute(newHint, True) + window.setAttribute(QtCore.Qt.WA_Maemo5LandscapeOrientation, True) + window.setAttribute(QtCore.Qt.WA_Maemo5PortraitOrientation, False) + elif orientation is None: + window.setAttribute(QtCore.Qt.WA_Maemo5LandscapeOrientation, True) + window.setAttribute(QtCore.Qt.WA_Maemo5PortraitOrientation, True) + else: + raise RuntimeError("Unknown orientation: %r" % orientation) try: diff --git a/src/util/qwrappers.py b/src/util/qwrappers.py index 7e6265b..1f5b8ea 100644 --- a/src/util/qwrappers.py +++ b/src/util/qwrappers.py @@ -18,6 +18,11 @@ _moduleLogger = logging.getLogger(__name__) class ApplicationWrapper(object): + DEFAULT_ORIENTATION = "Default" + AUTO_ORIENTATION = "Auto" + LANDSCAPE_ORIENTATION = "Landscape" + PORTRAIT_ORIENTATION = "Portrait" + def __init__(self, qapp, constants): self._constants = constants self._qapp = qapp @@ -32,11 +37,12 @@ class ApplicationWrapper(object): self._fullscreenAction.setShortcut(QtGui.QKeySequence("CTRL+Enter")) self._fullscreenAction.toggled.connect(self._on_toggle_fullscreen) + self._orientation = self.DEFAULT_ORIENTATION self._orientationAction = QtGui.QAction(None) - self._orientationAction.setText("Orientation") + self._orientationAction.setText("Next Orientation") self._orientationAction.setCheckable(True) self._orientationAction.setShortcut(QtGui.QKeySequence("CTRL+o")) - self._orientationAction.toggled.connect(self._on_toggle_orientation) + self._orientationAction.triggered.connect(self._on_next_orientation) self._logAction = QtGui.QAction(None) self._logAction.setText("Log") @@ -95,6 +101,10 @@ class ApplicationWrapper(object): return self._orientationAction @property + def orientation(self): + return self._orientation + + @property def logAction(self): return self._logAction @@ -106,6 +116,15 @@ class ApplicationWrapper(object): def quitAction(self): return self._quitAction + @classmethod + def _next_orientation(cls, current): + return { + cls.DEFAULT_ORIENTATION: cls.AUTO_ORIENTATION, + cls.AUTO_ORIENTATION: cls.LANDSCAPE_ORIENTATION, + cls.LANDSCAPE_ORIENTATION: cls.PORTRAIT_ORIENTATION, + cls.PORTRAIT_ORIENTATION: cls.DEFAULT_ORIENTATION, + }[current] + def _close_windows(self): if self._mainWindow is not None: self.save_settings() @@ -135,9 +154,10 @@ class ApplicationWrapper(object): self._mainWindow.set_fullscreen(checked) @misc_utils.log_exception(_moduleLogger) - def _on_toggle_orientation(self, checked = False): + def _on_next_orientation(self, checked = False): with qui_utils.notify_error(self._errorLog): - self._mainWindow.set_orientation(checked) + self._orientation = self._next_orientation(self._orientation) + self._mainWindow.update_orientation(self._orientation) @misc_utils.log_exception(_moduleLogger) def _on_about(self, checked = True): @@ -196,6 +216,24 @@ class WindowWrapper(object): def window(self): return self._window + @property + def windowOrientation(self): + geom = self._window.size() + if geom.width() <= geom.height(): + return QtCore.Qt.Vertical + else: + return QtCore.Qt.Horizontal + + @property + def idealWindowOrientation(self): + if self._app.orientation == self._app.LANDSCAPE_ORIENTATION: + windowOrientation = QtCore.Qt.Horizontal + elif self._app.orientation == self._app.PORTRAIT_ORIENTATION: + windowOrientation = QtCore.Qt.Vertical + else: + windowOrientation = self.windowOrientation + return windowOrientation + def walk_children(self): return () @@ -231,13 +269,23 @@ class WindowWrapper(object): for child in self.walk_children(): child.set_fullscreen(isFullscreen) - def set_orientation(self, isPortrait): - if isPortrait: + def update_orientation(self, orientation): + if orientation == self._app.DEFAULT_ORIENTATION: + qui_utils.set_autorient(self.window, False) + qui_utils.set_window_orientation(self.window, None) + elif orientation == self._app.AUTO_ORIENTATION: + qui_utils.set_autorient(self.window, True) + qui_utils.set_window_orientation(self.window, None) + elif orientation == self._app.LANDSCAPE_ORIENTATION: + qui_utils.set_autorient(self.window, False) + qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal) + elif orientation == self._app.PORTRAIT_ORIENTATION: + qui_utils.set_autorient(self.window, False) qui_utils.set_window_orientation(self.window, QtCore.Qt.Vertical) else: - qui_utils.set_window_orientation(self.window, QtCore.Qt.Horizontal) + raise RuntimeError("Unknown orientation: %r" % orientation) for child in self.walk_children(): - child.set_orientation(isPortrait) + child.update_orientation(orientation) @misc_utils.log_exception(_moduleLogger) def _on_child_close(self, obj = None): -- 1.7.9.5