util/tests/manual/keypadnavigation/main.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 <QtGui>
       
    43 #include "ui_keypadnavigation.h"
       
    44 
       
    45 class KeypadNavigation : public QMainWindow
       
    46 {
       
    47     Q_OBJECT
       
    48 
       
    49 public:
       
    50     KeypadNavigation(QWidget *parent = 0)
       
    51         : QMainWindow(parent)
       
    52         , ui(new Ui_KeypadNavigation)
       
    53     {
       
    54         ui->setupUi(this);
       
    55 
       
    56         const struct {
       
    57             QObject *action;
       
    58             QWidget *page;
       
    59         } layoutMappings[] = {
       
    60             {ui->m_actionLayoutVerticalSimple,  ui->m_pageVerticalSimple},
       
    61             {ui->m_actionLayoutVerticalComplex, ui->m_pageVerticalComplex},
       
    62             {ui->m_actionLayoutTwoDimensional,  ui->m_pageTwoDimensional},
       
    63             {ui->m_actionLayoutSliderMagic,     ui->m_pageSliderMagic},
       
    64             {ui->m_actionLayoutChaos,           ui->m_pageChaos},
       
    65             {ui->m_actionLayoutDialogs,         ui->m_pageDialogs}
       
    66         };
       
    67         for (int i = 0; i < int(sizeof layoutMappings / sizeof layoutMappings[0]); ++i) {
       
    68             connect(layoutMappings[i].action, SIGNAL(triggered()), &m_layoutSignalMapper, SLOT(map()));
       
    69             m_layoutSignalMapper.setMapping(layoutMappings[i].action, layoutMappings[i].page);
       
    70         }
       
    71         connect(&m_layoutSignalMapper, SIGNAL(mapped(QWidget*)), ui->m_stackWidget, SLOT(setCurrentWidget(QWidget*)));
       
    72 
       
    73 #ifdef QT_KEYPAD_NAVIGATION
       
    74         const struct {
       
    75             QObject *action;
       
    76             Qt::NavigationMode mode;
       
    77         } modeMappings[] = {
       
    78             {ui->m_actionModeNone,                  Qt::NavigationModeNone},
       
    79             {ui->m_actionModeKeypadTabOrder,        Qt::NavigationModeKeypadTabOrder},
       
    80             {ui->m_actionModeKeypadDirectional,     Qt::NavigationModeKeypadDirectional},
       
    81             {ui->m_actionModeCursorAuto,            Qt::NavigationModeCursorAuto},
       
    82             {ui->m_actionModeCursorForceVisible,    Qt::NavigationModeCursorForceVisible}
       
    83         };
       
    84         for (int i = 0; i < int(sizeof modeMappings / sizeof modeMappings[0]); ++i) {
       
    85             connect(modeMappings[i].action, SIGNAL(triggered()), &m_modeSignalMapper, SLOT(map()));
       
    86             m_modeSignalMapper.setMapping(modeMappings[i].action, int(modeMappings[i].mode));
       
    87         }
       
    88         connect(&m_modeSignalMapper, SIGNAL(mapped(int)), SLOT(setNavigationMode(int)));
       
    89 #else // QT_KEYPAD_NAVIGATION
       
    90         ui->m_menuNavigation_mode->deleteLater();
       
    91 #endif // QT_KEYPAD_NAVIGATION
       
    92 
       
    93         const struct {
       
    94             QObject *button;
       
    95             Dialog dialog;
       
    96         } openDialogMappings[] = {
       
    97             {ui->m_buttonGetOpenFileName,       DialogGetOpenFileName},
       
    98             {ui->m_buttonGetSaveFileName,       DialogGetSaveFileName},
       
    99             {ui->m_buttonGetExistingDirectory,  DialogGetExistingDirectory},
       
   100             {ui->m_buttonGetColor,              DialogGetColor},
       
   101             {ui->m_buttonGetFont,               DialogGetFont},
       
   102             {ui->m_buttonQuestion,              DialogQuestion},
       
   103             {ui->m_buttonAboutQt,               DialogAboutQt},
       
   104             {ui->m_buttonGetItem,               DialogGetItem}
       
   105         };
       
   106         for (int i = 0; i < int(sizeof openDialogMappings / sizeof openDialogMappings[0]); ++i) {
       
   107             connect(openDialogMappings[i].button, SIGNAL(clicked()), &m_dialogSignalMapper, SLOT(map()));
       
   108             m_dialogSignalMapper.setMapping(openDialogMappings[i].button, int(openDialogMappings[i].dialog));
       
   109         }
       
   110         connect(&m_dialogSignalMapper, SIGNAL(mapped(int)), SLOT(openDialog(int)));
       
   111     }
       
   112 
       
   113     ~KeypadNavigation()
       
   114     {
       
   115         delete ui;
       
   116     }
       
   117 
       
   118 protected slots:
       
   119 #ifdef QT_KEYPAD_NAVIGATION
       
   120     void setNavigationMode(int mode)
       
   121     {
       
   122         QApplication::setNavigationMode(Qt::NavigationMode(mode));
       
   123     }
       
   124 #endif // QT_KEYPAD_NAVIGATION
       
   125 
       
   126     void openDialog(int dialog)
       
   127     {
       
   128         switch (Dialog(dialog)) {
       
   129             case DialogGetOpenFileName:
       
   130                 QFileDialog::getOpenFileName(this, QLatin1String("getOpenFileName"));
       
   131                 break;
       
   132             case DialogGetSaveFileName:
       
   133                 QFileDialog::getSaveFileName(this, QLatin1String("getSaveFileName"));
       
   134                 break;
       
   135             case DialogGetExistingDirectory:
       
   136                 QFileDialog::getExistingDirectory(this, QLatin1String("getExistingDirectory"));
       
   137                 break;
       
   138             case DialogGetColor:
       
   139                 QColorDialog::getColor(QColor(Qt::green), this, QLatin1String("getColor"));
       
   140                 break;
       
   141             case DialogGetFont:
       
   142                 QFontDialog::getFont(0, this);
       
   143                 break;
       
   144             case DialogQuestion:
       
   145                 QMessageBox::question(this, QLatin1String("question"), QLatin1String("¿Hola, que tal?"));
       
   146                 break;
       
   147             case DialogAboutQt:
       
   148                 QMessageBox::aboutQt(this);
       
   149                 break;
       
   150             case DialogGetItem:
       
   151                 QInputDialog::getItem(this, QLatin1String("getItem"), QLatin1String("Choose a color"), QColor::colorNames());
       
   152                 break;
       
   153             default:
       
   154             break;
       
   155         }
       
   156     }
       
   157 
       
   158 private:
       
   159     enum Dialog {
       
   160         DialogGetOpenFileName,
       
   161         DialogGetSaveFileName,
       
   162         DialogGetExistingDirectory,
       
   163         DialogGetColor,
       
   164         DialogGetFont,
       
   165         DialogQuestion,
       
   166         DialogAboutQt,
       
   167         DialogGetItem
       
   168     };
       
   169 
       
   170     Ui_KeypadNavigation *ui;
       
   171     QSignalMapper m_layoutSignalMapper;
       
   172 #ifdef QT_KEYPAD_NAVIGATION
       
   173     QSignalMapper m_modeSignalMapper;
       
   174 #endif // QT_KEYPAD_NAVIGATION
       
   175     QSignalMapper m_dialogSignalMapper;
       
   176 };
       
   177 
       
   178 int main(int argc, char *argv[])
       
   179 {
       
   180     QApplication a(argc, argv);
       
   181     KeypadNavigation w;
       
   182 #ifdef Q_WS_S60
       
   183     w.showMaximized();
       
   184 #else
       
   185     w.show();
       
   186 #endif
       
   187     return a.exec();
       
   188 }
       
   189 
       
   190 #include "main.moc"