这几天开始用KDE的桌面,突发奇想想自己做Plasma部件,岂不很个性。于是找到Plasma的开发者wiki
http://techbase.kde.org/Projects/Plasma
发现有个《入门的教程 》,尝试来N遍,终于可以将自定义的部件显示在桌面了。记下来,呵呵。
1、建立一个plasma-tutorial1 文件夹
mkdir plasma-tutorial1
2、建立plasma-applet-tutorial1.desktop
[ Desktop Entry]
Name = Tutorial 1
Comment = Plasma Tutorial 1
Type = Service
ServiceTypes = Plasma/Applet
X-KDE-Library = plasma_applet_tutorial1
X-KDE-PluginInfo-Author = Bas Grolleman
X-KDE-PluginInfo-Email = bgrolleman@emendo-it.nl
X-KDE-PluginInfo-Name = tutorial1
X-KDE-PluginInfo-Version = 0.1
X-KDE-PluginInfo-Website = http://plasma.kde.org/
X-KDE-PluginInfo-Category = Examples
X-KDE-PluginInfo-Depends =
X-KDE-PluginInfo-License = GPL
X-KDE-PluginInfo-EnabledByDefault = true
3、建立plasma-tutorial1.h
// Here we avoid loading the header multiple times
#ifndef Tutorial1_HEADER
#define Tutorial1_HEADER
// We need the Plasma Applet headers
#include <KIcon>
#include <Plasma/Applet>
#include <Plasma/Svg>
class QSizeF;
// Define our plasma Applet
class PlasmaTutorial1 : public Plasma:: Applet
{
Q_OBJECT
public :
// Basic Create/Destroy
PlasmaTutorial1( QObject * parent, const QVariantList & args) ;
~PlasmaTutorial1( ) ;
// The paintInterface procedure paints the applet to screen
void paintInterface( QPainter * painter,
const QStyleOptionGraphicsItem * option,
const QRect & contentsRect) ;
void init( ) ;
private :
Plasma:: Svg m_svg;
KIcon m_icon;
} ;
#endif
4、建立plasma-tutorial1.cpp
#include "plasma-tutorial1.h"
#include <QPainter>
#include <QFontMetrics>
#include <QSizeF>
#include <plasma/svg.h>
#include <plasma/theme.h>
// This is the command that links your applet to the .desktop file
K_EXPORT_PLASMA_APPLET( tutorial1, PlasmaTutorial1)
PlasmaTutorial1:: PlasmaTutorial1 ( QObject * parent, const QVariantList & args)
: Plasma:: Applet ( parent, args) ,
m_svg( this ) ,
m_icon( "document" )
{
m_svg.setImagePath ( "widgets/background" ) ;
// this will get us the standard applet background, for free!
setBackgroundHints( DefaultBackground) ;
resize( 200, 200) ;
}
PlasmaTutorial1:: ~PlasmaTutorial1 ( )
{
if ( hasFailedToLaunch( ) ) {
// Do some cleanup here
} else {
// Save settings
}
}
void PlasmaTutorial1:: init ( )
{
// A small demonstration of the setFailedToLaunch function
if ( m_icon.isNull ( ) ) {
setFailedToLaunch( true , "No world to say hello" ) ;
}
}
void PlasmaTutorial1:: paintInterface ( QPainter * p,
const QStyleOptionGraphicsItem * option, const QRect & contentsRect)
{
p-> setRenderHint ( QPainter :: SmoothPixmapTransform ) ;
p-> setRenderHint ( QPainter :: Antialiasing ) ;
// Now we draw the applet, starting with our svg
m_svg.resize ( ( int ) contentsRect.width ( ) , ( int ) contentsRect.height ( ) ) ;
m_svg.paint ( p, ( int ) contentsRect.left ( ) , ( int ) contentsRect.top ( ) ) ;
// We place the icon and text
p-> drawPixmap ( 7, 0, m_icon.pixmap ( ( int ) contentsRect.width ( ) , ( int ) contentsRect.width ( ) - 14) ) ;
p-> save ( ) ;
p-> setPen ( Qt :: white ) ;
p-> drawText ( contentsRect,
Qt :: AlignBottom | Qt :: AlignHCenter ,
"Hello Plasmoid!" ) ;
p-> restore ( ) ;
}
5、建立CMakeLists.txt
# Project Needs a name ofcourse
project( plasma-tutorial1)
# 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 ( tutorial1_SRCS plasma-tutorial1.cpp)
# Now make sure all files get to the right place
kde4_add_plugin( plasma_applet_tutorial1 ${tutorial1_SRCS} )
target_link_libraries( plasma_applet_tutorial1
${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS} )
install ( TARGETS plasma_applet_tutorial1
DESTINATION ${PLUGIN_INSTALL_DIR} )
install ( FILES plasma-applet-tutorial1.desktop
DESTINATION ${SERVICES_INSTALL_DIR} )
6、创建Makefile文件
cmake -DCMAKE_INSTALL_PREFIX=`kde-config --prefix` ./
7、构建部件
make
8、安装自定义部件
make install
9、添加自定义部件到桌面
10、卸载自定义部件
make unstall