Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / imports / inputcontext / declarativeinputcontext.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "declarativeinputcontext.h"
43
44 #include "inputcontextmodule.h"
45 #include "inputcontextfilter.h"
46
47 #include <QtCore/qdebug.h>
48
49 QT_BEGIN_NAMESPACE
50
51 DeclarativeInputContext::DeclarativeInputContext(QObject *parent)
52     : QInputContext(parent)
53     , m_module(0)
54 {
55 }
56
57 DeclarativeInputContext::~DeclarativeInputContext()
58 {
59 }
60
61 bool DeclarativeInputContext::isComposing() const
62 {
63     return m_module && !m_module->preeditText().isEmpty();
64 }
65
66 QString DeclarativeInputContext::identifierName()
67 {
68     return QLatin1String("Qt.labs.inputcontext/1.0");
69 }
70
71 QString DeclarativeInputContext::language()
72 {
73     return QString();
74 }
75
76 void DeclarativeInputContext::setFocusWidget(QWidget *widget)
77 {
78     QInputContext::setFocusWidget(widget);
79
80     if (m_module)
81         m_module->setFocusWidget(widget);
82 }
83
84 void DeclarativeInputContext::mouseHandler(int x, QMouseEvent *event)
85 {
86     if (!m_mouseHandlers.isEmpty()) {
87         InputContextMouseEvent me(*event);
88         foreach (InputContextMouseHandler *handler, m_mouseHandlers) {
89             handler->processEvent(event->type(), x, &me);
90             if (me.isAccepted()) {
91                 event->setAccepted(true);
92                 return;
93             }
94         }
95     }
96 }
97
98 bool DeclarativeInputContext::filterMouseEvent(const QMouseEvent *event)
99 {
100     if (!m_mouseFilters.isEmpty()) {
101         InputContextMouseEvent me(*event);
102         foreach (InputContextMouseFilter *filter, m_mouseFilters) {
103             filter->processEvent(event->type(), &me);
104             if (me.isAccepted())
105                 return true;
106         }
107     }
108
109     return false;
110 }
111
112 bool DeclarativeInputContext::filterKeyEvent(const QKeyEvent *event)
113 {
114     if (!m_keyFilters.isEmpty()) {
115         InputContextKeyEvent ke(*event);
116         foreach (InputContextKeyFilter *filter, m_keyFilters) {
117             filter->processEvent(event->type(), &ke);
118             if (ke.isAccepted())
119                 return true;
120         }
121     }
122     return false;
123 }
124
125 bool DeclarativeInputContext::filterEvent(const QEvent *event)
126 {
127     switch (event->type()) {
128     case QEvent::RequestSoftwareInputPanel:
129         if (m_module)
130             m_module->setVisible(true);
131         return true;
132     case QEvent::CloseSoftwareInputPanel:
133         if (m_module)
134             m_module->setVisible(false);
135         return true;
136     case QEvent::MouseButtonPress:
137     case QEvent::MouseButtonRelease:
138     case QEvent::MouseButtonDblClick:
139     case QEvent::MouseMove:
140         return filterMouseEvent(static_cast<const QMouseEvent *>(event));
141     case QEvent::KeyPress:
142     case QEvent::KeyRelease:
143         return filterKeyEvent(static_cast<const QKeyEvent *>(event));
144     default:
145         return false;
146     }
147 }
148
149 void DeclarativeInputContext::reset()
150 {
151     if (m_module)
152         m_module->commit();
153 }
154
155 void DeclarativeInputContext::update()
156 {
157     if (m_module)
158         m_module->update();
159 }
160
161 void DeclarativeInputContext::setModule(InputContextModule *module)
162 {
163     m_module = module;
164 }
165
166 void DeclarativeInputContext::registerMouseHandler(InputContextMouseHandler *handler)
167 {
168     connect(handler, SIGNAL(destroyed(QObject*)), this, SLOT(mouseHandlerDestroyed(QObject*)));
169     m_mouseHandlers.append(handler);
170 }
171
172 void DeclarativeInputContext::registerMouseFilter(InputContextMouseFilter *filter)
173 {
174     connect(filter, SIGNAL(destroyed(QObject*)), this, SLOT(mouseFilterDestroyed(QObject*)));
175     m_mouseFilters.append(filter);
176 }
177
178 void DeclarativeInputContext::registerKeyFilter(InputContextKeyFilter *filter)
179 {
180     connect(filter, SIGNAL(destroyed(QObject*)), this, SLOT(keyFilterDestroyed(QObject*)));
181     m_keyFilters.append(filter);
182 }
183
184 void DeclarativeInputContext::mouseHandlerDestroyed(QObject *handler)
185 {
186     m_mouseHandlers.removeAll(static_cast<InputContextMouseHandler *>(handler));
187 }
188
189 void DeclarativeInputContext::mouseFilterDestroyed(QObject *filter)
190 {
191     m_mouseFilters.removeAll(static_cast<InputContextMouseFilter *>(filter));
192 }
193
194 void DeclarativeInputContext::keyFilterDestroyed(QObject *filter)
195 {
196     m_keyFilters.removeAll(static_cast<InputContextKeyFilter *>(filter));
197 }
198
199 QT_END_NAMESPACE