Step by step example for CubePlugin

The following sections describe the steps that are required to create a plugin. For simplicity, a separate project is created and the generated binary will to be copied to the plugin directory of the given cube installation.

Qt project file

To create a cube plugins, a makefile and source files have to be generated. The makefile can be generated automatically from a Qt project file

First we specify the path to the "cube-config" script of the cube installation. This script delivers correct flags for compiling and linking.

CUBE_CONFIG = /opt/cube/bin/cube-config
INCLUDEPATH += $$system($$CUBE_CONFIG --gui-include-path) $$system($$CUBE_CONFIG --cube-include-path)
LIBS += $$system($$CUBE_CONFIG --gui-ldflags) $$system($$CUBE_CONFIG --cube-ldflags)
DESTDIR = $$system($$CUBE_CONFIG --cube-dir)/plugins
TEMPLATE = lib
CONFIG += plugin
HEADERS = ExampleSimple.h
SOURCES = ExampleSimple.cpp
TARGET = $$qtLibraryTarget(ExamplePluginSimple)

qmake && make will build the first plugin example libExamplePluginSimple.so. The plugin will be copied to the plugin directory, e.g. /opt/cube/lib64/plugins.

SimpleExample.h

The example describes a minimal cube plugin, which is inserted as an additional tab next to the SystemTree. It shows the text of the recently selected tree item. The complete source of the example can be found in $CUBE_INSTALL_PREFIX/share/doc/cube/example/gui/plugin-example.

Every cube plugin has to derive from CubePlugin. To use Qt's signal and slot mechanism it also has to derive from QObject. If the plugin should be added as a tab next to a tree widget, it has to derive from TabInterface.

class SimpleExample : public QObject, public CubePlugin, TabInterface

The class header is followed by the following macro definitions:

  • Q_OBJECT is required to handle signals and slots.

  • Q_INTERFACES( CubePlugin ) tells Qt that the class implements the CubePlugin interface and generates the method qt_metacast(char*) to cast the plugin object to CubePlugin using the class name given as as character array.

  • For Qt versions >= 5.0 the plugin has to be exported using the Q_PLUGIN_METADATA() macro. The unique plugin name "SimpleExamplePlugin" is assigned. For Qt versions < 5.0, Q_EXPORT_PLUGIN2 has be be used (see Section 'SimpleExample.cpp' ).
class SimpleExample : public QObject, public CubePlugin, TabInterface
{
Q_OBJECT
Q_INTERFACES( CubePlugin )
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "SimpleExamplePlugin") // unique plugin name
#endif

The class SimpleExample has to implement all pure virtual methods from CubePlugin and TabInterface.

public:
// CubePlugin implementation
virtual bool cubeOpened( PluginServices* service );
virtual void cubeClosed();
virtual QString name() const;
virtual void version( int& major, int& minor, int& bugfix ) const;
virtual QString getHelpText() const;
// TabInterface implementation
virtual QString label() const;
virtual QWidget* widget();
private slots:
void treeItemIsSelected( TreeType type, TreeItem* item );
private:
QWidget* widget_;
QLabel* qlabel_;
PluginServices* service;
};

SimpleExample.cpp

For Qt versions < 5.0, Q_EXPORT_PLUGIN2 is used to export the plugin. The first argument is a unique name for the plugin, the second the name of the class.

#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2( SimpleExamplePlugin, SimpleExample );
#endif
#include <QVBoxLayout>
#include <QtPlugin>
#include "ExampleSimple.h"
#include "PluginServices.h"

The function cubeOpened(PluginServices* service) is the starting point of our plugin. Here we create the main widget, which should be added as a system tab. As our plugin derives from TabInterface, this is done by service->addTab(SYSTEMTAB, this).

If the user selects a tree item, service will emit a corresponding signal. To react on this event, the signal has to be connected to the slot treeItemIsSelected() of our plugin class.

The function returns true, if the plugin should be started. It it returns false, the plugin is closed and deleted.

The function cubeClosed() is called if the cube file is closed or if the plugin is unloaded by the user. All resources which have been allocated in cubeOpened have to be deleted here.

bool SimpleExample::cubeOpened( PluginServices* service )
{
this->service = service;
// create widget_ and place a label on it
widget_ = new QWidget();
qlabel_ = new QLabel( "example string" );
QVBoxLayout* layout = new QVBoxLayout();
widget_->setLayout( layout );
layout->addWidget( qlabel_ );
service->addTab( SYSTEMTAB, this );
connect(service, SIGNAL( treeItemIsSelected( TreeType, TreeItem * )),
this, SLOT( treeItemIsSelected( TreeType, TreeItem * ) ));
return true; // initialisation is ok => plugin should be shown
}
SimpleExample::cubeClosed()
{
delete widget_;
}

Each plugin has to set a version number. If several plugins with the same identifier (see function name()) exist, the one with the highest version number will be loaded.

void SimpleExample::version( int& major, int& minor, int& bugfix ) const
{
major = 1;
minor = 0;
bugfix = 0;
}

This function returns the unique plugin name. Only one plugin with this name will be loaded.

QString SimpleExample::name() const
{
return "Simple Example Plugin";
}

The following function returns a text to describe the plugin. It will be used by help menu of the cube GUI.

QString SimpleExample::getHelpText() const
{
return "Just a simple example.";
}

The following two functions contain the implementation of TabInterface.

The function widget() returns the QWidget that will be placed into the tab, which has been created with service->addTab in initialize().

QWidget* SimpleExample::widget()
{
return widget_;
}

The function label() returns the label of the new tab.

QString
SimpleExample::label() const
{
return "Example Plugin Label";
}

This method is a slot, which is called if a tree item is selected. The first arguments shows whether the selected item is part of a metric tree, call tree, flat view or system tree. The second argument provides information about the selected item.

void
SimpleExample::treeItemIsSelected( TreeType type, TreeItem* item )
{
QString txt = item->getName() + " " + QString::number( item->getValue() );
qlabel_->setText( txt );
}

Scalasca     Copyright © 1998–2015 Forschungszentrum Jülich, Jülich Supercomputing Centre