Просмотр исходного кода

Persist main window geometry

Added support for saving and restoring main window geometry.

Saving is done in a somewhat contrieved manner. In order to avoid
exposing App.defaults (or App) to the UI class, a geomUpdate
signal was added to to the FlatCAMGUI class. The signal is emitted
whenever FlatCAMGUI thinks its geometry should be saved (which, so
far, seems to be only in closeEvent()). FlatCAMApp has a slot for
this signal, which updates the defaults dictionary.

Restoring is done by explicitly applying the loaded geometry to
the UI. The UI is initialized (i.e. FlatCAMGUI's __init__ is
called) very early in the initialization sequence, before the
defaults are loaded, so at that time the persisted geometry is
not known to the program. As soon as it is known (i.e. after
load_defaults() is completed), we apply it.

Signed-off-by: Alexandru Lazar <alex@zencoding.org>
Alexandru Lazar 10 лет назад
Родитель
Сommit
ae2e227682
2 измененных файлов с 28 добавлено и 0 удалено
  1. 23 0
      FlatCAMApp.py
  2. 5 0
      FlatCAMGUI.py

+ 23 - 0
FlatCAMApp.py

@@ -146,6 +146,9 @@ class App(QtCore.QObject):
         QtCore.QObject.__init__(self)
 
         self.ui = FlatCAMGUI(self.version)
+        self.connect(self.ui,
+                     QtCore.SIGNAL("geomUpdate(int, int, int, int)"),
+                     self.save_geometry)
 
         #### Plot Area ####
         # self.plotcanvas = PlotCanvas(self.ui.splitter)
@@ -243,6 +246,11 @@ class App(QtCore.QObject):
 
             # Persistence
             "last_folder": None,
+            # Default window geometry
+            "def_win_x": 100,
+            "def_win_y": 100,
+            "def_win_w": 1024,
+            "def_win_h": 650,
 
             # Constants...
             "defaults_save_period_ms": 20000,   # Time between default saves.
@@ -269,6 +277,7 @@ class App(QtCore.QObject):
             self.save_defaults(silent=True)
 
         self.propagate_defaults()
+        self.restore_main_win_geom()
 
         def auto_save_defaults():
             try:
@@ -675,6 +684,14 @@ class App(QtCore.QObject):
             return
         self.defaults.update(defaults)
 
+    def save_geometry(self, x, y, width, height):
+        self.defaults["def_win_x"] = x
+        self.defaults["def_win_y"] = y
+        self.defaults["def_win_w"] = width
+        self.defaults["def_win_h"] = height
+        self.save_defaults()
+        print self.defaults
+
     def message_dialog(self, title, message, kind="info"):
         icon = {"info": QtGui.QMessageBox.Information,
                 "warning": QtGui.QMessageBox.Warning,
@@ -1800,6 +1817,12 @@ class App(QtCore.QObject):
                         routes[param].defaults[p] = self.defaults[param]
                         self.log.debug("  " + param + " OK!")
 
+    def restore_main_win_geom(self):
+        self.ui.setGeometry(self.defaults["def_win_x"],
+                            self.defaults["def_win_y"],
+                            self.defaults["def_win_w"],
+                            self.defaults["def_win_h"])
+
     def plot_all(self):
         """
         Re-generates all plots from all objects.

+ 5 - 0
FlatCAMGUI.py

@@ -4,6 +4,9 @@ from GUIElements import *
 
 class FlatCAMGUI(QtGui.QMainWindow):
 
+    # Emitted when persistent window geometry needs to be retained
+    geom_update = QtCore.pyqtSignal(int, int, int, int, name='geomUpdate')
+
     def __init__(self, version):
         super(FlatCAMGUI, self).__init__()
 
@@ -241,6 +244,8 @@ class FlatCAMGUI(QtGui.QMainWindow):
         self.show()
 
     def closeEvent(self, event):
+        grect = self.geometry()
+        self.geom_update.emit(grect.x(), grect.y(), grect.width(), grect.height())
         QtGui.qApp.quit()