util/examples/widgets/softkeys/softkeys.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "softkeys.h"
       
    43 
       
    44 MainWindow::MainWindow(QWidget *parent)
       
    45     : QMainWindow(parent)
       
    46 {
       
    47     central = new QWidget(this);
       
    48     central->setContextMenuPolicy(Qt::NoContextMenu); // explicitly forbid usage of context menu so actions item is not shown menu
       
    49     setCentralWidget(central);
       
    50 
       
    51     // Create text editor and set softkeys to it
       
    52     textEditor= new QTextEdit(tr("Navigate in UI to see context sensitive softkeys in action"), this);
       
    53     QAction* clear = new QAction(tr("Clear"), this);
       
    54     clear->setSoftKeyRole(QAction::NegativeSoftKey);
       
    55 
       
    56     textEditor->addAction(clear);
       
    57 
       
    58     ok = new QAction(tr("Ok"), this);
       
    59     ok->setSoftKeyRole(QAction::PositiveSoftKey);
       
    60     connect(ok, SIGNAL(triggered()), this, SLOT(okPressed()));
       
    61 
       
    62     cancel = new QAction(tr("Cancel"), this);
       
    63     cancel->setSoftKeyRole(QAction::NegativeSoftKey);
       
    64     connect(cancel, SIGNAL(triggered()), this, SLOT(cancelPressed()));
       
    65 
       
    66     infoLabel = new QLabel(tr(""), this);
       
    67     infoLabel->setContextMenuPolicy(Qt::NoContextMenu);
       
    68 
       
    69     toggleButton = new QPushButton(tr("Custom"), this);
       
    70     toggleButton->setContextMenuPolicy(Qt::NoContextMenu);
       
    71     toggleButton->setCheckable(true);
       
    72 
       
    73     modeButton = new QPushButton(tr("Loop SK window type"), this);
       
    74     modeButton->setContextMenuPolicy(Qt::NoContextMenu);
       
    75 
       
    76     modeLabel = new QLabel(tr("Normal maximized"), this);
       
    77     modeLabel->setContextMenuPolicy(Qt::NoContextMenu);
       
    78 
       
    79     pushButton = new QPushButton(tr("File Dialog"), this);
       
    80     pushButton->setContextMenuPolicy(Qt::NoContextMenu);
       
    81 
       
    82     QComboBox* comboBox = new QComboBox(this);
       
    83     comboBox->setContextMenuPolicy(Qt::NoContextMenu);
       
    84     comboBox->insertItems(0, QStringList()
       
    85      << QApplication::translate("MainWindow", "Selection1", 0, QApplication::UnicodeUTF8)
       
    86      << QApplication::translate("MainWindow", "Selection2", 0, QApplication::UnicodeUTF8)
       
    87      << QApplication::translate("MainWindow", "Selection3", 0, QApplication::UnicodeUTF8)
       
    88     );
       
    89 
       
    90     layout = new QGridLayout;
       
    91     layout->addWidget(textEditor, 0, 0, 1, 2);
       
    92     layout->addWidget(infoLabel, 1, 0, 1, 2);
       
    93     layout->addWidget(toggleButton, 2, 0);
       
    94     layout->addWidget(pushButton, 2, 1);
       
    95     layout->addWidget(comboBox, 3, 0, 1, 2);
       
    96     layout->addWidget(modeButton, 4, 0, 1, 2);
       
    97     layout->addWidget(modeLabel, 5, 0, 1, 2);
       
    98     central->setLayout(layout);
       
    99 
       
   100     fileMenu = menuBar()->addMenu(tr("&File"));
       
   101     exit = new QAction(tr("&Exit"), this);
       
   102     fileMenu->addAction(exit);
       
   103 
       
   104     connect(clear, SIGNAL(triggered()), this, SLOT(clearTextEditor()));
       
   105     connect(pushButton, SIGNAL(clicked()), this, SLOT(openDialog()));
       
   106     connect(exit, SIGNAL(triggered()), this, SLOT(exitApplication()));
       
   107     connect(toggleButton, SIGNAL(clicked()), this, SLOT(setCustomSoftKeys()));
       
   108     connect(modeButton, SIGNAL(clicked()), this, SLOT(setMode()));
       
   109     pushButton->setFocus();
       
   110 }
       
   111 
       
   112 MainWindow::~MainWindow()
       
   113 {
       
   114 }
       
   115 
       
   116 void MainWindow::clearTextEditor()
       
   117 {
       
   118     textEditor->setText(tr(""));
       
   119 }
       
   120 
       
   121 void MainWindow::openDialog()
       
   122 {
       
   123     QFileDialog::getOpenFileName(this);
       
   124 }
       
   125 
       
   126 void MainWindow::addSoftKeys()
       
   127 {
       
   128     addAction(ok);
       
   129     addAction(cancel);
       
   130 }
       
   131 
       
   132 void MainWindow::setCustomSoftKeys()
       
   133 {
       
   134     if (toggleButton->isChecked()) {
       
   135         infoLabel->setText(tr("Custom softkeys set"));
       
   136         addSoftKeys();
       
   137         }
       
   138     else {
       
   139         infoLabel->setText(tr("Custom softkeys removed"));
       
   140         removeAction(ok);
       
   141         removeAction(cancel);
       
   142     }
       
   143 }
       
   144 
       
   145 void MainWindow::setMode()
       
   146 {
       
   147     if(isMaximized()) {
       
   148         showFullScreen();
       
   149         modeLabel->setText(tr("Normal Fullscreen"));
       
   150     } else {
       
   151         Qt::WindowFlags flags = windowFlags();
       
   152         if(flags & Qt::WindowSoftkeysRespondHint) {
       
   153             flags |= Qt::WindowSoftkeysVisibleHint;
       
   154             flags &= ~Qt::WindowSoftkeysRespondHint;
       
   155             setWindowFlags(flags); // Hides visible window
       
   156             showFullScreen();
       
   157             modeLabel->setText(tr("Fullscreen with softkeys"));
       
   158         } else if(flags & Qt::WindowSoftkeysVisibleHint) {
       
   159             flags &= ~Qt::WindowSoftkeysVisibleHint;
       
   160             flags &= ~Qt::WindowSoftkeysRespondHint;
       
   161             setWindowFlags(flags); // Hides visible window
       
   162             showMaximized();
       
   163             modeLabel->setText(tr("Normal Maximized"));
       
   164         } else {
       
   165             flags &= ~Qt::WindowSoftkeysVisibleHint;
       
   166             flags |= Qt::WindowSoftkeysRespondHint;
       
   167             setWindowFlags(flags); // Hides visible window
       
   168             showFullScreen();
       
   169             modeLabel->setText(tr("Fullscreen with SK respond"));
       
   170         }
       
   171     }
       
   172 }
       
   173 
       
   174 void MainWindow::exitApplication()
       
   175 {
       
   176     qApp->exit();
       
   177 }
       
   178 
       
   179 void MainWindow::okPressed()
       
   180 {
       
   181     infoLabel->setText(tr("OK pressed"));
       
   182 }
       
   183 
       
   184 void MainWindow::cancelPressed()
       
   185 {
       
   186     infoLabel->setText(tr("Cancel pressed"));
       
   187 }
       
   188 
       
   189