Merge pull request #853 from laanwj/2012_02_altminimizetray
authorWladimir J. van der Laan <laanwj@gmail.com>
Mon, 27 Feb 2012 06:27:25 +0000 (22:27 -0800)
committerWladimir J. van der Laan <laanwj@gmail.com>
Mon, 27 Feb 2012 06:27:25 +0000 (22:27 -0800)
Yet another alternative "minimize to tray" implementation

Fixes problems with window positioning.

1  2 
src/qt/bitcoingui.cpp
src/qt/bitcoingui.h

diff --combined src/qt/bitcoingui.cpp
@@@ -46,8 -46,6 +46,8 @@@
  #include <QStackedWidget>
  #include <QDateTime>
  #include <QMovie>
 +#include <QFileDialog>
 +#include <QDesktopServices>
  
  #include <QDragEnterEvent>
  #include <QUrl>
@@@ -58,7 -56,6 +58,6 @@@ BitcoinGUI::BitcoinGUI(QWidget *parent)
      QMainWindow(parent),
      clientModel(0),
      walletModel(0),
-     dummyWidget(0),
      encryptWalletAction(0),
      changePassphraseAction(0),
      aboutQtAction(0),
@@@ -88,9 -85,6 +87,6 @@@
      // Create the tray icon (or setup the dock icon)
      createTrayIcon();
  
-     // Dummy widget used when restoring window state after minimization
-     dummyWidget = new QWidget();
      // Create tabs
      overviewPage = new OverviewPage();
  
  
  BitcoinGUI::~BitcoinGUI()
  {
 +    if(trayIcon) // Hide tray icon, as deleting will let it linger until quit (on Ubuntu)
 +        trayIcon->hide();
  #ifdef Q_WS_MAC
      delete appMenuBar;
  #endif
-     delete dummyWidget;
  }
  
  void BitcoinGUI::createActions()
  #endif
      tabGroup->addAction(messageAction);
  
-     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(overviewAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
-     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(historyAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
-     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
-     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
-     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
-     connect(messageAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(messageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(messageAction, SIGNAL(triggered()), this, SLOT(gotoMessagePage()));
  
      quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
      encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
      encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
      encryptWalletAction->setCheckable(true);
 +    backupWalletAction = new QAction(QIcon(":/icons/filesave"), tr("&Backup Wallet"), this);
 +    backupWalletAction->setToolTip(tr("Backup wallet to another location"));
      changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
      changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
  
      connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
      connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
      connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
-     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
+     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
      connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
 +    connect(backupWalletAction, SIGNAL(triggered()), this, SLOT(backupWallet()));
      connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
  }
  
@@@ -274,12 -262,11 +269,12 @@@ void BitcoinGUI::createMenuBar(
  
      // Configure the menus
      QMenu *file = appMenuBar->addMenu(tr("&File"));
 +    file->addAction(backupWalletAction);
      file->addAction(exportAction);
  #ifndef FIRST_CLASS_MESSAGING
      file->addAction(messageAction);
 -    file->addSeparator();
  #endif
 +    file->addSeparator();
      file->addAction(quitAction);
  
      QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
@@@ -422,14 -409,6 +417,6 @@@ void BitcoinGUI::trayIconActivated(QSys
  }
  #endif
  
- void BitcoinGUI::showNormal()
- {
-     // Reparent window to the desktop (in case it was hidden on minimize)
-     if(parent() != NULL)
-         setParent(NULL, Qt::Window);
-     QMainWindow::showNormal();
- }
  void BitcoinGUI::optionsClicked()
  {
      if(!clientModel || !clientModel->getOptionsModel())
@@@ -505,11 -484,7 +492,11 @@@ void BitcoinGUI::setNumBlocks(int count
      QString text;
  
      // Represent time from last generated block in human readable text
 -    if(secs < 60)
 +    if(secs <= 0)
 +    {
 +        // Fully up to date. Leave text empty.
 +    }
 +    else if(secs < 60)
      {
          text = tr("%n second(s) ago","",secs);
      }
      // Set icon state: spinning if catching up, tick otherwise
      if(secs < 30*60)
      {
 -        tooltip = tr("Up to date") + QString("\n") + tooltip;
 +        tooltip = tr("Up to date") + QString(".\n") + tooltip;
          labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
      }
      else
          syncIconMovie->start();
      }
  
 -    tooltip += QString("\n");
 -    tooltip += tr("Last received block was generated %1.").arg(text);
 +    if(!text.isEmpty())
 +    {
 +        tooltip += QString("\n");
 +        tooltip += tr("Last received block was generated %1.").arg(text);
 +    }
  
      labelBlocksIcon->setToolTip(tooltip);
      progressBarLabel->setToolTip(tooltip);
@@@ -576,15 -548,19 +563,19 @@@ void BitcoinGUI::changeEvent(QEvent *e
      {
          if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
          {
-             if(isMinimized())
+             QWindowStateChangeEvent *wsevt = static_cast<QWindowStateChangeEvent*>(e);
+             bool wasMinimized = wsevt->oldState() & Qt::WindowMinimized;
+             bool isMinimized = windowState() & Qt::WindowMinimized;
+             if(!wasMinimized && isMinimized)
              {
-                 // Hiding the window from taskbar
-                 setParent(dummyWidget, Qt::SubWindow);
+                 // Minimized, hide the window from taskbar
+                 setWindowFlags(windowFlags() | Qt::Tool);
                  return;
              }
-             else
+             else if(wasMinimized && !isMinimized)
              {
-                 showNormal();
+                 // Unminimized, show the window in taskbar
+                 setWindowFlags(windowFlags() &~ Qt::Tool);
              }
          }
      }
@@@ -738,7 -714,7 +729,7 @@@ void BitcoinGUI::dropEvent(QDropEvent *
          QList<QUrl> urls = event->mimeData()->urls();
          foreach(const QUrl &url, urls)
          {
 -            sendCoinsPage->handleURL(&url);
 +            sendCoinsPage->handleURL(url.toString());
          }
      }
  
  void BitcoinGUI::handleURL(QString strURL)
  {
      gotoSendCoinsPage();
 -    QUrl url = QUrl(strURL);
 -    sendCoinsPage->handleURL(&url);
 +    sendCoinsPage->handleURL(strURL);
  }
  
  void BitcoinGUI::setEncryptionStatus(int status)
@@@ -792,17 -769,6 +783,17 @@@ void BitcoinGUI::encryptWallet(bool sta
      setEncryptionStatus(walletModel->getEncryptionStatus());
  }
  
 +void BitcoinGUI::backupWallet()
 +{
 +    QString saveDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
 +    QString filename = QFileDialog::getSaveFileName(this, tr("Backup Wallet"), saveDir, tr("Wallet Data (*.dat)"));
 +    if(!filename.isEmpty()) {
 +        if(!walletModel->backupWallet(filename)) {
 +            QMessageBox::warning(this, tr("Backup Failed"), tr("There was an error trying to save the wallet data to the new location."));
 +        }
 +    }
 +}
 +
  void BitcoinGUI::changePassphrase()
  {
      AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
@@@ -822,3 -788,11 +813,11 @@@ void BitcoinGUI::unlockWallet(
          dlg.exec();
      }
  }
+ void BitcoinGUI::showNormalIfMinimized()
+ {
+     if(!isVisible()) // Show, if hidden
+         show();
+     if(isMinimized()) // Unminimize, if minimized
+         showNormal();
+ }
diff --combined src/qt/bitcoingui.h
@@@ -58,8 -58,6 +58,6 @@@ private
  
      QStackedWidget *centralWidget;
  
-     QWidget *dummyWidget;
      OverviewPage *overviewPage;
      QWidget *transactionsPage;
      AddressBookPage *addressBookPage;
@@@ -86,7 -84,6 +84,7 @@@
      QAction *openBitcoinAction;
      QAction *exportAction;
      QAction *encryptWalletAction;
 +    QAction *backupWalletAction;
      QAction *changePassphraseAction;
      QAction *aboutQtAction;
  
@@@ -134,8 -131,6 +132,6 @@@ public slots
      void gotoMessagePage();
      void gotoMessagePage(QString);
  
-     void showNormal();
  private slots:
      /** Switch to overview (home) page */
      void gotoOverviewPage();
      void incomingTransaction(const QModelIndex & parent, int start, int end);
      /** Encrypt the wallet */
      void encryptWallet(bool status);
 +    /** Backup the wallet */
 +    void backupWallet();
      /** Change encrypted wallet passphrase */
      void changePassphrase();
      /** Ask for pass phrase to unlock wallet temporarily */
      void unlockWallet();
+     /** Show window if hidden, unminimize when minimized */
+     void showNormalIfMinimized();
  };
  
  #endif