Fix restoring maximized state at restart on Windows
[quassel.git] / src / qtui / mainwin.cpp
index 50e2731..f31354c 100644 (file)
@@ -37,6 +37,7 @@
 #include "awaylogview.h"
 #include "action.h"
 #include "actioncollection.h"
+#include "bufferhotlistfilter.h"
 #include "buffermodel.h"
 #include "bufferview.h"
 #include "bufferviewoverlay.h"
 #include "coreinfodlg.h"
 #include "coreconnectdlg.h"
 #include "contextmenuactionprovider.h"
+#include "debugbufferviewoverlay.h"
 #include "debuglogwidget.h"
 #include "debugmessagemodelfilter.h"
+#include "flatproxymodel.h"
 #include "iconloader.h"
 #include "inputwidget.h"
 #include "inputline.h"
@@ -68,7 +71,6 @@
 #include "qtuiapplication.h"
 #include "qtuimessageprocessor.h"
 #include "qtuisettings.h"
-#include "sessionsettings.h"
 #include "settingsdlg.h"
 #include "settingspagedlg.h"
 #include "systemtray.h"
@@ -133,17 +135,10 @@ MainWin::MainWin(QWidget *parent)
   installEventFilter(new JumpKeyHandler(this));
 
   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
-  connect(app, SIGNAL(saveStateToSession(const QString&)), SLOT(saveStateToSession(const QString&)));
-  connect(app, SIGNAL(saveStateToSessionSettings(SessionSettings&)), SLOT(saveStateToSessionSettings(SessionSettings&)));
+  connect(app, SIGNAL(aboutToQuit()), SLOT(aboutToQuit()));
 }
 
 void MainWin::init() {
-  QtUiSettings s;
-  if(s.value("MainWinSize").isValid())
-    resize(s.value("MainWinSize").toSize());
-  else
-    resize(QSize(800, 500));
-
   connect(QApplication::instance(), SIGNAL(aboutToQuit()), SLOT(saveLayout()));
   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(clientNetworkCreated(NetworkId)));
   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(clientNetworkRemoved(NetworkId)));
@@ -170,6 +165,7 @@ void MainWin::init() {
   setupToolBars();
   setupSystray();
   setupTitleSetter();
+  setupHotList();
 
 #ifndef HAVE_KDE
   QtUi::registerNotificationBackend(new TaskbarNotificationBackend(this));
@@ -185,16 +181,15 @@ void MainWin::init() {
   QtUi::registerNotificationBackend(new KNotificationBackend(this));
 #endif /* HAVE_KDE */
 
+  setDisconnectedState();  // Disable menus and stuff
+
   // restore mainwin state
-  restoreState(s.value("MainWinState").toByteArray());
+  QtUiSettings s;
+  restoreStateFromSettings(s);
 
   // restore locked state of docks
   QtUi::actionCollection("General")->action("LockLayout")->setChecked(s.value("LockLayout", false).toBool());
 
-  setDisconnectedState();  // Disable menus and stuff
-
-  show();
-
   if(Quassel::runMode() != Quassel::Monolithic) {
     showCoreConnectionDlg(true); // autoconnect if appropriate
   } else {
@@ -203,10 +198,48 @@ void MainWin::init() {
 }
 
 MainWin::~MainWin() {
+
+}
+
+void MainWin::aboutToQuit() {
   QtUiSettings s;
-  s.setValue("MainWinSize", size());
-  s.setValue("MainWinPos", pos());
+  saveStateToSettings(s);
+}
+
+void MainWin::saveStateToSettings(UiSettings &s) {
+  s.setValue("MainWinSize", _normalSize);
+  s.setValue("MainWinPos", _normalPos);
   s.setValue("MainWinState", saveState());
+  s.setValue("MainWinGeometry", saveGeometry());
+  s.setValue("MainWinMinimized", isMinimized());
+  s.setValue("MainWinMaximized", isMaximized());
+  s.setValue("MainWinHidden", _isHidden);
+}
+
+void MainWin::restoreStateFromSettings(UiSettings &s) {
+  _normalSize = s.value("MainWinSize", size()).toSize();
+  _normalPos = s.value("MainWinPos", pos()).toPoint();
+  bool maximized = s.value("MainWinMaximized", false).toBool();
+
+  restoreGeometry(s.value("MainWinGeometry").toByteArray());
+
+  if(maximized) {
+    // restoreGeometry() fails if the windows was maximized, so we resize and position explicitly
+    resize(_normalSize);
+    move(_normalPos);
+  }
+
+  restoreState(s.value("MainWinState").toByteArray());
+
+  _isHidden = false;
+  if(s.value("MainWinHidden").toBool())
+    hideToTray();
+  else if(s.value("MainWinMinimized").toBool())
+    showMinimized();
+  else if(maximized)
+    showMaximized();
+  else
+    show();
 }
 
 void MainWin::updateIcon() {
@@ -269,8 +302,14 @@ void MainWin::setupActions() {
                                        this, SLOT(on_actionDebugBufferViewOverlay_triggered())));
   coll->addAction("DebugMessageModel", new Action(SmallIcon("tools-report-bug"), tr("Debug &MessageModel"), coll,
                                        this, SLOT(on_actionDebugMessageModel_triggered())));
+  coll->addAction("DebugHotList", new Action(SmallIcon("tools-report-bug"), tr("Debug &HotList"), coll,
+                                       this, SLOT(on_actionDebugHotList_triggered())));
   coll->addAction("DebugLog", new Action(SmallIcon("tools-report-bug"), tr("Debug &Log"), coll,
                                        this, SLOT(on_actionDebugLog_triggered())));
+
+  // Navigation
+  coll->addAction("JumpHotBuffer", new Action(tr("Jump to hot buffer"), coll,
+                                              this, SLOT(on_jumpHotBuffer_triggered()), QKeySequence(Qt::META + Qt::Key_A)));
 }
 
 void MainWin::setupMenus() {
@@ -329,6 +368,7 @@ void MainWin::setupMenus() {
   _helpDebugMenu->addAction(coll->action("DebugNetworkModel"));
   _helpDebugMenu->addAction(coll->action("DebugBufferViewOverlay"));
   _helpDebugMenu->addAction(coll->action("DebugMessageModel"));
+  _helpDebugMenu->addAction(coll->action("DebugHotList"));
   _helpDebugMenu->addAction(coll->action("DebugLog"));
 }
 
@@ -552,6 +592,12 @@ void MainWin::setupStatusBar() {
   connect(showStatusbar, SIGNAL(toggled(bool)), this, SLOT(saveStatusBarStatus(bool)));
 }
 
+void MainWin::setupHotList() {
+  FlatProxyModel *flatProxy = new FlatProxyModel(this);
+  flatProxy->setSourceModel(Client::bufferModel());
+  _bufferHotList = new BufferHotListFilter(flatProxy);
+}
+
 void MainWin::saveStatusBarStatus(bool enabled) {
   QtUiSettings uiSettings;
   uiSettings.setValue("ShowStatusBar", enabled);
@@ -774,12 +820,34 @@ void MainWin::showShortcutsDlg() {
 }
 #endif
 
+/********************************************************************************************************/
+
+bool MainWin::event(QEvent *event) {
+  if(event->type() == QEvent::WindowActivate)
+    QtUi::closeNotifications();
+  return QMainWindow::event(event);
+}
+
+void MainWin::moveEvent(QMoveEvent *event) {
+  if(!(windowState() & Qt::WindowMaximized))
+    _normalPos = event->pos();
+
+  QMainWindow::moveEvent(event);
+}
+
+void MainWin::resizeEvent(QResizeEvent *event) {
+  if(!(windowState() & Qt::WindowMaximized))
+    _normalSize = event->size();
+
+  QMainWindow::resizeEvent(event);
+}
+
 void MainWin::closeEvent(QCloseEvent *event) {
   QtUiSettings s;
   QtUiApplication* app = qobject_cast<QtUiApplication*> qApp;
   Q_ASSERT(app);
-  if(!app->aboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
-    toggleMinimizedToTray();
+  if(!app->isAboutToQuit() && s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnClose").toBool()) {
+    hideToTray();
     event->ignore();
   } else {
     event->accept();
@@ -787,30 +855,13 @@ void MainWin::closeEvent(QCloseEvent *event) {
   }
 }
 
-bool MainWin::event(QEvent *event) {
-  if(event->type() == QEvent::WindowActivate)
-    QtUi::closeNotifications();
-  return QMainWindow::event(event);
-}
-
 void MainWin::changeEvent(QEvent *event) {
-  if(event->type() == QEvent::WindowStateChange) {
-    if(windowState() & Qt::WindowMinimized) {
-      QtUiSettings s;
-      if(s.value("UseSystemTrayIcon").toBool() && s.value("MinimizeOnMinimize").toBool()) {
-        hideToTray();
-        event->accept();
-        return;
-      }
-    }
-  }
-
 #ifdef Q_WS_WIN
-  else if(event->type() == QEvent::ActivationChange)
+  if(event->type() == QEvent::ActivationChange)
     dwTickCount = GetTickCount();  // needed for toggleMinimizedToTray()
 #endif
 
-  event->ignore();
+  QMainWindow::changeEvent(event);
 }
 
 void MainWin::hideToTray() {
@@ -820,6 +871,7 @@ void MainWin::hideToTray() {
   }
   hide();
   systemTray()->setIconVisible();
+  _isHidden = true;
 }
 
 void MainWin::toggleMinimizedToTray() {
@@ -856,10 +908,12 @@ void MainWin::forceActivated() {
     setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
   }
 
-  move(frameGeometry().topLeft()); // avoid placement policies
+  // this does not actually work on all platforms... and causes more evil than good
+  // move(frameGeometry().topLeft()); // avoid placement policies
   show();
   raise();
   activateWindow();
+  _isHidden = false;
 }
 
 void MainWin::messagesInserted(const QModelIndex &parent, int start, int end) {
@@ -965,6 +1019,15 @@ void MainWin::connectOrDisconnectFromNet() {
   else net->requestDisconnect();
 }
 
+void MainWin::on_jumpHotBuffer_triggered() {
+  if(!_bufferHotList->rowCount())
+    return;
+
+  QModelIndex topIndex = _bufferHotList->index(0, 0);
+  BufferId bufferId = _bufferHotList->data(topIndex, NetworkModel::BufferIdRole).value<BufferId>();
+  Client::bufferModel()->switchToBuffer(bufferId);
+}
+
 void MainWin::on_actionDebugNetworkModel_triggered() {
   QTreeView *view = new QTreeView;
   view->setAttribute(Qt::WA_DeleteOnClose);
@@ -977,20 +1040,19 @@ void MainWin::on_actionDebugNetworkModel_triggered() {
   view->show();
 }
 
-void MainWin::on_actionDebugBufferViewOverlay_triggered() {
+void MainWin::on_actionDebugHotList_triggered() {
   QTreeView *view = new QTreeView;
   view->setAttribute(Qt::WA_DeleteOnClose);
-  view->setWindowTitle("Debug BufferViewOverlay View");
-  BufferViewOverlayFilter *filter = new BufferViewOverlayFilter(Client::bufferModel(), Client::bufferViewOverlay());
-  filter->setParent(view);
-  view->setModel(filter);
-  view->setColumnWidth(0, 250);
-  view->setColumnWidth(1, 250);
-  view->setColumnWidth(2, 80);
-  view->resize(610, 300);
+  view->setModel(_bufferHotList);
   view->show();
 }
 
+void MainWin::on_actionDebugBufferViewOverlay_triggered() {
+  DebugBufferViewOverlay *overlay = new DebugBufferViewOverlay(0);
+  overlay->setAttribute(Qt::WA_DeleteOnClose);
+  overlay->show();
+}
+
 void MainWin::on_actionDebugMessageModel_triggered() {
   QTableView *view = new QTableView(0);
   DebugMessageModelFilter *filter = new DebugMessageModelFilter(view);
@@ -1007,22 +1069,6 @@ void MainWin::on_actionDebugLog_triggered() {
   logWidget->show();
 }
 
-void MainWin::saveStateToSession(const QString &sessionId) {
-  return;
-  SessionSettings s(sessionId);
-
-  s.setValue("MainWinSize", size());
-  s.setValue("MainWinPos", pos());
-  s.setValue("MainWinState", saveState());
-}
-
-void MainWin::saveStateToSessionSettings(SessionSettings & s)
-{
-  s.setValue("MainWinSize", size());
-  s.setValue("MainWinPos", pos());
-  s.setValue("MainWinState", saveState());
-}
-
 void MainWin::showStatusBarMessage(const QString &message) {
   statusBar()->showMessage(message, 10000);
 }