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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*
* Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* either version 2, or (at your option) any later version of the License.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "WebTool.h"
#include <kundo2command.h>
#include <QPainter>
#include <KoCanvasBase.h>
#include <KoPointerEvent.h>
#include <KoShapeManager.h>
#include <KoSelection.h>
#include "WebShape.h"
#include <klocalizedstring.h>
#include "WebToolWidget.h"
class ChangeScroll : public KUndo2Command
{
public:
ChangeScroll(WebShape* shape, const QPointF& oldScroll) : m_shape(shape), m_newScroll(shape->scroll()), m_oldScroll(oldScroll) {
}
virtual void undo() {
m_shape->setScroll(m_oldScroll);
m_shape->update();
}
virtual void redo() {
m_shape->setScroll(m_newScroll);
m_shape->update();
}
private:
WebShape *m_shape;
QPointF m_newScroll;
QPointF m_oldScroll;
};
class ChangeZoom : public KUndo2Command
{
public:
ChangeZoom(WebShape* shape, qreal oldZoom) : m_shape(shape), m_newZoom(shape->zoom()), m_oldZoom(oldZoom) {
}
virtual void undo() {
m_shape->setZoom(m_oldZoom);
m_shape->update();
}
virtual void redo() {
m_shape->setZoom(m_newZoom);
m_shape->update();
}
private:
WebShape *m_shape;
qreal m_newZoom;
qreal m_oldZoom;
};
WebTool::WebTool(KoCanvasBase *canvas) : KoToolBase(canvas), m_tmpShape(0), m_dragMode(NO_DRAG)
{
}
WebTool::~WebTool()
{
}
void WebTool::activate(ToolActivation /*toolActivation*/, const QSet<KoShape*> &/*shapes*/)
{
Q_ASSERT(m_dragMode == NO_DRAG);
KoSelection *selection = canvas()->shapeManager()->selection();
foreach(KoShape * shape, selection->selectedShapes()) {
m_currentShape = dynamic_cast<WebShape*>(shape);
if(m_currentShape)
break;
}
emit(shapeChanged(m_currentShape));
if(m_currentShape == 0) {
// none found
emit done();
return;
}
}
void WebTool::paint(QPainter &painter, const KoViewConverter &converter)
{
Q_UNUSED(painter);
Q_UNUSED(converter);
}
void WebTool::mousePressEvent(KoPointerEvent *event)
{
WebShape *hit = 0;
QRectF roi(event->point, QSizeF(1, 1));
QList<KoShape*> shapes = canvas()->shapeManager()->shapesAt(roi);
KoSelection *selection = canvas()->shapeManager()->selection();
foreach(KoShape * shape, shapes) {
hit = dynamic_cast<WebShape*>(shape);
if(hit) {
if(hit == m_currentShape) {
m_scrollPoint = event->point;
Q_ASSERT(m_dragMode == NO_DRAG);
if(event->modifiers() & Qt::ShiftModifier) {
m_oldZoom = m_currentShape->zoom();
m_dragMode = ZOOM_DRAG;
} else {
m_oldScroll = m_currentShape->scroll();
m_dragMode = SCROLL_DRAG;
}
} else {
selection->deselectAll();
m_currentShape = hit;
selection->select(m_currentShape);
emit(shapeChanged(m_currentShape));
}
}
}
}
void WebTool::mouseMoveEvent(KoPointerEvent *event)
{
switch(m_dragMode) {
case NO_DRAG:
break;
case SCROLL_DRAG: {
m_currentShape->scrollOf(m_scrollPoint - event->point);
m_scrollPoint = event->point;
m_currentShape->update();
break;
}
case ZOOM_DRAG: {
m_currentShape->zoomOf(1.0 - (event->point.y() - m_scrollPoint.y()) / 100.0);
m_scrollPoint = event->point;
m_currentShape->update();
}
}
}
void WebTool::mouseReleaseEvent(KoPointerEvent *event)
{
Q_UNUSED(event);
switch(m_dragMode) {
case NO_DRAG:
break;
case SCROLL_DRAG:
canvas()->addCommand(new ChangeScroll(m_currentShape, m_oldScroll));
break;
case ZOOM_DRAG:
canvas()->addCommand(new ChangeZoom(m_currentShape, m_oldZoom));
break;
}
m_dragMode = NO_DRAG;
}
QList<QPointer<QWidget> > WebTool::createOptionWidgets()
{
QList<QPointer<QWidget> > widgets;
WebToolWidget* widget = new WebToolWidget(this);
widget->open(m_currentShape);
widgets.append(widget);
return widgets;
}
|