摘要
该plasmoid将包含一个文本框和按钮。
代码
.desktop文件
每个plasmoid都需要一个.desktop文件告诉plasma怎样启动它以及它的名字,文件内容如下:
plasma-applet-myplasmaapplet.desktop
[Desktop Entry] Name=MyPlasmaApplet Name[zh_CN]=我的Plasma小应用程序 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 |
其中,最重要的是X-KDE_Library和X-KDE-PluginInfo-Name,它们是自己开发为类与plasma之间的“粘合剂”,没有它,plasamoid将不能启动。
头文件
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)
这可认为是将plasmoid画到屏幕上的主函数。在该函数中,你可以定义plasmoid的界面。你应该使用在contentsRect定义的矩形区域内进行绘制,避免使用geometry()。当一个plasmoid没有标准的背景时,或当它调用setBackgroundHints()禁用背景时,或当它在一个面板中时,geometry()和boundingRect()行为一致;然而,当plasmoid使用标准背景时,小应用程序(applet)将与它本应画的位置之间有一个空隙。
实际工作文件
下面是一些实现函数体:
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> )
这是一个小而非常重要的部分,它将类名与.desktop文件中定义的小应用程序的名字联系在一起。
注:K_EXPORT_PLASMA_APPLET自动添加“plasma_applet_”前缀到名字上,所以在编辑.desktop文件时需要小心。
setBackgroundHints(DefaultBackground)
这是一个快速且更容易的绘制背景的函数接口,默认的Plasma背景将绘制在plasmoid的后面。这不但节省时间和代码,而且为用户创建了更一致的用户呈现。
Theinit() method
在构造函数中,你仅仅告诉plasma关于背景以及配置文件的一些信息(如果有的话)。也可以在构造函数中设置启动时的初始大小。之后,plasma将会处理任意的窗口缩放,你无需关注其大小。在init()方法中,你可以初始化任何需要初始化的数据,如读取配置数据。
hasFailedToLaunch()
如果由于某些原因,小应用程序未能成功启动(库未找到或是必要的硬件支持不存在等等),该方法会返回true。使用该方法可以为应用程序在退出之前提供清理的一个机会。
setFailedToLaunch(bool,QString)
当应用程序不能成功启动时,该函数会允许你通知plasma并给出一个可能的原因。plasma会绘制一个标准化的错误界面通知用户程序启动失败的原因,之后,你的应用程序也不会被调用去绘制自己。如果你的plasmoid变得更复杂,并且依赖更多因素,那么这是这好的清理办法。
dataUpdated(constQString &source, const Plasma::DataEngine::Data &data)
如果你想连接到任何一个plasma的DataEngine上,可以实现dataUpdated方法。当一个DataEngine直接连接到你的Applet子类时,当该DateEngine向你发送更新的数据时,dataUpdated方法会被调用。
决定applet的大小和几何属性:geometry() and contentsRect()
如果在你的代码中需要知道小应用程序的大小以及一些几何属性,调用contentsRect()和contentsRect().size()方法。避免调用geometry()和size()方法,因为它们没有将默认背景设置的空隙考虑进来。同样,避免使用绝对数字来设置位置如QPoint(0,0)以表示小应用程序的左上方,而是应当使用contentsRect().topLeft()。
通过CMakeLists.txt构建
CMakeLists.txt文件内容如下:
# 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})
测试applet
如果你当前的开发环境不同于测试安装,运行cmake,并添加选项-DCMAKE_INSTALL_PREFIX=`kde4-config–prefix`。然后运行make以及sudo makeinstall,或者通过如下方式手动安装:
1.复制plasma_applet_myplasmaapplet.so到$KDEDIR/lib/kde4
2.复制plasma_applet_myplasmaapplet.desktop到$KDEDIR/share/kde4/services。
然后,运行kbuildsycoca4(这样,KDEapps就会知道新的desktop文件)。为了测试代码,可以使用plasmoidviewer程序:
kbuildsycoca4 #Needed once to let KDE know there is a new plugin plasmoidviewer applet_name
你也可以在一个小桌面上观察你的Applet:
plasmoidviewer -c desktop applet_name
其中, applet_name就是.desktop文件中X-KDE-PluginInfo-Name对应的键值。
也可以重启plasma,这样新建的Applet可以在Applet浏览器中出现。
kbuildsycoca4 kquitapp plasma-desktop plasma-desktop