examples/desktop/systray/window.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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     showIconCheckBox = new QCheckBox(tr("Show icon"));
       
   164     showIconCheckBox->setChecked(true);
       
   165 
       
   166     QHBoxLayout *iconLayout = new QHBoxLayout;
       
   167     iconLayout->addWidget(iconLabel);
       
   168     iconLayout->addWidget(iconComboBox);
       
   169     iconLayout->addStretch();
       
   170     iconLayout->addWidget(showIconCheckBox);
       
   171     iconGroupBox->setLayout(iconLayout);
       
   172 }
       
   173 
       
   174 void Window::createMessageGroupBox()
       
   175 {
       
   176     messageGroupBox = new QGroupBox(tr("Balloon Message"));
       
   177 
       
   178     typeLabel = new QLabel(tr("Type:"));
       
   179 
       
   180     typeComboBox = new QComboBox;
       
   181     typeComboBox->addItem(tr("None"), QSystemTrayIcon::NoIcon);
       
   182     typeComboBox->addItem(style()->standardIcon(
       
   183             QStyle::SP_MessageBoxInformation), tr("Information"),
       
   184             QSystemTrayIcon::Information);
       
   185     typeComboBox->addItem(style()->standardIcon(
       
   186             QStyle::SP_MessageBoxWarning), tr("Warning"),
       
   187             QSystemTrayIcon::Warning);
       
   188     typeComboBox->addItem(style()->standardIcon(
       
   189             QStyle::SP_MessageBoxCritical), tr("Critical"),
       
   190             QSystemTrayIcon::Critical);
       
   191     typeComboBox->setCurrentIndex(1);
       
   192 
       
   193     durationLabel = new QLabel(tr("Duration:"));
       
   194 
       
   195     durationSpinBox = new QSpinBox;
       
   196     durationSpinBox->setRange(5, 60);
       
   197     durationSpinBox->setSuffix(" s");
       
   198     durationSpinBox->setValue(15);
       
   199 
       
   200     durationWarningLabel = new QLabel(tr("(some systems might ignore this "
       
   201                                          "hint)"));
       
   202     durationWarningLabel->setIndent(10);
       
   203 
       
   204     titleLabel = new QLabel(tr("Title:"));
       
   205 
       
   206     titleEdit = new QLineEdit(tr("Cannot connect to network"));
       
   207 
       
   208     bodyLabel = new QLabel(tr("Body:"));
       
   209 
       
   210     bodyEdit = new QTextEdit;
       
   211     bodyEdit->setPlainText(tr("Don't believe me. Honestly, I don't have a "
       
   212                               "clue.\nClick this balloon for details."));
       
   213 
       
   214     showMessageButton = new QPushButton(tr("Show Message"));
       
   215     showMessageButton->setDefault(true);
       
   216 
       
   217     QGridLayout *messageLayout = new QGridLayout;
       
   218     messageLayout->addWidget(typeLabel, 0, 0);
       
   219     messageLayout->addWidget(typeComboBox, 0, 1, 1, 2);
       
   220     messageLayout->addWidget(durationLabel, 1, 0);
       
   221     messageLayout->addWidget(durationSpinBox, 1, 1);
       
   222     messageLayout->addWidget(durationWarningLabel, 1, 2, 1, 3);
       
   223     messageLayout->addWidget(titleLabel, 2, 0);
       
   224     messageLayout->addWidget(titleEdit, 2, 1, 1, 4);
       
   225     messageLayout->addWidget(bodyLabel, 3, 0);
       
   226     messageLayout->addWidget(bodyEdit, 3, 1, 2, 4);
       
   227     messageLayout->addWidget(showMessageButton, 5, 4);
       
   228     messageLayout->setColumnStretch(3, 1);
       
   229     messageLayout->setRowStretch(4, 1);
       
   230     messageGroupBox->setLayout(messageLayout);
       
   231 }
       
   232 
       
   233 void Window::createActions()
       
   234 {
       
   235     minimizeAction = new QAction(tr("Mi&nimize"), this);
       
   236     connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
       
   237 
       
   238     maximizeAction = new QAction(tr("Ma&ximize"), this);
       
   239     connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
       
   240 
       
   241     restoreAction = new QAction(tr("&Restore"), this);
       
   242     connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
       
   243 
       
   244     quitAction = new QAction(tr("&Quit"), this);
       
   245     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
       
   246 }
       
   247 
       
   248 void Window::createTrayIcon()
       
   249 {
       
   250     trayIconMenu = new QMenu(this);
       
   251     trayIconMenu->addAction(minimizeAction);
       
   252     trayIconMenu->addAction(maximizeAction);
       
   253     trayIconMenu->addAction(restoreAction);
       
   254     trayIconMenu->addSeparator();
       
   255     trayIconMenu->addAction(quitAction);
       
   256 
       
   257     trayIcon = new QSystemTrayIcon(this);
       
   258     trayIcon->setContextMenu(trayIconMenu);
       
   259 }