[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 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 | showIconCheckBox = new QCheckBox(tr("Show icon"));
|
---|
| 163 | showIconCheckBox->setChecked(true);
|
---|
| 164 |
|
---|
| 165 | QHBoxLayout *iconLayout = new QHBoxLayout;
|
---|
| 166 | iconLayout->addWidget(iconLabel);
|
---|
| 167 | iconLayout->addWidget(iconComboBox);
|
---|
| 168 | iconLayout->addStretch();
|
---|
| 169 | iconLayout->addWidget(showIconCheckBox);
|
---|
| 170 | iconGroupBox->setLayout(iconLayout);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void Window::createMessageGroupBox()
|
---|
| 174 | {
|
---|
| 175 | messageGroupBox = new QGroupBox(tr("Balloon Message"));
|
---|
| 176 |
|
---|
| 177 | typeLabel = new QLabel(tr("Type:"));
|
---|
| 178 |
|
---|
| 179 | typeComboBox = new QComboBox;
|
---|
| 180 | typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
|
---|
| 181 | typeComboBox->addItem(style()->standardIcon(
|
---|
| 182 | QStyle::SP_MessageBoxInformation), tr("Information"),
|
---|
| 183 | QSystemTrayIcon::Information);
|
---|
| 184 | typeComboBox->addItem(style()->standardIcon(
|
---|
| 185 | QStyle::SP_MessageBoxWarning), tr("Warning"),
|
---|
| 186 | QSystemTrayIcon::Warning);
|
---|
| 187 | typeComboBox->addItem(style()->standardIcon(
|
---|
| 188 | QStyle::SP_MessageBoxCritical), tr("Critical"),
|
---|
| 189 | QSystemTrayIcon::Critical);
|
---|
| 190 | typeComboBox->setCurrentIndex(1);
|
---|
| 191 |
|
---|
| 192 | durationLabel = new QLabel(tr("Duration:"));
|
---|
| 193 |
|
---|
| 194 | durationSpinBox = new QSpinBox;
|
---|
| 195 | durationSpinBox->setRange(5, 60);
|
---|
| 196 | durationSpinBox->setSuffix(" s");
|
---|
| 197 | durationSpinBox->setValue(15);
|
---|
| 198 |
|
---|
| 199 | durationWarningLabel = new QLabel(tr("(some systems might ignore this "
|
---|
| 200 | "hint)"));
|
---|
| 201 | durationWarningLabel->setIndent(10);
|
---|
| 202 |
|
---|
| 203 | titleLabel = new QLabel(tr("Title:"));
|
---|
| 204 |
|
---|
| 205 | titleEdit = new QLineEdit(tr("Cannot connect to network"));
|
---|
| 206 |
|
---|
| 207 | bodyLabel = new QLabel(tr("Body:"));
|
---|
| 208 |
|
---|
| 209 | bodyEdit = new QTextEdit;
|
---|
| 210 | bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
|
---|
| 211 | "clue.\nClick this balloon for details."));
|
---|
| 212 |
|
---|
| 213 | showMessageButton = new QPushButton(tr("Show Message"));
|
---|
| 214 | showMessageButton->setDefault(true);
|
---|
| 215 |
|
---|
| 216 | QGridLayout *messageLayout = new QGridLayout;
|
---|
| 217 | messageLayout->addWidget(typeLabel, 0, 0);
|
---|
| 218 | messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
|
---|
| 219 | messageLayout->addWidget(durationLabel, 1, 0);
|
---|
| 220 | messageLayout->addWidget(durationSpinBox, 1, 1);
|
---|
| 221 | messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
|
---|
| 222 | messageLayout->addWidget(titleLabel, 2, 0);
|
---|
| 223 | messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
|
---|
| 224 | messageLayout->addWidget(bodyLabel, 3, 0);
|
---|
| 225 | messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
|
---|
| 226 | messageLayout->addWidget(showMessageButton, 5, 4);
|
---|
| 227 | messageLayout->setColumnStretch(3, 1);
|
---|
| 228 | messageLayout->setRowStretch(4, 1);
|
---|
| 229 | messageGroupBox->setLayout(messageLayout);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | void Window::createActions()
|
---|
| 233 | {
|
---|
| 234 | minimizeAction = new QAction(tr("Mi&nimize"), this);
|
---|
| 235 | connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
|
---|
| 236 |
|
---|
| 237 | maximizeAction = new QAction(tr("Ma&ximize"), this);
|
---|
| 238 | connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
|
---|
| 239 |
|
---|
| 240 | restoreAction = new QAction(tr("&Restore"), this);
|
---|
| 241 | connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
|
---|
| 242 |
|
---|
| 243 | quitAction = new QAction(tr("&Quit"), this);
|
---|
| 244 | connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | void Window::createTrayIcon()
|
---|
| 248 | {
|
---|
| 249 | trayIconMenu = new QMenu(this);
|
---|
| 250 | trayIconMenu->addAction(minimizeAction);
|
---|
| 251 | trayIconMenu->addAction(maximizeAction);
|
---|
| 252 | trayIconMenu->addAction(restoreAction);
|
---|
| 253 | trayIconMenu->addSeparator();
|
---|
| 254 | trayIconMenu->addAction(quitAction);
|
---|
| 255 |
|
---|
| 256 | trayIcon = new QSystemTrayIcon(this);
|
---|
| 257 | trayIcon->setContextMenu(trayIconMenu);
|
---|
| 258 | }
|
---|