call update after commitString.
[ibus.git] / qt / ibus-input-context.cpp
blobbd3e3d1c64926024411242d25debc32062fc0026
1 #include "ibus-input-context.h"
2 #include "ibus-client.h"
3 #include <QtDebug>
4 #include <QInputMethodEvent>
5 #include <QTextCharFormat>
7 typedef QInputMethodEvent::Attribute QAttribute;
9 IBusInputContext::IBusInputContext (QObject *parent, IBusClient *client, QString &ic)
10 : QInputContext (parent), client (client), ic (ic), preedit_visible (false)
14 IBusInputContext::~IBusInputContext ()
16 client->releaseInputContext (this);
19 bool
20 IBusInputContext::filterEvent (const QEvent *event)
22 #ifndef Q_WS_X11
23 if (client->filterEvent (this, event))
24 return true;
25 return QInputContext::filterEvent (event);
26 #else
27 return QInputContext::filterEvent (event);
28 #endif
31 QFont
32 IBusInputContext::font () const
34 return QInputContext::font ();
37 QString
38 IBusInputContext::identifierName ()
40 return QString ("ibus");
43 QString
44 IBusInputContext::language()
46 return QString ("");
49 void
50 IBusInputContext::mouseHandler (int x, QMouseEvent *event)
52 client->mouseHandler (this, x, event);
53 QInputContext::mouseHandler (x, event);
56 void
57 IBusInputContext::reset()
59 client->reset (this);
62 void
63 IBusInputContext::update ()
65 QWidget *widget;
67 if ((widget = focusWidget ()) == NULL)
68 return;
70 QRect rect = widget->inputMethodQuery(Qt::ImMicroFocus).toRect ();
71 #if 0
72 QFont font = widget->inputMethodQuery(Qt::ImFont).value <QFont> ();
73 qDebug () << rect << preedit_string << preedit_cursor_pos;
75 QFontMetrics fm(font);
76 int textWidth = fm.width (preedit_string.left (preedit_cursor_pos));
77 rect.translate (textWidth, 0);
78 #endif
79 QPoint topleft = widget->mapToGlobal(QPoint(0,0));
80 rect.translate (topleft);
81 client->setCursorLocation (this, rect);
83 // value = widget->inputMethodQuery(Qt::ImFont);
84 // qDebug () << value;
85 // value = widget->inputMethodQuery(Qt::ImCursorPosition);
86 // qDebug () << value;
87 // value = widget->inputMethodQuery(Qt::ImSurroundingText);
88 // qDebug () << value;
89 // value = widget->inputMethodQuery(Qt::ImCurrentSelection);
90 // qDebug () << value;
93 bool
94 IBusInputContext::isComposing() const
96 return (!preedit_string.isEmpty ()) && preedit_visible;
99 void
100 IBusInputContext::setFocusWidget (QWidget *widget)
102 // qDebug () << "setFocusWidget (" << widget << ")";
103 QInputContext::setFocusWidget (widget);
104 update ();
107 void
108 IBusInputContext::widgetDestroyed (QWidget *widget)
110 QInputContext::widgetDestroyed (widget);
111 update ();
114 #ifdef Q_WS_X11
115 bool
116 IBusInputContext::x11FilterEvent (QWidget *keywidget, XEvent *xevent)
118 if (client->x11FilterEvent (this, keywidget, xevent))
119 return true;
120 return QInputContext::x11FilterEvent (keywidget, xevent);
122 #endif
124 void
125 IBusInputContext::setIC (QString ic)
127 this->ic = ic;
130 QString
131 IBusInputContext::getIC ()
133 return ic;
136 void
137 IBusInputContext::commitString (QString text)
139 QInputMethodEvent event;
140 event.setCommitString (text);
141 sendEvent (event);
142 update ();
145 void
146 IBusInputContext::updatePreedit (QString text, QList <QList <quint32> > attr_list, int cursor_pos, bool show)
148 // qDebug () << text << cursor_pos << show;
149 QList <QAttribute> qattrs;
151 if (show) {
152 // append cursor pos
153 qattrs.append (QAttribute (QInputMethodEvent::Cursor, cursor_pos, true, 0));
155 // append attributes
156 for (QList <QList <quint32> >::iterator it = attr_list.begin (); it != attr_list.end(); ++ it) {
158 QList <quint32> attr = *it;
159 QTextCharFormat format;
161 switch (attr[0]) {
162 case 1: // underline
163 format.setUnderlineStyle (QTextCharFormat::SingleUnderline);
164 break;
165 case 2: // foreground
166 format.setForeground (QBrush (QColor (attr[1])));
167 break;
168 case 3: // background
169 format.setBackground (QBrush (QColor (attr[1])));
170 break;
171 default:
172 break;
175 qattrs.append (QAttribute (QInputMethodEvent::TextFormat, attr[2], attr[3] - attr[2], QVariant (format)));
176 // qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
179 else {
180 qattrs.append (QAttribute (QInputMethodEvent::Cursor, 0, true, 0));
181 text = "";
182 cursor_pos = 0;
185 preedit_string = text;
186 preedit_visible = show;
187 preedit_cursor_pos = cursor_pos;
189 QInputMethodEvent event (text, qattrs);
190 sendEvent (event);
191 update ();