Commit afa2118f authored by Thomas Zander's avatar Thomas Zander
Browse files

As it can be seen as a bit odd that the way to add/remove a shape is to fetch

a Tool, I split the class and made the API so all adding/removing now goes via
the KoShapeController class.

Tools that want to add/delete something should do a:
  KoShapeController *sc = KoToolManager::instance()->shapeController(m_canvas)
and eventually things like  sv->deleteShape(myShape);

svn path=/trunk/koffice/; revision=599769
parent 750fc8fc
......@@ -39,6 +39,7 @@ set(flake_SRCS
KoParameterChangeStrategy.cpp
KoRectangleShape.cpp
KoRectangleShapeFactory.cpp
KoShapeController.cpp
)
kde4_automoc(${flake_SRCS})
......
......@@ -28,9 +28,7 @@
#include <kcommand.h>
KoCreateShapesTool::KoCreateShapesTool(KoCanvasBase *canvas)
: KoInteractionTool( canvas )
, m_shapeController( 0 )
, m_newShapeProperties( 0 )
: KoShapeController( canvas ), KoInteractionTool( canvas )
{
}
......
......@@ -22,6 +22,7 @@
#define KOCREATESHAPESTOOL_H
#include "KoInteractionTool.h"
#include "KoShapeController.h"
#include "KoShapeFactory.h"
#include <koffice_export.h>
......@@ -36,7 +37,7 @@ class KoShapeControllerBase;
/**
* A tool to create shapes with.
*/
class FLAKE_EXPORT KoCreateShapesTool : public KoInteractionTool
class FLAKE_EXPORT KoCreateShapesTool : public KoShapeController, public KoInteractionTool
{
public:
/**
......@@ -49,53 +50,6 @@ public:
void mouseReleaseEvent( KoPointerEvent *event );
void paint( QPainter &painter, KoViewConverter &converter );
/**
* Returns the shape Controller that is registered to hold the shapes this tool creates.
* @return the shape controller.
*/
KoShapeControllerBase* controller() const { return m_shapeController; }
/**
* Set the shape controller that this tool adds its created shapes to.
* This tool will create a shape after user input and that shape has to be
* registered to the hosting application. We add/remove the shape via the
* KoShapeControllerBase interface.<br>
* Note that this tool will create a command that can be used for undo/redo.
* The undo will actually call the remove.
* @param sc the controller that will be used to add/remove the created shape.
*/
void setShapeController(KoShapeControllerBase *sc) { m_shapeController = sc; }
/**
* Each shape-type has an Id; as found in KoShapeFactory::id().id(), to choose which
* shape this tool should actually create; set the id before the user starts to
* create the new shape.
* @param id the SHAPEID of the to be generated shape
*/
void setShapeId(const QString &id) { m_shapeId = id; }
/**
* return the shape Id that is to be created.
* @return the shape Id that is to be created.
*/
const QString &shapeId() const { return m_shapeId; }
/**
* Set the shape properties that the create tool will use for the next shape it will
* create.
* @param properties the properties or 0 if the default shape should be created.
*/
void setShapeProperties(KoProperties *properties) { m_newShapeProperties = properties; }
/**
* return the properties to be used for creating the next shape
* @return the properties to be used for creating the next shape
*/
KoProperties const * shapeProperties() { return m_newShapeProperties; }
private:
KoShapeControllerBase *m_shapeController;
QString m_shapeId;
KoProperties *m_newShapeProperties;
};
#endif
/* This file is part of the KDE project
*
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoShapeController.h"
KoShapeController::KoShapeController(KoCanvasBase *canvas)
: m_canvas( canvas )
{
}
/* This file is part of the KDE project
*
* Copyright (C) 2006 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KOSHAPECONTROLLER_H
#define KOSHAPECONTROLLER_H
#include <koffice_export.h>
#include <kcommand.h>
#include <QString>
class KoCanvasBase;
class KoShape;
class KoShapeControllerBase;
class KoProperties;
class FLAKE_EXPORT KoShapeController {
public:
/**
* Create a new tool; typically not called by applications, only by the KoToolManager
* @param canvas the canvas this tool works for.
*/
KoShapeController(KoCanvasBase *canvas);
/// destructor
~KoShapeController() {};
/**
* Set the shape controller that this tool adds its created shapes to.
* This tool will create a shape after user input and that shape has to be
* registered to the hosting application. We add/remove the shape via the
* KoShapeControllerBase interface.<br>
* Note that this tool will create a command that can be used for undo/redo.
* The undo will actually call the remove.
* @param sc the controller that will be used to add/remove the created shape.
*/
void setShapeController(KoShapeControllerBase *sc) { m_shapeController = sc; }
/**
* Each shape-type has an Id; as found in KoShapeFactory::id().id(), to choose which
* shape this tool should actually create; set the id before the user starts to
* create the new shape.
* @param id the SHAPEID of the to be generated shape
*/
void setShapeId(const QString &id) { m_shapeId = id; }
/**
* return the shape Id that is to be created.
* @return the shape Id that is to be created.
*/
const QString &shapeId() const { return m_shapeId; }
/**
* Set the shape properties that the create tool will use for the next shape it will
* create.
* @param properties the properties or 0 if the default shape should be created.
*/
void setShapeProperties(KoProperties *properties) { m_newShapeProperties = properties; }
/**
* return the properties to be used for creating the next shape
* @return the properties to be used for creating the next shape
*/
KoProperties const * shapeProperties() { return m_newShapeProperties; }
/// Suggested API for deleting a shape
KCommand* deleteShape( KoShape *shape);
/// Suggested API for creating a new shape, and registring it with the hosting application.
KCommand* createShape( KoShape *shape );
protected:
friend class KoCreateShapeStrategy;
/**
* Returns the shape Controller that is registered to hold the shapes this tool creates.
* @return the shape controller.
*/
KoShapeControllerBase* controller() const { return m_shapeController; }
private:
KoShapeControllerBase *m_shapeController;
QString m_shapeId;
KoProperties *m_newShapeProperties;
KoCanvasBase *m_canvas;
};
#endif
......@@ -29,6 +29,7 @@
#include <KoInteractionTool.h>
#include <KoShapeMoveStrategy.h>
#include <KoCreateShapesTool.h>
#include <KoShapeController.h>
#include <KoCanvasController.h>
#include <QKeyEvent>
......@@ -52,7 +53,7 @@ public:
resize(m_icon.size());
}
virtual void visit(KoCreateShapesTool *tool) = 0;
virtual void visit(KoShapeController *tool) = 0;
virtual QString toolTip() = 0;
void paint(QPainter &painter, const KoViewConverter &converter) {
......@@ -73,7 +74,7 @@ public:
m_shapeTemplate = shapeTemplate;
}
void visit(KoCreateShapesTool *tool) {
void visit(KoShapeController *tool) {
tool->setShapeId(m_shapeTemplate.id);
tool->setShapeProperties(m_shapeTemplate.properties);
}
......@@ -95,7 +96,7 @@ public:
m_shapeFactory = shapeFactory;
}
void visit(KoCreateShapesTool *tool) {
void visit(KoShapeController *tool) {
tool->setShapeId(m_shapeFactory->shapeId());
tool->setShapeProperties(0);
}
......@@ -172,8 +173,8 @@ void KoShapeSelector::itemSelected() {
KoCanvasController* canvasController = KoToolManager::instance()->activeCanvasController();
if(canvasController) {
KoCreateShapesTool* createTool = KoToolManager::instance()->shapeCreatorTool(canvasController->canvas());
shape->visit(createTool);
KoShapeController* controller = KoToolManager::instance()->shapeController(canvasController->canvas());
shape->visit(controller);
KoToolManager::instance()->switchToolRequested(KoCreateShapesTool_ID);
}
}
......
......@@ -438,14 +438,18 @@ void KoToolManager::switchBackRequested() {
switchTool(m_stack.pop(), false);
}
KoCreateShapesTool *KoToolManager::shapeCreatorTool(KoCanvasBase *canvas) const {
KoShapeController *KoToolManager::shapeCreatorTool(KoCanvasBase *canvas) const {
return shapeController(canvas);
}
KoShapeController *KoToolManager::shapeController(KoCanvasBase *canvas) const {
foreach(KoCanvasController *controller, m_canvases) {
if (controller->canvas() == canvas) {
QMap<QString, KoTool*> tools = m_allTools.value(controller);
KoCreateShapesTool *tool =
dynamic_cast<KoCreateShapesTool*>(tools.value(KoCreateShapesTool_ID));
Q_ASSERT(tool /* ID changed? */);
return tool;
KoShapeController *sc =
dynamic_cast<KoShapeController*>(tools.value(KoCreateShapesTool_ID));
Q_ASSERT(sc /* ID changed? */);
return sc;
}
}
kWarning(30004) << "KoToolManager: can't find the canvas, did you register it?" << endl;
......
......@@ -34,7 +34,7 @@ class KoCanvasBase;
class KoTool;
class KoShapeControllerBase;
class KoToolBox;
class KoCreateShapesTool;
class KoShapeController;
class KActionCollection;
class KoShape;
......@@ -155,6 +155,8 @@ public:
/// @return the active canvas controller
KoCanvasController *activeCanvasController() const;
/// @deprecated. Method is renamed to shapeController()
KoShapeController *shapeCreatorTool(KoCanvasBase *canvas) const;
/**
* Return the tool that is able to create shapes for this param canvas.
* This is typically used by the KoShapeSelector to set which shape to create next.
......@@ -162,7 +164,7 @@ public:
* who's tool you want.
* @see addControllers()
*/
KoCreateShapesTool *shapeCreatorTool(KoCanvasBase *canvas) const;
KoShapeController *shapeController(KoCanvasBase *canvas) const;
public slots:
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment