Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 7f05a6c0 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Add the factory/registry objects for flake.

svn path=/trunk/koffice/; revision=548187
parent d68d3326
include_directories( ${KDE4_INCLUDE_DIR} ${QT_INCLUDES} )
include_directories( ${KDE4_INCLUDE_DIR} ${QT_INCLUDES} ${KOFFICECORE_INCLUDES})
add_subdirectory(tests)
......@@ -23,13 +23,14 @@ set(flake_SRCS
KoCanvasView.cpp
KoCreateShapeStrategy.cpp
KoCreateShapesTool.cpp
KoShapeRegistry.cpp
)
kde4_automoc(${flake_SRCS})
kde4_add_library(flake SHARED ${flake_SRCS})
target_link_libraries(flake ${KDE4_KDEUI_LIBS})
target_link_libraries(flake ${KDE4_KDECORE_LIBS} ${KDE4_KDE3SUPPORT_LIBS} ${KDE4_KDEUI_LIBS})
set_target_properties(flake PROPERTIES VERSION 1.0.0 SOVERSION 1 )
......
......@@ -23,6 +23,7 @@
#define KORECTANGLESHAPE_H
#include <KoShape.h>
#include <KoShapeFactory.h>
#include <koffice_export.h>
......@@ -38,5 +39,6 @@ public:
void paint(QPainter &painter, KoViewConverter &converter);
};
#endif
......@@ -40,19 +40,30 @@ class KoRepaintManager;
class KoShapeBorderModel;
/**
*
* Base class for all flake objects. Flake objects extend this class
* to allow themselves to be manipulated. This class just represents
* a graphical shape in the document and can be manipulated by some default
* tools in this library.
*
* <p>Due to the limited responsibility of this class, the extending object
* can have any data backend and is responsible for painting itself.
*
* <p>We strongly suggest that any extending class will use a Model View
* Controller (MVC) design where the View part is all in this class, as well
* as the one that inharits from this one. This allows the data that rests
* in the model to be reused in different parts of the document. For example
* by having two flake objects that show it. Or each showing a section of it.
*
* <p>The KoShape data is completely in postscript-points (pt) (see KoUnit
* for conversion methods to and from pt).
*
* <p>Flake objects can be created in three ways:
* <ul>
* <li>a simple new KoDerivedFlake(),
* <li>through an associated tool,
* <li>through a factory
* </ul>
*/
class FLAKE_EXPORT KoShape
{
......@@ -376,6 +387,7 @@ public:
* @param keepAspect the new value
*/
void setKeepAspectRatio(bool keepAspect) { m_keepAspect = keepAspect; }
/**
* Setting the shape to keep its aspect-ratio has the effect that user-scaling will
* keep the width/hight ratio intact so as not to distort shapes that rely on that
......@@ -391,6 +403,7 @@ public:
* @return the point that is the absolute, centered position of this shape.
*/
QPointF absolutePosition() const;
/**
* Move this shape to an absolute position where the end location will be the same
* regardless of the shape's rotation/skew/scaling and regardless of this shape having
......
/*
* KoShapeFactory.h -- Part of KOffice
*
* Copyright (c) 2006 Boudewijn Rempt (boud@valdyas.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _KO_SHAPE_FACTORY_
#define _KO_SHAPE_FACTORY_
#include <QString>
#include <QWidget>
#include <QPixmap>
#include <QList>
#include <KoProperties.h>
#include "KoShape.h"
/**
* For the design: see krita's filter configuration objects. Maybe a
* straight copy is good enough?
*
* XXX: Should we move this to KoShape itself?
*/
class KoShapeParameters : KoProperties {
public:
virtual ~KoShapeParameters(){}
};
/**
* Contains a KoShapeParameters object that describes the settings of a
* particular variant of a shape object, together with a name, a description
* and a pixmap for use in the user interface.
*/
struct KoShapeTemplate {
QString name;
QString description;
QString tooltip;
QPixmap pixmap;
KoShapeParameters * params;
};
/**
* An option widget that can create a KoShapeParameters for the creation
* of KoShapes.
*/
class KoShapeOptionsWidget : public QWidget {
public:
/// get the parameters as set in the widget's widget
virtual KoShapeParameters * shapeParameters() = 0;
/// set the widget's widgets according to the given parameters
virtual void setShapeParameters( KoShapeParameters * ) = 0;
};
/**
* The shape factory can create a flake object without needing a tool or a direct
* new().
*
* XXX: Should we wrap the shapes the factory returns in a shared pointer?
*/
class KoShapeFactory {
public:
KoShapeFactory() { m_optionWidget = 0; }
virtual ~KoShapeFactory() {}
virtual KoShape * createDefaultShape() = 0;
virtual KoShape * createShape(KoShapeParameters * params) const = 0;
virtual KoShape * createShapeFromTemplate(KoShapeTemplate * shapeTemplate) const = 0;
virtual QWidget * createOptionWidget(QWidget * parent) { Q_UNUSED(parent); return 0; }
virtual QWidget * optionWidget() const { return m_optionWidget; }
const QList<KoShapeParameters*> getTemplates() const { return m_templates; }
const QString & name() const { return m_name; }
const QString & description() const { return m_description; }
const QString & tooltip() const { return m_tooltip; }
const QPixmap & pixmap() const { return m_pixmap; }
protected:
void addTemplate(KoShapeParameters * params) { m_templates.append(params); }
void setName(const QString & name) { m_name = name; }
void setDescription(const QString & description) { m_description = description; }
void setToolTip(const QString & tooltip) { m_tooltip = tooltip; }
void setPixmap(const QPixmap & pixmap) { m_pixmap = pixmap; }
void setOptionWidget(QWidget * widget) { m_optionWidget = widget; }
private:
QList<KoShapeParameters*> m_templates;
QString m_name;
QString m_description;
QString m_tooltip;
QPixmap m_pixmap;
QWidget * m_optionWidget;
};
#endif // _KO_SHAPE_FACTORY_
/*
* KoShapeRegistry.h -- Part of KOffice
*
* Copyright (c) 2006 Boudewijn Rempt (boud@valdyas.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QString>
#include <kaction.h>
#include <kdebug.h>
#include <klocale.h>
#include <kparts/plugin.h>
#include <kservice.h>
#include <kservicetypetrader.h>
#include <kparts/componentfactory.h>
#include <KoGenericRegistry.h>
#include <KoShapeRegistry.h>
KoShapeRegistry *KoShapeRegistry::m_singleton = 0;
KoShapeRegistry::KoShapeRegistry()
{
Q_ASSERT(KoShapeRegistry::m_singleton == 0);
KoShapeRegistry::m_singleton = this;
KService::List offers = KServiceTypeTrader::self()->query(QString::fromLatin1("KOffice/FlakeShape"),
QString::fromLatin1("(Type == 'Service') and "
"([X-KOffice-Version] == 2)"));
KService::List::ConstIterator iter;
for(iter = offers.begin(); iter != offers.end(); ++iter)
{
KService::Ptr service = *iter;
int errCode = 0;
// Create a plugin: the plugin will register the necessary
// factory classes with us.
KParts::Plugin* plugin =
KParts::ComponentFactory::createInstanceFromService<KParts::Plugin> ( service, this, QStringList(), &errCode);
if ( plugin )
kDebug() << "found plugin " << service->property("Name").toString() << "\n";
else {
kDebug() << "found plugin " << service->property("Name").toString() << ", " << errCode << "\n";
if( errCode == KParts::ComponentFactory::ErrNoLibrary)
{
kWarning(41006) << " Error loading plugin was : ErrNoLibrary " << KLibLoader::self()->lastErrorMessage() << endl;
}
}
}
}
KoShapeRegistry::~KoShapeRegistry()
{
}
KoShapeRegistry* KoShapeRegistry::instance()
{
if(KoShapeRegistry::m_singleton == 0)
{
KoShapeRegistry::m_singleton = new KoShapeRegistry();
}
return KoShapeRegistry::m_singleton;
}
#include "KoShapeRegistry.moc"
/*
* KoShapeRegistry.h -- Part of KOffice
*
* Copyright (c) 2006 Boudewijn Rempt (boud@valdyas.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef KOSHAPEREGISTRY_H
#define KOSHAPEREGISTRY_H
#include <QObject>
#include <KoGenericRegistry.h>
class KoShape;
class KoShapeFactory;
/**
* This singleton class keeps a register of all available flake shapes,
* or rather, of the factories that applications can use to create flake
* shape objects.
*/
class KoShapeRegistry : public QObject, public KoGenericRegistry<KoShapeFactory*>
{
Q_OBJECT
public:
virtual ~KoShapeRegistry();
static KoShapeRegistry * instance();
private:
KoShapeRegistry();
KoShapeRegistry(const KoShapeRegistry&);
KoShapeRegistry operator=(const KoShapeRegistry&);
private:
static KoShapeRegistry *m_singleton;
};
#endif // FLAKESHAPES_H
* Shapes are graphical objects that can render themselves.
* Tools manipulate shapes. Some tools also create shapes
* Shape factories create shapes
* the shape registry keeps a list of all shape factories
......@@ -8,6 +8,7 @@ include_directories( ${KWMF_INCLUDES} ${KOSTORE_INCLUDES} )
set(kofficecore_LIB_SRCS
KoDocument.cpp
KoProperties.cpp
KoGlobal.cpp
KoUnit.cpp
KoFilterManager.cpp
......@@ -105,6 +106,7 @@ install_files( ${SERVICES_INSTALL_DIR} FILES kodocinfopropspage.desktop )
install_files( ${DATA_INSTALL_DIR}/koffice FILES koffice_shell.rc )
install_files( /include FILES
KoContainerHandler.h
KoProperties.h
KoFilter.h
KoFilterChain.h
KoGlobal.h
......
/*
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _KO_GENERIC_REGISTRY_H_
#define _KO_GENERIC_REGISTRY_H_
#include <map>
#include <QString>
/**
* A KoID is a combination of a user-visible string and a string that uniquely
* identifies a given resource across languages.
*/
class KoID {
public:
KoID() : m_id(QString::null), m_name(QString::null) {}
KoID(const QString & id, const QString & name = QString::null)
: m_id(id),
m_name(name) {};
QString id() const { return m_id; };
QString name() const { return m_name; };
friend inline bool operator==(const KoID &, const KoID &);
friend inline bool operator!=(const KoID &, const KoID &);
friend inline bool operator<(const KoID &, const KoID &);
friend inline bool operator>(const KoID &, const KoID &);
private:
QString m_id;
QString m_name;
};
inline bool operator==(const KoID &v1, const KoID &v2)
{
return v1.m_id == v2.m_id;
}
inline bool operator!=(const KoID &v1, const KoID &v2)
{
return v1.m_id != v2.m_id;
}
inline bool operator<(const KoID &v1, const KoID &v2)
{
return v1.m_id < v2.m_id;
}
inline bool operator>(const KoID &v1, const KoID &v2)
{
return v1.m_id < v2.m_id;
}
typedef QList<KoID> KoIDList;
/**
* Base class for registry objects.
*
* Items are mapped by KoID. A KoID is the combination of
* a non-localized string that can be used in files and a
* user-visible, translated string that can be used in the
* user interface.
*/
template<typename T>
class KoGenericRegistry {
protected:
typedef std::map<KoID, T> storageMap;
public:
KoGenericRegistry() { };
virtual ~KoGenericRegistry() { };
public:
/**
* add an object to the registry
* @param item the item to add (NOTE: T must have an KoID id() function)
*/
void add(T item)
{
m_storage.insert( typename storageMap::value_type( item->id(), item) );
}
/**
* add an object to the registry
* @param id the id of the object
* @param item the item
*/
void add(KoID id, T item)
{
m_storage.insert(typename storageMap::value_type(id, item));
}
/**
* This function remove an item from the registry
* @return the object which have been remove from the registry and which can be safely delete
*/
T remove(const KoID& name)
{
T p = 0;
typename storageMap::iterator it = m_storage.find(name);
if (it != m_storage.end()) {
m_storage.erase(it);
p = it->second;
}
return p;
}
/**
* This function remove an item from the registry
* @param id the identifiant of the object
* @return the object which have been remove from the registry and which can be safely delete
*/
T remove(const QString& id)
{
return remove(KoID(id,""));
}
/**
* This function allow to get an object from its KoID
* @param name the KoID of the object
* @return T the object
*/
T get(const KoID& name) const
{
T p = T(0);
typename storageMap::const_iterator it = m_storage.find(name);
if (it != m_storage.end()) {
p = it->second;
}
return p;
}
/**
* Get a single entry based on the identifying part of KoID, not the
* the descriptive part.
*/
T get(const QString& id) const
{
return get(KoID(id, ""));
}
/**
* @param id
* @return true if there is an object corresponding to id
*/
bool exists(const KoID& id) const
{
typename storageMap::const_iterator it = m_storage.find(id);
return (it != m_storage.end());
}
bool exists(const QString& id) const
{
return exists(KoID(id, ""));
}
/**
* This function allow to search a KoID from the name.
* @param t the name to search
* @param result The result is filled in this variable
* @return true if the search has been successfull, false otherwise
*/
bool search(const QString& t, KoID& result) const
{
for(typename storageMap::const_iterator it = m_storage.begin();
it != m_storage.end(); ++it)
{
if(it->first.name() == t)
{
result = it->first;
return true;
}
}
return false;
}
/** This function return a list of all the keys
*/
KoIDList listKeys() const
{
KoIDList list;
typename storageMap::const_iterator it = m_storage.begin();
typename storageMap::const_iterator endit = m_storage.end();
while( it != endit )
{
list.append(it->first);
++it;
}
return list;
}
protected:
KoGenericRegistry(const KoGenericRegistry&) { };
KoGenericRegistry operator=(const KoGenericRegistry&) { };
storageMap m_storage;
};
#endif
/*
* Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
* Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _KO_GENERIC_REGISTRY_H_
#define _KO_GENERIC_REGISTRY_H_
#include <map>
#include <QString>
/**
* A KoID is a combination of a user-visible string and a string that uniquely
* identifies a given resource across languages.
*/
class KoID {
public:
KoID() : m_id(QString::null), m_name(QString::null) {}
KoID(const QString & id, const QString & name = QString::null)
: m_id(id),
m_name(name) {};
QString id() const { return m_id; };
QString name() const { return m_name; };
friend inline bool operator==(const KoID &, const KoID &)
{
return v1.m_id == v2.m_id;
}
friend inline bool operator!=(const KoID &, const KoID &);
{
return v1.m_id != v2.m_id;
}
friend inline bool operator<(const KoID &, const KoID &);
{
return v1.m_id < v2.m_id;
}
friend inline bool operator>(const KoID &, const KoID &);
{
return v1.m_id < v2.m_id;
}
private:
QString m_id;
QString m_name;
};
typedef QList<KoID> KoIDList;
/**
* Base class for registry objects.
*
* Items are mapped by KoID. A KoID is the combination of
* a non-localized string that can be used in files and a
* user-visible, translated string that can be used in the
* user interface.
*/
template<typename T>
class KoGenericRegistry {
protected:
typedef std::map<KoID, T> storageMap;
public:
KoGenericRegistry() { };
virtual ~KoGenericRegistry() { };
public:
/**
* add an object to the registry
* @param item the item to add (NOTE: T must have an KoID id() function)
*/
void add(T item)
{
m_storage.insert( typename storageMap::value_type( item->id(), item) );
}
/**
* add an object to the registry
* @param id the id of the object
* @param item the item
*/