~michael-sheldon/ubuntu-keyboard/fix-oxide-dismiss-test

« back to all changes in this revision

Viewing changes to src/lib/logic/eventhandler.cpp

  • Committer: Thomas Moenicke
  • Date: 2013-07-19 12:05:07 UTC
  • Revision ID: thomas.moenicke@canonical.com-20130719120507-lzw5oq50xm567x0j
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of Maliit Plugins
 
3
 *
 
4
 * Copyright (C) 2012 Openismus GmbH. All rights reserved.
 
5
 *
 
6
 * Contact: maliit-discuss@lists.maliit.org
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without modification,
 
9
 * are permitted provided that the following conditions are met:
 
10
 *
 
11
 * Redistributions of source code must retain the above copyright notice, this list
 
12
 * of conditions and the following disclaimer.
 
13
 * Redistributions in binary form must reproduce the above copyright notice, this list
 
14
 * of conditions and the following disclaimer in the documentation and/or other materials
 
15
 * provided with the distribution.
 
16
 * Neither the name of Nokia Corporation nor the names of its contributors may be
 
17
 * used to endorse or promote products derived from this software without specific
 
18
 * prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 
21
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
22
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 
23
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
27
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
28
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 *
 
30
 */
 
31
 
 
32
#include "eventhandler.h"
 
33
#include "layoutupdater.h"
 
34
#include "models/layout.h"
 
35
 
 
36
namespace MaliitKeyboard {
 
37
namespace Logic {
 
38
 
 
39
class EventHandlerPrivate
 
40
{
 
41
public:
 
42
    Model::Layout * const layout;
 
43
    LayoutUpdater * const updater;
 
44
 
 
45
    explicit EventHandlerPrivate(Model::Layout * const new_layout,
 
46
                                 LayoutUpdater * const new_updater);
 
47
};
 
48
 
 
49
 
 
50
EventHandlerPrivate::EventHandlerPrivate(Model::Layout *const new_layout,
 
51
                                         LayoutUpdater *const new_updater)
 
52
    : layout(new_layout)
 
53
    , updater(new_updater)
 
54
{
 
55
    Q_ASSERT(new_layout != 0);
 
56
    Q_ASSERT(new_updater != 0);
 
57
}
 
58
 
 
59
 
 
60
//! \brief Performs event handling for Model::Layout instance, using a LayoutUpdater instance.
 
61
//!
 
62
//! Does not take ownership of either layout or updater.
 
63
EventHandler::EventHandler(Model::Layout * const layout,
 
64
                           LayoutUpdater * const updater,
 
65
                           QObject *parent)
 
66
    : QObject(parent)
 
67
    , d_ptr(new EventHandlerPrivate(layout, updater))
 
68
{}
 
69
 
 
70
 
 
71
EventHandler::~EventHandler()
 
72
{}
 
73
 
 
74
 
 
75
void EventHandler::onExtendedKeysShown(const Key &key)
 
76
{
 
77
    Q_D(EventHandler);
 
78
    d->updater->onExtendedKeysShown(key);
 
79
}
 
80
 
 
81
 
 
82
void EventHandler::onEntered(int index)
 
83
{
 
84
    Q_D(EventHandler);
 
85
 
 
86
    const QVector<Key> &keys(d->layout->keyArea().keys());
 
87
 
 
88
    if (index >= keys.count()) {
 
89
        qWarning() << __PRETTY_FUNCTION__
 
90
                   << "Invalid index:" << index
 
91
                   << "Keys available:" << keys.count();
 
92
        return;
 
93
    }
 
94
 
 
95
    const Key &key(keys.at(index));
 
96
 
 
97
    const Key pressed_key(d->updater->modifyKey(key, KeyDescription::PressedState));
 
98
    d->layout->replaceKey(index, pressed_key);
 
99
    d->updater->onKeyEntered(key);
 
100
 
 
101
    Q_EMIT keyEntered(key);
 
102
}
 
103
 
 
104
 
 
105
void EventHandler::onExited(int index)
 
106
{
 
107
    Q_D(EventHandler);
 
108
 
 
109
    const QVector<Key> &keys(d->layout->keyArea().keys());
 
110
 
 
111
    if (index >= keys.count()) {
 
112
        qWarning() << __PRETTY_FUNCTION__
 
113
                   << "Invalid index:" << index
 
114
                   << "Keys available:" << keys.count();
 
115
        return;
 
116
    }
 
117
 
 
118
    const Key &key(keys.at(index));
 
119
 
 
120
    const Key normal_key(d->updater->modifyKey(key, KeyDescription::NormalState));
 
121
    d->layout->replaceKey(index, normal_key);
 
122
    d->updater->onKeyExited(normal_key);
 
123
 
 
124
    Q_EMIT keyExited(key);
 
125
}
 
126
 
 
127
 
 
128
void EventHandler::onPressed(int index)
 
129
{
 
130
    Q_D(EventHandler);
 
131
 
 
132
    const QVector<Key> &keys(d->layout->keyArea().keys());
 
133
 
 
134
    if (index >= keys.count()) {
 
135
        qWarning() << __PRETTY_FUNCTION__
 
136
                   << "Invalid index:" << index
 
137
                   << "Keys available:" << keys.count();
 
138
        return;
 
139
    }
 
140
 
 
141
    const Key &key(keys.at(index));
 
142
 
 
143
    const Key pressed_key(d->updater->modifyKey(key, KeyDescription::PressedState));
 
144
    d->layout->replaceKey(index, pressed_key);
 
145
    d->updater->onKeyPressed(pressed_key);
 
146
 
 
147
    Q_EMIT keyPressed(pressed_key);
 
148
}
 
149
 
 
150
 
 
151
void EventHandler::onReleased(int index)
 
152
{
 
153
    Q_D(EventHandler);
 
154
 
 
155
    const QVector<Key> &keys(d->layout->keyArea().keys());
 
156
 
 
157
    if (index >= keys.count()) {
 
158
        qWarning() << __PRETTY_FUNCTION__
 
159
                   << "Invalid index:" << index
 
160
                   << "Keys available:" << keys.count();
 
161
        return;
 
162
    }
 
163
 
 
164
    const Key &key(keys.at(index));
 
165
 
 
166
    const Key normal_key(d->updater->modifyKey(key, KeyDescription::NormalState));
 
167
    d->layout->replaceKey(index, normal_key);
 
168
    d->updater->onKeyReleased(normal_key);
 
169
 
 
170
    Q_EMIT keyReleased(normal_key);
 
171
}
 
172
 
 
173
 
 
174
void EventHandler::onPressAndHold(int index)
 
175
{
 
176
    Q_D(EventHandler);
 
177
 
 
178
    const QVector<Key> &keys(d->layout->keyArea().keys());
 
179
 
 
180
    if (index >= keys.count()) {
 
181
        qWarning() << __PRETTY_FUNCTION__
 
182
                   << "Invalid index:" << index
 
183
                   << "Keys available:" << keys.count();
 
184
        return;
 
185
    }
 
186
 
 
187
    const Key &key(keys.at(index));
 
188
 
 
189
    // FIXME: long-press on space needs to work again to save words to dictionary!
 
190
    if (key.hasExtendedKeys()) {
 
191
        Q_EMIT extendedKeysShown(key);
 
192
    }
 
193
 
 
194
    Q_EMIT keyLongPressed(key);
 
195
}
 
196
 
 
197
void EventHandler::onWordCandidatePressed(QString word)
 
198
{
 
199
    WordCandidate candidate(WordCandidate::SourcePrediction, word);
 
200
    Q_EMIT wordCandidatePressed(candidate);
 
201
}
 
202
 
 
203
void EventHandler::onWordCandidateReleased(QString word)
 
204
{
 
205
    WordCandidate candidate(WordCandidate::SourcePrediction, word);
 
206
    Q_EMIT wordCandidateReleased(candidate);
 
207
}
 
208
 
 
209
void EventHandler::onLanguageChangeRequested(QString languageId)
 
210
{
 
211
    Q_D(EventHandler);
 
212
 
 
213
    d->updater->setActiveKeyboardId(languageId);
 
214
}
 
215
 
 
216
}} // namespace Logic, MaliitKeyboard