source: trunk/examples/desktop/systray/window.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.6 KB
Line 
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]
47Window::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]
80void 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]
90void 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]
105void 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]
116void 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]
134void 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]
144void 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
152void 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 showIconCheckBox = new QCheckBox(tr("Show icon"));
164 showIconCheckBox->setChecked(true);
165
166 QHBoxLayout *iconLayout = new QHBoxLayout;
167 iconLayout->addWidget(iconLabel);
168 iconLayout->addWidget(iconComboBox);
169 iconLayout->addStretch();
170 iconLayout->addWidget(showIconCheckBox);
171 iconGroupBox->setLayout(iconLayout);
172}
173
174void Window::createMessageGroupBox()
175{
176 messageGroupBox = new QGroupBox(tr("Balloon Message"));
177
178 typeLabel = new QLabel(tr("Type:"));
179
180 typeComboBox = new QComboBox;
181 typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
182 typeComboBox->addItem(style()->standardIcon(
183 QStyle::SP_MessageBoxInformation), tr("Information"),
184 QSystemTrayIcon::Information);
185 typeComboBox->addItem(style()->standardIcon(
186 QStyle::SP_MessageBoxWarning), tr("Warning"),
187 QSystemTrayIcon::Warning);
188 typeComboBox->addItem(style()->standardIcon(
189 QStyle::SP_MessageBoxCritical), tr("Critical"),
190 QSystemTrayIcon::Critical);
191 typeComboBox->setCurrentIndex(1);
192
193 durationLabel = new QLabel(tr("Duration:"));
194
195 durationSpinBox = new QSpinBox;
196 durationSpinBox->setRange(5, 60);
197 durationSpinBox->setSuffix(" s");
198 durationSpinBox->setValue(15);
199
200 durationWarningLabel = new QLabel(tr("(some systems might ignore this "
201 "hint)"));
202 durationWarningLabel->setIndent(10);
203
204 titleLabel = new QLabel(tr("Title:"));
205
206 titleEdit = new QLineEdit(tr("Cannot connect to network"));
207
208 bodyLabel = new QLabel(tr("Body:"));
209
210 bodyEdit = new QTextEdit;
211 bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
212 "clue.\nClick this balloon for details."));
213
214 showMessageButton = new QPushButton(tr("Show Message"));
215 showMessageButton->setDefault(true);
216
217 QGridLayout *messageLayout = new QGridLayout;
218 messageLayout->addWidget(typeLabel, 0, 0);
219 messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
220 messageLayout->addWidget(durationLabel, 1, 0);
221 messageLayout->addWidget(durationSpinBox, 1, 1);
222 messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
223 messageLayout->addWidget(titleLabel, 2, 0);
224 messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
225 messageLayout->addWidget(bodyLabel, 3, 0);
226 messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
227 messageLayout->addWidget(showMessageButton, 5, 4);
228 messageLayout->setColumnStretch(3, 1);
229 messageLayout->setRowStretch(4, 1);
230 messageGroupBox->setLayout(messageLayout);
231}
232
233void Window::createActions()
234{
235 minimizeAction = new QAction(tr("Mi&nimize"), this);
236 connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
237
238 maximizeAction = new QAction(tr("Ma&ximize"), this);
239 connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
240
241 restoreAction = new QAction(tr("&Restore"), this);
242 connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
243
244 quitAction = new QAction(tr("&Quit"), this);
245 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
246}
247
248void Window::createTrayIcon()
249{
250 trayIconMenu = new QMenu(this);
251 trayIconMenu->addAction(minimizeAction);
252 trayIconMenu->addAction(maximizeAction);
253 trayIconMenu->addAction(restoreAction);
254 trayIconMenu->addSeparator();
255 trayIconMenu->addAction(quitAction);
256
257 trayIcon = new QSystemTrayIcon(this);
258 trayIcon->setContextMenu(trayIconMenu);
259}
Note: See TracBrowser for help on using the repository browser.