init
[qstardict] / qstardict / trayicon.cpp
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 #ifndef MAEMO
20
21 #include "trayicon.h"
22
23 #include <QClipboard>
24 #include <QMenu>
25 #include <QSettings>
26
27 #include "application.h"
28 #include "mainwindow.h"
29 #include "popupwindow.h"
30 #include "settingsdialog.h"
31
32 namespace QStarDict
33 {
34
35 TrayIcon::TrayIcon(QObject *parent)
36     : QSystemTrayIcon(parent)
37 {
38     QMenu *trayMenu = new QMenu(tr("QStarDict"));
39     QAction *actionScan = new QAction(tr("&Scan"), this);
40     actionScan->setCheckable(true);
41     actionScan->setChecked(Application::instance()->popupWindow()->isScan());
42     setScanEnabled(Application::instance()->popupWindow()->isScan());
43     connect(actionScan, SIGNAL(toggled(bool)),
44             Application::instance()->popupWindow(), SLOT(setScan(bool)));
45     connect(Application::instance()->popupWindow(), SIGNAL(scanChanged(bool)),
46             actionScan, SLOT(setChecked(bool)));
47     connect(Application::instance()->popupWindow(), SIGNAL(scanChanged(bool)), SLOT(setScanEnabled(bool)));
48     trayMenu->addAction(actionScan);
49     QAction *actionSettings = new QAction(QIcon(":/icons/configure.png"), tr("&Configure QStarDict"), this);
50     connect(actionSettings, SIGNAL(triggered()), SLOT(on_actionSettings_triggered()));
51     trayMenu->addAction(actionSettings);
52     QAction *actionQuit = new QAction(QIcon(":/icons/application-exit.png"), tr("&Quit"), this);
53     connect(actionQuit, SIGNAL(triggered()), Application::instance(), SLOT(quit()));
54     trayMenu->addAction(actionQuit);
55     setContextMenu(trayMenu);
56     connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
57             SLOT(on_activated(QSystemTrayIcon::ActivationReason)));
58
59     loadSettings();
60 }
61
62 TrayIcon::~TrayIcon()
63 {
64     saveSettings();
65 }
66
67 void TrayIcon::on_activated(QSystemTrayIcon::ActivationReason reason)
68 {
69     switch (reason)
70     {
71         case QSystemTrayIcon::Trigger:
72             Application::instance()->mainWindow()->setVisible(!
73                     Application::instance()->mainWindow()->isVisible());
74             break;
75         case QSystemTrayIcon::MiddleClick:
76             Application::instance()->popupWindow()->showTranslation(Application::clipboard()->text(QClipboard::Selection));
77             break;
78         default:
79             ; // nothing
80     }
81 }
82
83 void TrayIcon::on_actionSettings_triggered()
84 {
85     SettingsDialog dialog(Application::instance()->mainWindow());
86     dialog.exec();
87 }
88
89 void TrayIcon::setScanEnabled(bool enabled)
90 {
91     QIcon icon(enabled ? ":/icons/qstardict.png" : ":/icons/qstardict-disabled.png");
92     setIcon(icon);
93     setToolTip(tr("QStarDict: scanning is %1").arg(enabled ? tr("enabled") : tr("disabled")));
94 }
95
96 void TrayIcon::saveSettings()
97 {
98     QSettings config;
99     config.setValue("TrayIcon/visible", isVisible());
100 }
101
102 void TrayIcon::loadSettings()
103 {
104     QSettings config;
105     setVisible(config.value("TrayIcon/visible", true).toBool());
106 }
107
108 }
109
110 #endif // MAEMO
111
112 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
113