libs/flake

KoCreateShapeStrategy.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2006 Thorsten Zachmann <zachmann@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "KoCreateShapeStrategy.h"
00022 #include "KoCreateShapesTool.h"
00023 #include "KoShapeRegistry.h"
00024 #include "KoShapeManager.h"
00025 #include "KoCanvasBase.h"
00026 #include "KoShapeFactory.h"
00027 #include "KoShapeController.h"
00028 
00029 #include <kdebug.h>
00030 #include <QUndoCommand>
00031 
00032 KoCreateShapeStrategy::KoCreateShapeStrategy(KoCreateShapesTool *tool, KoCanvasBase *canvas, const QPointF &clicked)
00033         : KoShapeRubberSelectStrategy(tool, canvas, clicked, canvas->snapToGrid())
00034 {
00035     KoCreateShapesTool *parent = static_cast<KoCreateShapesTool*>(m_parent);
00036     KoShapeFactory *factory = KoShapeRegistry::instance()->value(parent->shapeId());
00037     if (factory) {
00038         QMap<QString, KoDataCenter *> dataCenterMap;
00039         const KoProperties *props = parent->shapeProperties();
00040         KoShape *shape;
00041         if (props) {
00042             // it is ok that the data center map is empty as the shape is never added to the document
00043             shape = factory->createShapeAndInit(props, dataCenterMap);
00044         } else {
00045             // it is ok that the data center map is empty as the shape is never added to the document
00046             shape = factory->createDefaultShapeAndInit(dataCenterMap);
00047         }
00048 
00049         m_outline = shape->outline();
00050         m_outlineBoundingRect = m_outline.boundingRect();
00051         delete shape;
00052     }
00053 }
00054 
00055 QUndoCommand* KoCreateShapeStrategy::createCommand()
00056 {
00057     KoCreateShapesTool *parent = static_cast<KoCreateShapesTool*>(m_parent);
00058     KoShapeFactory *factory = KoShapeRegistry::instance()->value(parent->shapeId());
00059     if (! factory) {
00060         kWarning(30006) << "Application requested a shape that is not registered" << parent->shapeId();
00061         return 0;
00062     }
00063 
00064     const KoProperties *props = parent->shapeProperties();
00065     KoShape *shape;
00066     if (props)
00067         shape = factory->createShapeAndInit(props, parent->m_canvas->shapeController()->dataCenterMap());
00068     else
00069         shape = factory->createDefaultShapeAndInit(parent->m_canvas->shapeController()->dataCenterMap());
00070     if (shape->shapeId().isEmpty())
00071         shape->setShapeId(factory->id());
00072     QRectF rect = selectRect();
00073     shape->setPosition(rect.topLeft());
00074     QSizeF newSize = rect.size();
00075     // if the user has dragged when creating the shape,
00076     // resize the shape to the dragged size
00077     if (newSize.width() > 1.0 && newSize.height() > 1.0)
00078         shape->setSize(newSize);
00079 
00080     QUndoCommand * cmd = parent->m_canvas->shapeController()->addShape(shape);
00081     if (cmd) {
00082         KoSelection *selection = parent->m_canvas->shapeManager()->selection();
00083         selection->deselectAll();
00084         selection->select(shape);
00085     }
00086     return cmd;
00087 }
00088 
00089 void KoCreateShapeStrategy::finishInteraction(Qt::KeyboardModifiers modifiers)
00090 {
00091     Q_UNUSED(modifiers);
00092     m_canvas->updateCanvas(selectRect());
00093 }
00094 
00095 void KoCreateShapeStrategy::paint(QPainter &painter, const KoViewConverter &converter)
00096 {
00097     if (m_outline.isEmpty())
00098         KoShapeRubberSelectStrategy::paint(painter, converter);
00099     else {
00100         painter.save();
00101         painter.setRenderHint(QPainter::Antialiasing, false);
00102 
00103         QColor selectColor(Qt::blue);   // TODO make configurable
00104         selectColor.setAlphaF(0.5);
00105         QBrush sb(selectColor, Qt::SolidPattern);
00106         painter.setPen(QPen(sb, 0));
00107         painter.setBrush(sb);
00108         QRectF paintRect = converter.documentToView(selectRect());
00109 
00110         qreal xscale = paintRect.width() / m_outlineBoundingRect.width();
00111         qreal yscale = paintRect.height() / m_outlineBoundingRect.height();
00112         QMatrix matrix;
00113         matrix.translate(-m_outlineBoundingRect.left(), -m_outlineBoundingRect.top());
00114         matrix.scale(xscale, yscale);
00115         painter.translate(paintRect.left(), paintRect.top());
00116 
00117         if (painter.hasClipping())
00118             paintRect = paintRect.intersect(painter.clipRegion().boundingRect());
00119 
00120         painter.setMatrix(matrix, true);
00121         painter.drawPath(m_outline);
00122         painter.restore();
00123     }
00124 }
00125 
00126 void KoCreateShapeStrategy::handleMouseMove(const QPointF &point, Qt::KeyboardModifiers modifiers)
00127 {
00128     KoShapeRubberSelectStrategy::handleMouseMove(point, modifiers);
00129     if (! m_outline.isEmpty())
00130         m_canvas->updateCanvas(selectRect());
00131 }