Commit b7db1796 authored by Patrick Julien's avatar Patrick Julien

Use the Observer and Externsion Interface patterns to hide the view and the document more.

	This setups tools so they can be dynamically loaded.
	It  allows for special cases (i.e. paste) to be exterminated.
	This allows for tools to have more information about their settings even
		if they are not the currently selected, yet still have better
		encapsulation.
This of course, at the same time, breaks all existing tools.

svn path=/trunk/koffice/; revision=265115
parent 1fd7d2ee
......@@ -10,7 +10,8 @@ libkiscore_la_SOURCES = kis_paint_device.cc kis_layer.cc kis_channel.cc kis_mask
kis_brush.cc kis_krayon.cc kis_pattern.cc kis_nameserver.cc \
kis_painter.cc kis_tool_factory.cc kis_cursor.cc kis_selection.cc kis_command.cc \
kis_background.cc kis_global.cc kis_config.cc kis_tool.cc kis_resource.cc kis_resource_mediator.cc \
kis_guide.cc
kis_guide.cc kis_memento_originator.cc \
kis_canvas_subject.cc kis_canvas_controller.cc kis_canvas_observer.cc
libkiscore_la_METASOURCES = AUTO
libkiscore_la_LDFLAGS = $(KDE_RPATH)
......
......@@ -23,7 +23,9 @@
#include "kis_strategy_colorspace_cmyk.h"
#include "kis_strategy_colorspace_rgb.h"
KisColorSpaceFactoryFlyweight moveMe; // XXX Where should we create singletons in Krita?!?
namespace {
KisColorSpaceFactoryFlyweight moveMe; // XXX Where should we create singletons in Krita?!?
}
KisColorSpaceFactoryFlyweight::KisColorSpaceFactoryFlyweight()
{
......
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "kis_canvas_controller.h"
KisCanvasControllerInterface::KisCanvasControllerInterface()
{
}
KisCanvasControllerInterface::~KisCanvasControllerInterface()
{
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined KIS_CANVAS_CONTROLLER_H_
#define KIS_CANVAS_CONTROLLER_H_
#include <qglobal.h>
class QRect;
class KisTool;
class KisCanvasControllerInterface {
public:
KisCanvasControllerInterface();
virtual ~KisCanvasControllerInterface();
public:
virtual void activateTool(KisTool *tool) = 0;
virtual KisTool *currentTool() const = 0;
virtual void updateCanvas() = 0;
virtual void updateCanvas(Q_INT32 x, Q_INT32 y, Q_INT32 w, Q_INT32 h) = 0;
virtual void updateCanvas(const QRect& rc) = 0;
private:
KisCanvasControllerInterface(const KisCanvasControllerInterface&);
KisCanvasControllerInterface& operator=(const KisCanvasControllerInterface&);
};
#endif // KIS_CANVAS_CONTROLLER_H_
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "kis_canvas_observer.h"
KisCanvasObserver::KisCanvasObserver()
{
}
KisCanvasObserver::~KisCanvasObserver()
{
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined KIS_CANVAS_OBSERVER_H_
#define KIS_CANVAS_OBSERVER_H_
class KisCanvasSubject;
class KisCanvasObserver {
public:
KisCanvasObserver();
virtual ~KisCanvasObserver();
public:
virtual void update(KisCanvasSubject *subject) = 0;
private:
KisCanvasObserver(const KisCanvasObserver&);
KisCanvasObserver& operator=(const KisCanvasObserver&);
};
#endif // KIS_CANVAS_OBSERVER_H_
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "kis_canvas_subject.h"
KisCanvasSubject::KisCanvasSubject()
{
}
KisCanvasSubject::~KisCanvasSubject()
{
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined KIS_CANVAS_SUBJECT_H_
#define KIS_CANVAS_SUBJECT_H_
#include <qstring.h>
#include <koColor.h>
#include "kis_types.h"
class KoDocument;
class KisBrush;
class KisCanvasControllerInterface;
class KisCanvasObserver;
class KisGradient;
class KisPattern;
class KisUndoAdapter;
class KisCanvasSubject {
public:
KisCanvasSubject();
virtual ~KisCanvasSubject();
public:
virtual void attach(KisCanvasObserver *observer) = 0;
virtual void detach(KisCanvasObserver *observer) = 0;
virtual void notify() = 0;
virtual KisImageSP currentImg() const = 0;
virtual QString currentImgName() const = 0;
virtual KoColor bgColor() const = 0;
virtual void setBGColor(const KoColor& c) = 0;
virtual KoColor fgColor() const = 0;
virtual void setFGColor(const KoColor& c) = 0;
virtual KisBrush *currentBrush() const = 0;
virtual KisPattern *currentPattern() const = 0;
virtual KisGradient *currentGradient() const = 0;
virtual double zoomFactor() const = 0;
virtual KisUndoAdapter *undoAdapter() const = 0;
virtual KisCanvasControllerInterface *controller() const = 0;
virtual KoDocument *document() const = 0;
private:
KisCanvasSubject(const KisCanvasSubject&);
KisCanvasSubject& operator=(const KisCanvasSubject&);
};
#endif // KIS_CANVAS_SUBJECT_H_
......@@ -19,9 +19,15 @@
#if !defined KIS_MEMENTO_H_
#define KIS_MEMENTO_H_
#include <qcstring.h>
class KisMemento {
public:
virtual ~KisMemento();
};
typedef QByteArray KisMemento;
inline
KisMemento::~KisMemento()
{
}
#endif // KIS_MEMENTO_H_
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "kis_memento_originator.h"
KisMementoOriginatorInterface::KisMementoOriginatorInterface()
{
}
KisMementoOriginatorInterface::~KisMementoOriginatorInterface()
{
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined KIS_MEMENTO_ORIGINATOR_H_
#define KIS_MEMENTO_ORIGINATOR_H_
#include "kis_types.h"
class KisMementoOriginatorInterface {
public:
KisMementoOriginatorInterface();
virtual ~KisMementoOriginatorInterface();
public:
virtual KisMementoSP save() const = 0;
virtual void restore(KisMementoSP memento) = 0;
private:
KisMementoOriginatorInterface(const KisMementoOriginatorInterface&);
KisMementoOriginatorInterface& operator=(const KisMementoOriginatorInterface&);
};
#endif // KIS_MEMENTO_ORIGINATOR_H_
/*
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2002, 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -15,5 +15,15 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "kis_tool.h"
#include "kis_tool.moc"
KisTool::KisTool()
{
}
KisTool::~KisTool()
{
}
/*
* Copyright (c) 1999 Matthias Elter <me@kde.org>
* Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2002, 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -16,31 +16,34 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined KIS_TOOL_H_
#define KIS_TOOL_H_
#include <qevent.h>
#include <qobject.h>
#include <qstring.h>
#include <koColor.h>
#include "kis_types.h"
#include "kis_cursor.h"
#include "kis_brush.h"
#include "kis_pattern.h"
#include "kis_gradient.h"
#include "kis_canvas_observer.h"
class QCursor;
class QEvent;
class QKeyEvent;
class QMouseEvent;
class QPainter;
class QRect;
class QTabletEvent;
class QWidget;
class KActionCollection;
class KDialog;
class KoColor;
class KisBrush;
class KisGradient;
class KisPattern;
class KisToolInterface : public QObject, public KShared {
class KisTool : public QObject, public KisCanvasObserver {
Q_OBJECT
public:
KisToolInterface();
virtual ~KisToolInterface();
KisTool();
virtual ~KisTool();
public:
virtual void paint(QPainter& gc) = 0;
......@@ -48,46 +51,25 @@ public:
virtual void clear() = 0;
virtual void clear(const QRect& rc) = 0;
virtual void setup() = 0;
virtual void activate() = 0;
virtual void setup(KActionCollection *collection) = 0;
virtual void enter(QEvent *e) = 0;
virtual void leave(QEvent *e) = 0;
virtual void mousePress(QMouseEvent *e) = 0;
virtual void mouseMove(QMouseEvent *e) = 0;
virtual void mouseRelease(QMouseEvent *e) = 0;
virtual void tabletEvent(QTabletEvent *e) = 0;
virtual void tabletEvent(QTabletEvent *e) = 0;
virtual void keyPress(QKeyEvent *e) = 0;
virtual void keyRelease(QKeyEvent *e) = 0;
virtual void setCursor(const QCursor& cursor) = 0;
virtual void cursor(QWidget *w) const = 0;
virtual KDialog *options() = 0;
virtual void save(KisToolMementoSP memento) = 0;
virtual void restore(KisToolMementoSP memento) = 0;
virtual void setBrush(KisBrush *brush) = 0;
virtual void setPattern(KisPattern *pattern) = 0;
virtual void setGradient(KisGradient *gradient) = 0;
virtual void setFGColor(const KoColor& color) = 0;
public slots:
virtual void activateSelf() = 0;
virtual KDialog *options(QWidget *parent) = 0;
private:
KisToolInterface(const KisToolInterface&);
KisToolInterface& operator=(const KisToolInterface&);
KisTool(const KisTool&);
KisTool& operator=(const KisTool&);
};
inline
KisToolInterface::KisToolInterface() : QObject(), KShared()
{
}
inline
KisToolInterface::~KisToolInterface()
{
}
#endif // KIS_TOOL_H_
/*
* Copyright (c) 2000 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -15,33 +15,12 @@
* along with view program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <kaction.h>
#include "kis_global.h"
#include "kis_types.h"
#include "kis_tool.h"
#include "kis_tool_factory.h"
// tools
// Non-working tools (Re-ordered because I became confused).
#if 0
#include "kis_tool_select_freehand.h"
#include "kis_tool_select_polygonal.h"
#include "kis_tool_select_elliptical.h"
#include "kis_tool_select_contiguous.h"
#include "kis_tool_airbrush.h"
#include "kis_tool_pen.h"
#include "kis_tool_line.h"
#include "kis_tool_polyline.h"
#include "kis_tool_polygon.h"
#include "kis_tool_rectangle.h"
#include "kis_tool_ellipse.h"
#include "kis_tool_colorchanger.h"
#include "kis_tool_eraser.h"
#include "kis_tool_fill.h"
#include "kis_tool_stamp.h"
#endif
// Working tools -- note that the 'paste' tool is added in the KisView, not here.
#include "kis_tool_select_rectangular.h"
#include "kis_tool_move.h"
......@@ -50,22 +29,27 @@
#include "kis_tool_test.h"
#include "kis_tool_qpen.h"
#include "kis_tool_brush.h"
#endif
/*
* toolFactory
*
* Load every know tool, and make it available to the KisView. Hopefully, someday
* these tools will be parts, so we can discover which tools are available at runtime
* instead of compile time.
*
* The doc becomes the parent of these tools, so you don't need to delete them. When
* the doc is destroyed, they will get automatically deleted. In other words, to be
* safe, you should store the vector returned by this function inside the parent. This
* way, nobody will use this vector after the parent dies.
*/
vKisToolSP toolFactory(KisView *view, KisDoc *doc)
namespace {
KisToolFactory moveMe; // XXX Where to create singletons in Krita?!?
}
KisToolFactory *KisToolFactory::m_singleton = 0;
KisToolFactory::KisToolFactory()
{
vKisToolSP tools;
KisToolFactory::m_singleton = this;
}
KisToolFactory::~KisToolFactory()
{
}
void KisToolFactory::create(KActionCollection *actionCollection, KisCanvasSubject *subject)
{
#if 0
vKisTool tools;
Q_ASSERT(view);
Q_ASSERT(doc);
......@@ -111,9 +95,16 @@ vKisToolSP toolFactory(KisView *view, KisDoc *doc)
tools.push_back(new EllipseTool(doc, canvas));
#endif
for (vKisToolSP_it it = tools.begin(); it != tools.end(); it++)
for (vKisTool_it it = tools.begin(); it != tools.end(); it++)
(*it) -> setup();
return tools;
#endif
}
KisToolFactory *KisToolFactory::singleton()
{
return KisToolFactory::m_singleton;
}
/*
* Copyright (c) 2000 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -19,12 +19,37 @@
#if !defined KIS_TOOL_FACTORY_H_
#define KIS_TOOL_FACTORY_H_
#include "kis_global.h"
#include <vector>
#include "kis_types.h"
class KisDoc;
class KisView;
class KActionCollection;
class KisCanvasSubject;
vKisToolSP toolFactory(KisView *view, KisDoc *doc);
class KisToolFactory {
typedef std::vector<KisTool*> vKisTool;
typedef vKisTool::iterator vKisTool_it;
typedef vKisTool::const_iterator vKisTool_cit;
public:
KisToolFactory();
~KisToolFactory();
public:
void create(KActionCollection *actionCollection, KisCanvasSubject *subject);
public:
static KisToolFactory *singleton();
private:
KisToolFactory(const KisToolFactory&);
KisToolFactory& operator=(const KisToolFactory&);
private:
vKisTool m_tools;
private:
static KisToolFactory *m_singleton;
};
#endif // KIS_TOOL_FACTORY_H_
......@@ -80,14 +80,10 @@ typedef QValueVector<QPoint> vKisSegments;
class KoColor;
typedef QValueVector<KoColor> KoColorMap;
class KisToolInterface;
typedef KSharedPtr<KisToolInterface> KisToolSP;
typedef QValueVector<KisToolSP> vKisToolSP;
typedef vKisToolSP::iterator vKisToolSP_it;
typedef vKisToolSP::const_iterator vKisToolSP_cit;
class KisToolMemento;
typedef KSharedPtr<KisToolMemento> KisToolMementoSP;
class KisTool;
typedef QValueVector<KisTool*> vKisTool;
typedef vKisTool::iterator vKisTool_it;
typedef vKisTool::const_iterator vKisTool_cit;
class KisStrategyColorSpace;
typedef KSharedPtr<KisStrategyColorSpace> KisStrategyColorSpaceSP;
......@@ -96,5 +92,8 @@ typedef QMap<Q_INT32, KisStrategyColorSpaceSP> KisStrategyColorSpaceMap;
class KisGuide;
typedef KSharedPtr<KisGuide> KisGuideSP;
class KisMemento;
typedef KSharedPtr<KisMemento> KisMementoSP;
#endif // KISTYPES_H_
......@@ -19,6 +19,8 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <algorithm>
// Qt
#include <qapplication.h>
#include <qbutton.h>
......@@ -79,7 +81,6 @@
#include "kis_tabbar.h"
#include "kis_tool.h"
#include "kis_tool_factory.h"
#include "kis_tool_paste.h"
#include "kis_view.h"
#include "kis_undo_adapter.h"
#include "kis_util.h"
......@@ -103,6 +104,7 @@ KisView::KisView(KisDoc *doc, KisUndoAdapter *adapter, QWidget *parent, const ch
else
setXMLFile("krita.rc");
m_tool = 0;
m_doc = doc;
m_adapter = adapter;
m_canvas = 0;
......@@ -608,42 +610,24 @@ void KisView::clearCanvas(const QRect& rc)
gc.eraseRect(rc);
}
void KisView::activateTool(KisToolSP tool)
void KisView::activateTool(KisTool *tool)
{
if (m_tool)
m_tool -> clear();
if (tool && qFind(m_toolSet.begin(), m_toolSet.end(), tool) != m_toolSet.end()) {
if (tool) {
m_tool = tool;
m_tool -> cursor(m_canvas);
m_tool -> setFGColor( m_fg );
m_tool -> setBrush( m_brush );
m_tool -> setPattern( m_pattern );
m_tool -> setGradient( m_gradient );
m_tool -> activate();
notify();
} else {
m_tool = 0;
m_canvas -> setCursor(KisCursor::arrowCursor());
}
}
KisImageSP KisView::currentImg() const
KisTool *KisView::currentTool() const
{
if (m_current && m_doc -> contains(m_current))
return m_current;
m_current = m_doc -> imageNum(m_doc -> nimages() - 1);
connectCurrentImg();
return m_current;
}
QString KisView::currentImgName() const
{
if (currentImg())
return currentImg() -> name();
return QString::null;
return m_tool;
}
Q_INT32 KisView::horzValue() const
......@@ -831,8 +815,8 @@ void KisView::copy()
void KisView::paste()
{
Q_ASSERT(!QApplication::clipboard() -> image().isNull());
activateTool(m_paste);
// Q_ASSERT(!QApplication::clipboard() -> image().isNull());
// activateTool(m_paste);
}
void KisView::paste_into()
......@@ -1631,18 +1615,17 @@ void KisView::brushActivated(KisResource *brush)