| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 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 qt-sales@nokia.com.
|
|---|
| 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 16/32"));
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | {
|
|---|
| 171 | QIcon test;
|
|---|
| 172 | test.addFile(":/images/test20.xpm");
|
|---|
| 173 | test.addFile(":/images/test40.xpm");
|
|---|
| 174 | iconComboBox->addItem(test, tr("Test 20/40"));
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | showIconCheckBox = new QCheckBox(tr("Show icon"));
|
|---|
| 178 | showIconCheckBox->setChecked(true);
|
|---|
| 179 |
|
|---|
| 180 | QHBoxLayout *iconLayout = new QHBoxLayout;
|
|---|
| 181 | iconLayout->addWidget(iconLabel);
|
|---|
| 182 | iconLayout->addWidget(iconComboBox);
|
|---|
| 183 | iconLayout->addStretch();
|
|---|
| 184 | iconLayout->addWidget(showIconCheckBox);
|
|---|
| 185 | iconGroupBox->setLayout(iconLayout);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | void Window::createMessageGroupBox()
|
|---|
| 189 | {
|
|---|
| 190 | messageGroupBox = new QGroupBox(tr("Balloon Message"));
|
|---|
| 191 |
|
|---|
| 192 | typeLabel = new QLabel(tr("Type:"));
|
|---|
| 193 |
|
|---|
| 194 | typeComboBox = new QComboBox;
|
|---|
| 195 | typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
|
|---|
| 196 | typeComboBox->addItem(style()->standardIcon(
|
|---|
| 197 | QStyle::SP_MessageBoxInformation), tr("Information"),
|
|---|
| 198 | QSystemTrayIcon::Information);
|
|---|
| 199 | typeComboBox->addItem(style()->standardIcon(
|
|---|
| 200 | QStyle::SP_MessageBoxWarning), tr("Warning"),
|
|---|
| 201 | QSystemTrayIcon::Warning);
|
|---|
| 202 | typeComboBox->addItem(style()->standardIcon(
|
|---|
| 203 | QStyle::SP_MessageBoxCritical), tr("Critical"),
|
|---|
| 204 | QSystemTrayIcon::Critical);
|
|---|
| 205 | typeComboBox->setCurrentIndex(1);
|
|---|
| 206 |
|
|---|
| 207 | durationLabel = new QLabel(tr("Duration:"));
|
|---|
| 208 |
|
|---|
| 209 | durationSpinBox = new QSpinBox;
|
|---|
| 210 | durationSpinBox->setRange(5, 60);
|
|---|
| 211 | durationSpinBox->setSuffix(" s");
|
|---|
| 212 | durationSpinBox->setValue(15);
|
|---|
| 213 |
|
|---|
| 214 | durationWarningLabel = new QLabel(tr("(some systems might ignore this "
|
|---|
| 215 | "hint)"));
|
|---|
| 216 | durationWarningLabel->setIndent(10);
|
|---|
| 217 |
|
|---|
| 218 | titleLabel = new QLabel(tr("Title:"));
|
|---|
| 219 |
|
|---|
| 220 | titleEdit = new QLineEdit(tr("Cannot connect to network"));
|
|---|
| 221 |
|
|---|
| 222 | bodyLabel = new QLabel(tr("Body:"));
|
|---|
| 223 |
|
|---|
| 224 | bodyEdit = new QTextEdit;
|
|---|
| 225 | bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
|
|---|
| 226 | "clue.\nClick this balloon for details."));
|
|---|
| 227 |
|
|---|
| 228 | showMessageButton = new QPushButton(tr("Show Message"));
|
|---|
| 229 | showMessageButton->setDefault(true);
|
|---|
| 230 |
|
|---|
| 231 | QGridLayout *messageLayout = new QGridLayout;
|
|---|
| 232 | messageLayout->addWidget(typeLabel, 0, 0);
|
|---|
| 233 | messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
|
|---|
| 234 | messageLayout->addWidget(durationLabel, 1, 0);
|
|---|
| 235 | messageLayout->addWidget(durationSpinBox, 1, 1);
|
|---|
| 236 | messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
|
|---|
| 237 | messageLayout->addWidget(titleLabel, 2, 0);
|
|---|
| 238 | messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
|
|---|
| 239 | messageLayout->addWidget(bodyLabel, 3, 0);
|
|---|
| 240 | messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
|
|---|
| 241 | messageLayout->addWidget(showMessageButton, 5, 4);
|
|---|
| 242 | messageLayout->setColumnStretch(3, 1);
|
|---|
| 243 | messageLayout->setRowStretch(4, 1);
|
|---|
| 244 | messageGroupBox->setLayout(messageLayout);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | void Window::createActions()
|
|---|
| 248 | {
|
|---|
| 249 | minimizeAction = new QAction(tr("Mi&nimize"), this);
|
|---|
| 250 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
|
|---|
| 251 |
|
|---|
| 252 | maximizeAction = new QAction(tr("Ma&ximize"), this);
|
|---|
| 253 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
|
|---|
| 254 |
|
|---|
| 255 | restoreAction = new QAction(tr("&Restore"), this);
|
|---|
| 256 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
|
|---|
| 257 |
|
|---|
| 258 | quitAction = new QAction(tr("&Quit"), this);
|
|---|
| 259 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void Window::createTrayIcon()
|
|---|
| 263 | {
|
|---|
| 264 | trayIconMenu = new QMenu(this);
|
|---|
| 265 | trayIconMenu->addAction(minimizeAction);
|
|---|
| 266 | trayIconMenu->addAction(maximizeAction);
|
|---|
| 267 | trayIconMenu->addAction(restoreAction);
|
|---|
| 268 | trayIconMenu->addSeparator();
|
|---|
| 269 | trayIconMenu->addAction(quitAction);
|
|---|
| 270 |
|
|---|
| 271 | trayIcon = new QSystemTrayIcon(this);
|
|---|
| 272 | trayIcon->setContextMenu(trayIconMenu);
|
|---|
| 273 | }
|
|---|