Warning, file /office/calligra/libs/flake/KoToolBase.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006, 2010 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoToolBase.h"
0022 #include "KoToolBase_p.h"
0023 
0024 #include "KoCanvasBase.h"
0025 #include "KoPointerEvent.h"
0026 #include "KoDocumentResourceManager.h"
0027 #include "KoCanvasResourceManager.h"
0028 #include "KoViewConverter.h"
0029 #include "KoShapeController.h"
0030 #include "KoShapeBasedDocumentBase.h"
0031 #include "KoToolSelection.h"
0032 
0033 #include <klocalizedstring.h>
0034 #include <kactioncollection.h>
0035 
0036 #include <QAction>
0037 #include <QWidget>
0038 #include <QFile>
0039 #include <QDomDocument>
0040 #include <QDomElement>
0041 
0042 KoToolBase::KoToolBase(KoCanvasBase *canvas)
0043     : d_ptr(new KoToolBasePrivate(this, canvas))
0044 {
0045     Q_D(KoToolBase);
0046     d->connectSignals();
0047 }
0048 
0049 KoToolBase::KoToolBase(KoToolBasePrivate &dd)
0050     : d_ptr(&dd)
0051 {
0052     Q_D(KoToolBase);
0053     d->connectSignals();
0054 }
0055 
0056 KoToolBase::~KoToolBase()
0057 {
0058 
0059     //Q_D(const KoToolBase);
0060 
0061 // Enable this to easily generate action files for tools
0062 //    if (d->actionCollection.size() > 0) {
0063 
0064 //        QDomDocument doc;
0065 //        QDomElement e = doc.createElement("Actions");
0066 //        e.setAttribute("name", toolId());
0067 //        e.setAttribute("version", "1");
0068 //        doc.appendChild(e);
0069 
0070 //        foreach(QAction *ac, d->actionCollection.values()) {
0071 //            QAction *action = qobject_cast<QAction*>(ac);
0072 //            if (action) {
0073 //                QDomElement a = doc.createElement("Action");
0074 //                a.setAttribute("name", action->objectName());
0075 //                a.setAttribute("icon", action->icon().name());
0076 //                a.setAttribute("text" , action->text());
0077 //                a.setAttribute("whatsThis" , action->whatsThis());
0078 //                a.setAttribute("toolTip" , action->toolTip());
0079 //                a.setAttribute("iconText" , action->iconText());
0080 //                a.setAttribute("shortcut" , action->shortcut(QAction::ActiveShortcut).toString());
0081 //                a.setAttribute("defaultShortcut" , action->shortcut(QAction::DefaultShortcut).toString());
0082 //                a.setAttribute("isCheckable" , QString((action->isChecked() ? "true" : "false")));
0083 //                a.setAttribute("statusTip", action->statusTip());
0084 //                e.appendChild(a);
0085 //            }
0086 //            else {
0087 //                qDebug() << "Got a QAction:" << ac->objectName();
0088 //            }
0089 
0090 //        }
0091 //        QFile f(toolId() + ".action");
0092 //        f.open(QFile::WriteOnly);
0093 //        f.write(doc.toString().toUtf8());
0094 //        f.close();
0095 
0096 //    }
0097 //    else {
0098 //        qDebug() << "Tool" << toolId() << "has no actions";
0099 //    }
0100     qDeleteAll(d_ptr->optionWidgets);
0101     delete d_ptr;
0102 }
0103 
0104 /// Ultimately only called from Calligra Sheets
0105 void KoToolBase::updateShapeController(KoShapeBasedDocumentBase *shapeController)
0106 {
0107     if (shapeController) {
0108         KoDocumentResourceManager *scrm = shapeController->resourceManager();
0109         if (scrm) {
0110             connect(scrm, SIGNAL(resourceChanged(int,QVariant)),
0111                     this, SLOT(documentResourceChanged(int,QVariant)));
0112         }
0113     }
0114 }
0115 
0116 void KoToolBase::deactivate()
0117 {
0118 }
0119 
0120 void KoToolBase::canvasResourceChanged(int key, const QVariant & res)
0121 {
0122     Q_UNUSED(key);
0123     Q_UNUSED(res);
0124 }
0125 
0126 void KoToolBase::documentResourceChanged(int key, const QVariant &res)
0127 {
0128     Q_UNUSED(key);
0129     Q_UNUSED(res);
0130 }
0131 
0132 bool KoToolBase::wantsAutoScroll() const
0133 {
0134     return true;
0135 }
0136 
0137 void KoToolBase::mouseDoubleClickEvent(KoPointerEvent *event)
0138 {
0139     event->ignore();
0140 }
0141 
0142 void KoToolBase::mouseTripleClickEvent(KoPointerEvent *event)
0143 {
0144     event->ignore();
0145 }
0146 
0147 void KoToolBase::shortcutOverrideEvent(QKeyEvent *e)
0148 {
0149     e->ignore();
0150 }
0151 
0152 void KoToolBase::keyPressEvent(QKeyEvent *e)
0153 {
0154     e->ignore();
0155 }
0156 
0157 void KoToolBase::keyReleaseEvent(QKeyEvent *e)
0158 {
0159     e->ignore();
0160 }
0161 
0162 void KoToolBase::wheelEvent(KoPointerEvent * e)
0163 {
0164     e->ignore();
0165 }
0166 
0167 void KoToolBase::touchEvent(QTouchEvent *event)
0168 {
0169     event->ignore();
0170 }
0171 
0172 QVariant KoToolBase::inputMethodQuery(Qt::InputMethodQuery query, const KoViewConverter &) const
0173 {
0174     Q_D(const KoToolBase);
0175     if (d->canvas->canvasWidget() == 0)
0176         return QVariant();
0177 
0178     switch (query) {
0179     case Qt::ImMicroFocus:
0180         return QRect(d->canvas->canvasWidget()->width() / 2, 0, 1, d->canvas->canvasWidget()->height());
0181     case Qt::ImFont:
0182         return d->canvas->canvasWidget()->font();
0183     default:
0184         return QVariant();
0185     }
0186 }
0187 
0188 void KoToolBase::inputMethodEvent(QInputMethodEvent * event)
0189 {
0190     if (! event->commitString().isEmpty()) {
0191         QKeyEvent ke(QEvent::KeyPress, -1, 0, event->commitString());
0192         keyPressEvent(&ke);
0193     }
0194     event->accept();
0195 }
0196 
0197 void KoToolBase::customPressEvent(KoPointerEvent * event)
0198 {
0199     event->ignore();
0200 }
0201 
0202 void KoToolBase::customReleaseEvent(KoPointerEvent * event)
0203 {
0204     event->ignore();
0205 }
0206 
0207 void KoToolBase::customMoveEvent(KoPointerEvent * event)
0208 {
0209     event->ignore();
0210 }
0211 
0212 bool KoToolBase::wantsTouch() const
0213 {
0214     return false;
0215 }
0216 
0217 void KoToolBase::useCursor(const QCursor &cursor)
0218 {
0219     Q_D(KoToolBase);
0220     d->currentCursor = cursor;
0221     emit cursorChanged(d->currentCursor);
0222 }
0223 
0224 QList<QPointer<QWidget> > KoToolBase::optionWidgets()
0225 {
0226     Q_D(KoToolBase);
0227     if (d->optionWidgets.empty()) {
0228         d->optionWidgets = createOptionWidgets();
0229     }
0230     return d->optionWidgets;
0231 }
0232 
0233 void KoToolBase::addAction(const QString &name, QAction *action)
0234 {
0235     Q_D(KoToolBase);
0236     if (action->objectName().isEmpty()) {
0237         action->setObjectName(name);
0238     }
0239     d->actionCollection.insert(name, action);
0240 }
0241 
0242 QHash<QString, QAction *> KoToolBase::actions() const
0243 {
0244     Q_D(const KoToolBase);
0245     return d->actionCollection;
0246 }
0247 
0248 QAction *KoToolBase::action(const QString &name) const
0249 {
0250     Q_D(const KoToolBase);
0251     return d->actionCollection.value(name);
0252 }
0253 
0254 QWidget * KoToolBase::createOptionWidget()
0255 {
0256     return 0;
0257 }
0258 
0259 QList<QPointer<QWidget> >  KoToolBase::createOptionWidgets()
0260 {
0261     QList<QPointer<QWidget> > ow;
0262     if (QWidget *widget = createOptionWidget()) {
0263         if (widget->objectName().isEmpty()) {
0264             widget->setObjectName(toolId());
0265         }
0266         ow.append(widget);
0267     }
0268     return ow;
0269 }
0270 
0271 void KoToolBase::setToolId(const QString &id)
0272 {
0273     Q_D(KoToolBase);
0274     d->toolId = id;
0275 }
0276 
0277 QString KoToolBase::toolId() const
0278 {
0279     Q_D(const KoToolBase);
0280     return d->toolId;
0281 }
0282 
0283 QCursor KoToolBase::cursor() const
0284 {
0285     Q_D(const KoToolBase);
0286     return d->currentCursor;
0287 }
0288 
0289 void KoToolBase::deleteSelection()
0290 {
0291 }
0292 
0293 void KoToolBase::cut()
0294 {
0295     copy();
0296     deleteSelection();
0297 }
0298 
0299 QList<QAction*> KoToolBase::popupActionList() const
0300 {
0301     Q_D(const KoToolBase);
0302     return d->popupActionList;
0303 }
0304 
0305 void KoToolBase::setPopupActionList(const QList<QAction*> &list)
0306 {
0307     Q_D(KoToolBase);
0308     d->popupActionList = list;
0309 }
0310 
0311 KoCanvasBase * KoToolBase::canvas() const
0312 {
0313     Q_D(const KoToolBase);
0314     return d->canvas;
0315 }
0316 
0317 void KoToolBase::setStatusText(const QString &statusText)
0318 {
0319     emit statusTextChanged(statusText);
0320 }
0321 
0322 uint KoToolBase::handleRadius() const
0323 {
0324     Q_D(const KoToolBase);
0325     if(d->canvas->shapeController()->resourceManager())
0326     {
0327         return d->canvas->shapeController()->resourceManager()->handleRadius();
0328     } else {
0329         return 3;
0330     }
0331 }
0332 
0333 uint KoToolBase::grabSensitivity() const
0334 {
0335     Q_D(const KoToolBase);
0336     if(d->canvas->shapeController()->resourceManager())
0337     {
0338         return d->canvas->shapeController()->resourceManager()->grabSensitivity();
0339     } else {
0340         return 3;
0341     }
0342 }
0343 
0344 QRectF KoToolBase::handleGrabRect(const QPointF &position) const
0345 {
0346     Q_D(const KoToolBase);
0347     const KoViewConverter * converter = d->canvas->viewConverter();
0348     uint handleSize = 2*grabSensitivity();
0349     QRectF r = converter->viewToDocument(QRectF(0, 0, handleSize, handleSize));
0350     r.moveCenter(position);
0351     return r;
0352 }
0353 
0354 QRectF KoToolBase::handlePaintRect(const QPointF &position) const
0355 {
0356     Q_D(const KoToolBase);
0357     const KoViewConverter * converter = d->canvas->viewConverter();
0358     uint handleSize = 2*handleRadius();
0359     QRectF r = converter->viewToDocument(QRectF(0, 0, handleSize, handleSize));
0360     r.moveCenter(position);
0361     return r;
0362 }
0363 
0364 void KoToolBase::setTextMode(bool value)
0365 {
0366     Q_D(KoToolBase);
0367     d->isInTextMode=value;
0368 }
0369 
0370 QStringList KoToolBase::supportedPasteMimeTypes() const
0371 {
0372     return QStringList();
0373 }
0374 
0375 bool KoToolBase::paste()
0376 {
0377     return false;
0378 }
0379 
0380 void KoToolBase::copy() const
0381 {
0382 }
0383 
0384 void KoToolBase::dragMoveEvent(QDragMoveEvent *event, const QPointF &point)
0385 {
0386     Q_UNUSED(event);
0387     Q_UNUSED(point);
0388 }
0389 
0390 void KoToolBase::dragLeaveEvent(QDragLeaveEvent *event)
0391 {
0392     Q_UNUSED(event);
0393 }
0394 
0395 void KoToolBase::dropEvent(QDropEvent *event, const QPointF &point)
0396 {
0397     Q_UNUSED(event);
0398     Q_UNUSED(point);
0399 }
0400 
0401 bool KoToolBase::hasSelection()
0402 {
0403     KoToolSelection *sel = selection();
0404     return (sel && sel->hasSelection());
0405 }
0406 
0407 KoToolSelection *KoToolBase::selection()
0408 {
0409     return 0;
0410 }
0411 
0412 void KoToolBase::repaintDecorations()
0413 {
0414 }
0415 
0416 bool KoToolBase::isInTextMode() const
0417 {
0418     Q_D(const KoToolBase);
0419     return d->isInTextMode;
0420 }