CHAPTER 4. DEVELOPING APPLICATIONS
4.9 PLUG-IN
A. An overview of Plug-in structure
MzLibLoader was created to support plug-in function in MIZI Prizm 2.0.
This section explains not only HOW-TO use the class but also explains HOW-TO
design plug-in.
The Plug-in consists of following three units;
- Body: This is a part which implements functionalities of your plug-in.
- Factory (class MzLibFactory): A plug-in contains multiple bodies which
are controlled by factory. Loader will provide information regarding to factory,
and then factory can create body. You need to create a factory which runs
with plug-in well.
- Loader (class MzLibLoader): Loader conducts two functionalities. It loads
target plug-in file, and then manages loaded plug-in. Loader can't detect
body information therefore it simply manages factory.
B. Plug-in Body Example
Next is a sample code that a body acts like QWidget for displaying time.
#include <stdio.h> #include <qlabel.h> #include <qdatetime.h> #include <mzlibloader.h> class TimeApplet : public QLabel { public: TimeApplet (QWidget *parent); protected: virtual void timerEvent (QTimerEvent *); }; TimeApplet::TimeApplet (QWidget *parent) : QLabel (parent) { timerEvent (NULL); startTimer (30 * 100); } void TimeApplet::timerEvent (QTimerEvent *e) { QTime time = QTime::currentTime (); QString text; text.sprintf ("%d:%02d", time.hour (), time.minute ()); setText (text); }
|
C. Plug-in Factory Example
Following sample was written to make plug-in. TimeApplet in the sample can be
worked as an actual plug-in.
class AppletFactory : public MzLibFactory { public: AppletFactory (); virtual QString description () const; virtual QString version () const; protected: virtual QObject* createObject (QObject* parent, const char* name, const char* classname, const QStringList &args); }; AppletFactory::AppletFactory () { // Constructor } QString AppletFactory::description () const { return QString ("Time Applet"); } QString AppletFactory::version () const { return QString ("1.0"); } QObject* AppletFactory::createObject (QObject* parent, const char* name, const char* classname, const QStringList &) { if (!strcmp (classname, "QWidget")) { if (parent && !parent->isWidgetType ()) return 0; return new TimeApplet ((QWidget *) parent); } return 0; } // MACRO for plug-in registration MZ_EXPORT_FACTORY (AppletFactory);
|
D. Plug-in Compile
Create a file, "time.cpp", which combines Body and Factory.
Create Makefile.am as follow, then compiles.
CXXLD = @CC@ INCLUDES = -I.. @LINUETTE_COMMON_INCLUDE@ -Wall interfaces_LTLIBRARIES = \ libtime.la interfacesdir = @LINUETTE_PLUG-IN_DIR@ libtime_la_SOURCES = \ time.cpp libtime_la_LDFLAGS = \ -export-dynamic \ -avoid-version
|
E. Load Plug-in
Now, let's load the above plug-in.
#include <mzlibloader.h> class Plug-inViewer : public QWidget { public: Plug-inViewer (); }; void Plug-inViewer::Plug-inViewer () { // Load "libtime.so" file. If fail then return NULL. MzLibFactory *factory = MzLibLoader::instance ()->load (¡°libtime.so¡±); if (factory) { // plug-in loaded // display about the factory qDebug ("Factory Info: %s (version %s)" , factory->description ().latin1 (), factory->version ().latin1 ()); // Create a widget through plug-in factory. QWidget *w = (QWidget *) factory->create (this, ¡°time¡±, ¡°QWidget¡±); w->show (); } }
|
See function factory->create () original code.
QObject * MzLibFactory::create (QObject *parent = 0, const char *name = 0, const char *classname = "QObject", const QstringList &args = QstringList ());
|
|