Commit 62b6e0a8 authored by Thomas Zander's avatar Thomas Zander

Refactor KoToolFactory to separate id/name for normal usage.

The toolFactory still returns a KoID on id(), so the impact should
be minimal.  The advantage is that other classes do not have to
provide an 'id/name' combination when in reality they are only
capable of providing the 'id' part.
Also implement the id->enum for compiler safety.

svn path=/trunk/koffice/; revision=550743
parent cca84825
......@@ -38,6 +38,8 @@ class KoShapeControllerBase;
class FLAKE_EXPORT KoCreateShapesTool : public KoInteractionTool
{
public:
enum createShapesToolenum { TOOLID = 58297 };
/**
* Create a new tool; typically not called by applications, only by the KoToolManager
* @param canvas the canvas this tool works for.
......
......@@ -28,7 +28,13 @@
#include <QRectF>
#include <QPixmap>
KoCreateShapesToolFactory::KoCreateShapesToolFactory() {
KoCreateShapesToolFactory::KoCreateShapesToolFactory()
: KoToolFactory(KoCreateShapesTool::TOOLID, i18n("Create Shapes"))
{
setToolTip(i18n("Create object"));
setToolType("main");
setPriority(1);
// TODO create pixmap
}
KoCreateShapesToolFactory::~KoCreateShapesToolFactory() {
......@@ -37,27 +43,3 @@ KoCreateShapesToolFactory::~KoCreateShapesToolFactory() {
KoTool* KoCreateShapesToolFactory::createTool(KoCanvasBase *canvas) {
return new KoCreateShapesTool(canvas);
}
KoID KoCreateShapesToolFactory::id() {
return KoID("createShapesTool", i18n("Create Shapes"));
}
quint32 KoCreateShapesToolFactory::priority() const {
return 1;
}
const QString& KoCreateShapesToolFactory::toolType() const {
return QString("main");
}
const QString& KoCreateShapesToolFactory::tooltipText() const {
return i18n("Create object");
}
KoID KoCreateShapesToolFactory::activationShapeId() const {
return KoID("createTool", i18n("Create object"));
}
const QPixmap& KoCreateShapesToolFactory::icon() const {
return QPixmap();
}
......@@ -34,11 +34,5 @@ public:
~KoCreateShapesToolFactory();
KoTool* createTool(KoCanvasBase *canvas);
KoID id();
quint32 priority() const;
const QString& toolType() const;
const QString& tooltipText() const;
KoID activationShapeId() const;
const QPixmap& icon() const;
};
#endif
......@@ -44,6 +44,8 @@ class KoInteractionStrategy;
class FLAKE_EXPORT KoInteractionTool : public KoTool
{
public:
enum InteractionToolenum { TOOLID = 20766 };
/**
* Constructor for basic interaction tool where user actions are translated
* and handled by interaction strategies of type KoInteractionStrategy.
......
......@@ -25,7 +25,12 @@
#include <QPixmap>
KoInteractionToolFactory::KoInteractionToolFactory() {
KoInteractionToolFactory::KoInteractionToolFactory()
: KoToolFactory(KoInteractionTool::TOOLID, i18n("Default Tool"))
{
setToolTip(i18n("Default tool"));
setToolType("main");
setPriority(0);
}
KoInteractionToolFactory::~KoInteractionToolFactory() {
......@@ -34,27 +39,3 @@ KoInteractionToolFactory::~KoInteractionToolFactory() {
KoTool* KoInteractionToolFactory::createTool(KoCanvasBase *canvas) {
return new KoInteractionTool(canvas);
}
KoID KoInteractionToolFactory::id() {
return KoID("defaultTool", i18n("Default"));
}
quint32 KoInteractionToolFactory::priority() const {
return 0;
}
const QString& KoInteractionToolFactory::toolType() const {
return QString("main");
}
const QString& KoInteractionToolFactory::tooltipText() const {
return i18n("Default tool");
}
KoID KoInteractionToolFactory::activationShapeId() const {
return KoID("defaultTool", i18n("Default Tool"));
}
const QPixmap& KoInteractionToolFactory::icon() const {
return QPixmap();
}
......@@ -33,11 +33,5 @@ public:
~KoInteractionToolFactory();
KoTool* createTool(KoCanvasBase *canvas);
KoID id();
quint32 priority() const;
const QString& toolType() const;
const QString& tooltipText() const;
KoID activationShapeId() const;
const QPixmap& icon() const;
};
#endif
......@@ -63,7 +63,7 @@ public:
virtual QWidget * optionWidget() const { return m_optionWidget; }
const QList<KoProperties*> templates() const { return m_templates; }
const QString & tooltip() const { return m_tooltip; }
const QString & toolTip() const { return m_tooltip; }
const QPixmap & icon() const { return m_icon; }
protected:
......
......@@ -165,7 +165,7 @@ signals:
*
* @param id the identification of the desired tool
*/
void sigActivateTool( const QString &id );
void sigActivateTool(int id );
/**
* Emitted when this tool wants itself to temporarily be replaced by another tool.
......@@ -174,7 +174,7 @@ signals:
* replaced by a colourpicker.
* @param id the identification of the desired tool
*/
void sigActivateTemporary(const QString &id);
void sigActivateTemporary(int id);
/**
* Emitted when the tool has been temporarily activated and wants
......
......@@ -18,12 +18,64 @@
#include "KoToolFactory.h"
KoToolFactory::KoToolFactory()
KoToolFactory::KoToolFactory(int id, const QString name)
: m_name(name)
, m_id(id)
{
m_priority=100;
m_activationId=-1;
}
KoToolFactory::~KoToolFactory()
{
}
int KoToolFactory::toolId() const {
return m_id;
}
int KoToolFactory::priority() const {
return m_priority;
}
const QString& KoToolFactory::toolType() const {
return m_toolType;
}
const QString& KoToolFactory::toolTip() const {
return m_tooltip;
}
const QPixmap& KoToolFactory::icon() const {
return m_icon;
}
int KoToolFactory::activationShapeId() const {
return m_activationId;
}
void KoToolFactory::setToolTip(const QString & tooltip) {
m_tooltip = tooltip;
}
void KoToolFactory::setToolType(const QString & toolType) {
m_toolType = toolType;
}
void KoToolFactory::setIcon(const QPixmap & icon) {
m_icon = icon;
}
void KoToolFactory::setPriority(int newPriority) {
m_priority = newPriority;
}
const KoID KoToolFactory::id() const {
return KoID(QString::number(m_id), m_name);
}
const QString& KoToolFactory::name() const {
return m_name;
}
#include "KoToolFactory.moc"
......@@ -26,7 +26,7 @@
#include <klocale.h>
#include <QObject>
#include <QObject>
#include <QPixmap>
/**
* A factory for KoTool objects.
......@@ -38,27 +38,46 @@ class FLAKE_EXPORT KoToolFactory : public QObject {
Q_OBJECT
public:
/// constructor
KoToolFactory();
KoToolFactory(int id, const QString name);
virtual ~KoToolFactory();
/// instanciate a new tool
virtual KoTool * createTool(KoCanvasBase *canvas) = 0;
/**
* return the ID for the tool this factory is associated with.
* @return the ID for the tool this factory is associated with.
* return the id for the tool this factory is associated with.
* @return the id for the tool this factory is associated with.
*/
virtual KoID id() = 0;
int toolId() const;
const QString& name() const;
const KoID id() const;
/// The priority of this tool in its section in the toolbox
virtual quint32 priority() const = 0;
int priority() const;
/// the type of tool, used to group tools in the toolbox
virtual const QString& toolType() const = 0;
const QString& toolType() const;
/// return a translated tooltip Text
virtual const QString& tooltipText() const = 0;
const QString& toolTip() const;
/// return an icon for this tool
virtual const QPixmap& icon() const = 0;
const QPixmap& icon() const;
/// The shape ID the tool is associated with, or 0 when the tool is a generic tool
virtual KoID activationShapeId() const = 0;
int activationShapeId() const;
protected:
/**
* Set the id of this tool.
* @param id the combination of a internal ID and a (translated) name
*/
void setToolTip(const QString & tooltip);
void setToolType(const QString & toolType);
void setIcon(const QPixmap & icon);
void setPriority(int newPriority);
private:
QString m_toolType;
QString m_tooltip;
QPixmap m_icon;
const QString m_name;
const int m_id;
int m_priority, m_activationId;
};
#endif
......
......@@ -74,7 +74,7 @@ void KoToolManager::setup() {
KoToolRegistry *registry = KoToolRegistry::instance();
foreach(KoID id, registry->listKeys()) {
ToolHelper *t = new ToolHelper(registry->get(id));
kDebug(30004) << " th" << t->id().name() << endl;
kDebug(30004) << " th" << t->name() << endl;
connect(t, SIGNAL(toolActivated(ToolHelper*)), this, SLOT(toolActivated(ToolHelper*)));
m_tools.append(t);
}
......@@ -134,7 +134,7 @@ void KoToolManager::removeCanvasController(KoCanvasController *controller) {
m_mutex.lock();
m_canvases.removeAll(controller);
m_shapeControllers.remove(controller);
QMap<QString, KoTool*> toolsMap = m_allTools.value(controller);
QMap<int, KoTool*> toolsMap = m_allTools.value(controller);
foreach(KoTool *tool, toolsMap.values())
delete tool;
m_allTools.remove(controller);
......@@ -146,15 +146,15 @@ void KoToolManager::removeCanvasController(KoCanvasController *controller) {
}
void KoToolManager::toolActivated(ToolHelper *tool) {
kDebug(30004) << "ToolActivated: '" << tool->id().name() << "'\n";
QMap<QString, KoTool*> toolsMap = m_allTools.value(m_activeCanvas);
KoTool *t = toolsMap.value(tool->id().id());
kDebug(30004) << "ToolActivated: '" << tool->name() << "'\n";
QMap<int, KoTool*> toolsMap = m_allTools.value(m_activeCanvas);
KoTool *t = toolsMap.value(tool->id());
switchTool(t, false);
}
void KoToolManager::switchTool(const QString &id, bool temporary) {
void KoToolManager::switchTool(int id, bool temporary) {
Q_ASSERT(m_activeCanvas);
QMap<QString, KoTool*> toolsMap = m_allTools.value(m_activeCanvas);
QMap<int, KoTool*> toolsMap = m_allTools.value(m_activeCanvas);
KoTool *tool = toolsMap.value(id);
if(! tool) {
kWarning(30004) << "Tool requested " << (temporary?"temporary":"") << "switch to unknown tool: '" << id << "'\n";
......@@ -174,10 +174,10 @@ void KoToolManager::switchTool(KoTool *tool, bool temporary) {
m_activeTool->deactivate();
disconnect(m_activeTool, SIGNAL(sigCursorChanged(QCursor)),
this, SLOT(updateCursor(QCursor)));
disconnect(m_activeTool, SIGNAL(sigActivateTool(const QString &)),
this, SLOT(switchToolRequested(const QString &)));
disconnect(m_activeTool, SIGNAL(sigActivateTemporary(const QString &)),
this, SLOT(switchToolTemporaryRequested(const QString &)));
disconnect(m_activeTool, SIGNAL(sigActivateTool(int)),
this, SLOT(switchToolRequested(int)));
disconnect(m_activeTool, SIGNAL(sigActivateTemporary(int)),
this, SLOT(switchToolTemporaryRequested(int)));
disconnect(m_activeTool, SIGNAL(sigDone()), this, SLOT(switchBackRequested()));
}
if(m_activeTool && temporary)
......@@ -185,10 +185,10 @@ void KoToolManager::switchTool(KoTool *tool, bool temporary) {
m_activeTool = tool;
connect(m_activeTool, SIGNAL(sigCursorChanged(QCursor)),
this, SLOT(updateCursor(QCursor)));
connect(m_activeTool, SIGNAL(sigActivateTool(const QString &)),
this, SLOT(switchToolRequested(const QString &)));
connect(m_activeTool, SIGNAL(sigActivateTemporary(const QString &)),
this, SLOT(switchToolTemporaryRequested(const QString &)));
connect(m_activeTool, SIGNAL(sigActivateTool(int)),
this, SLOT(switchToolRequested(int)));
connect(m_activeTool, SIGNAL(sigActivateTemporary(int)),
this, SLOT(switchToolTemporaryRequested(int)));
connect(m_activeTool, SIGNAL(sigDone()), this, SLOT(switchBackRequested()));
// and set it.
......@@ -207,11 +207,11 @@ void KoToolManager::attachCanvas(KoCanvasController *controller) {
kDebug(30004) << "KoToolManager::attachCanvas\n";
// TODO listen to focus changes
// TODO listen to selection changes
QMap<QString, KoTool*> toolsMap;
QMap<int, KoTool*> toolsMap;
foreach(ToolHelper *tool, m_tools)
toolsMap.insert(tool->id().id(), tool->m_toolFactory->createTool(controller->canvas()));
KoCreateShapesTool *createTool = dynamic_cast<KoCreateShapesTool*>(toolsMap["createShapesTool"]);
Q_ASSERT(createTool); // if this fails; check the ID didn't change
toolsMap.insert(tool->id(), tool->createTool(controller->canvas()));
KoCreateShapesTool *createTool = dynamic_cast<KoCreateShapesTool*>(toolsMap.value(KoCreateShapesTool::TOOLID));
Q_ASSERT(createTool);
createTool->setShapeController(m_shapeControllers[controller]);
m_mutex.lock();
......@@ -231,7 +231,7 @@ void KoToolManager::detachCanvas(KoCanvasController *controller) {
// TODO detach
if(m_activeCanvas == controller)
m_activeCanvas = 0;
QMap<QString, KoTool*> toolsMap = m_allTools.value(controller);
QMap<int, KoTool*> toolsMap = m_allTools.value(controller);
foreach(KoTool *tool, toolsMap.values())
delete tool;
toolsMap.clear();
......@@ -244,20 +244,20 @@ void KoToolManager::updateCursor(QCursor cursor) {
m_activeCanvas->canvas()->canvasWidget()->setCursor(cursor);
}
void KoToolManager::switchToolRequested(const QString &id) {
void KoToolManager::switchToolRequested(int id) {
while (!m_stack.isEmpty()) // switching means to flush the stack
m_stack.pop();
switchTool(id, false);
}
void KoToolManager::switchToolTemporaryRequested(const QString &id) {
void KoToolManager::switchToolTemporaryRequested(int id) {
switchTool(id, true);
}
void KoToolManager::switchBackRequested() {
if(m_stack.isEmpty()) {
// default to changing to the interactionTool
switchTool("defaultTool", false);
switchTool(KoInteractionTool::TOOLID, false);
return;
}
switchTool(m_stack.pop(), false);
......@@ -266,9 +266,9 @@ void KoToolManager::switchBackRequested() {
KoCreateShapesTool *KoToolManager::shapeCreatorTool(KoCanvasBase *canvas) const {
foreach(KoCanvasController *controller, m_canvases) {
if(controller->canvas() == canvas) {
QMap<QString, KoTool*> tools = m_allTools.value(controller);
QMap<int, KoTool*> tools = m_allTools.value(controller);
KoCreateShapesTool *tool =
dynamic_cast<KoCreateShapesTool*>(tools.value("createShapesTool"));
dynamic_cast<KoCreateShapesTool*>(tools.value(KoCreateShapesTool::TOOLID));
Q_ASSERT(tool /* ID changed? */);
return tool;
}
......@@ -280,7 +280,7 @@ KoCreateShapesTool *KoToolManager::shapeCreatorTool(KoCanvasBase *canvas) const
// ************ ToolHelper **********
QAbstractButton* ToolHelper::createButton(QWidget *parent) {
QToolButton *but = new QToolButton(parent);
but->setText(m_toolFactory->id().name());
but->setText(name());
connect(but, SIGNAL(clicked()), this, SLOT(buttonPressed()));
return but;
}
......@@ -289,6 +289,18 @@ void ToolHelper::buttonPressed() {
emit toolActivated(this);
}
int ToolHelper::id() const {
return m_toolFactory->toolId();
}
const QString& ToolHelper::name() const {
return m_toolFactory->name();
}
KoTool *ToolHelper::createTool(KoCanvasBase *canvas) const {
return m_toolFactory->createTool(canvas);
}
//static
KoToolManager* KoToolManager::s_instance = 0;
KoToolManager* KoToolManager::instance() {
......@@ -297,8 +309,4 @@ KoToolManager* KoToolManager::instance() {
return s_instance;
}
KoID ToolHelper::id() const {
return m_toolFactory->id();
}
#include "KoToolManager.moc"
......@@ -75,15 +75,15 @@ private:
KoToolManager operator=(const KoToolManager&);
void setup();
void switchTool(KoTool *tool, bool temporary);
void switchTool(const QString &id, bool temporary);
void switchTool(int id, bool temporary);
private slots:
void toolActivated(ToolHelper *tool);
void detachCanvas(KoCanvasController* controller);
void attachCanvas(KoCanvasController* controller);
void updateCursor(QCursor cursor);
void switchToolRequested(const QString &id);
void switchToolTemporaryRequested(const QString &id);
void switchToolRequested(int id);
void switchToolTemporaryRequested(int id);
void switchBackRequested();
private:
......@@ -102,7 +102,7 @@ private:
* and m_toolBox members. */
QMutex m_mutex;
QMap<KoCanvasController*, QMap<QString, KoTool*> > m_allTools;
QMap<KoCanvasController*, QMap<int, KoTool*> > m_allTools;
QStack<KoTool*> m_stack;
};
......@@ -111,14 +111,14 @@ class ToolHelper : public QObject {
public:
ToolHelper(KoToolFactory *tool) { m_toolFactory = tool; }
QAbstractButton *createButton(QWidget *parent);
KoID id() const;
int id() const;
const QString& name() const;
KoTool *createTool(KoCanvasBase *canvas) const;
signals:
void toolActivated(ToolHelper *tool);
private slots:
friend class KoToolManager;
void buttonPressed();
private:
......
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