1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "window.h"
52
53#ifndef QT_NO_SYSTEMTRAYICON
54
55#include <QAction>
56#include <QCheckBox>
57#include <QComboBox>
58#include <QCoreApplication>
59#include <QCloseEvent>
60#include <QGroupBox>
61#include <QLabel>
62#include <QLineEdit>
63#include <QMenu>
64#include <QPushButton>
65#include <QSpinBox>
66#include <QTextEdit>
67#include <QVBoxLayout>
68#include <QMessageBox>
69
70//! [0]
71Window::Window()
72{
73 createIconGroupBox();
74 createMessageGroupBox();
75
76 iconLabel->setMinimumWidth(durationLabel->sizeHint().width());
77
78 createActions();
79 createTrayIcon();
80
81 connect(showMessageButton, &QAbstractButton::clicked, this, &Window::showMessage);
82 connect(showIconCheckBox, &QAbstractButton::toggled, trayIcon, &QSystemTrayIcon::setVisible);
83 connect(iconComboBox, &QComboBox::currentIndexChanged,
84 this, &Window::setIcon);
85 connect(trayIcon, &QSystemTrayIcon::messageClicked, this, &Window::messageClicked);
86 connect(trayIcon, &QSystemTrayIcon::activated, this, &Window::iconActivated);
87
88 QVBoxLayout *mainLayout = new QVBoxLayout;
89 mainLayout->addWidget(iconGroupBox);
90 mainLayout->addWidget(messageGroupBox);
91 setLayout(mainLayout);
92
93 iconComboBox->setCurrentIndex(1);
94 trayIcon->show();
95
96 setWindowTitle(tr("Systray"));
97 resize(400, 300);
98}
99//! [0]
100
101//! [1]
102void Window::setVisible(bool visible)
103{
104 minimizeAction->setEnabled(visible);
105 maximizeAction->setEnabled(!isMaximized());
106 restoreAction->setEnabled(isMaximized() || !visible);
107 QDialog::setVisible(visible);
108}
109//! [1]
110
111//! [2]
112void Window::closeEvent(QCloseEvent *event)
113{
114#ifdef Q_OS_MACOS
115 if (!event->spontaneous() || !isVisible()) {
116 return;
117 }
118#endif
119 if (trayIcon->isVisible()) {
120 QMessageBox::information(this, tr("Systray"),
121 tr("The program will keep running in the "
122 "system tray. To terminate the program, "
123 "choose <b>Quit</b> in the context menu "
124 "of the system tray entry."));
125 hide();
126 event->ignore();
127 }
128}
129//! [2]
130
131//! [3]
132void Window::setIcon(int index)
133{
134 QIcon icon = iconComboBox->itemIcon(index);
135 trayIcon->setIcon(icon);
136 setWindowIcon(icon);
137
138 trayIcon->setToolTip(iconComboBox->itemText(index));
139}
140//! [3]
141
142//! [4]
143void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
144{
145 switch (reason) {
146 case QSystemTrayIcon::Trigger:
147 case QSystemTrayIcon::DoubleClick:
148 iconComboBox->setCurrentIndex((iconComboBox->currentIndex() + 1) % iconComboBox->count());
149 break;
150 case QSystemTrayIcon::MiddleClick:
151 showMessage();
152 break;
153 default:
154 ;
155 }
156}
157//! [4]
158
159//! [5]
160void Window::showMessage()
161{
162 showIconCheckBox->setChecked(true);
163 int selectedIcon = typeComboBox->itemData(typeComboBox->currentIndex()).toInt();
164 QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon);
165
166 if (selectedIcon == -1) { // custom icon
167 QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex()));
168 trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon,
169 durationSpinBox->value() * 1000);
170 } else {
171 trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon,
172 durationSpinBox->value() * 1000);
173 }
174}
175//! [5]
176
177//! [6]
178void Window::messageClicked()
179{
180 QMessageBox::information(nullptr, tr("Systray"),
181 tr("Sorry, I already gave what help I could.\n"
182 "Maybe you should try asking a human?"));
183}
184//! [6]
185
186void Window::createIconGroupBox()
187{
188 iconGroupBox = new QGroupBox(tr("Tray Icon"));
189
190 iconLabel = new QLabel("Icon:");
191
192 iconComboBox = new QComboBox;
193 iconComboBox->addItem(QIcon(":/images/bad.png"), tr("Bad"));
194 iconComboBox->addItem(QIcon(":/images/heart.png"), tr("Heart"));
195 iconComboBox->addItem(QIcon(":/images/trash.png"), tr("Trash"));
196
197 showIconCheckBox = new QCheckBox(tr("Show icon"));
198 showIconCheckBox->setChecked(true);
199
200 QHBoxLayout *iconLayout = new QHBoxLayout;
201 iconLayout->addWidget(iconLabel);
202 iconLayout->addWidget(iconComboBox);
203 iconLayout->addStretch();
204 iconLayout->addWidget(showIconCheckBox);
205 iconGroupBox->setLayout(iconLayout);
206}
207
208void Window::createMessageGroupBox()
209{
210 messageGroupBox = new QGroupBox(tr("Balloon Message"));
211
212 typeLabel = new QLabel(tr("Type:"));
213
214 typeComboBox = new QComboBox;
215 typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
216 typeComboBox->addItem(style()->standardIcon(
217 QStyle::SP_MessageBoxInformation), tr("Information"),
218 QSystemTrayIcon::Information);
219 typeComboBox->addItem(style()->standardIcon(
220 QStyle::SP_MessageBoxWarning), tr("Warning"),
221 QSystemTrayIcon::Warning);
222 typeComboBox->addItem(style()->standardIcon(
223 QStyle::SP_MessageBoxCritical), tr("Critical"),
224 QSystemTrayIcon::Critical);
225 typeComboBox->addItem(QIcon(), tr("Custom icon"),
226 -1);
227 typeComboBox->setCurrentIndex(1);
228
229 durationLabel = new QLabel(tr("Duration:"));
230
231 durationSpinBox = new QSpinBox;
232 durationSpinBox->setRange(5, 60);
233 durationSpinBox->setSuffix(" s");
234 durationSpinBox->setValue(15);
235
236 durationWarningLabel = new QLabel(tr("(some systems might ignore this "
237 "hint)"));
238 durationWarningLabel->setIndent(10);
239
240 titleLabel = new QLabel(tr("Title:"));
241
242 titleEdit = new QLineEdit(tr("Cannot connect to network"));
243
244 bodyLabel = new QLabel(tr("Body:"));
245
246 bodyEdit = new QTextEdit;
247 bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
248 "clue.\nClick this balloon for details."));
249
250 showMessageButton = new QPushButton(tr("Show Message"));
251 showMessageButton->setDefault(true);
252
253 QGridLayout *messageLayout = new QGridLayout;
254 messageLayout->addWidget(typeLabel, 0, 0);
255 messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
256 messageLayout->addWidget(durationLabel, 1, 0);
257 messageLayout->addWidget(durationSpinBox, 1, 1);
258 messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
259 messageLayout->addWidget(titleLabel, 2, 0);
260 messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
261 messageLayout->addWidget(bodyLabel, 3, 0);
262 messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
263 messageLayout->addWidget(showMessageButton, 5, 4);
264 messageLayout->setColumnStretch(3, 1);
265 messageLayout->setRowStretch(4, 1);
266 messageGroupBox->setLayout(messageLayout);
267}
268
269void Window::createActions()
270{
271 minimizeAction = new QAction(tr("Mi&nimize"), this);
272 connect(minimizeAction, &QAction::triggered, this, &QWidget::hide);
273
274 maximizeAction = new QAction(tr("Ma&ximize"), this);
275 connect(maximizeAction, &QAction::triggered, this, &QWidget::showMaximized);
276
277 restoreAction = new QAction(tr("&Restore"), this);
278 connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);
279
280 quitAction = new QAction(tr("&Quit"), this);
281 connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
282}
283
284void Window::createTrayIcon()
285{
286 trayIconMenu = new QMenu(this);
287 trayIconMenu->addAction(minimizeAction);
288 trayIconMenu->addAction(maximizeAction);
289 trayIconMenu->addAction(restoreAction);
290 trayIconMenu->addSeparator();
291 trayIconMenu->addAction(quitAction);
292
293 trayIcon = new QSystemTrayIcon(this);
294 trayIcon->setContextMenu(trayIconMenu);
295}
296
297#endif
298