libs/flake

KoPathToolHandle.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006,2008 Jan Hambrecht <jaham@gmx.net>
00003  * Copyright (C) 2006,2007 Thorsten Zachmann <zachmann@kde.org>
00004  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
00005  * Copyright (C) 2007 Boudewijn Rempt <boud@valdyas.org>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public License
00018  * along with this library; see the file COPYING.LIB.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #include "KoPathToolHandle.h"
00024 #include "KoPathTool.h"
00025 #include "KoPathPointMoveStrategy.h"
00026 #include "KoPathControlPointMoveStrategy.h"
00027 #include "KoPathConnectionPointStrategy.h"
00028 #include "commands/KoPathPointTypeCommand.h"
00029 #include "KoParameterChangeStrategy.h"
00030 #include <KoParameterShape.h>
00031 #include <KoCanvasBase.h>
00032 #include <KoCanvasResourceProvider.h>
00033 #include <KoShapeManager.h>
00034 #include <KoConnectionShape.h>
00035 #include <KoViewConverter.h>
00036 #include <KoPointerEvent.h>
00037 #include <QtGui/QPainter>
00038 
00039 KoPathToolHandle::KoPathToolHandle(KoPathTool *tool)
00040         : m_tool(tool)
00041 {
00042 }
00043 
00044 KoPathToolHandle::~KoPathToolHandle()
00045 {
00046 }
00047 
00048 PointHandle::PointHandle(KoPathTool *tool, KoPathPoint *activePoint, KoPathPoint::KoPointType activePointType)
00049         : KoPathToolHandle(tool)
00050         , m_activePoint(activePoint)
00051         , m_activePointType(activePointType)
00052 {
00053 }
00054 
00055 void PointHandle::paint(QPainter &painter, const KoViewConverter &converter)
00056 {
00057     painter.save();
00058     painter.setMatrix(m_activePoint->parent()->absoluteTransformation(&converter) * painter.matrix());
00059     KoShape::applyConversion(painter, converter);
00060 
00061     KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00062 
00063     KoPathPoint::KoPointType type = KoPathPoint::Node;
00064     if (selection && selection->contains(m_activePoint))
00065         type = KoPathPoint::All;
00066     int handleRadius = m_tool->canvas()->resourceProvider()->handleRadius();
00067     m_activePoint->paint(painter, handleRadius, type);
00068     painter.restore();
00069 }
00070 
00071 void PointHandle::repaint() const
00072 {
00073     bool active = false;
00074     KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00075     if (selection && selection->contains(m_activePoint))
00076         active = true;
00077     m_tool->repaint(m_activePoint->boundingRect(!active));
00078 }
00079 
00080 KoInteractionStrategy * PointHandle::handleMousePress(KoPointerEvent *event)
00081 {
00082     if ((event->button() & Qt::LeftButton) == 0)
00083         return 0;
00084     if ((event->modifiers() & Qt::ShiftModifier) == 0) { // no shift pressed.
00085         KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00086 
00087         // control select adds/removes points to/from the selection
00088         if (event->modifiers() & Qt::ControlModifier) {
00089             if (selection->contains(m_activePoint)) {
00090                 selection->remove(m_activePoint);
00091             } else {
00092                 selection->add(m_activePoint, false);
00093             }
00094             m_tool->repaint(m_activePoint->boundingRect(false));
00095         } else {
00096             // no control modifier, so clear selection and select active point
00097             if (!selection->contains(m_activePoint)) {
00098                 selection->add(m_activePoint, true);
00099                 m_tool->repaint(m_activePoint->boundingRect(false));
00100             }
00101         }
00102         // TODO remove canvas from call ?
00103         if (m_activePointType == KoPathPoint::Node) {
00104             QPointF startPoint = m_activePoint->parent()->shapeToDocument(m_activePoint->point());
00105             return new KoPathPointMoveStrategy(m_tool, m_tool->canvas(), startPoint);
00106         } else {
00107             KoPathShape * pathShape = m_activePoint->parent();
00108             KoPathPointData pd(pathShape, pathShape->pathPointIndex(m_activePoint));
00109             return new KoPathControlPointMoveStrategy(m_tool, m_tool->canvas(), pd, m_activePointType, event->point);
00110         }
00111     } else {
00112         KoPathPoint::KoPointProperties props = m_activePoint->properties();
00113         if (! m_activePoint->activeControlPoint1() || ! m_activePoint->activeControlPoint2())
00114             return 0;
00115 
00116         KoPathPointTypeCommand::PointType pointType = KoPathPointTypeCommand::Smooth;
00117         // cycle the smooth->symmetric->unsmooth state of the path point
00118         if (props & KoPathPoint::IsSmooth)
00119             pointType = KoPathPointTypeCommand::Symmetric;
00120         else if (props & KoPathPoint::IsSymmetric)
00121             pointType = KoPathPointTypeCommand::Corner;
00122 
00123         QList<KoPathPointData> pointData;
00124         pointData.append(KoPathPointData(m_activePoint->parent(), m_activePoint->parent()->pathPointIndex(m_activePoint)));
00125         m_tool->canvas()->addCommand(new KoPathPointTypeCommand(pointData, pointType));
00126     }
00127     return 0;
00128 }
00129 
00130 bool PointHandle::check()
00131 {
00132     if (m_tool->canvas()->shapeManager()->selection()->isSelected(m_activePoint->parent())) {
00133         return m_activePoint->parent()->pathPointIndex(m_activePoint) != KoPathPointIndex(-1, -1);
00134     }
00135     return false;
00136 }
00137 
00138 KoPathPoint * PointHandle::activePoint() const
00139 {
00140     return m_activePoint;
00141 }
00142 
00143 KoPathPoint::KoPointType PointHandle::activePointType() const
00144 {
00145     return m_activePointType;
00146 }
00147 
00148 ParameterHandle::ParameterHandle(KoPathTool *tool, KoParameterShape *parameterShape, int handleId)
00149         : KoPathToolHandle(tool)
00150         , m_parameterShape(parameterShape)
00151         , m_handleId(handleId)
00152 {
00153 }
00154 
00155 void ParameterHandle::paint(QPainter &painter, const KoViewConverter &converter)
00156 {
00157     painter.save();
00158     painter.setMatrix(m_parameterShape->absoluteTransformation(&converter) * painter.matrix());
00159 
00160     int handleRadius = m_tool->canvas()->resourceProvider()->handleRadius();
00161     m_parameterShape->paintHandle(painter, converter, m_handleId, handleRadius);
00162     painter.restore();
00163 }
00164 
00165 void ParameterHandle::repaint() const
00166 {
00167     m_tool->repaint(m_parameterShape->shapeToDocument(QRectF(m_parameterShape->handlePosition(m_handleId), QSize(1, 1))));
00168 }
00169 
00170 KoInteractionStrategy * ParameterHandle::handleMousePress(KoPointerEvent *event)
00171 {
00172     if (event->button() & Qt::LeftButton) {
00173         KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00174         if (selection)
00175             selection->clear();
00176         return new KoParameterChangeStrategy(m_tool, m_tool->canvas(), m_parameterShape, m_handleId);
00177     }
00178     return 0;
00179 }
00180 
00181 bool ParameterHandle::check()
00182 {
00183     return m_tool->canvas()->shapeManager()->selection()->isSelected(m_parameterShape);
00184 }
00185 
00186 
00187 ConnectionHandle::ConnectionHandle(KoPathTool *tool, KoParameterShape *parameterShape, int handleId)
00188         : ParameterHandle(tool, parameterShape, handleId)
00189 {
00190 }
00191 
00192 KoInteractionStrategy * ConnectionHandle::handleMousePress(KoPointerEvent *event)
00193 {
00194     if (event->button() & Qt::LeftButton) {
00195         KoPathToolSelection * selection = dynamic_cast<KoPathToolSelection*>(m_tool->selection());
00196         if (selection)
00197             selection->clear();
00198         KoConnectionShape * shape = dynamic_cast<KoConnectionShape*>(m_parameterShape);
00199         if (! shape)
00200             return 0;
00201         return new KoPathConnectionPointStrategy(m_tool, m_tool->canvas(), shape, m_handleId);
00202     }
00203     return 0;
00204 }