kde Plasmoid Applet Development

Posted by Ree on Tue, 17 Sep 2019 11:52:24 +0200

Links to the original text: https://my.oschina.net/fuyajun1983cn/blog/263829

abstract

The plasmoid will contain a text box and buttons.


 

Code

. desktop file

Each plasmoid needs A. desktop file to tell plasma how to start it and its name. The file is as follows:

plasma-applet-myplasmaapplet.desktop


[Desktop Entry]

Name=MyPlasmaApplet

Name[zh_CN]= My Plasma applet

Comment=PlasmaMyPlasmaApplet

Comment[zh_CN]=PlasmaMyPlasmaApplet

Type=Service


X-KDE-ServiceTypes=Plasma/Applet

X-KDE-Library=plasma_applet_myplasmaapplet

X-KDE-PluginInfo-Author=fuyajun

X-KDE-PluginInfo-Email=fuyajun1983cn@gmail.coms

X-KDE-PluginInfo-Name=myplasmaapplet

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


Most importantly, X-KDE_Library and X-KDE-PluginInfo-Name are self-developed "binders" between classes and plasma, without which plasamoid will not start.


header file

myplasmaapplet.h

// Here we avoid loading the header multiple times
#ifndef MYPLASMAAPPLET_HEADER
#define MYPLASMAAPPLET_HEADER

// We need the Plasma Applet headers
#include <Plasma/Applet>

class QSizeF;

namespace Plasma {
  class LineEdit;
  class PushButton;
}

// Define our plasma Applet
class MyPlasmaApplet : public Plasma::Applet
{
    Q_OBJECT
    public:
        // Basic Create/Destroy
        MyPlasmaApplet(QObject *parent, const QVariantList &args);
        ~MyPlasmaApplet();
        void init();

    private:
	Plasma::LineEdit *m_lineEdit;
	Plasma::PushButton *m_pushButton;
};
 
// This is the command that links your applet to the .desktop file
K_EXPORT_PLASMA_APPLET(myplasmaapplet, MyPlasmaApplet)
#endif

voidpaintInterface(QRectF contentsRect)

This can be considered as the main function of drawing plasmoid to the screen. In this function, you can define the plasmoid interface. You should use the rectangular area defined by contentsRect to draw and avoid geometry(). When a plasmoid does not have a standard background, or when it calls setBackgroundHints() to disable the background, or when it is in a panel, geometry() and boundingRect() behave the same; however, when a plasmoid uses a standard background, there is a gap between the applet and the location it should have drawn.


Actual working paper

Here are some implementation functions:

myplasmaapplet.cpp

#include "myplasmaapplet.h"
#include <QPainter>
#include <QSizeF>
#include <QGraphicsLinearLayout>

#include <plasma/theme.h>
#include <plasma/widgets/lineedit.h>
#include <plasma/widgets/pushbutton.h>

MyPlasmaApplet::MyPlasmaApplet(QObject *parent, const QVariantList &args)
        : Plasma::Applet(parent, args),
        m_lineEdit(0),
        m_pushButton(0)
{
    // this will get us the standard applet background, for free!
    setBackgroundHints(DefaultBackground);
    //tell if we need a user interface for configuring the applet
    setHasConfigurationInterface(true);
    resize(200, 200);
}


MyPlasmaApplet::~MyPlasmaApplet()
{
    if (hasFailedToLaunch()) {
        // Do some cleanup here
    } else {
        // Save settings
    }
}

void MyPlasmaApplet::init()
{
    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
    layout->setOrientation(Qt::Vertical); //so widgets will be stacked up/down

    m_lineEdit = new Plasma::LineEdit(this);
    m_lineEdit->setText("Hey! This is a Plasma line edit.");

    m_pushButton = new Plasma::PushButton(this);
    m_pushButton->setText("Whoa! This is a Plasma pushbutton.");

    layout->addItem(m_lineEdit);
    layout->addItem(m_pushButton);
}

#include "myplasmaapplet.moc"

K_EXPORT_PLASMA_APPLET( <name>, <class> )

This is a small and very important part that associates the class name with the name of the small application defined in the. desktop file.

Note: K_EXPORT_PLASMA_APPLET automatically prefixes "plasma_applet_" to the name, so be careful when editing. desktop files.


setBackgroundHints(DefaultBackground)

This is a fast and easy function interface for drawing background. The default Plasma background will be drawn behind the plasmoid. This not only saves time and code, but also creates a more consistent user presentation for users.


Theinit() method

In the constructor, you just tell plasma some information about the background and configuration file (if any). You can also set the initial size at startup in the constructor. After that, plasma will handle arbitrary window scaling, and you don't have to pay attention to its size. In the init() method, you can initialize any data that needs to be initialized, such as reading configuration data.


hasFailedToLaunch()

If, for some reason, the applet fails to start successfully (the library is not found or the necessary hardware support does not exist, etc.), the method returns true. Using this method can provide an opportunity for applications to clean up before exiting.


setFailedToLaunch(bool,QString)

When the application fails to start successfully, this function allows you to notify plasma and give a possible reason. Plasma will draw a standardized error interface to inform the user why the program failed to start, and then your application will not be called to draw itself. If your plasmoid becomes more complex and depends on more factors, this is a good way to clean it up.


dataUpdated(constQString &source, const Plasma::DataEngine::Data &data)

If you want to connect to any plasma's DataEngine, you can implement the dataUpdated method. When a DataEngine connects directly to your Applet subclass, the dataUpdated method is called when the DateEngine sends you updated data.


Determine the size and geometric properties of the applet: geometry() and contentsRect()

If you need to know the size of the small application and some geometric properties in your code, call contentsRect() and contentsRect().size() methods. Avoid calling geometry() and size() methods because they do not take into account the gap in the default background settings. Similarly, avoid using absolute numbers to set locations such as QPoint(0,0) to represent the upper left of the small application, but instead use contentsRect().topLeft().


Construction by CMakeLists.txt

The contents of the CMakeLists.txt file are as follows:

# Project Needs a name ofcourse
project(plasma-myplasmaapplet)
 
# Find the required Libaries
find_package(KDE4 REQUIRED)
include(KDE4Defaults)
 
add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
include_directories(
   ${CMAKE_SOURCE_DIR}
   ${CMAKE_BINARY_DIR}
   ${KDE4_INCLUDES}
   )
 
# We add our source code here
set(myplasmaapplet_SRCS myplasmaapplet.cpp)
 
# Now make sure all files get to the right place
kde4_add_plugin(plasma_applet_myplasmaapplet ${myplasmaapplet_SRCS})
target_link_libraries(plasma_applet_myplasmaapplet 
                      ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS})
 
install(TARGETS plasma_applet_myplasmaapplet
        DESTINATION ${PLUGIN_INSTALL_DIR})
 
install(FILES plasma-applet-myplasmaapplet.desktop
        DESTINATION ${SERVICES_INSTALL_DIR})

Test applet

If your current development environment is different from the test installation, run cmake and add the option - DCMAKE_INSTALL_PREFIX=`kde4-config-prefix'. Then run make and sudo make install, or install manually as follows:

1. Copy plasma_applet_myplasmaapplet.so to $KDEDIR/lib/kde4

2. Copy plasma_applet_myplasmaapplet.desktop to $KDEDIR/share/kde4/services.

Then, run kbuildsycoca4 (so that KDEapps will know the new desktop file). To test the code, you can use the plasmoidviewer program:

kbuildsycoca4 #Needed once to let KDE know there is a new plugin
plasmoidviewer applet_name

You can also watch your Applet on a small desktop:

plasmoidviewer -c desktop applet_name

Among them, applet_name is the key value corresponding to X-KDE-PluginInfo-Name in the. desktop file.

You can also restart plasma so that the new Applet can appear in the Applet browser.

kbuildsycoca4
kquitapp plasma-desktop
plasma-desktop



Reproduced in: https://my.oschina.net/fuyajun1983cn/blog/263829

Topics: Qt cmake sudo