From 4d46abc2125eb9665c8ddcae21d952dc7d6be098 Mon Sep 17 00:00:00 2001
From: Joseph Mirabel <jmirabel@laas.fr>
Date: Tue, 6 Mar 2018 11:05:59 +0100
Subject: [PATCH] Save and restore window state.

---
 include/gepetto/gui/settings.hh        |  4 ++++
 pyplugins/gepetto/gui/blenderexport.py |  8 +++----
 pyplugins/gepetto/gui/pythonwidget.py  |  7 ++----
 src/gui/main.cc.in                     |  5 +++-
 src/gui/mainwindow.cc                  |  1 +
 src/gui/pythonwidget.cc                |  1 +
 src/gui/settings.cc                    | 33 ++++++++++++++++++++++++++
 7 files changed, 48 insertions(+), 11 deletions(-)

diff --git a/include/gepetto/gui/settings.hh b/include/gepetto/gui/settings.hh
index 62469ba..c023077 100644
--- a/include/gepetto/gui/settings.hh
+++ b/include/gepetto/gui/settings.hh
@@ -38,6 +38,7 @@ namespace gepetto {
       std::string configurationFile;
       std::string predifinedRobotConf;
       std::string predifinedEnvConf;
+      std::string stateConf;
 
       bool verbose;
       bool noPlugin;
@@ -111,6 +112,9 @@ namespace gepetto {
       /// \note Prefer using Settings::fromFiles()
       void readSettingFile ();
 
+      void saveState () const;
+      void restoreState () const;
+
     private:
       void writeRobotFile ();
       void writeEnvFile ();
diff --git a/pyplugins/gepetto/gui/blenderexport.py b/pyplugins/gepetto/gui/blenderexport.py
index 71e4895..175cd7b 100644
--- a/pyplugins/gepetto/gui/blenderexport.py
+++ b/pyplugins/gepetto/gui/blenderexport.py
@@ -87,11 +87,9 @@ class _Widget (QtGui.QWidget):
 ###     gepetto.gui.blenderexport=true
 ###
 class Plugin(QtGui.QDockWidget):
-    def __init__ (self, mainWindow, flags = None):
-        if flags is None:
-            super(Plugin, self).__init__ ("Blender export plugin", mainWindow)
-        else:
-            super(Plugin, self).__init__ ("Blender export plugin", mainWindow, flags)
+    def __init__ (self, mainWindow):
+        super(Plugin, self).__init__ ("Blender export plugin", mainWindow)
+        self.setObjectName ("gepetto.gui.blenderexport")
         self.resetConnection()
         # Initialize the widget
         mainWidget = _Widget(self, self)
diff --git a/pyplugins/gepetto/gui/pythonwidget.py b/pyplugins/gepetto/gui/pythonwidget.py
index 33d614e..2ba8f46 100644
--- a/pyplugins/gepetto/gui/pythonwidget.py
+++ b/pyplugins/gepetto/gui/pythonwidget.py
@@ -81,11 +81,8 @@ class Plugin(QtGui.QDockWidget):
     Example of plugin of the Gepetto Viewer GUI. This can interact with
     PythonWidget C++ class.
     """
-    def __init__ (self, mainWindow, flags = None):
-        if flags is None:
-            super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow)
-        else:
-            super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow, flags)
+    def __init__ (self, mainWindow):
+        super(Plugin, self).__init__ ("Gepetto Viewer plugin", mainWindow)
         self.setObjectName("Gepetto Viewer plugin")
         self.client = Client()
         # Initialize the widget
diff --git a/src/gui/main.cc.in b/src/gui/main.cc.in
index f5bdb13..cf5bc14 100644
--- a/src/gui/main.cc.in
+++ b/src/gui/main.cc.in
@@ -83,8 +83,11 @@ int main(int argc, char *argv[])
   w.setWindowIcon (pixmap);
   settings.setMainWindow (&w);
   settings.initPlugins ();
+  settings.restoreState ();
   w.connect (&a, SIGNAL (log(QString)), SLOT (logError(const QString&)));
   w.show();
   splash.finish(&w);
-  return a.exec();
+  int retCode = a.exec();
+  settings.saveState ();
+  return retCode;
 }
diff --git a/src/gui/mainwindow.cc b/src/gui/mainwindow.cc
index 879e4a5..92dfd5f 100644
--- a/src/gui/mainwindow.cc
+++ b/src/gui/mainwindow.cc
@@ -258,6 +258,7 @@ namespace gepetto {
     {
       QDockWidget* dockOSG = new QDockWidget (
           tr("Window ") + osgWidget->objectName(), this);
+      dockOSG->setObjectName ("gepetto-gui.osg." + osgWidget->objectName());
       dockOSG->setWidget(osgWidget);
       connect(dockOSG,SIGNAL(visibilityChanged(bool)),SLOT(dockVisibilityChanged(bool)));
       addDockWidget(Qt::RightDockWidgetArea, dockOSG);
diff --git a/src/gui/pythonwidget.cc b/src/gui/pythonwidget.cc
index 1041426..612f8e3 100644
--- a/src/gui/pythonwidget.cc
+++ b/src/gui/pythonwidget.cc
@@ -57,6 +57,7 @@ namespace gepetto {
       PythonWidget::PythonWidget(QWidget *parent) :
         QDockWidget("&PythonQt console", parent)
       {
+        setObjectName ("gepetto-gui.pythonqtconsole");
         PythonQt::init(PythonQt::RedirectStdOut);
         PythonQt_init_QtBindings();
         PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
diff --git a/src/gui/settings.cc b/src/gui/settings.cc
index c26a8f5..8d7a28a 100644
--- a/src/gui/settings.cc
+++ b/src/gui/settings.cc
@@ -37,6 +37,7 @@ namespace gepetto {
       : configurationFile ("settings")
       , predifinedRobotConf ("robots")
       , predifinedEnvConf ("environments")
+      , stateConf (".state")
       , verbose (false)
       , noPlugin (false)
       , startGepettoCorbaServer (true)
@@ -198,6 +199,38 @@ namespace gepetto {
 #endif
     }
 
+    void Settings::restoreState () const
+    {
+      QSettings settings (QSettings::SystemScope,
+          QCoreApplication::organizationName (),
+          getQSettingsFileName (stateConf));
+      if (settings.status() != QSettings::NoError) {
+        qDebug () << "Could not restore the window state from" << settings.fileName();
+      } else {
+        settings.beginGroup("mainWindow");
+        mw->restoreGeometry (settings.value("geometry").toByteArray());
+        mw->restoreState (settings.value("state").toByteArray());
+        mw->centralWidget()->setVisible (settings.value("centralWidgetVisibility", true).toBool());
+        settings.endGroup();
+      }
+    }
+
+    void Settings::saveState () const
+    {
+      QSettings settings (QSettings::SystemScope,
+          QCoreApplication::organizationName (),
+          getQSettingsFileName (stateConf));
+      if (settings.status() != QSettings::NoError) {
+        qDebug () << "Could not save the window state to" << settings.fileName();
+      } else {
+        settings.beginGroup("mainWindow");
+        settings.setValue("geometry", mw->saveGeometry());
+        settings.setValue("state"   , mw->saveState());
+        settings.setValue("centralWidgetVisibility", mw->centralWidget()->isVisible ());
+        settings.endGroup();
+      }
+    }
+
     void Settings::setMainWindow(gepetto::gui::MainWindow *main)
     {
       mw = main;
-- 
GitLab