Development/Tutorials/Plasma4/GettingStarted: Difference between revisions
Bgrolleman (talk | contribs) |
Bgrolleman (talk | contribs) m (→The actual work file: - updating the code) |
||
Line 89: | Line 89: | ||
'''plasma-tutorial1.cpp''' | '''plasma-tutorial1.cpp''' | ||
<code cppqt> | <code cppqt> | ||
#include "plasma-tutorial1.h" | #include "plasma-tutorial1.h" | ||
#include <QPainter> | #include <QPainter> | ||
#include <QFontMetrics> | #include <QFontMetrics> | ||
#include <KIcon> | #include <KIcon> | ||
#include <Plasma/Svg> | |||
PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QStringList &args) | PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QStringList &args) | ||
: Plasma::Applet(parent, args) | |||
{ | { | ||
} | } | ||
Line 106: | Line 106: | ||
QRectF PlasmaTutorial1::boundingRect() const | QRectF PlasmaTutorial1::boundingRect() const | ||
{ | { | ||
// In this tutorial we use a fixed size | |||
return QRectF(0,0,128,128); | |||
} | } | ||
void PlasmaTutorial1::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget) | void PlasmaTutorial1::paintInterface(QPainter *p, | ||
const QStyleOptionGraphicsItem *option, QWidget *widget) | |||
{ | { | ||
// First we create the objects to hold the icon and background image | |||
KIcon icon("document"); | |||
Plasma::Svg* background = new Plasma::Svg("widgets/background",this); | |||
// Tell the Plasma/Svg object to use the whole image, | |||
// instead of parts of it. | |||
background->setContentType(Plasma::Svg::SingleImage); | |||
// Now we draw the applet, starting with the background | |||
background->resize(boundingRect().width(),boundingRect().height()); | |||
background->paint(p,boundingRect().left(),boundingRect().top()); | |||
// The we place the icon and text | |||
p->drawPixmap(20, 0, icon.pixmap(boundingRect().width()-40)); | |||
p->save(); | |||
p->setPen(Qt::white); | |||
p->drawText(boundingRect(),Qt::AlignBottom+Qt::AlignHCenter, | |||
"Hello Plasmoid!"); | |||
p->restore(); | |||
} | } | ||
#include "plasma-tutorial1.moc" | #include "plasma-tutorial1.moc" | ||
</code> | </code> |
Revision as of 05:15, 8 July 2007
Tutorial Series | Plasma Tutorial |
Previous | C++, Qt, KDE4 development environment |
What's Next | |
Further Reading | CMake |
Abstract
Where going to start creating a simple plasmoid in this tutorial, to keep things simple we will only create a static plasmoid, it will contain the following items.
- SVG Background
- Icon
- Some nice text
The Code
The .desktop file
Every Plasmoid needs a .desktop file to tell plasma how they should be started, and what name they carry.
plasma-applet-tutorial1.desktop
[Desktop Entry]
Encoding=UTF-8
Name=Tutorial 1
Comment=Plasma Tutorial 1
Type=Service
ServiceTypes=Plasma/Applet
X-KDE-Library=plasma_applet_tutorial1
X-KDE-PluginInfo-Author=Bas Grolleman
[email protected]
X-KDE-PluginInfo-Name=plasma_applet_tutorial1
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true
The most important bits are the X-KDE-Library and X-KDE-PluginInfo-Name, they are the paste between your class and plasma, without it, nothing will start.
The header file
This is the example header file, I will add lot's of comment's in the code to explain everything.
plasma-tutorial1.h
// Here we avoid loading the header multiple times
- ifndef Tutorial1_HEADER
- define Tutorial1_HEADER
// We need the Plasma Applet headers
- include <Plasma/Applet>
// Define our plasma Applet
class PlasmaTutorial1 : public Plasma::Applet
{
Q_OBJECT
public:
// Basic Create/Destroy
PlasmaTutorial1(QObject *parent, const QStringList &args);
~PlasmaTutorial1();
// This function returns the size of your applet
QRectF boundingRect() const;
// The paintInterface procedure paints the applet to screen
void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget =0);
};
// This is the command that links your applet to the .desktop file
K_EXPORT_PLASMA_APPLET(tutorial1, PlasmaTutorial1)
- endif
The actual work file
Here is the body of the function, again with a lot of comments in between.
plasma-tutorial1.cpp
- include "plasma-tutorial1.h"
- include <QPainter>
- include <QFontMetrics>
- include <KIcon>
- include <Plasma/Svg>
PlasmaTutorial1::PlasmaTutorial1(QObject *parent, const QStringList &args)
: Plasma::Applet(parent, args)
{
}
PlasmaTutorial1::~PlasmaTutorial1()
{
}
QRectF PlasmaTutorial1::boundingRect() const
{
// In this tutorial we use a fixed size
return QRectF(0,0,128,128);
}
void PlasmaTutorial1::paintInterface(QPainter *p,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// First we create the objects to hold the icon and background image
KIcon icon("document");
Plasma::Svg* background = new Plasma::Svg("widgets/background",this);
// Tell the Plasma/Svg object to use the whole image,
// instead of parts of it.
background->setContentType(Plasma::Svg::SingleImage);
// Now we draw the applet, starting with the background
background->resize(boundingRect().width(),boundingRect().height());
background->paint(p,boundingRect().left(),boundingRect().top());
// The we place the icon and text
p->drawPixmap(20, 0, icon.pixmap(boundingRect().width()-40));
p->save();
p->setPen(Qt::white);
p->drawText(boundingRect(),Qt::AlignBottom+Qt::AlignHCenter,
"Hello Plasmoid!");
p->restore();
}
- include "plasma-tutorial1.moc"