Commit 03d41494 authored by Thomas Zander's avatar Thomas Zander

Introduce a (not-pretty-but-working) toolmanager;

the toolmanager loads tools from all libraries that ship a .desktop file
with the serviceType KOffice/Tool.
They will then automatically appear in the toolBox of the application
and change the tool on the canvas should the user click it.

svn path=/trunk/koffice/; revision=549110
parent 01c2f279
......@@ -26,6 +26,7 @@ set(flake_SRCS
KoShapeRegistry.cpp
KoToolFactory.cpp
KoCreateShapesToolFactory.cpp
KoInteractionToolFactory.cpp
)
kde4_automoc(${flake_SRCS})
......@@ -38,6 +39,5 @@ set_target_properties(flake PROPERTIES VERSION ${GENERIC_KOFFICE_LIB_VERSION} SO
install_targets(${LIB_INSTALL_DIR} flake )
install_files( ${SERVICES_INSTALL_DIR} FILES flaketoolcreateshapes.desktop )
install_files( ${SERVICETYPES_INSTALL_DIR} FILES flaketool.desktop )
......@@ -91,7 +91,8 @@ public:
* Return the curently active tool, or 0 if non active.
* @return the curently active tool, or 0 if non active.
*/
virtual KoTool* activeTool() = 0;
virtual KoTool* tool() = 0;
virtual void setTool(KoTool *tool) = 0;
/**
* Return the viewConverter for this view.
......
......@@ -34,10 +34,14 @@ KoCanvasView::KoCanvasView(QWidget *parent)
}
void KoCanvasView::setCanvas(KoCanvasBase *canvas) {
Q_ASSERT(m_canvas == 0 /* Only allowed one time */);
Q_ASSERT(canvas); // param is not null
if(m_canvas) {
emit canvasRemoved(m_canvas);
m_viewport->removeCanvas(m_canvas->canvasWidget());
}
m_viewport->setCanvas(canvas->canvasWidget());
m_canvas = canvas;
emit canvasSet(m_canvas);
}
KoCanvasBase* KoCanvasView::canvas() const {
......@@ -87,10 +91,16 @@ void KoCanvasView::Viewport::setCanvas(QWidget *canvas) {
m_layout->addWidget(canvas, 0, 1, Qt::AlignHCenter);
}
void KoCanvasView::Viewport::removeCanvas(QWidget *canvas) {
m_layout->removeWidget(canvas);
}
void KoCanvasView::Viewport::centerCanvas(bool centered) {
m_layout->setColumnStretch(0,centered?1:0);
m_layout->setColumnStretch(1,1);
m_layout->setColumnStretch(2,centered?1:2);
}
#include "KoCanvasView.moc"
// TODO add a paintEvent here and paint a nice shadow to the bottom/right of the canvas
......@@ -31,6 +31,7 @@ class QGridLayout;
class QPaintEvent;
class FLAKE_EXPORT KoCanvasView : public QScrollArea {
Q_OBJECT
public:
KoCanvasView(QWidget *parent);
virtual ~KoCanvasView() {};
......@@ -46,12 +47,17 @@ public:
void centerCanvas(bool centered);
bool isCanvasCentered() const;
signals:
void canvasRemoved(KoCanvasBase*);
void canvasSet(KoCanvasBase*);
private:
class Viewport : public QWidget {
public:
Viewport();
~Viewport() {};
void setCanvas(QWidget *canvas);
void removeCanvas(QWidget *canvas);
void centerCanvas(bool centered);
private:
QGridLayout *m_layout;
......
......@@ -20,9 +20,14 @@
#include "KoCreateShapesToolFactory.h"
#include "KoCreateShapesTool.h"
#include "KoShapeControllerInterface.h"
#include "KoRectangleShape.h"
#include <klocale.h>
#include <QColor>
#include <QRectF>
KoCreateShapesToolFactory::KoCreateShapesToolFactory() {
}
......@@ -30,7 +35,21 @@ KoCreateShapesToolFactory::~KoCreateShapesToolFactory() {
}
KoTool* KoCreateShapesToolFactory::createTool(KoCanvasBase *canvas) {
return new KoCreateShapesTool(canvas, 0);
class DummyShapeController : public KoShapeControllerInterface {
public:
DummyShapeController() {};
~DummyShapeController() {};
void addShape(KoShape* shape) {};
void removeShape(KoShape* shape) {};
KoShape* createShape(const QRectF &outline) const {
KoShape *rect = new KoRectangleShape();
rect->setBackground(QColor(Qt::blue));
rect->setPosition(outline.topLeft());
rect->resize(outline.size());
return rect;
}
};
return new KoCreateShapesTool(canvas, new DummyShapeController());
}
KoID KoCreateShapesToolFactory::id() {
......
......@@ -23,7 +23,10 @@
#include "KoToolFactory.h"
class KoCreateShapesToolFactory : public KoToolFactory {
#include <koffice_export.h>
class FLAKE_EXPORT KoCreateShapesToolFactory : public KoToolFactory {
public:
KoCreateShapesToolFactory();
~KoCreateShapesToolFactory();
......
/* 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 "KoInteractionToolFactory.h"
#include "KoInteractionTool.h"
#include <klocale.h>
KoInteractionToolFactory::KoInteractionToolFactory() {
}
KoInteractionToolFactory::~KoInteractionToolFactory() {
}
KoTool* KoInteractionToolFactory::createTool(KoCanvasBase *canvas) {
return new KoInteractionTool(canvas);
}
KoID KoInteractionToolFactory::id() {
return KoID("Default", i18n("Default"));
}
quint32 KoInteractionToolFactory::priority() const {
return 0;
}
QString KoInteractionToolFactory::toolType() const {
return QString("main");
}
QString KoInteractionToolFactory::tooltipText() const {
return i18n("Default tool");
}
KoID* KoInteractionToolFactory::activationShapeId() const {
return 0;
}
/* 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 KOINTERACTIONTOOLFACTORY_H
#define KOINTERACTIONTOOLFACTORY_H
#include "KoToolFactory.h"
#include <koffice_export.h>
class FLAKE_EXPORT KoInteractionToolFactory : public KoToolFactory {
public:
KoInteractionToolFactory();
~KoInteractionToolFactory();
KoTool* createTool(KoCanvasBase *canvas);
KoID id();
quint32 priority() const;
QString toolType() const;
QString tooltipText() const;
KoID* activationShapeId() const;
};
#endif
......@@ -55,8 +55,8 @@ void KoRepaintManager::repaint(QRectF &rect, const KoShape *shape, bool selectio
return;
m_canvas->updateCanvas(rect);
if(selectionHandles && m_selection->isSelected(shape)) {
if(m_canvas->activeTool())
m_canvas->activeTool()->repaintDecorations();
if(m_canvas->tool())
m_canvas->tool()->repaintDecorations();
}
m_locked = true;
foreach(KoRepaintManager *manager, m_otherManagers) {
......
......@@ -64,7 +64,7 @@ public:
KoTool(KoCanvasBase *canvas );
virtual ~KoTool() {}
public: // Identification
public:
/**
* Return the option widget for this tool. Create it if it
......
[Desktop Entry]
Encoding=UTF-8
Name=Create Shapes
ServiceTypes=KOffice/Tool
Type=Service
X-KDE-Library=flake
X-Flake-Version=1
......@@ -21,6 +21,9 @@
#include "KoToolRegistry.h"
#include <KoToolFactory.h>
#include <KoCreateShapesToolFactory.h>
#include <KoInteractionToolFactory.h>
#include <KoCanvasView.h>
#include "kactioncollection.h"
#include "kdebug.h"
......@@ -32,16 +35,29 @@
KoToolManager::KoToolManager()
: QObject()
, m_toolBox(0)
, m_activeCanvas(0)
, m_activeTool(0)
{
m_dummyTool = new DummyTool();
#if 0
m_oldTool = 0;
m_paletteManager = 0;
m_tools_disabled = false;
#endif
}
KoToolManager::~KoToolManager()
{
delete m_dummyTool;
}
void KoToolManager::setup() {
if(m_tools.size() > 0)
return;
// add defaults
m_tools.append( new ToolHelper(new KoCreateShapesToolFactory()) );
m_tools.append( new ToolHelper(new KoInteractionToolFactory()) );
KoToolRegistry *registry = KoToolRegistry::instance();
foreach(KoID id, registry->listKeys()) {
ToolHelper *t = new ToolHelper(registry->get(id));
......@@ -50,33 +66,84 @@ kDebug(30004) << " th" << t->id().name() << endl;
m_tools.append(t);
}
// connect to all tools so we can hear their button-clicks
foreach(ToolHelper *tool, m_tools)
connect(tool, SIGNAL(toolActivated(ToolHelper*)), this, SLOT(toolActivated(ToolHelper*)));
// do some magic for the sorting here :) TODO
}
QWidget *KoToolManager::createToolBox() {
setup();
QWidget *widget = new QWidget();
widget->setMinimumSize(20, 100);
QButtonGroup *group = new QButtonGroup(widget);
QVBoxLayout *lay = new QVBoxLayout(widget);
foreach(ToolHelper *tool, m_tools) {
QAbstractButton *but = tool->createButton(widget);
group->addButton(but);
lay->addWidget(but);
QWidget *KoToolManager::toolBox() {
// TODO reentrant
if(! m_toolBox) {
setup();
QWidget *widget = new QWidget();
widget->setMinimumSize(20, 100);
QButtonGroup *group = new QButtonGroup(widget);
QVBoxLayout *lay = new QVBoxLayout(widget);
foreach(ToolHelper *tool, m_tools) {
QAbstractButton *but = tool->createButton(widget);
group->addButton(but);
lay->addWidget(but);
}
m_toolBox = widget;
}
return widget;
return m_toolBox;
}
void KoToolManager::registerTools(KActionCollection *ac) {
// TODO
}
void KoToolManager::addCanvasView(const KoCanvasView *view) {
// TODO
void KoToolManager::addCanvasView(KoCanvasView *view) {
if(m_canvases.contains(view))
return;
m_canvases.append(view);
if(view->canvas())
attachCanvas(view->canvas());
connect(view, SIGNAL(canvasRemoved(KoCanvasBase*)), this, SLOT(detachCanvas(KoCanvasBase*)));
connect(view, SIGNAL(canvasSet(KoCanvasBase*)), this, SLOT(attachCanvas(KoCanvasBase*)));
if(m_activeCanvas == 0)
m_activeCanvas = view;
}
void KoToolManager::removeCanvasView(KoCanvasView *view) {
m_canvases.removeAll(view);
if(view->canvas())
detachCanvas(view->canvas());
disconnect(view, SIGNAL(canvasRemoved(KoCanvasBase*)), this, SLOT(detachCanvas(KoCanvasBase*)));
disconnect(view, SIGNAL(canvasSet(KoCanvasBase*)), this, SLOT(attachCanvas(KoCanvasBase*)));
}
void KoToolManager::toolActivated(ToolHelper *tool) {
kDebug(30004) << "ToolActivated " << tool->id().name();
kDebug(30004) << "ToolActivated: '" << tool->id().name() << "'\n";
if(m_activeCanvas == 0)
return;
if(m_activeTool)
delete m_activeTool;
m_activeTool = tool->m_toolFactory->createTool(m_activeCanvas->canvas());
foreach(KoCanvasView *view, m_canvases) {
if(!view->canvas())
continue;
if(view == m_activeCanvas)
view->canvas()->setTool(m_activeTool);
else
view->canvas()->setTool(m_dummyTool);
}
}
void KoToolManager::attachCanvas(KoCanvasBase *cb) {
// TODO listen to focus changes
// TODO listen to selection changes
cb->setTool(m_dummyTool);
}
void KoToolManager::detachCanvas(KoCanvasBase *cb) {
// TODO detach
if(m_activeCanvas && m_activeCanvas->canvas() == cb)
m_activeCanvas = 0;
// TODO delete tool that canvas is holding?
}
// ************ ToolHelper **********
......
......@@ -22,7 +22,7 @@
#include "KoToolBox.h"
#include <KoID.h>
#include <KoCanvasView.h>
#include <KoTool.h>
#include <koffice_export.h>
#include <QMap>
......@@ -32,6 +32,9 @@ class ToolHelper;
class KActionCollection;
class QAbstractButton;
class KoToolFactory;
class KoCanvasView;
class KoCanvasBase;
class KoTool;
/**
* This class manages the activation and deactivation of tools for
......@@ -55,9 +58,10 @@ public:
static KoToolManager* instance();
~KoToolManager();
QWidget *createToolBox(); // TODO alter from QWidget to KoToolBox
QWidget *toolBox(); // TODO alter from QWidget to KoToolBox
void registerTools(KActionCollection *ac);
void addCanvasView(const KoCanvasView *view);
void addCanvasView(KoCanvasView *view);
void removeCanvasView(KoCanvasView *view);
private:
KoToolManager();
......@@ -67,13 +71,19 @@ private:
private slots:
void toolActivated(ToolHelper *tool);
void detachCanvas(KoCanvasBase* canvas);
void attachCanvas(KoCanvasBase* canvas);
private:
static KoToolManager* s_instance;
KoToolBox *m_toolBox;
//KoToolBox *m_toolBox;
QWidget *m_toolBox;
QList<ToolHelper*> m_tools;
QList<KoCanvasView*> m_canvases;
KoCanvasView *m_activeCanvas;
KoTool *m_activeTool, *m_dummyTool;
};
class ToolHelper : public QObject {
......@@ -88,10 +98,22 @@ signals:
void toolActivated(ToolHelper *tool);
private slots:
friend class KoToolManager;
void buttonPressed();
private:
KoToolFactory *m_toolFactory;
};
class DummyTool : public KoTool {
public:
DummyTool() : KoTool(0) {}
~DummyTool() {}
void paint( QPainter &painter, KoViewConverter &converter ) {}
QCursor cursor( const QPointF &position ) { return Qt::ForbiddenCursor; }
void mousePressEvent( KoGfxEvent *event ) {}
void mouseMoveEvent( KoGfxEvent *event ) {}
void mouseReleaseEvent( KoGfxEvent *event ) {}
};
#endif
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