Commit 8d719505 authored by Cyrille Berger's avatar Cyrille Berger

* add a KisGenericFactory template

* make KisColorspaceFactory using the KisGenericFactory
* add a KisFilter class and a KisFiltersFactory
* add a plugin for using our custom widgets in QT/Designer
* add a initSingleton function to KisDoc to create the singletons when the first doc is created (when the colorspaces are created outside a class QT/Designer crashes when loading the KisWidgetsPlugin)
* add a function in KisView that return the active view
* the example plugin has been converted to the new filter facilities

svn path=/trunk/koffice/; revision=332150
parent 089ddcf8
......@@ -71,7 +71,9 @@ libkritacore_la_SOURCES = kis_paint_device.cc \
KIsDocIface.cc \
KIsDocIface.skel \
KIsImageIface.cc \
KIsImageIface.skel
KIsImageIface.skel \
kis_filter.cc \
kis_filter_factory.cc
noinst_HEADERS = kis_paint_device.h \
kis_layer.h \
......@@ -117,8 +119,9 @@ noinst_HEADERS = kis_paint_device.h \
KRayonViewIface.h \
KIsDocIface.h \
KIsImageIface.h \
kis_matrix.h
kis_matrix.h \
kis_filter.h \
kis_filter_factory.h
libkritacore_la_METASOURCES = AUTO
libkritacore_la_LDFLAGS = $(all_libraries)
......
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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
......@@ -32,9 +33,9 @@ KisColorSpaceFactory::~KisColorSpaceFactory()
{
}
KisColorSpaceFactory *KisColorSpaceFactory::singleton()
KisColorSpaceFactory* KisColorSpaceFactory::singleton()
{
// Q_ASSERT(KisColorSpaceFactoryInterface::m_singleton);
// Q_ASSERT(KisColorSpaceFactoryInterface::m_singleton);
if(KisColorSpaceFactory::m_singleton == 0)
{
KisColorSpaceFactory::m_singleton = new KisColorSpaceFactory();
......@@ -42,61 +43,12 @@ KisColorSpaceFactory *KisColorSpaceFactory::singleton()
return KisColorSpaceFactory::m_singleton;
}
#if 0
KisStrategyColorSpaceSP KisColorSpaceFactory::create(enumImgType imgType)
{
switch (imgType) {
case IMAGE_TYPE_GREYA:
case IMAGE_TYPE_GREY:
return colorSpace("Grayscale + Alpha");
break;
case IMAGE_TYPE_RGB:
case IMAGE_TYPE_RGBA:
return colorSpace("RGBA");
break;
default:
kdDebug() << "Color space strategy not accessible by create ; " << imgType << endl;
abort();
break;
}
return 0;
}
void KisColorSpaceFactory::add(enumImgType, KisStrategyColorSpaceSP )
{
kdDebug() << "KisColorSpaceFactoryFlyweight::add(enumImgType , KisStrategyColorSpaceSP ) is deprecated" << endl;
}
#endif
KisStrategyColorSpaceSP KisColorSpaceFactory::colorSpace(const QString& name) const
{
kdDebug() << "Requesting colorspace : <" << name << ">" << endl;
KisStrategyColorSpaceSP p;
acFlyweights_cit it = m_flyweights.find(name);
if (it != m_flyweights.end()) {
p = it -> second;
}
Q_ASSERT(p);
return p;
}
void KisColorSpaceFactory::add(KisStrategyColorSpaceSP colorspace)
{
kdDebug() << "add a new colorspace : <" << colorspace->name() << "> " << endl;
m_flyweights.insert(acFlyweights::value_type( colorspace->name(),colorspace));
return get(name);
}
QStringList KisColorSpaceFactory::listColorSpaceNames() const
{
QStringList list;
acFlyweights_cit it = m_flyweights.begin();
acFlyweights_cit endit = m_flyweights.end();
while( it != endit )
{
list.append(it->first);
++it;
}
return list;
return listKeys();
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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,34 +20,22 @@
#if !defined KIS_COLORSPACE_FACTORY_H_
#define KIS_COLORSPACE_FACTORY_H_
#include "kis_global.h"
#include "kis_types.h"
#include <map>
#include <qstring.h>
#include <kdemacros.h>
#include "kis_generic_factory.h"
class QStringList;
class KisColorSpaceFactory {
typedef std::map<QString, KisStrategyColorSpaceSP> acFlyweights;
typedef acFlyweights::iterator acFlyweights_it;
typedef acFlyweights::const_iterator acFlyweights_cit;
class KisColorSpaceFactory : public KisGenericFactory<KisStrategyColorSpaceSP> {
public:
virtual ~KisColorSpaceFactory();
#if 0
public:
KisStrategyColorSpaceSP create(enumImgType imgType) KDE_DEPRECATED;
void add(enumImgType imgType, KisStrategyColorSpaceSP colorspace) KDE_DEPRECATED ;
#endif
KisStrategyColorSpaceSP colorSpace(const QString& name) const KDE_DEPRECATED;
QStringList listColorSpaceNames() const KDE_DEPRECATED;
public:
void add(KisStrategyColorSpaceSP colorspace);
KisStrategyColorSpaceSP colorSpace(const QString& name) const;
QStringList listColorSpaceNames() const;
public:
static KisColorSpaceFactory *singleton();
static KisColorSpaceFactory* singleton();
private:
KisColorSpaceFactory();
KisColorSpaceFactory(const KisColorSpaceFactory&);
......@@ -54,7 +43,6 @@ private:
private:
static KisColorSpaceFactory *m_singleton;
acFlyweights m_flyweights;
};
#endif // KIS_COLORSPACE_FACTORY_H_
......
......@@ -35,7 +35,6 @@
namespace {
const Q_INT32 MAX_CHANNEL_GRAYSCALE = 1;
const Q_INT32 MAX_CHANNEL_GRAYSCALEA = 2;
KisStrategyColorSpaceGrayscale moveMe; // XXX Where should we create singletons in Krita?!?
}
ChannelInfo KisStrategyColorSpaceGrayscale::channelInfo[1] = { ChannelInfo("Gray", 1) };
......
......@@ -35,7 +35,6 @@
namespace {
const Q_INT32 MAX_CHANNEL_RGB = 3;
const Q_INT32 MAX_CHANNEL_RGBA = 4;
KisStrategyColorSpaceRGB moveMe; // XXX Where should we create singletons in Krita?!?
}
ChannelInfo KisStrategyColorSpaceRGB::channelInfo[4] = { ChannelInfo("Red", 3), ChannelInfo("Red", 2), ChannelInfo("Green", 1), ChannelInfo("Blue", 0) };
......
......@@ -74,12 +74,15 @@
#include "builder/kis_builder_monitor.h"
#include "builder/kis_image_magick_converter.h"
#include "color_strategy/kis_strategy_colorspace.h"
#include "color_strategy/kis_strategy_colorspace_rgb.h"
#include "color_strategy/kis_strategy_colorspace_grayscale.h"
#include "kis_colorspace_factory.h"
#include "tiles/kistilemgr.h"
#include "KIsDocIface.h"
static const char *CURRENT_DTD_VERSION = "1.3";
bool KisDoc::m_singletonsHasBeenInited = false;
namespace {
class KisCommandImageAdd : public KisCommand {
......@@ -336,6 +339,7 @@ namespace {
KisDoc::KisDoc(QWidget *parentWidget, const char *widgetName, QObject *parent, const char *name, bool singleViewMode) :
super(parentWidget, widgetName, parent, name, singleViewMode)
{
initSingletons();
m_undo = false;
m_dcop = 0;
setInstance(KisFactory::global(), false);
......@@ -368,6 +372,16 @@ DCOPObject *KisDoc::dcopObject()
return m_dcop;
}
void KisDoc::initSingletons()
{
if(!KisDoc::m_singletonsHasBeenInited)
{
KisDoc::m_singletonsHasBeenInited = true;
new KisStrategyColorSpaceRGB();
new KisStrategyColorSpaceGrayscale();
}
}
bool KisDoc::initDoc()
{
if (!init())
......
......@@ -154,8 +154,10 @@ private:
QDomElement saveChannel(QDomDocument& doc, KisChannelSP channel);
KisChannelSP loadChannel(const QDomElement& elem, KisImageSP img);
bool init();
void initSingletons();
private:
static bool m_singletonsHasBeenInited;
bool m_undo;
KoCommandHistory *m_cmdHistory;
vKisImageSP m_images;
......
/*
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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_filter.h"
#include "kis_filter_factory.h"
#include "kis_view.h"
#include "kis_image.h"
#include "kis_layer.h"
#include "kis_tile_command.h"
#include "kis_undo_adapter.h"
KisFilterConfiguration::KisFilterConfiguration(Q_INT32 x, Q_INT32 y, Q_INT32 width, Q_INT32 height) :
m_x(x),
m_y(y),
m_width(width),
m_height(height)
{
}
KisFilterConfigurationWidget::KisFilterConfigurationWidget(QWidget * parent, const char * name, WFlags f ) :
QWidget(parent, name, f)
{
}
KisFilterConfiguration* KisFilterConfigurationWidget::config()
{
return 0;
}
KisFilter::KisFilter(const QString& name) :
m_name(name)
{
KisFilterFactory::singleton()->add( this );
}
KisFilterConfiguration* KisFilter::defaultConfiguration()
{
KisLayerSP lay = KisView::activeView()->currentImg()->activeLayer();
return new KisFilterConfiguration(0, 0, lay->width(), lay->height() );
}
void KisFilter::refreshPreview(KisFilterConfiguration* )
{
}
KisFilterConfigurationWidget* KisFilter::createConfigurationWidget(QWidget* )
{
return 0;
}
void KisFilter::slotActivated()
{
KisView* view = KisView::activeView();
QWidget* configwidget = createConfigurationWidget( view );
KisFilterConfiguration* config = 0;
if( configwidget != 0)
{
//TODO : the dialog with previewwidget
delete configwidget;
}
if( config == 0 )
{
config = defaultConfiguration();
}
KisImageSP img = view->currentImg();
KisPaintDeviceSP device = (KisPaintDeviceSP) img->activeLayer();
KisTileCommand* ktc = new KisTileCommand(name(), device ); // Create a command
process(device, config, ktc);
img->undoAdapter()->addCommand( ktc );
img->notify();
delete config;
}
/*
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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.
*/
#ifndef _KIS_PLUGIN_H_
#define _KIS_PLUGIN_H_
#include <ksharedptr.h>
#include <qwidget.h>
#include "kis_types.h"
class KisTileCommand;
class KisFilterConfiguration {
public:
KisFilterConfiguration(Q_INT32 x, Q_INT32 y, Q_INT32 width, Q_INT32 height);
public:
inline Q_INT32 x() { return m_x; };
inline Q_INT32 y() { return m_y; };
inline Q_INT32 width() { return m_width; };
inline Q_INT32 height() { return m_height; };
private:
Q_INT32 m_x;
Q_INT32 m_y;
Q_INT32 m_width;
Q_INT32 m_height;
};
class KisFilterConfigurationWidget : public QWidget {
// Q_OBJECT
public:
KisFilterConfigurationWidget ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 );
// public signals:
/** This signal is emited when a value of the configuration is changed
*/
void valueChanged(KisFilterConfiguration* config);
public:
/** This function return the configuration
*/
virtual KisFilterConfiguration* config();
};
class KisFilter : public QObject, public KShared {
Q_OBJECT
public:
KisFilter(const QString& name);
public:
virtual void process(KisPaintDeviceSP, KisFilterConfiguration*, KisTileCommand* ) =0;
public:
inline QString name() { return m_name; };
public slots:
void slotActivated();
protected:
virtual KisFilterConfigurationWidget* createConfigurationWidget(QWidget* parent);
/** This function return the default configuration of the filter.
*/
virtual KisFilterConfiguration* defaultConfiguration();
private slots:
/** This signal is emited when a value of the configuration is changed
*/
void refreshPreview(KisFilterConfiguration* config);
private:
QString m_name;
};
#endif
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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_filter_factory.h"
#include "kis_paint_device.h"
#include "kis_filter.h"
KisFilterFactory *KisFilterFactory::m_singleton = 0;
KisFilterFactory::KisFilterFactory()
{
kdDebug() << " creating a KisFilterFactory" << endl;
Q_ASSERT(KisFilterFactory::m_singleton == 0);
KisFilterFactory::m_singleton = this;
}
KisFilterFactory::~KisFilterFactory()
{
}
KisFilterFactory* KisFilterFactory::singleton()
{
if(KisFilterFactory::m_singleton == 0)
{
KisFilterFactory::m_singleton = new KisFilterFactory();
}
return KisFilterFactory::m_singleton;
}
/*
* Copyright (c) 2003 Patrick Julien <freak@codepimps.org>
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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_FILTER_FACTORY_H_
#define KIS_FILTER_FACTORY_H_
#include "kis_types.h"
#include "kis_generic_factory.h"
class QStringList;
class KisFilterFactory : public KisGenericFactory<KisFilterSP> {
public:
virtual ~KisFilterFactory();
public:
static KisFilterFactory* singleton();
private:
KisFilterFactory();
KisFilterFactory(const KisFilterFactory&);
KisFilterFactory operator=(const KisFilterFactory&);
private:
static KisFilterFactory *m_singleton;
};
#endif // KIS_FILTERSPACE_FACTORY_H_
/*
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
*
* 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.
*/
#ifndef _KIS_GENERIC_FACTORY_H_
#define _KIS_GENERIC_FACTORY_H_
#include <map>
#include <qstring.h>
template<class _T>
class KisGenericFactory {
typedef std::map<QString, _T> storageMap;
public:
virtual ~KisGenericFactory() { };
protected:
KisGenericFactory() { };
public:
void add(_T item)
{
m_storage.insert( storageMap::value_type( item->name(), item) );
}
_T get(const QString& name) const
{
_T p;
storageMap::const_iterator it = m_storage.find(name);
if (it != m_storage.end()) {
p = it -> second;
}
Q_ASSERT(p);
return p;
}
QStringList listKeys() const
{
QStringList list;
storageMap::const_iterator it = m_storage.begin();
storageMap::const_iterator endit = m_storage.end();
while( it != endit )
{
list.append(it->first);
++it;
}
return list;
}
private:
KisGenericFactory(const KisGenericFactory&) { };
KisGenericFactory operator=(const KisGenericFactory&) { };
storageMap m_storage;
};
#endif
......@@ -116,5 +116,9 @@ typedef KSharedPtr<KisAlphaMask> KisAlphaMaskSP;
typedef QValueVector<QUANTUM> vQuantums;
class KisFilter;
typedef KSharedPtr<KisFilter> KisFilterSP;
#endif // KISTYPES_H_
......@@ -125,6 +125,8 @@
// sent to a receiver if it does not accept the tablet event.
#define MOUSE_CHANGE_EVENT_DELAY 100
KisView* KisView::m_activeView = 0;
KisView::KisView(KisDoc *doc, KisUndoAdapter *adapter, QWidget *parent, const char *name) : super(doc, parent, name)
{
if (!doc -> isReadWrite())
......@@ -3053,6 +3055,15 @@ KoDocument *KisView::document() const
return koDocument();
}
void KisView::windowActivationChange ( bool oldActive )
{
if(isActiveWindow())
{
KisView::setActiveView(this);
}
}
#include "kis_view.moc"
......
......@@ -106,7 +106,10 @@ public:
Q_INT32 docWidth() const;
Q_INT32 docHeight() const;
Q_INT32 importImage(bool createLayer, bool modal = false, const QString& filename = QString::null);
/** This function return the active KisView
*/
static KisView* activeView();
virtual KisImageSP currentImg() const;
/**
* Refresh the complete view
......@@ -160,6 +163,9 @@ public slots:
protected:
virtual void resizeEvent(QResizeEvent*);
static void setActiveView(KisView* view);
protected slots:
virtual void windowActivationChange ( bool oldActive );
private:
// Implement KisCanvasSubject
......@@ -320,6 +326,7 @@ private slots:
void updateTabBar();
private:
static KisView* m_activeView;
KisDoc *m_doc;
KisCanvas *m_canvas;
......@@ -416,7 +423,7 @@ private:
KisUndoAdapter *m_adapter;
vKisCanvasObserver m_observers;
QLabel *m_statusBarZoomLabel;
QLabel *m_statusBarZoomLabel;
enumInputDevice m_inputDevice;
InputDeviceToolMap m_inputDeviceToolMap;
......@@ -430,5 +437,13 @@ private:
mutable KisImageSP m_current;
};
inline void KisView::setActiveView(KisView* view)
{
KisView::m_activeView = view;
}
inline KisView* KisView::activeView()
{
return m_activeView;
}
#endif // KIS_VIEW_H_