KoTextTool.cpp 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* This file is part of the KDE project
 * Copyright (C) 2006 Thomas Zander <zander@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <KoTextTool.h>
#include <KoCanvasBase.h>
#include <KoSelection.h>
#include <KoShapeManager.h>

#include <kdebug.h>
#include <QKeyEvent>

KoTextTool::KoTextTool(KoCanvasBase *canvas)
: KoTool(canvas)
, m_textShape(0)
{
}

KoTextTool::~KoTextTool() {
}

void KoTextTool::paint( QPainter &painter, KoViewConverter &converter) {
}

void KoTextTool::mousePressEvent( KoPointerEvent *event )  {
}

void KoTextTool::mouseDoubleClickEvent( KoPointerEvent *event ) {
}

void KoTextTool::mouseMoveEvent( KoPointerEvent *event ) {
}

void KoTextTool::mouseReleaseEvent( KoPointerEvent *event ) {
}

void KoTextTool::keyPressEvent(QKeyEvent *event) {
    QTextCursor::MoveOperation moveOperation = QTextCursor::NoMove;
    switch(event->key()) { // map input to moveOperation
        case Qt::Key_Backspace:
            m_caret.deletePreviousChar();
            break;
        case Qt::Key_Tab:
            kDebug() << "Tab key pressed";
            break;
        case Qt::Key_Delete:
            m_caret.deleteChar();
            break;
        case Qt::Key_Left:
            if(event->modifiers() & Qt::ControlModifier)
                moveOperation = QTextCursor::WordLeft;
            else
                moveOperation = QTextCursor::Left;
            break;
        case Qt::Key_Up:
            moveOperation = QTextCursor::Up;
            break;
        case Qt::Key_Right:
            if(event->modifiers() & Qt::ControlModifier)
                moveOperation = QTextCursor::NextWord;
            else
                moveOperation = QTextCursor::Right;
            break;
        case Qt::Key_Down:
            moveOperation = QTextCursor::Down;
            break;
        case Qt::Key_PageUp:
            //moveOperation = QTextCursor::Left;
            break;
        case Qt::Key_PageDown:
            //moveOperation = QTextCursor::Left;
            break;
        case Qt::Key_End:
            moveOperation = QTextCursor::EndOfLine;
            break;
        case Qt::Key_Home:
            moveOperation = QTextCursor::StartOfLine;
            break;
        default:
            m_caret.insertText(event->text());
    }
    m_caret.movePosition(moveOperation,
        (event->modifiers() & Qt::ShiftModifier)?QTextCursor::KeepAnchor:QTextCursor::MoveAnchor);
    event->accept();
    m_textShape->repaint();// TODO more fine grained repainting..
}

void KoTextTool::keyReleaseEvent(QKeyEvent *event) {
    event->accept();
}

void KoTextTool::activate (bool temporary) {
    Q_UNUSED(temporary);
    KoShape *shape = m_canvas->shapeManager()->selection()->firstSelectedObject();
    m_textShape = dynamic_cast<KoTextShape*> (shape);
    if(m_textShape == 0) {
        emit sigDone();
        return;
    }
    KoTextShapeData *data = static_cast<KoTextShapeData*> (shape->userData());
    m_caret = QTextCursor(data->document());
    useCursor(Qt::IBeamCursor, true);
}

void KoTextTool::deactivate() {
kDebug(32500) << "  deactivate" << endl;
    m_textShape = 0;
}