1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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/qaction_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
|
---|
54 | QT_BEGIN_NAMESPACE
|
---|
55 |
|
---|
56 | QSoftKeyManager *QSoftKeyManagerPrivate::self = 0;
|
---|
57 |
|
---|
58 | QString 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 |
|
---|
84 | QSoftKeyManager *QSoftKeyManager::instance()
|
---|
85 | {
|
---|
86 | if (!QSoftKeyManagerPrivate::self)
|
---|
87 | QSoftKeyManagerPrivate::self = new QSoftKeyManager;
|
---|
88 |
|
---|
89 | return QSoftKeyManagerPrivate::self;
|
---|
90 | }
|
---|
91 |
|
---|
92 | QSoftKeyManager::QSoftKeyManager() :
|
---|
93 | #ifdef Q_WS_S60
|
---|
94 | QObject(*(new QSoftKeyManagerPrivateS60), 0)
|
---|
95 | #else
|
---|
96 | QObject(*(new QSoftKeyManagerPrivate), 0)
|
---|
97 | #endif
|
---|
98 | {
|
---|
99 | }
|
---|
100 |
|
---|
101 | QAction *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 | QActionPrivate::get(action)->menuActionSoftkeys = true;
|
---|
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 | */
|
---|
129 | QAction *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 |
|
---|
141 | void QSoftKeyManager::cleanupHash(QObject *obj)
|
---|
142 | {
|
---|
143 | Q_D(QSoftKeyManager);
|
---|
144 | QAction *action = qobject_cast<QAction*>(obj);
|
---|
145 | d->keyedActions.remove(action);
|
---|
146 | }
|
---|
147 |
|
---|
148 | void 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 |
|
---|
163 | void QSoftKeyManager::updateSoftKeys()
|
---|
164 | {
|
---|
165 | QSoftKeyManager::instance()->d_func()->pendingUpdate = true;
|
---|
166 | QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
|
---|
167 | QApplication::postEvent(QSoftKeyManager::instance(), event);
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool QSoftKeyManager::appendSoftkeys(const QWidget &source, int level)
|
---|
171 | {
|
---|
172 | Q_D(QSoftKeyManager);
|
---|
173 | bool ret = false;
|
---|
174 | foreach(QAction *action, source.actions()) {
|
---|
175 | if (action->softKeyRole() != QAction::NoSoftKey
|
---|
176 | && (action->isVisible() || isForceEnabledInSofkeys(action))) {
|
---|
177 | d->requestedSoftKeyActions.insert(level, action);
|
---|
178 | ret = true;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | return ret;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | static bool isChildOf(const QWidget *c, const QWidget *p)
|
---|
186 | {
|
---|
187 | while (c) {
|
---|
188 | if (c == p)
|
---|
189 | return true;
|
---|
190 | c = c->parentWidget();
|
---|
191 | }
|
---|
192 | return false;
|
---|
193 | }
|
---|
194 |
|
---|
195 | QWidget *QSoftKeyManager::softkeySource(QWidget *previousSource, bool& recursiveMerging)
|
---|
196 | {
|
---|
197 | Q_D(QSoftKeyManager);
|
---|
198 | QWidget *source = NULL;
|
---|
199 | if (!previousSource) {
|
---|
200 | // Initial source is primarily focuswidget and secondarily activeWindow
|
---|
201 | QWidget *focus = QApplication::focusWidget();
|
---|
202 | QWidget *popup = QApplication::activePopupWidget();
|
---|
203 | if (popup) {
|
---|
204 | if (isChildOf(focus, popup))
|
---|
205 | source = focus;
|
---|
206 | else
|
---|
207 | source = popup;
|
---|
208 | }
|
---|
209 | if (!source) {
|
---|
210 | QWidget *modal = QApplication::activeModalWidget();
|
---|
211 | if (modal) {
|
---|
212 | if (isChildOf(focus, modal))
|
---|
213 | source = focus;
|
---|
214 | else
|
---|
215 | source = modal;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | if (!source) {
|
---|
219 | source = focus;
|
---|
220 | if (!source)
|
---|
221 | source = QApplication::activeWindow();
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | // Softkey merging is based on four criterias
|
---|
225 | // 1. Implicit merging is used whenever focus widget does not specify any softkeys
|
---|
226 | bool implicitMerging = d->requestedSoftKeyActions.isEmpty();
|
---|
227 | // 2. Explicit merging with parent is used whenever WA_MergeSoftkeys widget attribute is set
|
---|
228 | bool explicitMerging = previousSource->testAttribute(Qt::WA_MergeSoftkeys);
|
---|
229 | // 3. Explicit merging with all parents
|
---|
230 | recursiveMerging |= previousSource->testAttribute(Qt::WA_MergeSoftkeysRecursively);
|
---|
231 | // 4. Implicit and explicit merging always stops at window boundary
|
---|
232 | bool merging = (implicitMerging || explicitMerging || recursiveMerging) && !previousSource->isWindow();
|
---|
233 |
|
---|
234 | source = merging ? previousSource->parentWidget() : NULL;
|
---|
235 | }
|
---|
236 | return source;
|
---|
237 | }
|
---|
238 |
|
---|
239 | bool QSoftKeyManager::handleUpdateSoftKeys()
|
---|
240 | {
|
---|
241 | Q_D(QSoftKeyManager);
|
---|
242 | int level = 0;
|
---|
243 | d->requestedSoftKeyActions.clear();
|
---|
244 | bool recursiveMerging = false;
|
---|
245 | QWidget *source = softkeySource(NULL, recursiveMerging);
|
---|
246 | d->initialSoftKeySource = source;
|
---|
247 | while (source) {
|
---|
248 | if (appendSoftkeys(*source, level))
|
---|
249 | ++level;
|
---|
250 | source = softkeySource(source, recursiveMerging);
|
---|
251 | }
|
---|
252 |
|
---|
253 | d->updateSoftKeys_sys();
|
---|
254 | d->pendingUpdate = false;
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 |
|
---|
258 | void QSoftKeyManager::setForceEnabledInSoftkeys(QAction *action)
|
---|
259 | {
|
---|
260 | QActionPrivate::get(action)->forceEnabledInSoftkeys = true;
|
---|
261 | }
|
---|
262 |
|
---|
263 | bool QSoftKeyManager::isForceEnabledInSofkeys(QAction *action)
|
---|
264 | {
|
---|
265 | return QActionPrivate::get(action)->forceEnabledInSoftkeys;
|
---|
266 | }
|
---|
267 |
|
---|
268 | bool QSoftKeyManager::event(QEvent *e)
|
---|
269 | {
|
---|
270 | #ifndef QT_NO_ACTION
|
---|
271 | if (e->type() == QEvent::UpdateSoftKeys)
|
---|
272 | return handleUpdateSoftKeys();
|
---|
273 | #endif //QT_NO_ACTION
|
---|
274 | return false;
|
---|
275 | }
|
---|
276 |
|
---|
277 | #ifdef Q_WS_S60
|
---|
278 | bool QSoftKeyManager::handleCommand(int command)
|
---|
279 | {
|
---|
280 | if (QSoftKeyManager::instance()->d_func()->pendingUpdate)
|
---|
281 | (void)QSoftKeyManager::instance()->handleUpdateSoftKeys();
|
---|
282 |
|
---|
283 | return static_cast<QSoftKeyManagerPrivateS60*>(QSoftKeyManager::instance()->d_func())->handleCommand(command);
|
---|
284 | }
|
---|
285 | #endif
|
---|
286 |
|
---|
287 | QT_END_NAMESPACE
|
---|
288 | #endif //QT_NO_SOFTKEYMANAGER
|
---|