Logo Search packages:      
Sourcecode: qt4-x11 version File versions

void Q3TextEdit::keyPressEvent ( QKeyEvent e  )  [protected, virtual, inherited]

Processes the key event, e. By default key events are used to provide keyboard navigation and text editing.

Reimplemented from QWidget.

Reimplemented in Q3TextBrowser.

Definition at line 1185 of file q3textedit.cpp.

References Q3TextEdit::autoFormatting, Q3TextEdit::copy(), Q3TextEdit::cursor, Q3TextEdit::cursorPositionChanged(), Q3TextEdit::cut(), Q3TextEdit::del(), Q3TextEdit::doKeyboardAction(), Q3TextEdit::drawCursor(), QEvent::ignore(), Q3TextEdit::indent(), Q3TextEdit::insert(), QKeyEvent::key(), QWidget::keyPressEvent(), QString::length(), Q3TextEdit::moveCursor(), Q3TextEdit::paste(), Q3TextEdit::redo(), Q3TextEdit::removeSelectedText(), Q3TextEdit::repaintChanged(), Q3TextEdit::returnPressed(), Q3TextEdit::selectAll(), Q3TextEdit::setAlignment(), QWidget::setCursor(), Q3TextEdit::setParagType(), QTimer::start(), QTimer::stop(), QKeyEvent::text(), Q3TextEdit::textFormat, Q3TextEdit::undo(), QString::unicode(), and Q3ScrollView::viewport().

Referenced by Q3TextBrowser::keyPressEvent().

{
    changeIntervalTimer->stop();
    interval = 10;
    bool unknownKey = false;
    if (isReadOnly()) {
        if (!handleReadOnlyKeyEvent(e))
            Q3ScrollView::keyPressEvent(e);
        changeIntervalTimer->start(100, true);
        return;
    }


    bool selChanged = false;
    for (int i = 1; i < doc->numSelections(); ++i) // start with 1 as we don't want to remove the Standard-Selection
        selChanged = doc->removeSelection(i) || selChanged;

    if (selChanged) {
        cursor->paragraph()->document()->nextDoubleBuffered = true;
        repaintChanged();
    }

    bool clearUndoRedoInfo = true;


    switch (e->key()) {
    case Qt::Key_Left:
    case Qt::Key_Right: {
        // a bit hacky, but can't change this without introducing new enum values for move and keeping the
        // correct semantics and movement for BiDi and non BiDi text.
        CursorAction a;
        if (cursor->paragraph()->string()->isRightToLeft() == (e->key() == Qt::Key_Right))
            a = e->state() & Qt::ControlButton ? MoveWordBackward : MoveBackward;
        else
            a = e->state() & Qt::ControlButton ? MoveWordForward : MoveForward;
        moveCursor(a, e->state() & Qt::ShiftButton);
        break;
    }
    case Qt::Key_Up:
        moveCursor(e->state() & Qt::ControlButton ? MovePgUp : MoveUp, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_Down:
        moveCursor(e->state() & Qt::ControlButton ? MovePgDown : MoveDown, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_Home:
        moveCursor(e->state() & Qt::ControlButton ? MoveHome : MoveLineStart, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_End:
        moveCursor(e->state() & Qt::ControlButton ? MoveEnd : MoveLineEnd, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_Prior:
        moveCursor(MovePgUp, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_Next:
        moveCursor(MovePgDown, e->state() & Qt::ShiftButton);
        break;
    case Qt::Key_Return: case Qt::Key_Enter:
        if (doc->hasSelection(Q3TextDocument::Standard, false))
            removeSelectedText();
        if (textFormat() == Qt::RichText && (e->state() & Qt::ControlButton)) {
            // Ctrl-Enter inserts a line break in rich text mode
            insert(QString(QChar(QChar::LineSeparator)), true, false);
        } else {
#ifndef QT_NO_CURSOR
            viewport()->setCursor(isReadOnly() ? Qt::ArrowCursor : Qt::IBeamCursor);
#endif
            clearUndoRedoInfo = false;
            doKeyboardAction(ActionReturn);
            emit returnPressed();
        }
        break;
    case Qt::Key_Delete:
#if defined (Q_WS_WIN)
        if (e->state() & Qt::ShiftButton) {
            cut();
            break;
        } else
#endif
        if (doc->hasSelection(Q3TextDocument::Standard, true)) {
            removeSelectedText();
            break;
        }
        doKeyboardAction(e->state() & Qt::ControlButton ? ActionWordDelete
                          : ActionDelete);
        clearUndoRedoInfo = false;

        break;
    case Qt::Key_Insert:
        if (e->state() & Qt::ShiftButton)
            paste();
#if defined (Q_WS_WIN)
        else if (e->state() & Qt::ControlButton)
            copy();
#endif
        else
            setOverwriteMode(!isOverwriteMode());
        break;
    case Qt::Key_Backspace:
#if defined (Q_WS_WIN)
        if (e->state() & Qt::AltButton) {
            if (e->state() & Qt::ControlButton) {
                break;
            } else if (e->state() & Qt::ShiftButton) {
                redo();
                break;
            } else {
                undo();
                break;
            }
        } else
#endif
        if (doc->hasSelection(Q3TextDocument::Standard, true)) {
            removeSelectedText();
            break;
        }

        doKeyboardAction(e->state() & Qt::ControlButton ? ActionWordBackspace
                          : ActionBackspace);
        clearUndoRedoInfo = false;
        break;
    case Qt::Key_F16: // Copy key on Sun keyboards
        copy();
        break;
    case Qt::Key_F18:  // Paste key on Sun keyboards
        paste();
        break;
    case Qt::Key_F20:  // Cut key on Sun keyboards
        cut();
        break;
    case Qt::Key_Direction_L:
        if (doc->textFormat() == Qt::PlainText) {
            // change the whole doc
            Q3TextParagraph *p = doc->firstParagraph();
            while (p) {
                p->setDirection(QChar::DirL);
                p->setAlignment(Qt::AlignLeft);
                p->invalidate(0);
                p = p->next();
            }
        } else {
            if (!cursor->paragraph() || cursor->paragraph()->direction() == QChar::DirL)
                return;
            cursor->paragraph()->setDirection(QChar::DirL);
            if (cursor->paragraph()->length() <= 1&&
                 ((cursor->paragraph()->alignment() & (Qt::AlignLeft | Qt::AlignRight)) != 0))
                setAlignment(Qt::AlignLeft);
        }
        repaintChanged();
        break;
    case Qt::Key_Direction_R:
        if (doc->textFormat() == Qt::PlainText) {
            // change the whole doc
            Q3TextParagraph *p = doc->firstParagraph();
            while (p) {
                p->setDirection(QChar::DirR);
                p->setAlignment(Qt::AlignRight);
                p->invalidate(0);
                p = p->next();
            }
        } else {
            if (!cursor->paragraph() || cursor->paragraph()->direction() == QChar::DirR)
                return;
            cursor->paragraph()->setDirection(QChar::DirR);
            if (cursor->paragraph()->length() <= 1&&
                 ((cursor->paragraph()->alignment() & (Qt::AlignLeft | Qt::AlignRight)) != 0))
                setAlignment(Qt::AlignRight);
        }
        repaintChanged();
        break;
    default: {
            unsigned char ascii = e->text().length() ? e->text().unicode()->latin1() : 0;
            if (e->text().length() &&
                (!(e->state() & Qt::ControlButton) &&
#ifndef Q_OS_MAC
                  !(e->state() & Qt::AltButton) &&
#endif
                  !(e->state() & Qt::MetaButton) ||
                 (((e->state() & (Qt::ControlButton | Qt::AltButton))) == (Qt::ControlButton|Qt::AltButton))) &&
                 (!ascii || ascii >= 32 || e->text() == QString(QLatin1Char('\t')))) {
                clearUndoRedoInfo = false;
                if (e->key() == Qt::Key_Tab) {
                    if (d->tabChangesFocus) {
                        e->ignore();
                        break;
                    }
                    if (textFormat() == Qt::RichText && cursor->index() == 0
                         && (cursor->paragraph()->isListItem() || cursor->paragraph()->listDepth())) {
                        clearUndoRedo();
                        undoRedoInfo.type = UndoRedoInfo::Style;
                        undoRedoInfo.id = cursor->paragraph()->paragId();
                        undoRedoInfo.eid = undoRedoInfo.id;
                        undoRedoInfo.styleInformation = Q3TextStyleCommand::readStyleInformation(doc, undoRedoInfo.id, undoRedoInfo.eid);
                        cursor->paragraph()->setListDepth(cursor->paragraph()->listDepth() +1);
                        clearUndoRedo();
                        drawCursor(false);
                        repaintChanged();
                        drawCursor(true);
                        break;
                    }
                } else if (e->key() == Qt::Key_BackTab) {
                    if (d->tabChangesFocus) {
                        e->ignore();
                        break;
                    }
                }

                if ((autoFormatting() & AutoBulletList) &&
                     textFormat() == Qt::RichText && cursor->index() == 0
                     && !cursor->paragraph()->isListItem()
                     && (e->text()[0] == QLatin1Char('-') || e->text()[0] == QLatin1Char('*'))) {
                        clearUndoRedo();
                        undoRedoInfo.type = UndoRedoInfo::Style;
                        undoRedoInfo.id = cursor->paragraph()->paragId();
                        undoRedoInfo.eid = undoRedoInfo.id;
                        undoRedoInfo.styleInformation = Q3TextStyleCommand::readStyleInformation(doc, undoRedoInfo.id, undoRedoInfo.eid);
                        setParagType(Q3StyleSheetItem::DisplayListItem, Q3StyleSheetItem::ListDisc);
                        clearUndoRedo();
                        drawCursor(false);
                        repaintChanged();
                        drawCursor(true);
                        break;
                }
            if (overWrite && !cursor->atParagEnd() && !doc->hasSelection(Q3TextDocument::Standard)) {
                    doKeyboardAction(ActionDelete);
                    clearUndoRedoInfo = false;
                }
                QString t = e->text();
                insert(t, true, false);
                break;
            } else if (e->state() & Qt::ControlButton) {
                switch (e->key()) {
                case Qt::Key_C: case Qt::Key_F16: // Copy key on Sun keyboards
                    copy();
                    break;
                case Qt::Key_V:
                    paste();
                    break;
                case Qt::Key_X:
                    cut();
                    break;
                case Qt::Key_I: case Qt::Key_T: case Qt::Key_Tab:
                    if (!d->tabChangesFocus)
                        indent();
                    break;
                case Qt::Key_A:
#if defined(Q_WS_X11)
                    moveCursor(MoveLineStart, e->state() & Qt::ShiftButton);
#else
                    selectAll(true);
#endif
                    break;
                case Qt::Key_B:
                    moveCursor(MoveBackward, e->state() & Qt::ShiftButton);
                    break;
                case Qt::Key_F:
                    moveCursor(MoveForward, e->state() & Qt::ShiftButton);
                    break;
                case Qt::Key_D:
                    if (doc->hasSelection(Q3TextDocument::Standard)) {
                        removeSelectedText();
                        break;
                    }
                    doKeyboardAction(ActionDelete);
                    clearUndoRedoInfo = false;
                    break;
                case Qt::Key_H:
                    if (doc->hasSelection(Q3TextDocument::Standard)) {
                        removeSelectedText();
                        break;
                    }
                    if (!cursor->paragraph()->prev() &&
                         cursor->atParagStart())
                        break;

                    doKeyboardAction(ActionBackspace);
                    clearUndoRedoInfo = false;
                    break;
                case Qt::Key_E:
                    moveCursor(MoveLineEnd, e->state() & Qt::ShiftButton);
                    break;
                case Qt::Key_N:
                    moveCursor(MoveDown, e->state() & Qt::ShiftButton);
                    break;
                case Qt::Key_P:
                    moveCursor(MoveUp, e->state() & Qt::ShiftButton);
                    break;
                case Qt::Key_Z:
                    if(e->state() & Qt::ShiftButton)
                        redo();
                    else
                        undo();
                    break;
                case Qt::Key_Y:
                    redo();
                    break;
                case Qt::Key_K:
                    doKeyboardAction(ActionKill);
                    break;
#if defined(Q_WS_WIN)
                case Qt::Key_Insert:
                    copy();
                    break;
                case Qt::Key_Delete:
                    del();
                    break;
#endif
                default:
                    unknownKey = false;
                    break;
                }
            } else {
                unknownKey = true;
            }
        }
    }

    emit cursorPositionChanged(cursor);
    emit cursorPositionChanged(cursor->paragraph()->paragId(), cursor->index());
    if (clearUndoRedoInfo)
        clearUndoRedo();
    changeIntervalTimer->start(100, true);
    if (unknownKey)
        e->ignore();
}


Generated by  Doxygen 1.6.0   Back to index