1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "window.h"
|
---|
45 |
|
---|
46 | //! [0]
|
---|
47 | Window::Window()
|
---|
48 | {
|
---|
49 | createIconGroupBox();
|
---|
50 | createMessageGroupBox();
|
---|
51 |
|
---|
52 | iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
|
---|
53 |
|
---|
54 | createActions();
|
---|
55 | createTrayIcon();
|
---|
56 |
|
---|
57 | connect(showMessageButton, SIGNAL(clicked()), this, SLOT(showMessage()));
|
---|
58 | connect(showIconCheckBox, SIGNAL(toggled(bool)),
|
---|
59 | trayIcon, SLOT(setVisible(bool)));
|
---|
60 | connect(iconComboBox, SIGNAL(currentIndexChanged(int)),
|
---|
61 | this, SLOT(setIcon(int)));
|
---|
62 | connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(messageClicked()));
|
---|
63 | connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
---|
64 | this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
|
---|
65 |
|
---|
66 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
67 | mainLayout->addWidget(iconGroupBox);
|
---|
68 | mainLayout->addWidget(messageGroupBox);
|
---|
69 | setLayout(mainLayout);
|
---|
70 |
|
---|
71 | iconComboBox->setCurrentIndex(1);
|
---|
72 | trayIcon->show();
|
---|
73 |
|
---|
74 | setWindowTitle(tr("Systray"));
|
---|
75 | resize(400, 300);
|
---|
76 | }
|
---|
77 | //! [0]
|
---|
78 |
|
---|
79 | //! [1]
|
---|
80 | void Window::setVisible(bool visible)
|
---|
81 | {
|
---|
82 | minimizeAction->setEnabled(visible);
|
---|
83 | maximizeAction->setEnabled(!isMaximized());
|
---|
84 | restoreAction->setEnabled(isMaximized() || !visible);
|
---|
85 | QDialog::setVisible(visible);
|
---|
86 | }
|
---|
87 | //! [1]
|
---|
88 |
|
---|
89 | //! [2]
|
---|
90 | void Window::closeEvent(QCloseEvent *event)
|
---|
91 | {
|
---|
92 | if (trayIcon->isVisible()) {
|
---|
93 | QMessageBox::information(this, tr("Systray"),
|
---|
94 | tr("The program will keep running in the "
|
---|
95 | "system tray. To terminate the program, "
|
---|
96 | "choose <b>Quit</b> in the context menu "
|
---|
97 | "of the system tray entry."));
|
---|
98 | hide();
|
---|
99 | event->ignore();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | //! [2]
|
---|
103 |
|
---|
104 | //! [3]
|
---|
105 | void Window::setIcon(int index)
|
---|
106 | {
|
---|
107 | QIcon icon = iconComboBox->itemIcon(index);
|
---|
108 | trayIcon->setIcon(icon);
|
---|
109 | setWindowIcon(icon);
|
---|
110 |
|
---|
111 | trayIcon->setToolTip(iconComboBox->itemText(index));
|
---|
112 | }
|
---|
113 | //! [3]
|
---|
114 |
|
---|
115 | //! [4]
|
---|
116 | void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
|
---|
117 | {
|
---|
118 | switch (reason) {
|
---|
119 | case QSystemTrayIcon::Trigger:
|
---|
120 | case QSystemTrayIcon::DoubleClick:
|
---|
121 | iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1)
|
---|
122 | % iconComboBox->count());
|
---|
123 | break;
|
---|
124 | case QSystemTrayIcon::MiddleClick:
|
---|
125 | showMessage();
|
---|
126 | break;
|
---|
127 | default:
|
---|
128 | ;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | //! [4]
|
---|
132 |
|
---|
133 | //! [5]
|
---|
134 | void Window::showMessage()
|
---|
135 | {
|
---|
136 | QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(
|
---|
137 | typeComboBox->itemData(typeComboBox->currentIndex()).toInt());
|
---|
138 | trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
|
---|
139 | durationSpinBox->value() * 1000);
|
---|
140 | }
|
---|
141 | //! [5]
|
---|
142 |
|
---|
143 | //! [6]
|
---|
144 | void Window::messageClicked()
|
---|
145 | {
|
---|
146 | QMessageBox::information(0, tr("Systray"),
|
---|
147 | tr("Sorry, I already gave what help I could.\n"
|
---|
148 | "Maybe you should try asking a human?"));
|
---|
149 | }
|
---|
150 | //! [6]
|
---|
151 |
|
---|
152 | void Window::createIconGroupBox()
|
---|
153 | {
|
---|
154 | iconGroupBox = new QGroupBox(tr("Tray Icon"));
|
---|
155 |
|
---|
156 | iconLabel = new QLabel("Icon:");
|
---|
157 |
|
---|
158 | iconComboBox = new QComboBox;
|
---|
159 | iconComboBox->addItem(QIcon(":/images/bad.svg"), tr("Bad"));
|
---|
160 | iconComboBox->addItem(QIcon(":/images/heart.svg"), tr("Heart"));
|
---|
161 | iconComboBox->addItem(QIcon(":/images/trash.svg"), tr("Trash"));
|
---|
162 |
|
---|
163 | {
|
---|
164 | QIcon test;
|
---|
165 | test.addFile(":/images/test16.xpm");
|
---|
166 | test.addFile(":/images/test32.xpm");
|
---|
167 | iconComboBox->addItem(test, tr("Test"));
|
---|
168 | }
|
---|
169 |
|
---|
170 | showIconCheckBox = new QCheckBox(tr("Show icon"));
|
---|
171 | showIconCheckBox->setChecked(true);
|
---|
172 |
|
---|
173 | QHBoxLayout *iconLayout = new QHBoxLayout;
|
---|
174 | iconLayout->addWidget(iconLabel);
|
---|
175 | iconLayout->addWidget(iconComboBox);
|
---|
176 | iconLayout->addStretch();
|
---|
177 | iconLayout->addWidget(showIconCheckBox);
|
---|
178 | iconGroupBox->setLayout(iconLayout);
|
---|
179 | }
|
---|
180 |
|
---|
181 | void Window::createMessageGroupBox()
|
---|
182 | {
|
---|
183 | messageGroupBox = new QGroupBox(tr("Balloon Message"));
|
---|
184 |
|
---|
185 | typeLabel = new QLabel(tr("Type:"));
|
---|
186 |
|
---|
187 | typeComboBox = new QComboBox;
|
---|
188 | typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
|
---|
189 | typeComboBox->addItem(style()->standardIcon(
|
---|
190 | QStyle::SP_MessageBoxInformation), tr("Information"),
|
---|
191 | QSystemTrayIcon::Information);
|
---|
192 | typeComboBox->addItem(style()->standardIcon(
|
---|
193 | QStyle::SP_MessageBoxWarning), tr("Warning"),
|
---|
194 | QSystemTrayIcon::Warning);
|
---|
195 | typeComboBox->addItem(style()->standardIcon(
|
---|
196 | QStyle::SP_MessageBoxCritical), tr("Critical"),
|
---|
197 | QSystemTrayIcon::Critical);
|
---|
198 | typeComboBox->setCurrentIndex(1);
|
---|
199 |
|
---|
200 | durationLabel = new QLabel(tr("Duration:"));
|
---|
201 |
|
---|
202 | durationSpinBox = new QSpinBox;
|
---|
203 | durationSpinBox->setRange(5, 60);
|
---|
204 | durationSpinBox->setSuffix(" s");
|
---|
205 | durationSpinBox->setValue(15);
|
---|
206 |
|
---|
207 | durationWarningLabel = new QLabel(tr("(some systems might ignore this "
|
---|
208 | "hint)"));
|
---|
209 | durationWarningLabel->setIndent(10);
|
---|
210 |
|
---|
211 | titleLabel = new QLabel(tr("Title:"));
|
---|
212 |
|
---|
213 | titleEdit = new QLineEdit(tr("Cannot connect to network"));
|
---|
214 |
|
---|
215 | bodyLabel = new QLabel(tr("Body:"));
|
---|
216 |
|
---|
217 | bodyEdit = new QTextEdit;
|
---|
218 | bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
|
---|
219 | "clue.\nClick this balloon for details."));
|
---|
220 |
|
---|
221 | showMessageButton = new QPushButton(tr("Show Message"));
|
---|
222 | showMessageButton->setDefault(true);
|
---|
223 |
|
---|
224 | QGridLayout *messageLayout = new QGridLayout;
|
---|
225 | messageLayout->addWidget(typeLabel, 0, 0);
|
---|
226 | messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
|
---|
227 | messageLayout->addWidget(durationLabel, 1, 0);
|
---|
228 | messageLayout->addWidget(durationSpinBox, 1, 1);
|
---|
229 | messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
|
---|
230 | messageLayout->addWidget(titleLabel, 2, 0);
|
---|
231 | messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
|
---|
232 | messageLayout->addWidget(bodyLabel, 3, 0);
|
---|
233 | messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
|
---|
234 | messageLayout->addWidget(showMessageButton, 5, 4);
|
---|
235 | messageLayout->setColumnStretch(3, 1);
|
---|
236 | messageLayout->setRowStretch(4, 1);
|
---|
237 | messageGroupBox->setLayout(messageLayout);
|
---|
238 | }
|
---|
239 |
|
---|
240 | void Window::createActions()
|
---|
241 | {
|
---|
242 | minimizeAction = new QAction(tr("Mi&nimize"), this);
|
---|
243 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
|
---|
244 |
|
---|
245 | maximizeAction = new QAction(tr("Ma&ximize"), this);
|
---|
246 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
|
---|
247 |
|
---|
248 | restoreAction = new QAction(tr("&Restore"), this);
|
---|
249 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
|
---|
250 |
|
---|
251 | quitAction = new QAction(tr("&Quit"), this);
|
---|
252 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
---|
253 | }
|
---|
254 |
|
---|
255 | void Window::createTrayIcon()
|
---|
256 | {
|
---|
257 | trayIconMenu = new QMenu(this);
|
---|
258 | trayIconMenu->addAction(minimizeAction);
|
---|
259 | trayIconMenu->addAction(maximizeAction);
|
---|
260 | trayIconMenu->addAction(restoreAction);
|
---|
261 | trayIconMenu->addSeparator();
|
---|
262 | trayIconMenu->addAction(quitAction);
|
---|
263 |
|
---|
264 | trayIcon = new QSystemTrayIcon(this);
|
---|
265 | trayIcon->setContextMenu(trayIconMenu);
|
---|
266 | }
|
---|