From d063d5a2ce9528c44e05c14439486d9a4203c260 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Fri, 10 Aug 2018 12:56:23 -0400
Subject: [PATCH] mainwindow: save/load complete window geometry and state

- Replaces the use of position and size 2d vectors when saving qt
  application settings to the registry with geometry and state
  byte arrays which includes information about the window's screen
  number and fixes a bug which prevents the main window from ever
  starting correctly maximized due to the disregarded taskbar
  offset.

Change-Id: Iaa15f075970675e85ffcdb26da962a19a1914252
---
 main.cpp       | 14 +++++++-------
 mainwindow.cpp | 31 +++++++++++++++----------------
 mainwindow.h   |  5 ++++-
 settingskey.h  |  4 ++--
 4 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/main.cpp b/main.cpp
index 019780c..1f25c8c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -197,12 +197,12 @@ main(int argc, char *argv[])
         delete sem;
     });
 #endif
-
-    auto ret = a.exec();
-
-    QCoreApplication::exit();
-    GlobalSystemTray::instance().deleteLater();
-    GlobalSystemTray::instance().hide();
-
+
+    auto ret = a.exec();
+
+    QCoreApplication::exit();
+    GlobalSystemTray::instance().deleteLater();
+    GlobalSystemTray::instance().hide();
+
     return ret;
 }
diff --git a/mainwindow.cpp b/mainwindow.cpp
index 531d9c3..ed7b5aa 100644
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -83,17 +83,7 @@ MainWindow::MainWindow(QWidget* parent) :
     }
 #endif
 
-    QSettings settings;
-    QVariant size = settings.value(SettingsKey::savedSize);
-    QVariant posV = settings.value(SettingsKey::savedPos);
-    if (size.isValid() && posV.isValid()) {
-        resize(size.toSize());
-        auto screenSize = QApplication::desktop()->screenGeometry();
-        auto pos = posV.toPoint();
-        if (pos.rx() < screenSize.width() && pos.ry() < screenSize.height())
-            move(pos);
-    } else
-        resize(800, 600);
+    readSettingsFromRegistry();
 
     win_sparkle_set_appcast_url("http://dl.ring.cx/windows/winsparkle-ring.xml");
     win_sparkle_set_app_details(L"Savoir-faire Linux", L"Ring", QString(NIGHTLY_VERSION).toStdWString().c_str());
@@ -111,9 +101,6 @@ MainWindow::MainWindow(QWidget* parent) :
 
     setContextMenuPolicy(Qt::NoContextMenu);
 
-    if (not settings.contains(SettingsKey::enableNotifications)) {
-        settings.setValue(SettingsKey::enableNotifications, true);
-    }
     connect(&GlobalSystemTray::instance(), SIGNAL(messageClicked()), this, SLOT(notificationClicked()));
 
     connect(&netManager_, &QNetworkConfigurationManager::onlineStateChanged, [=](bool online) {
@@ -227,7 +214,19 @@ MainWindow::closeEvent(QCloseEvent* event)
         this->hide();
         event->ignore();
     } else {
-        settings.setValue(SettingsKey::savedSize, size());
-        settings.setValue(SettingsKey::savedPos, pos());
+        settings.setValue(SettingsKey::geometry, saveGeometry());
+        settings.setValue(SettingsKey::windowState, saveState());
     }
+    QMainWindow::closeEvent(event);
 }
+
+void
+MainWindow::readSettingsFromRegistry()
+{
+    QSettings settings;
+    restoreGeometry(settings.value(SettingsKey::geometry).toByteArray());
+    restoreState(settings.value(SettingsKey::windowState).toByteArray());
+    if (not settings.contains(SettingsKey::enableNotifications)) {
+        settings.setValue(SettingsKey::enableNotifications, true);
+    }
+}
\ No newline at end of file
diff --git a/mainwindow.h b/mainwindow.h
index 54c19fa..25306ea 100644
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -62,7 +62,10 @@ private slots:
 
 private:
     explicit MainWindow(QWidget* parent = 0);
-     ~MainWindow();
+    ~MainWindow();
+
+    void readSettingsFromRegistry();
+    
     Ui::MainWindow* ui;
     QNetworkConfigurationManager netManager_;
 };
diff --git a/settingskey.h b/settingskey.h
index d09a91e..9fd4234 100644
--- a/settingskey.h
+++ b/settingskey.h
@@ -22,8 +22,8 @@ namespace SettingsKey {
 
 constexpr static char closeOrMinimized[] = "closeOrMin";
 constexpr static char autoAnswer[] = "autoAnswer";
-constexpr static char savedSize[] = "savedSize";
-constexpr static char savedPos[] = "savedPos";
+constexpr static char geometry[] = "geometry";
+constexpr static char windowState[] = "windowState";
 constexpr static char imShowAuthor[] = "imShowAuthor";
 constexpr static char imShowDate[] = "imShowDate";
 constexpr static char enableNotifications[] = "enableNotifications";
-- 
GitLab