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

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

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 8.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 [email protected].
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
58QString QSoftKeyManager::standardSoftKeyText(StandardSoftKey standardKey)
59{
60 QString softKeyText;
61 switch (standardKey) {
62 case OkSoftKey:
63 softKeyText = QSoftKeyManager::tr("Ok");
64 break;
65 case SelectSoftKey:
66 softKeyText = QSoftKeyManager::tr("Select");
67 break;
68 case DoneSoftKey:
69 softKeyText = QSoftKeyManager::tr("Done");
70 break;
71 case MenuSoftKey:
72 softKeyText = QSoftKeyManager::tr("Options");
73 break;
74 case CancelSoftKey:
75 softKeyText = QSoftKeyManager::tr("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 QAction *action = new QAction(standardSoftKeyText(standardKey), actionWidget);
104 QAction::SoftKeyRole softKeyRole = QAction::NoSoftKey;
105 switch (standardKey) {
106 case MenuSoftKey: // FALL-THROUGH
107 action->setProperty(MENU_ACTION_PROPERTY, QVariant(true)); // TODO: can be refactored away to use _q_action_menubar
108 case OkSoftKey:
109 case SelectSoftKey:
110 case DoneSoftKey:
111 softKeyRole = QAction::PositiveSoftKey;
112 break;
113 case CancelSoftKey:
114 softKeyRole = QAction::NegativeSoftKey;
115 break;
116 }
117 action->setSoftKeyRole(softKeyRole);
118 action->setVisible(false);
119 setForceEnabledInSoftkeys(action);
120 return action;
121}
122
123/*! \internal
124
125 Creates a QAction and registers the 'triggered' signal to send the given key event to
126 \a actionWidget as a convenience.
127
128*/
129QAction *QSoftKeyManager::createKeyedAction(StandardSoftKey standardKey, Qt::Key key, QWidget *actionWidget)
130{
131#ifndef QT_NO_ACTION
132 QScopedPointer<QAction> action(createAction(standardKey, actionWidget));
133
134 connect(action.data(), SIGNAL(triggered()), QSoftKeyManager::instance(), SLOT(sendKeyEvent()));
135 connect(action.data(), SIGNAL(destroyed(QObject*)), QSoftKeyManager::instance(), SLOT(cleanupHash(QObject*)));
136 QSoftKeyManager::instance()->d_func()->keyedActions.insert(action.data(), key);
137 return action.take();
138#endif //QT_NO_ACTION
139}
140
141void QSoftKeyManager::cleanupHash(QObject *obj)
142{
143 Q_D(QSoftKeyManager);
144 QAction *action = qobject_cast<QAction*>(obj);
145 d->keyedActions.remove(action);
146}
147
148void QSoftKeyManager::sendKeyEvent()
149{
150 Q_D(QSoftKeyManager);
151 QAction *action = qobject_cast<QAction*>(sender());
152
153 if (!action)
154 return;
155
156 Qt::Key keyToSend = d->keyedActions.value(action, Qt::Key_unknown);
157
158 if (keyToSend != Qt::Key_unknown)
159 QApplication::postEvent(action->parentWidget(),
160 new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier));
161}
162
163void QSoftKeyManager::updateSoftKeys()
164{
165 QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
166 QApplication::postEvent(QSoftKeyManager::instance(), event);
167}
168
169bool QSoftKeyManager::appendSoftkeys(const QWidget &source, int level)
170{
171 Q_D(QSoftKeyManager);
172 bool ret = false;
173 foreach(QAction *action, source.actions()) {
174 if (action->softKeyRole() != QAction::NoSoftKey
175 && (action->isVisible() || isForceEnabledInSofkeys(action))) {
176 d->requestedSoftKeyActions.insert(level, action);
177 ret = true;
178 }
179 }
180 return ret;
181}
182
183
184static bool isChildOf(const QWidget *c, const QWidget *p)
185{
186 while (c) {
187 if (c == p)
188 return true;
189 c = c->parentWidget();
190 }
191 return false;
192}
193
194QWidget *QSoftKeyManager::softkeySource(QWidget *previousSource, bool& recursiveMerging)
195{
196 Q_D(QSoftKeyManager);
197 QWidget *source = NULL;
198 if (!previousSource) {
199 // Initial source is primarily focuswidget and secondarily activeWindow
200 QWidget *focus = QApplication::focusWidget();
201 QWidget *popup = QApplication::activePopupWidget();
202 if (popup) {
203 if (isChildOf(focus, popup))
204 source = focus;
205 else
206 source = popup;
207 }
208 if (!source) {
209 QWidget *modal = QApplication::activeModalWidget();
210 if (modal) {
211 if (isChildOf(focus, modal))
212 source = focus;
213 else
214 source = modal;
215 }
216 }
217 if (!source) {
218 source = focus;
219 if (!source)
220 source = QApplication::activeWindow();
221 }
222 } else {
223 // Softkey merging is based on four criterias
224 // 1. Implicit merging is used whenever focus widget does not specify any softkeys
225 bool implicitMerging = d->requestedSoftKeyActions.isEmpty();
226 // 2. Explicit merging with parent is used whenever WA_MergeSoftkeys widget attribute is set
227 bool explicitMerging = previousSource->testAttribute(Qt::WA_MergeSoftkeys);
228 // 3. Explicit merging with all parents
229 recursiveMerging |= previousSource->testAttribute(Qt::WA_MergeSoftkeysRecursively);
230 // 4. Implicit and explicit merging always stops at window boundary
231 bool merging = (implicitMerging || explicitMerging || recursiveMerging) && !previousSource->isWindow();
232
233 source = merging ? previousSource->parentWidget() : NULL;
234 }
235 return source;
236}
237
238bool QSoftKeyManager::handleUpdateSoftKeys()
239{
240 Q_D(QSoftKeyManager);
241 int level = 0;
242 d->requestedSoftKeyActions.clear();
243 bool recursiveMerging = false;
244 QWidget *source = softkeySource(NULL, recursiveMerging);
245 d->initialSoftKeySource = source;
246 while (source) {
247 if (appendSoftkeys(*source, level))
248 ++level;
249 source = softkeySource(source, recursiveMerging);
250 }
251
252 d->updateSoftKeys_sys();
253 return true;
254}
255
256void QSoftKeyManager::setForceEnabledInSoftkeys(QAction *action)
257{
258 action->setProperty(FORCE_ENABLED_PROPERTY, QVariant(true));
259}
260
261bool QSoftKeyManager::isForceEnabledInSofkeys(QAction *action)
262{
263 bool ret = false;
264 QVariant property = action->property(FORCE_ENABLED_PROPERTY);
265 if (property.isValid() && property.toBool())
266 ret = true;
267 return ret;
268}
269
270bool QSoftKeyManager::event(QEvent *e)
271{
272#ifndef QT_NO_ACTION
273 if (e->type() == QEvent::UpdateSoftKeys)
274 return handleUpdateSoftKeys();
275#endif //QT_NO_ACTION
276 return false;
277}
278
279#ifdef Q_WS_S60
280bool QSoftKeyManager::handleCommand(int command)
281{
282 return static_cast<QSoftKeyManagerPrivateS60*>(QSoftKeyManager::instance()->d_func())->handleCommand(command);
283}
284#endif
285
286QT_END_NAMESPACE
287#endif //QT_NO_SOFTKEYMANAGER
Note: See TracBrowser for help on using the repository browser.