Esempio n. 1
0
void XmlEdit::createConnections()
{
    connect(this, SIGNAL(cursorPositionChanged()),
            this, SLOT(highlightCurrentLine()));
    connect(completer, SIGNAL(activated(const QString&)),
            this, SLOT(insertCompletion(const QString&)));
    (void) new QShortcut(QKeySequence(tr("Ctrl+M", "Complete")),
                         this, SLOT(performCompletion()));
}
Esempio n. 2
0
void XmlEdit::performCompletion()
{
    QTextCursor cursor = textCursor();
    cursor.select(QTextCursor::WordUnderCursor);
    const QString completionPrefix = cursor.selectedText();
    if (!completionPrefix.isEmpty() &&
        completionPrefix.at(completionPrefix.length() - 1)
                            .isLetter())
        performCompletion(completionPrefix);
}
Esempio n. 3
0
void CodeEditor::keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Return && event->modifiers() == Qt::SHIFT)
        return;

    if (event->key() == Qt::Key_E && event->modifiers() == Qt::CTRL) {
        QTextBlock block = textCursor().block();
        foldUnfold(block);
        FULLRESIZE;
        return;
    }

    if (event->key() == Qt::Key_Space && event->modifiers() == Qt::CTRL) {
        performCompletion();
        return;
    }

    if (event->key() == Qt::Key_Backspace && config->backUnindent) {
        QTextCursor cursor = textCursor();

        if (!cursor.hasSelection() && !cursor.atBlockStart() && cursor.block().text().          \
                                                                left(cursor.positionInBlock()). \
                                                                trimmed().isEmpty()) {
            cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor,
                                qMin(config->indentSize, cursor.positionInBlock()));
            cursor.removeSelectedText();
            return;
        }
    }

    if (event->key() == Qt::Key_Tab && config->tabIndents) {
        QTextCursor cursor = textCursor();

        if (cursor.hasSelection()) {

            QTextBlock block = document()->findBlock(cursor.selectionStart());

            int end = document()->findBlock(cursor.selectionEnd()).blockNumber();

            if (end - block.blockNumber()) {

                cursor.beginEditBlock();

                do {
                    cursor.setPosition(block.position(), QTextCursor::MoveAnchor);
                    cursor.insertText(QString().fill(' ', config->indentSize));
                } while ((block = block.next()).isValid() && block.blockNumber() <= end);

                cursor.endEditBlock();

                return;
            }

        } else if (textCursor().block().text().          \
                   left(textCursor().positionInBlock()). \
                   trimmed().isEmpty()) {
                   textCursor().insertText(QString().fill(' ', config->indentSize));
                   return;
        }
    }

    if (event->key() == Qt::Key_Tab && config->spaceTabs) {
        textCursor().insertText(QString().fill(' ', config->tabSize));
        return;
    }

    if (completer->popup()->isVisible()) {
        switch (event->key()) {
            case Qt::Key_Up:
            case Qt::Key_Down:
            case Qt::Key_Enter:
            case Qt::Key_Return:
            case Qt::Key_Escape:
                event->ignore();
                return;
        }
    }

    QPlainTextEdit::keyPressEvent(event);

    if (completer->popup()->isVisible())
        performCompletion();

    if (event->key() == Qt::Key_Return && config->autoIndent) {
        int state = textCursor().block().userState();

        if (!(state & Error) && (state & Nested)) {
            QString txt = textCursor().block().previous().text();

            int i = 0;

            while (txt[i].isSpace()) ++i;

            int previousBlockState = textCursor().block().previous().userState();

            if (!(previousBlockState & Error) && previousBlockState & Begin)
                i += config->indentSize;

            textCursor().insertText(QString().fill(' ', i));
        }
    }
}