1 #include "ibus-input-context.h"
2 #include "ibus-client.h"
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);
20 IBusInputContext::filterEvent (const QEvent
*event
)
23 if (client
->filterEvent (this, event
))
25 return QInputContext::filterEvent (event
);
27 return QInputContext::filterEvent (event
);
32 IBusInputContext::font () const
34 return QInputContext::font ();
38 IBusInputContext::identifierName ()
40 return QString ("ibus");
44 IBusInputContext::language()
50 IBusInputContext::mouseHandler (int x
, QMouseEvent
*event
)
52 client
->mouseHandler (this, x
, event
);
53 QInputContext::mouseHandler (x
, event
);
57 IBusInputContext::reset()
63 IBusInputContext::update ()
67 if ((widget
= focusWidget ()) == NULL
)
70 QRect rect
= widget
->inputMethodQuery(Qt::ImMicroFocus
).toRect ();
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);
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;
94 IBusInputContext::isComposing() const
96 return (!preedit_string
.isEmpty ()) && preedit_visible
;
100 IBusInputContext::setFocusWidget (QWidget
*widget
)
102 // qDebug () << "setFocusWidget (" << widget << ")";
103 QInputContext::setFocusWidget (widget
);
108 IBusInputContext::widgetDestroyed (QWidget
*widget
)
110 QInputContext::widgetDestroyed (widget
);
116 IBusInputContext::x11FilterEvent (QWidget
*keywidget
, XEvent
*xevent
)
118 if (client
->x11FilterEvent (this, keywidget
, xevent
))
120 return QInputContext::x11FilterEvent (keywidget
, xevent
);
125 IBusInputContext::setIC (QString ic
)
131 IBusInputContext::getIC ()
137 IBusInputContext::commitString (QString text
)
139 QInputMethodEvent event
;
140 event
.setCommitString (text
);
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
;
153 qattrs
.append (QAttribute (QInputMethodEvent::Cursor
, cursor_pos
, true, 0));
156 for (QList
<QList
<quint32
> >::iterator it
= attr_list
.begin (); it
!= attr_list
.end(); ++ it
) {
158 QList
<quint32
> attr
= *it
;
159 QTextCharFormat format
;
163 format
.setUnderlineStyle (QTextCharFormat::SingleUnderline
);
165 case 2: // foreground
166 format
.setForeground (QBrush (QColor (attr
[1])));
168 case 3: // background
169 format
.setBackground (QBrush (QColor (attr
[1])));
175 qattrs
.append (QAttribute (QInputMethodEvent::TextFormat
, attr
[2], attr
[3] - attr
[2], QVariant (format
)));
176 // qDebug () << attr[0] << attr[2] << attr[3] - attr[2];
180 qattrs
.append (QAttribute (QInputMethodEvent::Cursor
, 0, true, 0));
185 preedit_string
= text
;
186 preedit_visible
= show
;
187 preedit_cursor_pos
= cursor_pos
;
189 QInputMethodEvent
event (text
, qattrs
);