remove spurious spaces
[lilass.git] / qt_frontend.py
index 62e3b842054ce3760668ed8c263314e0d04835cd..0c86555127b86aef9e6906a4c9690373653d9351 100644 (file)
@@ -19,9 +19,9 @@ from screen import RelativeScreenPosition, ScreenSetup
 
 try:
     # Be fine with PyQt4 not being installed
-    from PyQt4 import QtCore, QtGui, uic
+    from PyQt5 import QtCore, QtWidgets, uic
 
-    class PositionSelection(QtGui.QDialog):
+    class PositionSelection(QtWidgets.QDialog):
         def __init__(self, situation):
             # set up main window
             super(PositionSelection, self).__init__()
@@ -42,22 +42,46 @@ try:
             syncIfMirror(self.intRes, self.extRes)
             syncIfMirror(self.extRes, self.intRes)
 
-            # connect the update function, and make sure we are in a correct state
+            # if situation has a previousSetup, use its values as initial state
+            if situation.previousSetup:
+                p = situation.previousSetup
+                self.intEnabled.setChecked(p.intResolution is not None)
+                self.extEnabled.setChecked(p.extResolution is not None)
+                if p.relPosition:
+                    self.relPos.setCurrentIndex(p.relPosition.value - 1)
+                if p.extIsPrimary:
+                    self.extPrimary.setChecked(True)
+                else:
+                    self.intPrimary.setChecked(True)
+                # Pre-select the previous resolution
+                self._intDefaultRes = p.intResolution
+                self._extDefaultRes = p.extResolution
+                self._mirrorDefaultRes = p.intResolution if p.relPosition == RelativeScreenPosition.MIRROR else None # in case of a mirror, they would be the same anyway
+            else:
+                self._intDefaultRes = situation.internalConnector.getPreferredResolution()
+                self._extDefaultRes = situation.externalConnector.getPreferredResolution()
+                self._mirrorDefaultRes = None
+
+            # connect the update function
             self.intEnabled.toggled.connect(self.updateEnabledControls)
             self.extEnabled.toggled.connect(self.updateEnabledControls)
             self.relPos.currentIndexChanged.connect(self.updateEnabledControls)
+
+            # make sure we are in a correct state
             self.updateEnabledControls()
-        
+
         def getRelativeScreenPosition(self):
             idx = self.relPos.currentIndex()
             return self.relPos.itemData(idx)
         
-        def fillResolutionBox(self, box, resolutions):
+        def fillResolutionBox(self, box, resolutions, select = None):
             # if the count did not change, update in-place (this avoids flicker)
             if box.count() == len(resolutions):
                 for idx, res in enumerate(resolutions):
                     box.setItemText(idx, str(res))
                     box.setItemData(idx, res)
+                    if res == select:
+                        box.setCurrentIndex(idx)
             else:
                 # first clear it
                 while box.count() > 0:
@@ -65,6 +89,8 @@ try:
                 # then fill it
                 for res in resolutions:
                     box.addItem(str(res), res)
+                    if res == select:
+                        box.setCurrentIndex(box.count() - 1) # select the most recently added one
         
         def updateEnabledControls(self):
             intEnabled = self.intEnabled.isChecked()
@@ -83,19 +109,19 @@ try:
             # which resolutions do we offer?
             if self.isMirror:
                 commonRes = self._situation.commonResolutions()
-                self.fillResolutionBox(self.intRes, commonRes)
-                self.fillResolutionBox(self.extRes, commonRes)
+                self.fillResolutionBox(self.intRes, commonRes, select = self._mirrorDefaultRes)
+                self.fillResolutionBox(self.extRes, commonRes, select = self._mirrorDefaultRes)
                 self.intRes.setCurrentIndex(self.extRes.currentIndex())
             else:
-                self.fillResolutionBox(self.intRes, self._situation.internalResolutions())
-                self.fillResolutionBox(self.extRes, self._situation.externalResolutions())
+                self.fillResolutionBox(self.intRes, self._situation.internalConnector.getResolutionList(), select = self._intDefaultRes)
+                self.fillResolutionBox(self.extRes, self._situation.externalConnector.getResolutionList(), select = self._extDefaultRes)
             # configure position control
             self.posGroup.setEnabled(bothEnabled)
             self.posLabel1.setEnabled(bothEnabled)
             self.posLabel2.setEnabled(bothEnabled)
             self.relPos.setEnabled(bothEnabled)
             # avoid having no screen
-            self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(intEnabled or extEnabled)
+            self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(intEnabled or extEnabled)
         
         def run(self):
             self.exec_()
@@ -109,13 +135,13 @@ except ImportError:
 # Qt frontend
 class QtFrontend:
     def __init__(self):
-        from PyQt4 import QtGui
-        self.app = QtGui.QApplication(sys.argv)
+        from PyQt5 import QtWidgets
+        self.app = QtWidgets.QApplication(sys.argv)
         print("Qt loaded")
     
     def error(self, message):
-        from PyQt4 import QtGui
-        QtGui.QMessageBox.critical(None, 'Fatal error', message)
+        from PyQt5 import QtWidgets
+        QtWidgets.QMessageBox.critical(None, 'Fatal error', message)
     
     def setup(self, situation):
         return PositionSelection(situation).run()
@@ -123,7 +149,7 @@ class QtFrontend:
     @staticmethod
     def isAvailable():
         try:
-            import PyQt4
+            import PyQt5
             return True
         except ImportError:
             return False