Exemplo n.º 1
0
Arquivo: editor.cpp Projeto: KDE/khtml
bool Editor::handleKeyEvent(QKeyEvent *_ke)
{
    bool handled = false;

    bool ctrl  = _ke->modifiers() & Qt::ControlModifier;
    bool alt   = _ke->modifiers() & Qt::AltModifier;
    //bool shift = _ke->modifiers() & Qt::ShiftModifier;
    bool meta  = _ke->modifiers() & Qt::MetaModifier;

    if (ctrl || alt || meta) {
        return false;
    }

    switch (_ke->key()) {

    case Qt::Key_Delete: {
        Selection selectionToDelete = m_part->caret();
#ifdef DEBUG_COMMANDS
        // qDebug() << "========== KEY_DELETE ==========" << endl;
#endif
        if (selectionToDelete.state() == Selection::CARET) {
            Position pos(selectionToDelete.start());
#ifdef DEBUG_COMMANDS
            // qDebug() << "pos.inLastEditableInRootEditableElement " << pos.inLastEditableInRootEditableElement() << " pos.offset " << pos.offset() << " pos.max " << pos.node()->caretMaxRenderedOffset() << endl;
#endif
            if (pos.nextCharacterPosition() == pos) {
                // we're at the end of a root editable block...do nothing
#ifdef DEBUG_COMMANDS
                // qDebug() << "no delete!!!!!!!!!!" << endl;
#endif
                break;
            }
            m_part->d->editor_context.m_selection
                = Selection(pos, pos.nextCharacterPosition());
        }
        // fall through
    }
    case Qt::Key_Backspace:
        TypingCommandImpl::deleteKeyPressed0(m_part->xmlDocImpl());
        handled = true;
        break;

    case Qt::Key_Return:
    case Qt::Key_Enter:
//       if (shift)
        TypingCommandImpl::insertNewline0(m_part->xmlDocImpl());
//       else
//         TypingCommand::insertParagraph(m_part->xmlDocImpl());
        handled = true;
        break;

    case Qt::Key_Escape:
    case Qt::Key_Insert:
        // FIXME implement me
        handled = true;
        break;

    default:
// handle_input:
        if (m_part->caret().state() != Selection::CARET) {
            // We didn't get a chance to grab the caret, likely because
            // a script messed with contentEditable in the middle of events
            // acquire it now if there isn't a selection
            // qDebug() << "Editable node w/o caret!";
            DOM::NodeImpl *focus = m_part->xmlDocImpl()->focusNode();
            if (m_part->caret().state() == Selection::NONE) {
                if (focus) {
                    m_part->setCaret(Position(focus, focus->caretMinOffset()));
                } else {
                    break;
                }
            }
        }

        if (!_ke->text().isEmpty()) {
            TypingCommandImpl::insertText0(m_part->xmlDocImpl(), _ke->text());
            handled = true;
        }

    }

    //if (handled) {
    // ### check when to emit it
//     m_part->emitSelectionChanged();
    //}

    return handled;

}