~joel-auterson/ubuntu/maverick/ibus/newmenuname

« back to all changes in this revision

Viewing changes to client/qt4/ibus-input-context.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2009-10-05 20:45:18 UTC
  • mfrom: (1.1.5 upstream) (6.1.15 sid)
  • Revision ID: james.westby@ubuntu.com-20091005204518-069vlwrl3r8v7bbr
Tags: 1.2.0.20090927-2
* create po template when build (LP: #188690)
  - debian/rules: updated.
  - debian/clean: remove pot file when clean.
* debian/control: build depends on python-rsvg (LP: #432375)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* vim:set noet ts=4: */
2
 
/*
3
 
 * ibus - The Input Bus
4
 
 *
5
 
 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this program; if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
 
 * Boston, MA  02111-1307  USA
21
 
 */
22
 
#include <config.h>
23
 
#include "ibus-input-context.h"
24
 
#include "ibus-client.h"
25
 
#include <QtDebug>
26
 
#include <QInputMethodEvent>
27
 
#include <QTextCharFormat>
28
 
 
29
 
typedef QInputMethodEvent::Attribute QAttribute;
30
 
 
31
 
IBusInputContext::IBusInputContext (QObject *parent, IBusClient *client, QString &ic)
32
 
        : QInputContext (parent),
33
 
          client (client),
34
 
          ic (ic),
35
 
          preedit_visible (false),
36
 
          has_focus (false),
37
 
          caps (IBUS_CAP_PREEDIT | IBUS_CAP_FOCUS)
38
 
{
39
 
}
40
 
 
41
 
IBusInputContext::~IBusInputContext ()
42
 
{
43
 
        client->releaseInputContext (this);
44
 
}
45
 
 
46
 
bool
47
 
IBusInputContext::filterEvent (const QEvent *event)
48
 
{
49
 
#ifndef Q_WS_X11
50
 
        if (client->filterEvent (this, event)) {
51
 
                return true;
52
 
        }
53
 
        return QInputContext::filterEvent (event);
54
 
#else
55
 
        return QInputContext::filterEvent (event);
56
 
#endif
57
 
}
58
 
 
59
 
QFont
60
 
IBusInputContext::font () const
61
 
{
62
 
        return QInputContext::font ();
63
 
}
64
 
 
65
 
QString
66
 
IBusInputContext::identifierName ()
67
 
{
68
 
        return QString ("ibus");
69
 
}
70
 
 
71
 
QString
72
 
IBusInputContext::language()
73
 
{
74
 
        return QString ("");
75
 
}
76
 
 
77
 
void
78
 
IBusInputContext::mouseHandler (int x, QMouseEvent *event)
79
 
{
80
 
        client->mouseHandler (this, x, event);
81
 
        QInputContext::mouseHandler (x, event);
82
 
}
83
 
 
84
 
void
85
 
IBusInputContext::reset()
86
 
{
87
 
        client->reset (this);
88
 
}
89
 
 
90
 
void
91
 
IBusInputContext::update ()
92
 
{
93
 
        QWidget *widget = focusWidget ();
94
 
 
95
 
        if (widget == NULL) {
96
 
                return;
97
 
        }
98
 
 
99
 
        QRect rect = widget->inputMethodQuery(Qt::ImMicroFocus).toRect ();
100
 
 
101
 
        QPoint topleft = widget->mapToGlobal(QPoint(0,0));
102
 
        rect.translate (topleft);
103
 
 
104
 
        client->setCursorLocation (this, rect);
105
 
 
106
 
#if 0
107
 
        QVariant value;
108
 
        qDebug () << "== update == ";
109
 
        value = widget->inputMethodQuery(Qt::ImMicroFocus);
110
 
        qDebug () << "Qt::ImMicroFocus " << value;
111
 
        value = widget->inputMethodQuery(Qt::ImFont);
112
 
        qDebug () << "Qt::ImFont " <<value;
113
 
        value = widget->inputMethodQuery(Qt::ImCursorPosition);
114
 
        qDebug () << "Qt::ImCursorPosition " << value;
115
 
        value = widget->inputMethodQuery(Qt::ImSurroundingText);
116
 
        qDebug () << "Qt::ImSurroundingText " << value;
117
 
        value = widget->inputMethodQuery(Qt::ImCurrentSelection);
118
 
        qDebug () << "Qt::ImCurrentSelection " << value;
119
 
#endif
120
 
}
121
 
 
122
 
bool
123
 
IBusInputContext::isComposing() const
124
 
{
125
 
        return preedit_visible && !preedit_string.isEmpty ();
126
 
}
127
 
 
128
 
void
129
 
IBusInputContext::setFocusWidget (QWidget *widget)
130
 
{
131
 
        QInputContext::setFocusWidget (widget);
132
 
 
133
 
        if (widget == NULL) {
134
 
                has_focus = false;
135
 
                client->focusOut (this);
136
 
        }
137
 
        else {
138
 
                /* KateView can not support preedit well. */
139
 
                if (widget->inherits("KateViewInternal")) {
140
 
                        caps &= ~IBUS_CAP_PREEDIT;
141
 
                }
142
 
                else {
143
 
                        caps |= IBUS_CAP_PREEDIT;
144
 
                }
145
 
                client->setCapabilities (this, caps);
146
 
 
147
 
                has_focus = true;
148
 
                client->focusIn (this);
149
 
                update ();
150
 
        }
151
 
}
152
 
 
153
 
void
154
 
IBusInputContext::widgetDestroyed (QWidget *widget)
155
 
{
156
 
        QInputContext::widgetDestroyed (widget);
157
 
 
158
 
        if (has_focus) {
159
 
                setFocusWidget (NULL);
160
 
        }
161
 
 
162
 
        update ();
163
 
}
164
 
 
165
 
#ifdef Q_WS_X11
166
 
bool
167
 
IBusInputContext::x11FilterEvent (QWidget *keywidget, XEvent *xevent)
168
 
{
169
 
        if (client->x11FilterEvent (this, keywidget, xevent)) {
170
 
                        return true;
171
 
        }
172
 
        return QInputContext::x11FilterEvent (keywidget, xevent);
173
 
}
174
 
#endif
175
 
 
176
 
void
177
 
IBusInputContext::setIC (QString ic)
178
 
{
179
 
        this->ic = ic;
180
 
        if (ic.isEmpty ())
181
 
                return;
182
 
 
183
 
        client->setCapabilities (this, caps);
184
 
        if (has_focus) {
185
 
                client->focusIn (this);
186
 
        }
187
 
}
188
 
 
189
 
QString
190
 
IBusInputContext::getIC ()
191
 
{
192
 
        return ic;
193
 
}
194
 
 
195
 
void
196
 
IBusInputContext::commitString (QString text)
197
 
{
198
 
        QInputMethodEvent event;
199
 
        event.setCommitString (text);
200
 
        sendEvent (event);
201
 
        update ();
202
 
}
203
 
 
204
 
void
205
 
IBusInputContext::updatePreedit (QString text, QList <QList <quint32> > attr_list, int cursor_pos, bool visible)
206
 
{
207
 
        // qDebug () << text << cursor_pos << show;
208
 
        QList <QAttribute> qattrs;
209
 
 
210
 
        if (visible) {
211
 
                // append cursor pos
212
 
                qattrs.append (QAttribute (QInputMethodEvent::Cursor, cursor_pos, true, 0));
213
 
 
214
 
                // append attributes
215
 
                for (QList <QList <quint32> >::iterator it = attr_list.begin (); it != attr_list.end(); ++ it) {
216
 
 
217
 
                        QList <quint32> attr = *it;
218
 
                        QTextCharFormat format;
219
 
 
220
 
                        switch (attr[0]) {
221
 
                        case 1: // underline
222
 
                                format.setUnderlineStyle (QTextCharFormat::SingleUnderline);
223
 
                                break;
224
 
                        case 2: // foreground
225
 
                                format.setForeground (QBrush (QColor (attr[1])));
226
 
                                break;
227
 
                        case 3: // background
228
 
                                format.setBackground (QBrush (QColor (attr[1])));
229
 
                                break;
230
 
                        default:
231
 
                                break;
232
 
                        }
233
 
 
234
 
                        qattrs.append (QAttribute (QInputMethodEvent::TextFormat, attr[2], attr[3] - attr[2], QVariant (format)));
235
 
                        // qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
236
 
                }
237
 
        }
238
 
        else {
239
 
                qattrs.append (QAttribute (QInputMethodEvent::Cursor, 0, true, 0));
240
 
                text = "";
241
 
                cursor_pos = 0;
242
 
        }
243
 
 
244
 
        preedit_string = text;
245
 
        preedit_visible = visible;
246
 
        preedit_attrs = attr_list;
247
 
        preedit_cursor_pos = cursor_pos;
248
 
 
249
 
        QInputMethodEvent event (text, qattrs);
250
 
        sendEvent (event);
251
 
        update ();
252
 
}
253
 
 
254
 
void
255
 
IBusInputContext::showPreedit ()
256
 
{
257
 
        if (preedit_visible)
258
 
                return;
259
 
 
260
 
        updatePreedit (preedit_string, preedit_attrs, preedit_cursor_pos, TRUE);
261
 
}
262
 
 
263
 
void
264
 
IBusInputContext::hidePreedit ()
265
 
{
266
 
        if (!preedit_visible)
267
 
                return;
268
 
 
269
 
        updatePreedit (preedit_string, preedit_attrs, preedit_cursor_pos, FALSE);
270
 
}
271