summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qsoftkeymanager.cpp
blob: 9e615d519a5220cd876e1c1478ac6caf0364e9d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qapplication.h"
#include "qevent.h"
#include "qbitmap.h"
#include "private/qsoftkeymanager_p.h"
#include "private/qaction_p.h"
#include "private/qsoftkeymanager_common_p.h"

#ifdef Q_WS_S60
#include "private/qsoftkeymanager_s60_p.h"
#include "private/qt_s60_p.h"
#endif

#ifndef QT_NO_SOFTKEYMANAGER
QT_BEGIN_NAMESPACE

QScopedPointer<QSoftKeyManager> QSoftKeyManagerPrivate::self(0);

QString QSoftKeyManager::standardSoftKeyText(StandardSoftKey standardKey)
{
    QString softKeyText;
    switch (standardKey) {
    case OkSoftKey:
        softKeyText = QSoftKeyManager::tr("OK");
        break;
    case SelectSoftKey:
        softKeyText = QSoftKeyManager::tr("Select");
        break;
    case DoneSoftKey:
        softKeyText = QSoftKeyManager::tr("Done");
        break;
    case MenuSoftKey:
        softKeyText = QSoftKeyManager::tr("Options");
        break;
    case CancelSoftKey:
        softKeyText = QSoftKeyManager::tr("Cancel");
        break;
    default:
        break;
    };

    return softKeyText;
}

QSoftKeyManager *QSoftKeyManager::instance()
{
    if (!QSoftKeyManagerPrivate::self)
        QSoftKeyManagerPrivate::self.reset(new QSoftKeyManager);

    return QSoftKeyManagerPrivate::self.data();
}

QSoftKeyManager::QSoftKeyManager() :
#ifdef Q_WS_S60
    QObject(*(new QSoftKeyManagerPrivateS60), 0)
#else
    QObject(*(new QSoftKeyManagerPrivate), 0)
#endif
{
}

QAction *QSoftKeyManager::createAction(StandardSoftKey standardKey, QWidget *actionWidget)
{
    QAction *action = new QAction(standardSoftKeyText(standardKey), actionWidget);
#if defined(Q_WS_S60) && !defined(SYMBIAN_VERSION_9_4) && !defined(SYMBIAN_VERSION_9_3) && !defined(SYMBIAN_VERSION_9_2)
    int key = 0;
    switch (standardKey) {
    case OkSoftKey:
        key = EAknSoftkeyOk;
        break;
    case SelectSoftKey:
        key = EAknSoftkeySelect;
        break;
    case DoneSoftKey:
        key = EAknSoftkeyDone;
        break;
    case MenuSoftKey:
        key = EAknSoftkeyOptions;
        break;
    case CancelSoftKey:
        key = EAknSoftkeyCancel;
        break;
    default:
        break;
    };
    if (key != 0) {
        QSoftKeyManager::instance()->d_func()->softKeyCommandActions.insert(action, key);
        connect(action, SIGNAL(destroyed(QObject*)), QSoftKeyManager::instance(), SLOT(cleanupHash(QObject*)));
    }
#endif
    QAction::SoftKeyRole softKeyRole = QAction::NoSoftKey;
    switch (standardKey) {
    case MenuSoftKey: // FALL-THROUGH
        QActionPrivate::get(action)->menuActionSoftkeys = true;
    case OkSoftKey:
    case SelectSoftKey:
    case DoneSoftKey:
        softKeyRole = QAction::PositiveSoftKey;
        break;
    case CancelSoftKey:
        softKeyRole = QAction::NegativeSoftKey;
        break;
    }
    action->setSoftKeyRole(softKeyRole);
    action->setVisible(false);
    setForceEnabledInSoftkeys(action);
    return action;
}

/*! \internal

  Creates a QAction and registers the 'triggered' signal to send the given key event to
  \a actionWidget as a convenience.

*/
QAction *QSoftKeyManager::createKeyedAction(StandardSoftKey standardKey, Qt::Key key, QWidget *actionWidget)
{
#ifndef QT_NO_ACTION
    QScopedPointer<QAction> action(createAction(standardKey, actionWidget));

    connect(action.data(), SIGNAL(triggered()), QSoftKeyManager::instance(), SLOT(sendKeyEvent()));

#if defined(Q_WS_S60) && !defined(SYMBIAN_VERSION_9_4) && !defined(SYMBIAN_VERSION_9_3) && !defined(SYMBIAN_VERSION_9_2)
    // Don't connect destroyed slot if is was already connected in createAction
    if (!(QSoftKeyManager::instance()->d_func()->softKeyCommandActions.contains(action.data())))
#endif
    connect(action.data(), SIGNAL(destroyed(QObject*)), QSoftKeyManager::instance(), SLOT(cleanupHash(QObject*)));

    QSoftKeyManager::instance()->d_func()->keyedActions.insert(action.data(), key);
    return action.take();
#endif //QT_NO_ACTION
}

void QSoftKeyManager::cleanupHash(QObject *obj)
{
    Q_D(QSoftKeyManager);
    // Can't use qobject_cast in destroyed() signal handler as that'll return NULL,
    // so use static_cast instead. Since the pointer is only used as a hash key, it is safe.
    QAction *action = static_cast<QAction *>(obj);
    d->keyedActions.remove(action);
#if defined(Q_WS_S60) && !defined(SYMBIAN_VERSION_9_4) && !defined(SYMBIAN_VERSION_9_3) && !defined(SYMBIAN_VERSION_9_2)
    d->softKeyCommandActions.remove(action);
#endif
}

void QSoftKeyManager::sendKeyEvent()
{
    Q_D(QSoftKeyManager);
    QAction *action = qobject_cast<QAction*>(sender());

    if (!action)
        return;

    Qt::Key keyToSend = d->keyedActions.value(action, Qt::Key_unknown);

    if (keyToSend != Qt::Key_unknown)
        QApplication::postEvent(action->parentWidget(),
                                new QKeyEvent(QEvent::KeyPress, keyToSend, Qt::NoModifier));
}

void QSoftKeyManager::updateSoftKeys()
{
#ifdef Q_WS_S60
    // Do not adjust softkeys if application is not the topmost one
    if (S60->wsSession().GetFocusWindowGroup() != S60->windowGroup().WindowGroupId())
        return;
#endif
    QSoftKeyManager::instance()->d_func()->pendingUpdate = true;
    QEvent *event = new QEvent(QEvent::UpdateSoftKeys);
    QApplication::postEvent(QSoftKeyManager::instance(), event);
}

bool QSoftKeyManager::appendSoftkeys(const QWidget &source, int level)
{
    Q_D(QSoftKeyManager);
    bool ret = false;
    foreach(QAction *action, source.actions()) {
        if (action->softKeyRole() != QAction::NoSoftKey
            && (action->isVisible() || isForceEnabledInSofkeys(action))) {
            d->requestedSoftKeyActions.insert(level, action);
            ret = true;
        }
    }
    return ret;
}


static bool isChildOf(const QWidget *c, const QWidget *p)
{
    while (c) {
        if (c == p)
            return true;
        c = c->parentWidget();
    }
    return false;
}

QWidget *QSoftKeyManager::softkeySource(QWidget *previousSource, bool& recursiveMerging)
{
    Q_D(QSoftKeyManager);
    QWidget *source = NULL;
    if (!previousSource) {
        // Initial source is primarily focuswidget and secondarily activeWindow
        QWidget *focus = QApplication::focusWidget();
        QWidget *popup = QApplication::activePopupWidget();
        if (popup) {
            if (isChildOf(focus, popup))
                source = focus;
            else
                source = popup;
        }
        if (!source) {
            QWidget *modal = QApplication::activeModalWidget();
            if (modal) {
                if (isChildOf(focus, modal))
                    source = focus;
                else
                    source = modal;
            }
        }
        if (!source) {
            source = focus;
            if (!source)
                source = QApplication::activeWindow();
        }
    } else {
        // Softkey merging is based on four criterias
        // 1. Implicit merging is used whenever focus widget does not specify any softkeys
        bool implicitMerging = d->requestedSoftKeyActions.isEmpty();
        // 2. Explicit merging with parent is used whenever WA_MergeSoftkeys widget attribute is set
        bool explicitMerging = previousSource->testAttribute(Qt::WA_MergeSoftkeys);
        // 3. Explicit merging with all parents
        recursiveMerging |= previousSource->testAttribute(Qt::WA_MergeSoftkeysRecursively);
        // 4. Implicit and explicit merging always stops at window boundary
        bool merging = (implicitMerging || explicitMerging || recursiveMerging) && !previousSource->isWindow();

        source = merging ? previousSource->parentWidget() : NULL;
    }
    return source;
}

bool QSoftKeyManager::handleUpdateSoftKeys()
{
    Q_D(QSoftKeyManager);
    int level = 0;
    d->requestedSoftKeyActions.clear();
    bool recursiveMerging = false;
    QWidget *source = softkeySource(NULL, recursiveMerging);
    d->initialSoftKeySource = source;
    while (source) {
        if (appendSoftkeys(*source, level))
            ++level;
        source = softkeySource(source, recursiveMerging);
    }

    d->updateSoftKeys_sys();
    d->pendingUpdate = false;
    return true;
}

void QSoftKeyManager::setForceEnabledInSoftkeys(QAction *action)
{
    QActionPrivate::get(action)->forceEnabledInSoftkeys = true;
}

bool QSoftKeyManager::isForceEnabledInSofkeys(QAction *action)
{
    return QActionPrivate::get(action)->forceEnabledInSoftkeys;
}

bool QSoftKeyManager::event(QEvent *e)
{
#ifndef QT_NO_ACTION
    if (e->type() == QEvent::UpdateSoftKeys)
        return handleUpdateSoftKeys();
#endif //QT_NO_ACTION
    return false;
}

#ifdef Q_WS_S60
bool QSoftKeyManager::handleCommand(int command)
{
    if (QSoftKeyManager::instance()->d_func()->pendingUpdate)
        (void)QSoftKeyManager::instance()->handleUpdateSoftKeys();

    return static_cast<QSoftKeyManagerPrivateS60*>(QSoftKeyManager::instance()->d_func())->handleCommand(command);
}
#endif

QT_END_NAMESPACE
#endif //QT_NO_SOFTKEYMANAGER