Development/Tutorials/Games/Palapeli Slicers: Difference between revisions
abstract and structure paragraphs |
the code |
||
Line 18: | Line 18: | ||
A pattern plugin consists of two classes. The first one (derived from [http://api.kde.org/playground-api/games-apidocs/palapeli/lib/html/classPalapeli_1_1PatternConfiguration.html Palapeli::PatternConfiguration]) tells Palapeli what features this pattern plugin has and which configuration values it needs. The second one (derived from [http://api.kde.org/playground-api/games-apidocs/palapeli/lib/html/classPalapeli_1_1Pattern.html Palapeli::Pattern]) does the actual slicing. | A pattern plugin consists of two classes. The first one (derived from [http://api.kde.org/playground-api/games-apidocs/palapeli/lib/html/classPalapeli_1_1PatternConfiguration.html Palapeli::PatternConfiguration]) tells Palapeli what features this pattern plugin has and which configuration values it needs. The second one (derived from [http://api.kde.org/playground-api/games-apidocs/palapeli/lib/html/classPalapeli_1_1Pattern.html Palapeli::Pattern]) does the actual slicing. | ||
== The code == | == The code: mypattern.h == | ||
<code cppqt n> | |||
#ifndef MYPATTERN_H | |||
#define MYPATTERN_H | |||
#include <Palapeli/Pattern> | |||
#include <Palapeli/PatternConfiguration> | |||
class MyPattern : public Palapeli::Pattern | |||
{ | |||
public: | |||
MyPattern(); | |||
virtual ~MyPattern(); | |||
virtual int estimatePieceCount() const; | |||
protected: | |||
virtual void doSlice(const QImage& image); | |||
}; | |||
class MyPatternConfiguration : public Palapeli::PatternConfiguration | |||
{ | |||
public: | |||
MyPatternConfiguration(QObject* parent = 0, const QVariantList& args = QVariantList()); | |||
virtual ~MyPatternConfiguration(); | |||
virtual Palapeli::Pattern* createPattern() const; | |||
}; | |||
#endif // MYPATTERN_H | |||
</code> | |||
== The code: mypattern.cpp == | |||
<code cppqt n> | |||
#include "mypattern.h" | |||
#include <QImage> | |||
#include <KPluginFactory> | |||
#include <KPluginLoader> | |||
K_PLUGIN_FACTORY(MyPatternFactory, registerPlugin<MyPatternConfiguration>();) | |||
K_EXPORT_PLUGIN(MyPatternFactory("mypattern")) | |||
MyPattern::MyPattern() | |||
: Palapeli::Pattern() | |||
{ | |||
} | |||
MyPattern::~MyPattern() | |||
{ | |||
} | |||
int MyPattern::estimatePieceCount() const | |||
{ | |||
return 2; | |||
} | |||
void MyPattern::doSlice(const QImage& image) | |||
{ | |||
//construct pixmaps for the pieces | |||
const int pieceWidth = image.width() / 2, pieceHeight = image.height(); | |||
QImage leftPiece = image.copy(QRect(0, 0, pieceWidth, pieceHeight)); | |||
QImage rightPiece = image.copy(QRect(pieceWidth, 0, pieceWidth, pieceHeight)); | |||
//add pieces; define a neighborship relation between them | |||
addPiece(leftPiece, QRectF(0, 0, pieceWidth, pieceHeight)); | |||
addPiece(rightPiece, QRectF(pieceWidth, 0, pieceWidth, pieceHeight)); | |||
addRelation(0, 1, QPointF(pieceWidth, 0)); //0 and 1 are the consecutive indices of the pieces | |||
} | |||
MyPatternConfiguration::MyPatternConfiguration(QObject* parent, const QVariantList& args) | |||
: Palapeli::PatternConfiguration(parent, args) | |||
{ | |||
Q_UNUSED(parent) | |||
Q_UNUSED(args) | |||
setSizeDefinitionMode(Palapeli::PatternConfiguration::CustomSizeDefinition); | |||
} | |||
MyPatternConfiguration::~MyPatternConfiguration() | |||
{ | |||
} | |||
Palapeli::Pattern* MyPatternConfiguration::createPattern() const | |||
{ | |||
return new MyPattern; | |||
} | |||
</code> | |||
== Integrate into Palapeli: mypattern.desktop == | |||
<code cppqt n> | |||
[Desktop Entry] | |||
X-KDE-Library=mypattern | |||
X-KDE-PluginInfo-Author=The best KDE hacker | |||
X-KDE-PluginInfo-Name=mypattern | |||
X-KDE-PluginInfo-Version=1.0 | |||
X-KDE-PluginInfo-Website=http://kde-hackers.example.org/palapelipatterns | |||
X-KDE-PluginInfo-Category= | |||
X-KDE-PluginInfo-Depends= | |||
X-KDE-PluginInfo-License=GPL | |||
X-KDE-PluginInfo-EnabledByDefault=true | |||
X-KDE-ParentApp=libpalapelipattern | |||
X-KDE-ServiceTypes=Palapeli/PatternPlugin | |||
Type=Service | |||
Icon=palapeli-pattern-mypattern | |||
PatternIdentifier=mypattern | |||
Name=My pattern | |||
Name[de]=Mein Schnittmuster | |||
Comment=The best pattern in the world | |||
Comment[de]=Das beste Schnittmuster auf der ganzen Welt | |||
</code> | |||
== Build everything: CMakeLists.txt == | |||
<code cppqt n> | |||
project(mypattern) | |||
find_package(KDE4 REQUIRED) | |||
find_package(Palapeli REQUIRED) | |||
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS}) | |||
include_directories(${KDE4_INCLUDES} ${PALAPELI_INCLUDE_DIR}) | |||
set(mypattern_SRCS | |||
mypattern.cpp | |||
) | |||
kde4_add_plugin(mypattern ${mypattern_SRCS}) | |||
target_link_libraries(mypattern ${KDE4_KDEUI_LIBS} palapelipattern) | |||
install(TARGETS mypattern DESTINATION ${PLUGIN_INSTALL_DIR}) | |||
install(FILES mypattern.desktop DESTINATION ${SERVICES_INSTALL_DIR}) | |||
</code> |
Revision as of 16:32, 20 July 2008
Tutorial Series | Programming with the Palapeli API |
Previous | Introduction to KDE4 programming |
What's Next | n/a |
Further Reading | Palapeli::Pattern, Palapeli::PatternConfiguration |
Abstract
This tutorial shows you how to create a pattern for Palapeli, that is: a plugin for the Palapeli libraries that describes an algorithm to split an image into pieces.
The pattern we will be constructing is quite easy: It splits an image into two equally sized pieces.
Structure
A pattern plugin consists of two classes. The first one (derived from Palapeli::PatternConfiguration) tells Palapeli what features this pattern plugin has and which configuration values it needs. The second one (derived from Palapeli::Pattern) does the actual slicing.
The code: mypattern.h
- ifndef MYPATTERN_H
- define MYPATTERN_H
- include <Palapeli/Pattern>
- include <Palapeli/PatternConfiguration>
class MyPattern : public Palapeli::Pattern
{
public:
MyPattern();
virtual ~MyPattern();
virtual int estimatePieceCount() const;
protected:
virtual void doSlice(const QImage& image);
};
class MyPatternConfiguration : public Palapeli::PatternConfiguration
{
public:
MyPatternConfiguration(QObject* parent = 0, const QVariantList& args = QVariantList());
virtual ~MyPatternConfiguration();
virtual Palapeli::Pattern* createPattern() const;
};
- endif // MYPATTERN_H
The code: mypattern.cpp
- include "mypattern.h"
- include <QImage>
- include <KPluginFactory>
- include <KPluginLoader>
K_PLUGIN_FACTORY(MyPatternFactory, registerPlugin<MyPatternConfiguration>();)
K_EXPORT_PLUGIN(MyPatternFactory("mypattern"))
MyPattern::MyPattern()
: Palapeli::Pattern()
{
}
MyPattern::~MyPattern()
{
}
int MyPattern::estimatePieceCount() const
{
return 2;
}
void MyPattern::doSlice(const QImage& image)
{
//construct pixmaps for the pieces
const int pieceWidth = image.width() / 2, pieceHeight = image.height();
QImage leftPiece = image.copy(QRect(0, 0, pieceWidth, pieceHeight));
QImage rightPiece = image.copy(QRect(pieceWidth, 0, pieceWidth, pieceHeight));
//add pieces; define a neighborship relation between them
addPiece(leftPiece, QRectF(0, 0, pieceWidth, pieceHeight));
addPiece(rightPiece, QRectF(pieceWidth, 0, pieceWidth, pieceHeight));
addRelation(0, 1, QPointF(pieceWidth, 0)); //0 and 1 are the consecutive indices of the pieces
}
MyPatternConfiguration::MyPatternConfiguration(QObject* parent, const QVariantList& args)
: Palapeli::PatternConfiguration(parent, args)
{
Q_UNUSED(parent)
Q_UNUSED(args)
setSizeDefinitionMode(Palapeli::PatternConfiguration::CustomSizeDefinition);
}
MyPatternConfiguration::~MyPatternConfiguration()
{
}
Palapeli::Pattern* MyPatternConfiguration::createPattern() const
{
return new MyPattern;
}
Integrate into Palapeli: mypattern.desktop
[Desktop Entry]
X-KDE-Library=mypattern
X-KDE-PluginInfo-Author=The best KDE hacker
[email protected]
X-KDE-PluginInfo-Name=mypattern
X-KDE-PluginInfo-Version=1.0
X-KDE-PluginInfo-Website=http://kde-hackers.example.org/palapelipatterns
X-KDE-PluginInfo-Category=
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
X-KDE-ParentApp=libpalapelipattern
X-KDE-ServiceTypes=Palapeli/PatternPlugin
Type=Service
Icon=palapeli-pattern-mypattern
PatternIdentifier=mypattern
Name=My pattern
Name[de]=Mein Schnittmuster
Comment=The best pattern in the world
Comment[de]=Das beste Schnittmuster auf der ganzen Welt
Build everything: CMakeLists.txt
project(mypattern)
find_package(KDE4 REQUIRED)
find_package(Palapeli REQUIRED)
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include_directories(${KDE4_INCLUDES} ${PALAPELI_INCLUDE_DIR})
set(mypattern_SRCS
mypattern.cpp
)
kde4_add_plugin(mypattern ${mypattern_SRCS})
target_link_libraries(mypattern ${KDE4_KDEUI_LIBS} palapelipattern)
install(TARGETS mypattern DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES mypattern.desktop DESTINATION ${SERVICES_INSTALL_DIR})