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