Make KDE applets with KDevelop

In previous tutorial I gave a short introduction how you create an KDE applet. Of course I explained how you do it manually. Because you develop Java programs with Eclipse or Netbeans, it’s obviously to have some sort of IDE to create KDE applets. I suggest you use KDevelop, it’s a powerfull IDE for C++ coding. Okej let’s start creating an small KDE applet.

Installation

You have to install two things to start with KDevelop:

  • KDevelop
  • kde4-kapptemplate
  • G++ (compiler)
  • CMake

With a package manager this job should be easy.

Let’s start!

Open KDevelop. In the menu “Project” select “New from template”. Then you get a window like this.

Choose “Plasma Applate Template” from the category “C++ | KDE”. Give it a name and a location. Press “Next”. If you want a CVS system managing your files, you should select one. Press “Finish”.
It will ask where the “cmake” executable is located, normally it’s in /usr/bin/cmake. When you build your code, it needs a build directory where it can dump all make files, the executable and some log files. The other two fields are optional, so I leave them blank.

Under the “Projects” window you find your project. Is the “Projects” window not visible, activate it by clicking on the “Window” menu and then select “Tool views | Projects”. Under the project you find a lot of files like README, Messages.sh, … There are four important files for you KDE widget:

  • CMakeLists.txt: the make file to make the binairy
  • plasma-applet-tutorial.desktop: the widget desktop file
  • tutorial.cpp: source code
  • tutorial.h: header file

If you open “tutorial.cpp” you find some C++ code. This is you main file for your widget. You can see this because it extends from the class “Plasma::Applet”. In the CMakeLists.txt you find what files you need to compile, what libraries the code depends on, what it requires. If you get an build error and your code is fine, it’s because somethings wrong in this file. The desktop file is needed to find you widget and to put the widget on your desktop. It contains various information of the widget (name, author, category, …).

Build and install

Now we’re going to build the project. If you press F8 or right click on the project and choose “Build”, it starts building. It create some files in the “build” sub-directory of your project. When you don’t have red text in your build console and only green (you get a warning about an unused parameter option, ignore it), your build is successful.

In KDevelop you can “Install” the project by right click on the project and choose “Install”. In my case it fails because you need to be root. So the installation part should be done manually. Just execute the follow shell lines:

cd /path/project
cd build/
sudo make install

When you get the output similar on the screenshot, you’re installation succeeded.

Run your applet

Keep your console open and type the follow command:

plasmoidviewer -c desktop tutorial

Congratz, you created your first KDE widget trough KDevelop.

KDE applets maken: introductie

Voor het vak Small ICT Projects moesten we een project zoeken die bijdraagt aan de mensheid en het moest opensource zijn en het moest jou iets bijleren. Na lang denken kwam ik uit op een KDE applet maken. Dat zijn van die dingen die je op desktop plaatst zoals een kalender, uurwerk, cpu informatie, … Dus ik maak een applet die de uurroosters weergeeft van de KHL. Nu dit helemaal terzijde want ik ga in deze post uitleggen hoe je begint met het bouwen van zo’n applet. Dit schrijf ik omdat ik zelf  wat gesukkeld heb de laatste twee dagen. Het is gebasseerd op de KDE tutorial. Let’s go!

Eerst en vooral maak een map waarin je je applet bestanden gaat plaatsen. Ik plaats mijn bestanden in ~/projects/tutorial1, waar tutorial1 de naam is van mijn applet.

Nu gaan we 4 bestanden maken. Je vind ze hieronder en als je op de naam klikt, ga je naar de broncode. Stop die gewoon in je project map. Wat de code allemaal betekend ga ik niet uitleggen, ik stel voor dat je de KDE tutorial hiervoor raadpleegt. Ik kan zeggen dat de .desktop file belangrijk is voor de naam en de omschrijving van de applet. Ook de regel “K_EXPORT_PLASMA_APPLET” in het .cpp bestand is belangrijk omdat die een link maakt tussen je code en je .desktop file. De CmakeLists.txt is een bestand dat het commando cmake gebruikt (zie verder).

plasma-applet-tutorial1.desktop
plasma-tutorial1.h
plasma-tutorial1.cpp
CMakeLists.txt

Nu we onze applet geschreven hebben, gaan we hem builden. Builden is twee commando’s werk. We gaan eerst een console openen en we gaan met cd naar onze project map (~/projects/tutorial1). Daarin typen we het volgende commando:

cmake -DCMAKE_INSTALL_PREFIX=`kde4-config –prefix`

Hij gaat nu een heleboel output geven en wat files in je project map plaatsen. Even een rand opmerking over die -DCMAKE_INSTALL_PREFIX. Er wordt een commando uitgevoerd, namelijk kde4-config –prefix. Dit commando zegt waar je kde4 geinstalleerd staat. Kijk eerst eens wat voor output dit commando geeft, is dit iets als /usr dan is dit goed. Geeft hij niets, probeer dan eens met kde-config –prefix. Werkt dit ook niet, ga dan de folders share/kde4/services en  lib/kde4 zoeken.

ls -R / 2> /dev/null | grep “share/kde4/services”
ls -R / 2> /dev/null | grep “lib/kde4”

Je krijgt dan output als /usr/share/kde4/services, de /usr is je prefix voor cmake.

Genoeg over die cmake en zijn prefix, normaal werkt dit zonder problemen. Nu we cmake uitgevoerd hebben, gaan we het commando make uitvoeren.

make

Dit gaat een shared object (.so) versie van onze tutorial1.cpp plaatsen in lib/ (plasma_applet_tutorial1.so). Als je dat hebt, dan mag je het gaan installeren.

Het installeren is makkelijk. Je kopieert die .so naar /usr/lib/kde4 en je kopieert de .desktop naar /usr/share/kde4/services. De /usr is de kde4 prefix, zie hogerop als die niet /usr is.

sudo cp lib/plasma_applet_tutorial1.so /usr/lib/kde4/
sudo cp plasma-applet-tutorial1.desktop /usr/share/kde4/services/

Nu is het geinstalleerd en kunnen we het runnen. Dankzij plasmoidviewer kunnen we hem in preview zien.

plasmoidviewer -c desktop tutorial1

tutorial1 is de naam van je applet. Je applet staat ook in de lijst van applets wanneer je uitlogt en terug inlogt in KDE, of gewoon eens je plasma herstarten.

Vragen, suggesties en opmerkingen mogen altijd geplaatst worden in de reacties.

ps: ik gebruik opensuse 11.3