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(sender: showMessageButton, signal: &QAbstractButton::clicked, receiver: this, slot: &Window::showMessage);
82 connect(sender: showIconCheckBox, signal: &QAbstractButton::toggled, receiver: trayIcon, slot: &QSystemTrayIcon::setVisible);
83 connect(sender: iconComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged),
84 receiver: this, slot: &Window::setIcon);
85 connect(sender: trayIcon, signal: &QSystemTrayIcon::messageClicked, receiver: this, slot: &Window::messageClicked);
86 connect(sender: trayIcon, signal: &QSystemTrayIcon::activated, receiver: this, slot: &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(s: "Systray"));
97 resize(w: 400, h: 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(parent: this, title: tr(s: "Systray"),
121 text: tr(s: "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(index: typeComboBox->currentIndex()).toInt();
164 QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon(selectedIcon);
165
166 if (selectedIcon == -1) { // custom icon
167 QIcon icon(iconComboBox->itemIcon(index: iconComboBox->currentIndex()));
168 trayIcon->showMessage(title: titleEdit->text(), msg: bodyEdit->toPlainText(), icon,
169 msecs: durationSpinBox->value() * 1000);
170 } else {
171 trayIcon->showMessage(title: titleEdit->text(), msg: bodyEdit->toPlainText(), icon: msgIcon,
172 msecs: durationSpinBox->value() * 1000);
173 }
174}
175//! [5]
176
177//! [6]
178void Window::messageClicked()
179{
180 QMessageBox::information(parent: nullptr, title: tr(s: "Systray"),
181 text: tr(s: "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(s: "Tray Icon"));
189
190 iconLabel = new QLabel("Icon:");
191
192 iconComboBox = new QComboBox;
193 iconComboBox->addItem(aicon: QIcon(":/images/bad.png"), atext: tr(s: "Bad"));
194 iconComboBox->addItem(aicon: QIcon(":/images/heart.png"), atext: tr(s: "Heart"));
195 iconComboBox->addItem(aicon: QIcon(":/images/trash.png"), atext: tr(s: "Trash"));
196
197 showIconCheckBox = new QCheckBox(tr(s: "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(s: "Balloon Message"));
211
212 typeLabel = new QLabel(tr(s: "Type:"));
213
214 typeComboBox = new QComboBox;
215 typeComboBox->addItem(atext: tr(s: "None"), auserData: QSystemTrayIcon::NoIcon);
216 typeComboBox->addItem(aicon: style()->standardIcon(
217 standardIcon: QStyle::SP_MessageBoxInformation), atext: tr(s: "Information"),
218 auserData: QSystemTrayIcon::Information);
219 typeComboBox->addItem(aicon: style()->standardIcon(
220 standardIcon: QStyle::SP_MessageBoxWarning), atext: tr(s: "Warning"),
221 auserData: QSystemTrayIcon::Warning);
222 typeComboBox->addItem(aicon: style()->standardIcon(
223 standardIcon: QStyle::SP_MessageBoxCritical), atext: tr(s: "Critical"),
224 auserData: QSystemTrayIcon::Critical);
225 typeComboBox->addItem(aicon: QIcon(), atext: tr(s: "Custom icon"),
226 auserData: -1);
227 typeComboBox->setCurrentIndex(1);
228
229 durationLabel = new QLabel(tr(s: "Duration:"));
230
231 durationSpinBox = new QSpinBox;
232 durationSpinBox->setRange(min: 5, max: 60);
233 durationSpinBox->setSuffix(" s");
234 durationSpinBox->setValue(15);
235
236 durationWarningLabel = new QLabel(tr(s: "(some systems might ignore this "
237 "hint)"));
238 durationWarningLabel->setIndent(10);
239
240 titleLabel = new QLabel(tr(s: "Title:"));
241
242 titleEdit = new QLineEdit(tr(s: "Cannot connect to network"));
243
244 bodyLabel = new QLabel(tr(s: "Body:"));
245
246 bodyEdit = new QTextEdit;
247 bodyEdit->setPlainText(tr(s: "Don't believe me. Honestly, I don't have a "
248 "clue.\nClick this balloon for details."));
249
250 showMessageButton = new QPushButton(tr(s: "Show Message"));
251 showMessageButton->setDefault(true);
252
253 QGridLayout *messageLayout = new QGridLayout;
254 messageLayout->addWidget(typeLabel, row: 0, column: 0);
255 messageLayout->addWidget(typeComboBox, row: 0, column: 1, rowSpan: 1, columnSpan: 2);
256 messageLayout->addWidget(durationLabel, row: 1, column: 0);
257 messageLayout->addWidget(durationSpinBox, row: 1, column: 1);
258 messageLayout->addWidget(durationWarningLabel, row: 1, column: 2, rowSpan: 1, columnSpan: 3);
259 messageLayout->addWidget(titleLabel, row: 2, column: 0);
260 messageLayout->addWidget(titleEdit, row: 2, column: 1, rowSpan: 1, columnSpan: 4);
261 messageLayout->addWidget(bodyLabel, row: 3, column: 0);
262 messageLayout->addWidget(bodyEdit, row: 3, column: 1, rowSpan: 2, columnSpan: 4);
263 messageLayout->addWidget(showMessageButton, row: 5, column: 4);
264 messageLayout->setColumnStretch(column: 3, stretch: 1);
265 messageLayout->setRowStretch(row: 4, stretch: 1);
266 messageGroupBox->setLayout(messageLayout);
267}
268
269void Window::createActions()
270{
271 minimizeAction = new QAction(tr(s: "Mi&nimize"), this);
272 connect(sender: minimizeAction, signal: &QAction::triggered, receiver: this, slot: &QWidget::hide);
273
274 maximizeAction = new QAction(tr(s: "Ma&ximize"), this);
275 connect(sender: maximizeAction, signal: &QAction::triggered, receiver: this, slot: &QWidget::showMaximized);
276
277 restoreAction = new QAction(tr(s: "&Restore"), this);
278 connect(sender: restoreAction, signal: &QAction::triggered, receiver: this, slot: &QWidget::showNormal);
279
280 quitAction = new QAction(tr(s: "&Quit"), this);
281 connect(sender: quitAction, signal: &QAction::triggered, qApp, slot: &QCoreApplication::quit);
282}
283
284void Window::createTrayIcon()
285{
286 trayIconMenu = new QMenu(this);
287 trayIconMenu->addAction(action: minimizeAction);
288 trayIconMenu->addAction(action: maximizeAction);
289 trayIconMenu->addAction(action: restoreAction);
290 trayIconMenu->addSeparator();
291 trayIconMenu->addAction(action: quitAction);
292
293 trayIcon = new QSystemTrayIcon(this);
294 trayIcon->setContextMenu(trayIconMenu);
295}
296
297#endif
298

source code of qtbase/examples/widgets/desktop/systray/window.cpp