|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbPlugins module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 #include <QTimer> |
|
26 #include <hbinputmethod.h> |
|
27 #include <hbinputkeymapfactory.h> |
|
28 #include <hbinputpredictionengine.h> |
|
29 |
|
30 #include "hbhardwareinputprediction12keyhandler.h" |
|
31 #include "hbinputpredictionhandler_p.h" |
|
32 #include "hbinputabstractbase.h" |
|
33 |
|
34 class HbHardwareInputPrediction12KeyHandlerPrivate: public HbInputPredictionHandlerPrivate |
|
35 { |
|
36 Q_DECLARE_PUBLIC(HbHardwareInputPrediction12KeyHandler) |
|
37 |
|
38 public: |
|
39 HbHardwareInputPrediction12KeyHandlerPrivate(); |
|
40 ~HbHardwareInputPrediction12KeyHandlerPrivate(); |
|
41 |
|
42 bool keyReleased(const QKeyEvent *keyEvent); |
|
43 bool keyPressed(const QKeyEvent *keyEvent); |
|
44 void _q_timeout(); |
|
45 |
|
46 public: |
|
47 int mLastKey; |
|
48 bool mButtonDown; |
|
49 QChar mCurrentChar; |
|
50 }; |
|
51 |
|
52 HbHardwareInputPrediction12KeyHandlerPrivate::HbHardwareInputPrediction12KeyHandlerPrivate() |
|
53 :mLastKey(0), |
|
54 mButtonDown(false), |
|
55 mCurrentChar(0) |
|
56 { |
|
57 } |
|
58 |
|
59 HbHardwareInputPrediction12KeyHandlerPrivate::~HbHardwareInputPrediction12KeyHandlerPrivate() |
|
60 { |
|
61 } |
|
62 |
|
63 void HbHardwareInputPrediction12KeyHandlerPrivate::_q_timeout() |
|
64 { |
|
65 qDebug("HbHardwareInputPrediction12KeyHandlerPrivate::_q_timeout()"); |
|
66 Q_Q(HbHardwareInputPrediction12KeyHandler); |
|
67 |
|
68 // let's stop the timer first. |
|
69 mTimer->stop(); |
|
70 |
|
71 //Long key press number key is applicable to all keys |
|
72 if (mButtonDown && mCanContinuePrediction) { |
|
73 if (mLastKey == Qt::Key_Asterisk) { |
|
74 q->actionHandler(HbInputModeHandler::HbInputModeActionLaunchCandidatePopup); |
|
75 } else if (mLastKey == Qt::Key_Shift) { |
|
76 updateTextCase(); |
|
77 mInputMethod->switchMode(Qt::Key_Shift); |
|
78 } else { |
|
79 q->actionHandler(HbInputModeHandler::HbInputModeActionDeleteAndCommit); |
|
80 q->commitFirstMappedNumber(mLastKey); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 bool HbHardwareInputPrediction12KeyHandlerPrivate::keyPressed(const QKeyEvent *keyEvent) |
|
86 { |
|
87 Q_Q(HbHardwareInputPrediction12KeyHandler); |
|
88 HbInputFocusObject *focusObject = 0; |
|
89 focusObject = mInputMethod->focusObject(); |
|
90 if (!focusObject) { |
|
91 return false; |
|
92 } |
|
93 |
|
94 int buttonId = keyEvent->key(); |
|
95 // A new key has been pressed, so the timer for the previous key is not valid. |
|
96 if (((mLastKey != buttonId)) && mTimer->isActive()) { |
|
97 mTimer->stop(); |
|
98 mInputMethod->updateState(); |
|
99 } |
|
100 |
|
101 if (buttonId == Qt::Key_Return) { |
|
102 |
|
103 return true; |
|
104 } else if (buttonId == Qt::Key_Shift) { |
|
105 if (mTimer->isActive() && (mLastKey == buttonId)){ |
|
106 mTimer->stop(); |
|
107 //Since the mode change should not commit the auto-completed part |
|
108 //we need to disable the tail. |
|
109 mShowTail = false; |
|
110 //This should switch to the numeric mode and with a long key press |
|
111 //should be back to the prediction mode again. |
|
112 //mInputMethod->activateNextState(); |
|
113 mInputMethod->switchMode(Qt::Key_Shift); |
|
114 } else { |
|
115 updateTextCase(); |
|
116 mTimer->start(HbLongPressTimerTimeout); |
|
117 } |
|
118 mLastKey = buttonId; |
|
119 mButtonDown = true; |
|
120 return true; |
|
121 } else if(buttonId == Qt::Key_Down) { |
|
122 q->actionHandler(HbInputModeHandler::HbInputModeActionLaunchCandidatePopup); |
|
123 return true; |
|
124 } |
|
125 |
|
126 mLastKey = buttonId; |
|
127 mButtonDown = true; |
|
128 |
|
129 // Asterisk key / sym key is handled in this class it self, so not passing it to |
|
130 // the base mode handlers. |
|
131 if (buttonId != Qt::Key_Asterisk) { |
|
132 q->HbInputPredictionHandler::filterEvent(keyEvent); |
|
133 } |
|
134 |
|
135 // custom button should not start timer. |
|
136 if ((buttonId & CUSTOM_INPUT_MASK) != CUSTOM_INPUT_MASK) { |
|
137 mTimer->start(HbLongPressTimerTimeout); |
|
138 } |
|
139 |
|
140 return true; |
|
141 } |
|
142 |
|
143 /*! |
|
144 Handles the key release events from the VKB. Launches the SCT with key release event of |
|
145 asterisk. |
|
146 */ |
|
147 bool HbHardwareInputPrediction12KeyHandlerPrivate::keyReleased(const QKeyEvent *keyEvent) |
|
148 { |
|
149 Q_Q(HbHardwareInputPrediction12KeyHandler); |
|
150 // since button is released we can set buttonDown back to false. |
|
151 mButtonDown = false; |
|
152 |
|
153 if (keyEvent->key() == Qt::Key_Asterisk) { |
|
154 //Same asterisk key is used for launching candidate list (long key press) |
|
155 //and also for SCT. So, do not launch SCT if candidate list is already launched. |
|
156 mInputMethod->switchMode(Qt::Key_Asterisk); |
|
157 return true; |
|
158 } else if (keyEvent->key() == Qt::Key_Delete) { |
|
159 QKeyEvent keyEvent(QEvent::KeyRelease, Qt::Key_Backspace, Qt::NoModifier); |
|
160 q->sendAndUpdate(keyEvent); |
|
161 return true; |
|
162 } |
|
163 |
|
164 return false; |
|
165 } |
|
166 |
|
167 |
|
168 HbHardwareInputPrediction12KeyHandler::HbHardwareInputPrediction12KeyHandler(HbInputAbstractMethod *inputMethod) |
|
169 :HbInputPredictionHandler(* new HbHardwareInputPrediction12KeyHandlerPrivate, inputMethod) |
|
170 { |
|
171 Q_D(HbHardwareInputPrediction12KeyHandler); |
|
172 d->q_ptr = this; |
|
173 } |
|
174 |
|
175 HbHardwareInputPrediction12KeyHandler::~HbHardwareInputPrediction12KeyHandler() |
|
176 { |
|
177 } |
|
178 |
|
179 /*! |
|
180 this function lists different modes. |
|
181 */ |
|
182 void HbHardwareInputPrediction12KeyHandler::listInputModes(QVector<HbInputModeProperties>& modes) const |
|
183 { |
|
184 Q_UNUSED(modes); |
|
185 /*HbInputMode binding; |
|
186 binding.iMode = HbInputModeLatinPredictive; |
|
187 binding.iKeyboard = HbKeyboard12Key; |
|
188 QVector<int> languages; |
|
189 HbKeyMapFactory::instance()->listLanguages(languages); |
|
190 |
|
191 const QVector<HbInputLanguage>* engineLanguages = supportedLanguages(); |
|
192 if (engineLanguages) { |
|
193 binding.iMode = HbInputModeLatinPredictive; |
|
194 foreach (const HbInputLanguage& languageCode, *engineLanguages) { |
|
195 if (languages.contains(languageCode.language())) { |
|
196 binding.iLanguage = languageCode; |
|
197 modes.push_back(binding); |
|
198 } |
|
199 } |
|
200 }*/ |
|
201 } |
|
202 |
|
203 /*! |
|
204 filterEvent to handler keypress/release events. |
|
205 */ |
|
206 bool HbHardwareInputPrediction12KeyHandler::filterEvent(const QKeyEvent * event) |
|
207 { |
|
208 Q_D(HbHardwareInputPrediction12KeyHandler); |
|
209 |
|
210 HbInputFocusObject *focusObject = 0; |
|
211 focusObject = d->mInputMethod->focusObject(); |
|
212 |
|
213 //If there was a handling for empty candidate-list, i.e. the engine did not predict |
|
214 //any meaningful word for the input sequence. |
|
215 if(!d->mCanContinuePrediction) { |
|
216 int eventKey = event->key(); |
|
217 switch(eventKey) { |
|
218 case Qt::Key_0: |
|
219 case Qt::Key_Asterisk: |
|
220 case Qt::Key_Space: { |
|
221 if(d->mCandidates->size() && focusObject) { |
|
222 //Remove the "?" mark |
|
223 (*d->mCandidates)[d->mBestGuessLocation].chop(1); |
|
224 d->updateEditor(); |
|
225 d->mCanContinuePrediction = true; |
|
226 } |
|
227 } |
|
228 break; |
|
229 case Qt::Key_Shift: { |
|
230 if(event->type() == QEvent::KeyRelease) { |
|
231 //Remove the "?" mark |
|
232 deleteOneCharacter(); |
|
233 } |
|
234 } |
|
235 //For the following set of keys, it does not matter. |
|
236 case Qt::Key_Backspace: |
|
237 case Qt::Key_Delete: |
|
238 case Qt::Key_Return: |
|
239 case Qt::Key_Enter: { |
|
240 } |
|
241 break; |
|
242 //The default behavior for any other key press is just to consume the key event and |
|
243 //not to do anything. |
|
244 default: { |
|
245 return true; |
|
246 } |
|
247 break; |
|
248 } |
|
249 } |
|
250 |
|
251 // prediction mode can't handle Qt::Key_0, so we will emit a passFilterEvent |
|
252 // this signal must be connected to by the plugin to a modehandler. |
|
253 // which can handle it. |
|
254 if (event->key() == Qt::Key_0) { |
|
255 // if this previous key is not Qt::Key_0, we must commit it. |
|
256 if (d->mLastKey != Qt::Key_0) { |
|
257 actionHandler(HbInputModeHandler::HbInputModeActionCommit); |
|
258 } |
|
259 emit passFilterEvent(event); |
|
260 d->mLastKey = Qt::Key_0; |
|
261 return true; |
|
262 } else { |
|
263 if (d->mLastKey == Qt::Key_0) { |
|
264 emit passActionHandler(HbInputModeActionCommit); |
|
265 } |
|
266 if (event->isAutoRepeat()) { |
|
267 // Ignore all repeat events after first repeat event |
|
268 return true; |
|
269 } else if (event->type() == QEvent::KeyRelease) { |
|
270 return d->keyReleased(event); |
|
271 } else { |
|
272 return d->keyPressed(event); |
|
273 } |
|
274 } |
|
275 } |
|
276 |
|
277 /*! |
|
278 Action handler |
|
279 */ |
|
280 bool HbHardwareInputPrediction12KeyHandler::actionHandler(HbInputModeAction action) |
|
281 { |
|
282 Q_D(HbHardwareInputPrediction12KeyHandler); |
|
283 bool ret = true; |
|
284 switch (action) { |
|
285 case HbInputModeActionReset: |
|
286 HbInputPredictionHandler::actionHandler(HbInputModeActionReset); |
|
287 d->mTimer->stop(); |
|
288 break; |
|
289 case HbInputModeActionFocusRecieved: |
|
290 HbInputPredictionHandler::actionHandler(HbInputModeActionSetCandidateList); |
|
291 HbInputPredictionHandler::actionHandler(HbInputModeActionSetKeypad); |
|
292 d->mTimer->stop(); |
|
293 break; |
|
294 default: |
|
295 ret = HbInputPredictionHandler::actionHandler(action); |
|
296 break; |
|
297 } |
|
298 |
|
299 return ret; |
|
300 } |
|
301 |
|
302 /*! |
|
303 Returns true if preidciton engine is available and initialized. |
|
304 */ |
|
305 bool HbHardwareInputPrediction12KeyHandler::isActive() const |
|
306 { |
|
307 Q_D(const HbHardwareInputPrediction12KeyHandler); |
|
308 return d->mEngine != 0; |
|
309 } |
|
310 |
|
311 //EOF |