source: trunk/src/gui/kernel/qsoftkeymanager.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

  • Property svn:eol-style set to native
File size: 8.0 KB
Line 
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 QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qapplication.h"
43#include "qevent.h"
44#include "qbitmap.h"
45#include "private/qsoftkeymanager_p.h"
46#include "private/qobject_p.h"
47#include "private/qsoftkeymanager_common_p.h"
48
49#ifdef Q_WS_S60
50#include "private/qsoftkeymanager_s60_p.h"
51#endif
52
53#ifndef QT_NO_SOFTKEYMANAGER
54QT_BEGIN_NAMESPACE
55
56QSoftKeyManager *QSoftKeyManagerPrivate::self = 0;
57
58const char *QSoftKeyManager::standardSoftKeyText(StandardSoftKey standardKey)
59{
60 const char *softKeyText = 0;
61 switch (standardKey) {
62 case OkSoftKey:
63 softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Ok");
64 break;
65 case SelectSoftKey:
66 softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Select");
67 break;
68 case DoneSoftKey:
69 softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Done");
70 break;
71 case MenuSoftKey:
72 softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Options");
73 break;
74 case CancelSoftKey:
75 softKeyText = QT_TRANSLATE_NOOP("QSoftKeyManager", "Cancel");
76 break;
77 default:
78 break;
79 };
80
81 return softKeyText;
82}
83
84QSoftKeyManager *QSoftKeyManager::instance()
85{
86 if (!QSoftKeyManagerPrivate::self)
87 QSoftKeyManagerPrivate::self = new QSoftKeyManager;
88
89 return QSoftKeyManagerPrivate::self;
90}
91
92QSoftKeyManager::QSoftKeyManager() :
93#ifdef Q_WS_S60
94 QObject(*(new QSoftKeyManagerPrivateS60), 0)
95#else
96 QObject(*(new QSoftKeyManagerPrivate), 0)
97#endif
98{
99}
100
101QAction *QSoftKeyManager::createAction(StandardSoftKey standardKey, QWidget *actionWidget)
102{
103 const char* text = standardSoftKeyText(standardKey);
104 QAction *action = new QAction(QSoftKeyManager::tr(text), actionWidget);
105 QAction::SoftKeyRole softKeyRole = QAction::NoSoftKey;
106 switch (standardKey) {
107 case MenuSoftKey: // FALL-THROUGH
108 action->setProperty(MENU_ACTION_PROPERTY, QVariant(true)); // TODO: can be refactored away to use _q_action_menubar
109 case OkSoftKey:
110 case SelectSoftKey:
111 case DoneSoftKey:
112 softKeyRole = QAction::PositiveSoftKey;
113 break;
114 case CancelSoftKey:
115 softKeyRole = QAction::NegativeSoftKey;
116 break;
117 }
118 action->setSoftKeyRole(softKeyRole);
119 return action;
120}
121
122/*! \internal
123
124 Creates a QAction and registers the 'triggered' signal to send the given key event to
125 \a actionWidget as a convenience.
126
127*/
128QAction *QSoftKeyManager::createKeyedAction(StandardSoftKey standardKey, Qt::Key key, QWidget *actionWidget)
129{
130#ifndef QT_NO_ACTION
131 QScopedPointer<QAction> action(createAction(standardKey, actionWidget));
132
133 connect(action.data(), SIGNAL(triggered()), QSoftKeyManager::instance(), SLOT(sendKeyEvent()));
134 connect(action.data(), SIGNAL(destroyed(QObject*)), QSoftKeyManager::instance(), SLOT(cleanupHash(QObject*)));
135 QSoftKeyManager::instance()->d_func()->keyedActions.insert(action.data(), key);
136 return action.take();
137#endif //QT_NO_ACTION
138}
139
140void QSoftKeyManager::cleanupHash(QObject *obj)
141{
142 Q_D(QSoftKeyManager);
143 QAction *action = qobject_cast<QAction*>(obj);
144 d->keyedActions.remove(action);
145}
146
147void QSoftKeyManager::sendKeyEvent()
148{
149 Q_D(QSoftKeyManager);
150 QAction *action = qobject_cast<QAction*>(sender());
151
152 if (!action)
153 return;
154
155 Qt::Key keyToSend = d->keyedActions.value(action, Qt::Key_unknown);
156
157 if (keyToSend != Qt::Key_unknown)
158 QApplication::postEvent(action->parentWidget(),
159 new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier));
160}
161
162void QSoftKeyManager::updateSoftKeys()
163{
164 QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
165 QApplication::postEvent(QSoftKeyManager::instance(), event);
166}
167
168bool QSoftKeyManager::appendSoftkeys(const QWidget &source, int level)
169{
170 Q_D(QSoftKeyManager);
171 bool ret = false;
172 QList<QAction*> actions = source.actions();
173 for (int i = 0; i < actions.count(); ++i) {
174 if (actions.at(i)->softKeyRole() != QAction::NoSoftKey) {
175 d->requestedSoftKeyActions.insert(level, actions.at(i));
176 ret = true;
177 }
178 }
179 return ret;
180}
181
182QWidget *QSoftKeyManager::softkeySource(QWidget *previousSource, bool& recursiveMerging)
183{
184 Q_D(QSoftKeyManager);
185 QWidget *source = NULL;
186 if (!previousSource) {
187 // Initial source is primarily focuswidget and secondarily activeWindow
188 source = QApplication::focusWidget();
189 if (!source)
190 source = QApplication::activeWindow();
191 } else {
192 // Softkey merging is based on four criterias
193 // 1. Implicit merging is used whenever focus widget does not specify any softkeys
194 bool implicitMerging = d->requestedSoftKeyActions.isEmpty();
195 // 2. Explicit merging with parent is used whenever WA_MergeSoftkeys widget attribute is set
196 bool explicitMerging = previousSource->testAttribute(Qt::WA_MergeSoftkeys);
197 // 3. Explicit merging with all parents
198 recursiveMerging |= previousSource->testAttribute(Qt::WA_MergeSoftkeysRecursively);
199 // 4. Implicit and explicit merging always stops at window boundary
200 bool merging = (implicitMerging || explicitMerging || recursiveMerging) && !previousSource->isWindow();
201
202 source = merging ? previousSource->parentWidget() : NULL;
203 }
204 return source;
205}
206
207bool QSoftKeyManager::handleUpdateSoftKeys()
208{
209 Q_D(QSoftKeyManager);
210 int level = 0;
211 d->requestedSoftKeyActions.clear();
212 bool recursiveMerging = false;
213 QWidget *source = softkeySource(NULL, recursiveMerging);
214 do {
215 if (source) {
216 bool added = appendSoftkeys(*source, level);
217 source = softkeySource(source, recursiveMerging);
218 level = added ? ++level : level;
219 }
220 } while (source);
221
222 d->updateSoftKeys_sys();
223 return true;
224}
225
226bool QSoftKeyManager::event(QEvent *e)
227{
228#ifndef QT_NO_ACTION
229 if (e->type() == QEvent::UpdateSoftKeys)
230 return handleUpdateSoftKeys();
231#endif //QT_NO_ACTION
232 return false;
233}
234
235#ifdef Q_WS_S60
236bool QSoftKeyManager::handleCommand(int command)
237{
238 return static_cast<QSoftKeyManagerPrivateS60*>(QSoftKeyManager::instance()->d_func())->handleCommand(command);
239}
240#endif
241
242QT_END_NAMESPACE
243#endif //QT_NO_SOFTKEYMANAGER
Note: See TracBrowser for help on using the repository browser.