trayicon.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*****************************************************************************
  2. * trayicon.cpp - QStarDict, a StarDict clone written with using Qt *
  3. * Copyright (C) 2008 Alexander Rodin *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  18. *****************************************************************************/
  19. #include "trayicon.h"
  20. #include <QClipboard>
  21. #include <QMenu>
  22. #include <QSettings>
  23. #include "application.h"
  24. #include "mainwindow.h"
  25. #include "popupwindow.h"
  26. #include "settingsdialog.h"
  27. namespace QStarDict
  28. {
  29. TrayIcon::TrayIcon(QObject *parent)
  30. : QSystemTrayIcon(parent)
  31. {
  32. QMenu *trayMenu = new QMenu(tr("QStarDict"));
  33. QAction *actionScan = new QAction(QIcon(":/icons/edit-select.png"), tr("&Scan"), this);
  34. actionScan->setCheckable(true);
  35. actionScan->setChecked(Application::instance()->popupWindow()->isScan());
  36. setScanEnabled(Application::instance()->popupWindow()->isScan());
  37. connect(actionScan, SIGNAL(toggled(bool)),
  38. Application::instance()->popupWindow(), SLOT(setScan(bool)));
  39. connect(Application::instance()->popupWindow(), SIGNAL(scanChanged(bool)),
  40. actionScan, SLOT(setChecked(bool)));
  41. connect(Application::instance()->popupWindow(), SIGNAL(scanChanged(bool)), SLOT(setScanEnabled(bool)));
  42. trayMenu->addAction(actionScan);
  43. QAction *actionSettings = new QAction(QIcon(":/icons/configure.png"), tr("&Configure QStarDict"), this);
  44. connect(actionSettings, SIGNAL(triggered()), SLOT(on_actionSettings_triggered()));
  45. trayMenu->addAction(actionSettings);
  46. QAction *actionQuit = new QAction(QIcon(":/icons/application-exit.png"), tr("&Quit"), this);
  47. connect(actionQuit, SIGNAL(triggered()), Application::instance(), SLOT(quit()));
  48. trayMenu->addAction(actionQuit);
  49. setContextMenu(trayMenu);
  50. connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
  51. SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
  52. loadSettings();
  53. }
  54. TrayIcon::~TrayIcon()
  55. {
  56. saveSettings();
  57. }
  58. void TrayIcon::on_activated(QSystemTrayIcon::ActivationReason reason)
  59. {
  60. switch (reason)
  61. {
  62. case QSystemTrayIcon::Trigger:
  63. // It's quite uncomfortable on OS X to handle show/hide main window
  64. // in all cases... at least for me (petr)
  65. #ifndef Q_WS_MAC
  66. Application::instance()->mainWindow()->setVisible(!
  67. Application::instance()->mainWindow()->isVisible());
  68. #else
  69. Application::instance()->mainWindow()->show();
  70. #endif
  71. break;
  72. case QSystemTrayIcon::MiddleClick:
  73. Application::instance()->popupWindow()->showTranslation(Application::clipboard()->text(QClipboard::Selection));
  74. break;
  75. default:
  76. ; // nothing
  77. }
  78. }
  79. void TrayIcon::on_actionSettings_triggered()
  80. {
  81. SettingsDialog dialog(Application::instance()->mainWindow());
  82. dialog.exec();
  83. }
  84. void TrayIcon::setScanEnabled(bool enabled)
  85. {
  86. QIcon icon(enabled ? ":/icons/qstardict.png" : ":/icons/qstardict-disabled.png");
  87. setIcon(icon);
  88. setToolTip(tr("QStarDict: scanning is %1").arg(enabled ? tr("enabled") : tr("disabled")));
  89. }
  90. void TrayIcon::saveSettings()
  91. {
  92. QSettings config;
  93. config.setValue("TrayIcon/visible", isVisible());
  94. }
  95. void TrayIcon::loadSettings()
  96. {
  97. QSettings config;
  98. setVisible(config.value("TrayIcon/visible", true).toBool());
  99. }
  100. }
  101. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc