Exemplo n.º 1
0
void MInputContext::commit()
{
    if (debug) qDebug() << InputContextName << "in" << __PRETTY_FUNCTION__;

    const bool hadPreedit = !preedit.isEmpty();

    if (hadPreedit) {
        QList<QInputMethodEvent::Attribute> attributes;
        if (preeditCursorPos >= 0) {
            bool valid = false;
            int start = cursorStartPosition(&valid);
            if (valid) {
                attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection,
                                                           start + preeditCursorPos, 0, QVariant());
            }
        }

        QInputMethodEvent event("", attributes);
        event.setCommitString(preedit);
        if (qGuiApp->focusObject()) {
            QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
        }

        preedit.clear();
        preeditCursorPos = -1;
        Q_EMIT preeditChanged();
    }

    imServer->reset(hadPreedit);
}
void MInputContextConnection::setPreedit(unsigned int connectionId,
                                                 const QString &text, int cursorPos)
{
    if (activeConnection != connectionId)
        return;

    preedit = text;

    Q_EMIT preeditChanged(text, cursorPos);
}
bool MDeclarativeIMObserver::sceneEventFilter(QGraphicsItem * watched, QEvent * event)
{
    if (event->type()==QEvent::InputMethod) {        
        if (m_omitInputMethodEvents) {
            return true;
        }

        QInputMethodEvent *ime = static_cast<QInputMethodEvent*>(event);
		QString newPreedit = ime->preeditString();
		
        QGraphicsObject *g = parentObject();
        if (g != 0 && g->property("maximumLength").isValid()) {
            int maximumTextLength = g->property("maximumLength").toInt();
            int textLength = g->property("text").toString().length();
            int selectedTextLength = g->property("selectedText").toString().length();
            if (textLength == maximumTextLength &&
                newPreedit.length() - ime->replacementLength() > 0 &&
                selectedTextLength == 0) {
                    m_omitInputMethodEvents = true;
                    QInputContext *ic = qApp->inputContext();
                    ic->reset();
                    m_omitInputMethodEvents = false;
                    return true;
            }				
        }

        if (newPreedit!=m_preedit) {
            m_preedit = newPreedit;
            emit preeditChanged();
        }
        
        QList<QInputMethodEvent::Attribute> attributes = ime->attributes();
        QList<QInputMethodEvent::Attribute>::iterator i;
        for (i = attributes.begin(); i != attributes.end(); ++i) {
            QInputMethodEvent::Attribute attribute = *i;
            if (attribute.type == QInputMethodEvent::Cursor) {
                m_preeditCursorPosition = attribute.start;
                emit preeditCursorPositionChanged();
            }
        }
    }

    return QDeclarativeItem::sceneEventFilter(watched,event);
}
Exemplo n.º 4
0
void MInputContext::commitString(const QString &string, int replacementStart,
                                 int replacementLength, int cursorPos)
{
    if (debug) qDebug() << InputContextName << "in" << __PRETTY_FUNCTION__;

    if (imServer->pendingResets()) {
        return;
    }

    bool hadPreedit = !preedit.isEmpty();
    preedit.clear();
    preeditCursorPos = -1;

    int start = -1;
    if (cursorPos >= 0) {
        bool valid = false;
        int currentStart = cursorStartPosition(&valid);
        if (valid) {
            start = cursorPos + currentStart + replacementStart;
        }
    }

    if (start >= 0) {
        QList<QInputMethodEvent::Attribute> attributes;
        attributes << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, start, 0, QVariant());
        QInputMethodEvent event("", attributes);
        event.setCommitString(string, replacementStart, replacementLength);
        if (qGuiApp->focusObject()) {
            QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
        }

    } else {
        QInputMethodEvent event;
        event.setCommitString(string, replacementStart, replacementLength);
        if (qGuiApp->focusObject()) {
            QGuiApplication::sendEvent(qGuiApp->focusObject(), &event);
        }
    }

    if (hadPreedit) {
        Q_EMIT preeditChanged();
    }
}