libs/flake
KoCreateShapeStrategy.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00043 shape = factory->createShapeAndInit(props, dataCenterMap);
00044 } else {
00045
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
00076
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);
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 }
|