Qt 4.8
qmultiinputcontext.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 /****************************************************************************
43 **
44 ** Implementation of QMultiInputContext class
45 **
46 ** Copyright (C) 2004 immodule for Qt Project. All rights reserved.
47 **
48 ** This file is written to contribute to Nokia Corporation and/or its subsidiary(-ies) under their own
49 ** license. You may use this file under your Qt license. Following
50 ** description is copied from their original file headers. Contact
51 ** immodule-qt@freedesktop.org if any conditions of this licensing are
52 ** not clear to you.
53 **
54 ****************************************************************************/
55 
56 #ifndef QT_NO_IM
57 #include "qmultiinputcontext.h"
58 #include <qinputcontextfactory.h>
59 #include <qstringlist.h>
60 #include <qaction.h>
61 #include <qsettings.h>
62 #include <qmenu.h>
63 
64 #include <stdlib.h>
65 
67 
69  : QInputContext(), current(-1)
70 {
72  for (int i = keys.size()-1; i >= 0; --i)
73  if (keys.at(i).contains(QLatin1String("imsw")))
74  keys.removeAt(i);
75 
76  QString def = QLatin1String(getenv("QT4_IM_MODULE"));
77  if (def.isEmpty())
78  def = QLatin1String(getenv("QT_IM_MODULE"));
79  if (def.isEmpty()) {
80  QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
81  settings.beginGroup(QLatin1String("Qt"));
82  def = settings.value(QLatin1String("DefaultInputMethod"), QLatin1String("xim")).toString();
83  }
84  current = keys.indexOf(def);
85  if (current < 0)
86  current = 0;
87 
88  menu = new QMenu(tr("Select IM"));
89  separator = new QAction(this);
90  separator->setSeparator(true);
91 
92  QActionGroup *group = new QActionGroup(this);
93  for (int i = 0; i < keys.size(); ++i) {
94  slaves.append(0);
95  const QString key = keys.at(i);
97  a->setData(key);
98  a->setCheckable(true);
99  group->addAction(a);
100  if (i == current) {
102  a->setChecked(true);
103  }
104  }
105  connect(group, SIGNAL(triggered(QAction*)), this, SLOT(changeSlave(QAction*)));
106 }
107 
109 {
110  delete menu;
111 }
112 
113 
115 {
116  return (slave()) ? slave()->identifierName() : QLatin1String("");
117 }
118 
120 {
121  return (slave()) ? slave()->language() : QLatin1String("");
122 }
123 
124 
125 #if defined(Q_WS_X11)
127 {
128  return (slave()) ? slave()->x11FilterEvent(keywidget, event) : false;
129 }
130 #endif // Q_WS_X11
131 
132 
134 {
135  return (slave()) ? slave()->filterEvent(event) : false;
136 }
137 
139 {
140  if (slave())
141  slave()->reset();
142 }
143 
145 {
146  if (slave())
147  slave()->update();
148 }
149 
151 {
152  if (slave())
153  slave()->mouseHandler(x, event);
154 }
155 
157 {
158  return (slave()) ? slave()->font() : QInputContext::font();
159 }
160 
162 {
164  if (slave())
165  slave()->setFocusWidget(w);
166 }
167 
169 {
171 }
172 
174 {
175  if (slave())
176  slave()->widgetDestroyed(w);
177 }
178 
180 {
181  return (slave()) ? slave()->isComposing() : false;
182 }
183 
185 {
187  a.append(separator);
188  a.append(menu->menuAction());
189  return a;
190 }
191 
193 {
194  for (int i = 0; i < slaves.size(); ++i) {
195  if (keys.at(i) == a->data().toString()) {
196  if (slaves.at(i) == 0)
198  QInputContext *qic = slaves.at(current);
199  QWidget *oldWidget = qic->focusWidget();
200  qic->reset();
201  qic->setFocusWidget(0);
202  current = i;
203  qic = slaves.at(current);
204  qic->setFocusWidget(oldWidget);
205  return;
206  }
207  }
208 }
209 
211 
212 #endif // QT_NO_IM
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
void update()
This virtual function is called when a state in the focus widget has changed.
static QInputContext * create(const QString &key, QObject *parent)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Returns the value for setting key.
Definition: qsettings.cpp:3460
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setSeparator(bool b)
If b is true then this action will be considered a separator.
Definition: qaction.cpp:823
void setFocusWidget(QWidget *w)
Sets the widget that has an input focus for this input context.
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
void setCheckable(bool)
Definition: qaction.cpp:1094
static QString displayName(const QString &key)
Returns a user friendly internationalized name of the QInputContext object specified by key...
#define SLOT(a)
Definition: qobjectdefs.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
void beginGroup(const QString &prefix)
Appends prefix to the current group.
Definition: qsettings.cpp:3073
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
virtual bool isComposing() const =0
This function indicates whether InputMethodStart event had been sent to the current focus widget...
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
long ASN1_INTEGER_get ASN1_INTEGER * a
QString identifierName()
This function must be implemented in any subclasses to return the identifier name of the input method...
QWidget * focusWidget() const
Returns the widget that has an input focus for this input context.
The QString class provides a Unicode character string.
Definition: qstring.h:83
QAction * addAction(const QString &text)
This convenience function creates a new action with text.
Definition: qmenu.cpp:1453
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
virtual QList< QAction * > actions()
This is a preliminary interface for Qt 4.
The QActionGroup class groups actions together.
Definition: qactiongroup.h:57
void setData(const QVariant &var)
Sets the action&#39;s internal data to the given userData.
Definition: qaction.cpp:1297
void widgetDestroyed(QWidget *w)
This virtual function is called when the specified widget is destroyed.
union _XEvent XEvent
Definition: qwindowdefs.h:116
#define SIGNAL(a)
Definition: qobjectdefs.h:227
QFont font() const
Returns the font of the current input widget.
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QList< QAction * > actions()
This is a preliminary interface for Qt 4.
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void widgetDestroyed(QWidget *w)
This virtual function is called when the specified widget is destroyed.
QInputContext * slave()
virtual void update()
This virtual function is called when a state in the focus widget has changed.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString language()
This function must be implemented in any subclasses to return a language code (e. ...
QAction * addAction(QAction *a)
Adds the action to this group, and returns it.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QList< QInputContext * > slaves
void mouseHandler(int x, QMouseEvent *event)
This function can be reimplemented in a subclass to handle mouse press, release, double-click, and move events within the preedit text.
virtual void mouseHandler(int x, QMouseEvent *event)
This function can be reimplemented in a subclass to handle mouse press, release, double-click, and move events within the preedit text.
static QStringList keys()
Returns the list of keys this factory can create input contexts for.
virtual QString identifierName()=0
This function must be implemented in any subclasses to return the identifier name of the input method...
void replace(int i, const T &t)
Replaces the item at index position i with value.
Definition: qlist.h:609
virtual QFont font() const
Returns the font of the current input widget.
The QMouseEvent class contains parameters that describe a mouse event.
Definition: qevent.h:85
virtual QString language()=0
This function must be implemented in any subclasses to return a language code (e. ...
bool filterEvent(const QEvent *event)
This function can be reimplemented in a subclass to filter input events.
The QMenu class provides a menu widget for use in menu bars, context menus, and other popup menus...
Definition: qmenu.h:72
void setChecked(bool)
Definition: qaction.cpp:1138
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
int key
QVariant data() const
Returns the user data as set in QAction::setData.
Definition: qaction.cpp:1280
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
virtual void reset()=0
This function can be reimplemented in a subclass to reset the state of the input method.
QAction * menuAction() const
Returns the action associated with this menu.
Definition: qmenu.cpp:711
bool isComposing() const
This function indicates whether InputMethodStart event had been sent to the current focus widget...
virtual bool x11FilterEvent(QWidget *keywidget, XEvent *event)
This function may be overridden only if input method is depending on X11 and you need raw XEvent...
The QInputContext class abstracts the input method dependent data and composing state.
Definition: qinputcontext.h:83
void reset()
This function can be reimplemented in a subclass to reset the state of the input method.
virtual void setFocusWidget(QWidget *w)
Sets the widget that has an input focus for this input context.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
QWidget * focusWidget() const
void changeSlave(QAction *)
bool x11FilterEvent(QWidget *keywidget, XEvent *event)
This function may be overridden only if input method is depending on X11 and you need raw XEvent...
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
virtual bool filterEvent(const QEvent *event)
This function can be reimplemented in a subclass to filter input events.
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void removeAt(int i)
Removes the item at index position i.
Definition: qlist.h:480