File indexing completed on 2024-05-12 16:33:43

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2010 Carlos Licea <carlos@kdab.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "CommentTool.h"
0021 #include "CommentShape.h"
0022 #include "InitialsCommentShape.h"
0023 
0024 #include <KoShape.h>
0025 #include <KoCanvasBase.h>
0026 #include <KoShapeManager.h>
0027 
0028 #include <QSet>
0029 #include <KoPointerEvent.h>
0030 
0031 CommentTool::CommentTool(KoCanvasBase* canvas)
0032 : KoToolBase(canvas)
0033 , m_canvas(canvas)
0034 , m_previouseActiveCommentShape(0)
0035 {
0036 }
0037 
0038 CommentTool::~CommentTool()
0039 {
0040 }
0041 
0042 void CommentTool::activate(KoToolBase::ToolActivation toolActivation, const QSet< KoShape* >& /*shapes*/)
0043 {
0044     const QCursor cursor(Qt::ArrowCursor);
0045     emit useCursor(cursor);
0046 
0047     QList<KoShape*> allShapes = m_canvas->shapeManager()->shapes();
0048     foreach( KoShape* shape, allShapes ) {
0049         InitialsCommentShape* initialsShape = dynamic_cast<InitialsCommentShape*>(shape);
0050         if(initialsShape) {
0051             initialsShape->setSelectable(true);
0052         }
0053     }
0054 
0055     m_temporary = toolActivation == KoToolBase::TemporaryActivation;
0056 }
0057 
0058 void CommentTool::deactivate()
0059 {
0060     QList<KoShape*> allShapes = m_canvas->shapeManager()->shapes();
0061     foreach( KoShape* shape, allShapes ) {
0062         InitialsCommentShape* initialsShape = dynamic_cast<InitialsCommentShape*>(shape);
0063         if(initialsShape) {
0064             initialsShape->setSelectable(false);
0065         }
0066     }
0067 
0068     if(m_previouseActiveCommentShape) {
0069         m_previouseActiveCommentShape->toogleActive();
0070         m_previouseActiveCommentShape = 0;
0071     }
0072 }
0073 
0074 
0075 void CommentTool::mouseReleaseEvent(KoPointerEvent* event)
0076 {
0077     //disable the previous activeshape
0078     if(m_previouseActiveCommentShape) {
0079         m_previouseActiveCommentShape->setActive(false);
0080     }
0081 
0082     InitialsCommentShape* initialsUnderCursor = dynamic_cast<InitialsCommentShape*>(m_canvas->shapeManager()->shapeAt(event->point, KoFlake::ShapeOnTop, false));
0083     if(initialsUnderCursor) {
0084         //don't re-activate the shape we just deactivated
0085         if(m_previouseActiveCommentShape == initialsUnderCursor->parent()) {
0086             m_previouseActiveCommentShape = 0;
0087             return;
0088         }
0089 
0090         CommentShape* commentUnderCursor = dynamic_cast<CommentShape*>(initialsUnderCursor->parent());
0091         Q_ASSERT(commentUnderCursor);
0092         commentUnderCursor->setActive(true);
0093 
0094         m_previouseActiveCommentShape = commentUnderCursor;
0095     }
0096     event->accept();
0097 }
0098 
0099 void CommentTool::mouseMoveEvent(KoPointerEvent* event)
0100 {
0101     InitialsCommentShape* shapeUnderCursor = dynamic_cast<InitialsCommentShape*>(m_canvas->shapeManager()->shapeAt(event->point, KoFlake::ShapeOnTop, false));
0102     if(shapeUnderCursor) {
0103         const QCursor cursor(Qt::PointingHandCursor);
0104         emit useCursor(cursor);
0105     }
0106     else {
0107         const QCursor cursor(Qt::ArrowCursor);
0108         emit useCursor(cursor);
0109     }
0110 }
0111 
0112 void CommentTool::mousePressEvent(KoPointerEvent* /*event*/)
0113 {
0114 }
0115 
0116 void CommentTool::paint(QPainter& /*painter*/, const KoViewConverter& /*converter*/)
0117 {
0118 }