Before we get coding we need to set up some files for our wallpaper. Create a directory called plasma-wallpaper-tutorial. Inside it create a file called plasma-wallpaper-tutorial.desktop. A .desktop file contains information about the wallpaper such as its name, author, etc. Copy the following into the file:

Before we get coding we need to set up some files for our wallpaper. Create a directory called plasma-wallpaper-tutorial. Inside it create a file called plasma-wallpaper-tutorial.desktop. A .desktop file contains information about the wallpaper such as its name, author, etc. Copy the following into the file:

Revision as of 19:50, 29 June 2011

Contents

Plama Wallpaper Tutorial 1 - Hello World

Introduction

The plasma desktop allows different kinds of plugins to be used as wallpapers. Unlike normal wallpapers, these wallpapers can be configurable and also be animated.

This tutorial will show you how to make a basic plasma wallpaper that draws a message to the screen.

To compile the wallpaper you will need to have the kdebase workspace development files installed on your system.

On ubuntu/debian systems you can type in the terminal:

sudoapt-get install kdebase-workspace-dev

openSUSE:

sudo zypper install kdebase4-workspace-devel

This tutorial assumes that you are using KDE 4.3 or newer. Some parts of this tutorial may not work with older versions.

Setting Up

Before we get coding we need to set up some files for our wallpaper. Create a directory called plasma-wallpaper-tutorial. Inside it create a file called plasma-wallpaper-tutorial.desktop. A .desktop file contains information about the wallpaper such as its name, author, etc. Copy the following into the file:

Every plasma wallpaper has to inherit from Plasma::Wallpaper. We have to add a Q_OBJECT since it inherits a QObject. The constructor will be used to intialize our wallpaper. The paint function is where we will draw our wallpaper. Finally the K_EXPORT_PLASMA_WALLPAPER macro tells us that the class 'Tutorial' will make a wallpaper plugin called 'tutorial'.

Now make another file called tutorial.cpp. Put the following code inside it:

We leave the constructor blank for now and just pass the arguments to the Plasma::Walllpaper.

The paint function is where we draw our wallpaper. It has two arguments: the first is a QPainter which to draw with, and the second is a QRectF telling us which part of the wallpaper needs to be repainted. For now we will ignore exposedRect.

In the paint function we first draw a white rectangle that covers the whole wallpaper. The boundingRect() function returns the geometry of the wallpaper as a QRectF.

Next we draw some big, blue text in the middle of the screen.

Finally at the end of the file we include the tutorial.moc file that gets generated from our tutorial.h.

Compiling

We will compile our wallpaper inside a directory called build. Type the following inside the terminal:

The cmake command will generate Makefiles by using the information in our CMakeLists.txt file. We set the install directory to the output of `kde4-config --prefix` which is usually something like /usr.

If everything went well then we should be able to view it.

Running the Wallpaper

To see our wallpaper we can use a tool called plasmawallpaperviwer. To run our wallpaper type the following inside the terminal:

plasmawallpaperviewer -p tutorial

Alternatively you can run the wallpaper inside your actual desktop. You may need to restart plasma first by logging out or running the following command:

kquitapp plasma-desktop; sleep1; plasma-desktop

Now right click on your desktop, go to Desktop Settings>Wallpaper>Type and select 'Tutorial'.

Congratulations! You have just made your very first plamsa wallpaper!

You can try changing the code inside the paint function to whatever you want. The QPainter class has many functions that allow you to draw draw images, text and shapes.

In the next tutorial we will add some configuration options to our wallpaper.