Refine coding style.
[ibus.git] / qt4 / ibus-input-context.cpp
blob6a00ec7ed9b62e664ffaec29ddc2fc23ecd414fb
1 /* vim:set noet ts=4: */
2 /*
3 * ibus - The Input Bus
5 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
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.
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.
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
22 #include "ibus-input-context.h"
23 #include "ibus-client.h"
24 #include <QtDebug>
25 #include <QInputMethodEvent>
26 #include <QTextCharFormat>
28 typedef QInputMethodEvent::Attribute QAttribute;
30 IBusInputContext::IBusInputContext (QObject *parent, IBusClient *client, QString &ic)
31 : QInputContext (parent), client (client), ic (ic), preedit_visible (false)
35 IBusInputContext::~IBusInputContext ()
37 client->releaseInputContext (this);
40 bool
41 IBusInputContext::filterEvent (const QEvent *event)
43 #ifndef Q_WS_X11
44 if (client->filterEvent (this, event))
45 return true;
46 return QInputContext::filterEvent (event);
47 #else
48 return QInputContext::filterEvent (event);
49 #endif
52 QFont
53 IBusInputContext::font () const
55 return QInputContext::font ();
58 QString
59 IBusInputContext::identifierName ()
61 return QString ("ibus");
64 QString
65 IBusInputContext::language()
67 return QString ("");
70 void
71 IBusInputContext::mouseHandler (int x, QMouseEvent *event)
73 client->mouseHandler (this, x, event);
74 QInputContext::mouseHandler (x, event);
77 void
78 IBusInputContext::reset()
80 client->reset (this);
83 void
84 IBusInputContext::update ()
86 QWidget *widget;
88 if ((widget = focusWidget ()) == NULL)
89 return;
91 QRect rect = widget->inputMethodQuery(Qt::ImMicroFocus).toRect ();
93 #if 0
94 QFont font = widget->inputMethodQuery(Qt::ImFont).value <QFont> ();
95 qDebug () << rect << preedit_string << preedit_cursor_pos;
97 QFontMetrics fm(font);
98 int textWidth = fm.width (preedit_string.left (preedit_cursor_pos));
99 rect.translate (textWidth, 0);
100 #endif
102 QPoint topleft = widget->mapToGlobal(QPoint(0,0));
103 rect.translate (topleft);
104 if (cursor_location != rect ) {
105 client->setCursorLocation (this, rect);
106 cursor_location = rect;
109 #if 0
110 QVariant value;
111 value = widget->inputMethodQuery(Qt::ImMicroFocus);
112 qDebug () << value;
113 value = widget->inputMethodQuery(Qt::ImFont);
114 qDebug () << value;
115 value = widget->inputMethodQuery(Qt::ImCursorPosition);
116 qDebug () << value;
117 value = widget->inputMethodQuery(Qt::ImSurroundingText);
118 qDebug () << value;
119 value = widget->inputMethodQuery(Qt::ImCurrentSelection);
120 qDebug () << value;
121 #endif
124 bool
125 IBusInputContext::isComposing() const
127 return (!preedit_string.isEmpty ()) && preedit_visible;
130 void
131 IBusInputContext::setFocusWidget (QWidget *widget)
133 // qDebug () << "setFocusWidget (" << widget << ")";
134 QInputContext::setFocusWidget (widget);
135 update ();
138 void
139 IBusInputContext::widgetDestroyed (QWidget *widget)
141 QInputContext::widgetDestroyed (widget);
142 update ();
145 #ifdef Q_WS_X11
146 bool
147 IBusInputContext::x11FilterEvent (QWidget *keywidget, XEvent *xevent)
149 if (client->x11FilterEvent (this, keywidget, xevent))
150 return true;
151 return QInputContext::x11FilterEvent (keywidget, xevent);
153 #endif
155 void
156 IBusInputContext::setIC (QString ic)
158 this->ic = ic;
161 QString
162 IBusInputContext::getIC ()
164 return ic;
167 void
168 IBusInputContext::commitString (QString text)
170 QInputMethodEvent event;
171 event.setCommitString (text);
172 sendEvent (event);
173 update ();
176 void
177 IBusInputContext::updatePreedit (QString text, QList <QList <quint32> > attr_list, int cursor_pos, bool show)
179 // qDebug () << text << cursor_pos << show;
180 QList <QAttribute> qattrs;
182 if (show) {
183 // append cursor pos
184 qattrs.append (QAttribute (QInputMethodEvent::Cursor, cursor_pos, true, 0));
186 // append attributes
187 for (QList <QList <quint32> >::iterator it = attr_list.begin (); it != attr_list.end(); ++ it) {
189 QList <quint32> attr = *it;
190 QTextCharFormat format;
192 switch (attr[0]) {
193 case 1: // underline
194 format.setUnderlineStyle (QTextCharFormat::SingleUnderline);
195 break;
196 case 2: // foreground
197 format.setForeground (QBrush (QColor (attr[1])));
198 break;
199 case 3: // background
200 format.setBackground (QBrush (QColor (attr[1])));
201 break;
202 default:
203 break;
206 qattrs.append (QAttribute (QInputMethodEvent::TextFormat, attr[2], attr[3] - attr[2], QVariant (format)));
207 // qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
210 else {
211 qattrs.append (QAttribute (QInputMethodEvent::Cursor, 0, true, 0));
212 text = "";
213 cursor_pos = 0;
216 preedit_string = text;
217 preedit_visible = show;
218 preedit_cursor_pos = cursor_pos;
220 QInputMethodEvent event (text, qattrs);
221 sendEvent (event);
222 update ();