1 /* This file is part of the KDE project
2 * Copyright (c) 2010 Jan Hambrecht <jaham@gmx.net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "FilterRegionEditStrategy.h"
21 #include "FilterRegionChangeCommand.h"
22 #include <KoShape.h>
23 #include <KoFilterEffect.h>
24 #include <KoViewConverter.h>
25 #include <QPainter>
26
FilterRegionEditStrategy(KoToolBase * parent,KoShape * shape,KoFilterEffect * effect,KarbonFilterEffectsTool::EditMode mode)27 FilterRegionEditStrategy::FilterRegionEditStrategy(KoToolBase* parent, KoShape * shape, KoFilterEffect *effect, KarbonFilterEffectsTool::EditMode mode)
28 : KoInteractionStrategy(parent), m_effect(effect), m_shape(shape), m_editMode(mode)
29 {
30 Q_ASSERT(m_effect);
31 Q_ASSERT(m_shape);
32 // get the size rect of the shape
33 m_sizeRect = QRectF(QPointF(), m_shape->size());
34 // get the filter rectangle in shape coordinates
35 m_filterRect = m_effect->filterRectForBoundingRect(m_sizeRect);
36 }
37
handleMouseMove(const QPointF & mouseLocation,Qt::KeyboardModifiers modifiers)38 void FilterRegionEditStrategy::handleMouseMove(const QPointF &mouseLocation, Qt::KeyboardModifiers modifiers)
39 {
40 Q_UNUSED(modifiers);
41 QPointF shapePoint = m_shape->documentToShape(mouseLocation);
42 if (m_lastPosition.isNull()) {
43 m_lastPosition = shapePoint;
44 }
45 QPointF delta = shapePoint-m_lastPosition;
46 if( delta.isNull())
47 return;
48
49 switch(m_editMode) {
50 case KarbonFilterEffectsTool::MoveAll:
51 m_filterRect.translate(delta.x(), delta.y());
52 break;
53 case KarbonFilterEffectsTool::MoveLeft:
54 m_filterRect.setLeft(m_filterRect.left()+delta.x());
55 break;
56 case KarbonFilterEffectsTool::MoveRight:
57 m_filterRect.setRight(m_filterRect.right()+delta.x());
58 break;
59 case KarbonFilterEffectsTool::MoveTop:
60 m_filterRect.setTop(m_filterRect.top()+delta.y());
61 break;
62 case KarbonFilterEffectsTool::MoveBottom:
63 m_filterRect.setBottom(m_filterRect.bottom()+delta.y());
64 break;
65 default:
66 // nothing to do here
67 return;
68 }
69 tool()->repaintDecorations();
70 m_lastPosition = shapePoint;
71 }
72
createCommand()73 KUndo2Command *FilterRegionEditStrategy::createCommand()
74 {
75 qreal x = m_filterRect.left() / m_sizeRect.width();
76 qreal y = m_filterRect.top() / m_sizeRect.height();
77 qreal w = m_filterRect.width() / m_sizeRect.width();
78 qreal h = m_filterRect.height() / m_sizeRect.height();
79 return new FilterRegionChangeCommand(m_effect, QRectF(x,y,w,h), m_shape);
80 }
81
finishInteraction(Qt::KeyboardModifiers modifiers)82 void FilterRegionEditStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
83 {
84 Q_UNUSED(modifiers);
85 }
86
paint(QPainter & painter,const KoViewConverter & converter)87 void FilterRegionEditStrategy::paint(QPainter &painter, const KoViewConverter &converter)
88 {
89 Q_UNUSED(converter);
90 // paint the filter subregion rect
91 painter.setBrush(Qt::NoBrush);
92 painter.setPen(QPen(Qt::red, 0));
93 painter.drawRect(m_filterRect);
94 }
95