Applying various optimizations
[gc-dialer] / src / util / qwrappers.py
index 9527dc6..cc458bf 100644 (file)
@@ -61,7 +61,7 @@ class ApplicationWrapper(object):
                self._idleDelay = QtCore.QTimer()
                self._idleDelay.setSingleShot(True)
                self._idleDelay.setInterval(0)
-               self._idleDelay.timeout.connect(lambda: self._mainWindow.start())
+               self._idleDelay.timeout.connect(self._on_delayed_start)
                self._idleDelay.start()
 
        def load_settings(self):
@@ -74,6 +74,10 @@ class ApplicationWrapper(object):
                raise NotImplementedError("Booh")
 
        @property
+       def qapp(self):
+               return self._qapp
+
+       @property
        def constants(self):
                return self._constants
 
@@ -109,6 +113,10 @@ class ApplicationWrapper(object):
                        self._mainWindow = None
 
        @misc_utils.log_exception(_moduleLogger)
+       def _on_delayed_start(self):
+               self._mainWindow.start()
+
+       @misc_utils.log_exception(_moduleLogger)
        def _on_app_quit(self, checked = False):
                if self._mainWindow is not None:
                        self.save_settings()
@@ -122,11 +130,13 @@ class ApplicationWrapper(object):
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_toggle_fullscreen(self, checked = False):
-               self._mainWindow.set_fullscreen(checked)
+               with qui_utils.notify_error(self._errorLog):
+                       self._mainWindow.set_fullscreen(checked)
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_toggle_orientation(self, checked = False):
-               self._mainWindow.set_orientation(checked)
+               with qui_utils.notify_error(self._errorLog):
+                       self._mainWindow.set_orientation(checked)
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_about(self, checked = True):
@@ -134,14 +144,16 @@ class ApplicationWrapper(object):
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_log(self, checked = False):
-               with open(self._constants._user_logpath_, "r") as f:
-                       logLines = f.xreadlines()
-                       log = "".join(logLines)
-                       self._clipboard.setText(log)
+               with qui_utils.notify_error(self._errorLog):
+                       with open(self._constants._user_logpath_, "r") as f:
+                               logLines = f.xreadlines()
+                               log = "".join(logLines)
+                               self._clipboard.setText(log)
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_quit(self, checked = False):
-               self._close_windows()
+               with qui_utils.notify_error(self._errorLog):
+                       self._close_windows()
 
 
 class WindowWrapper(object):
@@ -153,13 +165,17 @@ class WindowWrapper(object):
 
                self._layout = QtGui.QBoxLayout(QtGui.QBoxLayout.LeftToRight)
                self._layout.setContentsMargins(0, 0, 0, 0)
-               self._layout.addWidget(self._errorDisplay.toplevel)
+
+               self._superLayout = QtGui.QVBoxLayout()
+               self._superLayout.addWidget(self._errorDisplay.toplevel)
+               self._superLayout.setContentsMargins(0, 0, 0, 0)
+               self._superLayout.addLayout(self._layout)
 
                centralWidget = QtGui.QWidget()
-               centralWidget.setLayout(self._layout)
+               centralWidget.setLayout(self._superLayout)
                centralWidget.setContentsMargins(0, 0, 0, 0)
 
-               self._window = QtGui.QMainWindow(parent)
+               self._window = qui_utils.QSignalingMainWindow(parent)
                self._window.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
                qui_utils.set_stackable(self._window, True)
                self._window.setCentralWidget(centralWidget)
@@ -195,10 +211,10 @@ class WindowWrapper(object):
                pass
 
        def show(self):
-               self.set_fullscreen(self._app.fullscreenAction.isChecked())
                self._window.show()
                for child in self.walk_children():
                        child.show()
+               self.set_fullscreen(self._app.fullscreenAction.isChecked())
 
        def hide(self):
                for child in self.walk_children():
@@ -206,10 +222,11 @@ class WindowWrapper(object):
                self._window.hide()
 
        def set_fullscreen(self, isFullscreen):
-               if isFullscreen:
-                       self._window.showFullScreen()
-               else:
-                       self._window.showNormal()
+               if self._window.isVisible():
+                       if isFullscreen:
+                               self._window.showFullScreen()
+                       else:
+                               self._window.showNormal()
                for child in self.walk_children():
                        child.set_fullscreen(isFullscreen)
 
@@ -222,9 +239,34 @@ class WindowWrapper(object):
                        child.set_orientation(isPortrait)
 
        @misc_utils.log_exception(_moduleLogger)
-       def _on_child_close(self):
+       def _on_child_close(self, obj = None):
                raise NotImplementedError("Booh")
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_close_window(self, checked = True):
-               self.close()
+               with qui_utils.notify_error(self._errorLog):
+                       self.close()
+
+
+class AutoFreezeWindowFeature(object):
+
+       def __init__(self, app, window):
+               self._app = app
+               self._window = window
+               self._app.qapp.focusChanged.connect(self._on_focus_changed)
+               if self._app.qapp.focusWidget() is not None:
+                       self._window.setUpdatesEnabled(True)
+               else:
+                       self._window.setUpdatesEnabled(False)
+
+       def close(self):
+               self._app.qapp.focusChanged.disconnect(self._on_focus_changed)
+               self._window.setUpdatesEnabled(True)
+
+       @misc_utils.log_exception(_moduleLogger)
+       def _on_focus_changed(self, oldWindow, newWindow):
+               with qui_utils.notify_error(self._app.errorLog):
+                       if oldWindow is None and newWindow is not None:
+                               self._window.setUpdatesEnabled(True)
+                       elif oldWindow is not None and newWindow is None:
+                               self._window.setUpdatesEnabled(False)