1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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:
KeypadNavigation(QWidget * parent=0)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 
~KeypadNavigation()113     ~KeypadNavigation()
114     {
115         delete ui;
116     }
117 
118 protected slots:
119 #ifdef QT_KEYPAD_NAVIGATION
setNavigationMode(int mode)120     void setNavigationMode(int mode)
121     {
122         QApplication::setNavigationMode(Qt::NavigationMode(mode));
123     }
124 #endif // QT_KEYPAD_NAVIGATION
125 
openDialog(int dialog)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 
main(int argc,char * argv[])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"
191