Mbarcode Plugin Tutorial
m (→Prerequisites) |
|||
(6 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
- | This tutorial will give you a quick introduction to writing plugins for mBarcode. We will try to guide you from your very first steps all the way to a full fledged plugin. | + | This tutorial will give you a quick introduction to writing plugins for [[mBarcode]]. We will try to guide you from your very first steps all the way to a full fledged plugin. |
The plugin we are going to create will show a quick dialog with info about the data contained in the barcode. | The plugin we are going to create will show a quick dialog with info about the data contained in the barcode. | ||
- | = Prerequisites = | + | == Prerequisites == |
In this tutorial we'll be using Qt together with C++ to connect to the mBarcode plugin framework. To make the process very easy we are also using Qt Creator to write our code and Scratchbox to test and compile it. You don't have to use Scratchbox or Qt Creator if you don't want to, but this is not covered by this tutorial. | In this tutorial we'll be using Qt together with C++ to connect to the mBarcode plugin framework. To make the process very easy we are also using Qt Creator to write our code and Scratchbox to test and compile it. You don't have to use Scratchbox or Qt Creator if you don't want to, but this is not covered by this tutorial. | ||
Line 19: | Line 19: | ||
* Qt Creator | * Qt Creator | ||
- | Using MADDE for development is currently a bit of a pain since it lacks an option for third party libraries. | + | Using [[MADDE]] for development is currently a bit of a pain since it lacks an option for third party libraries. There is a tutorial that covers writing these plugins using the Nokia Qt SDK available here [[Mbarcode_Plugin_Tutorial_MADDE]] |
- | = Writing the plugin = | + | == Writing the plugin == |
Using Scratchbox is the easiest way to get started. Log in to Scratchbox by typing | Using Scratchbox is the easiest way to get started. Log in to Scratchbox by typing | ||
- | + | /scratchbox/login | |
- | Install the mbarcode-dev package | + | Install the <code>mbarcode-dev</code> package |
- | + | apt-get install mbarcode-dev | |
- | Next, create a new "Empty Qt project" using Qt Creator and store this in /scratchbox/users/<your username>/home/<your username>/. For the sake of simplicity, we'll name our project "plugin". | + | Next, create a new "Empty Qt project" using Qt Creator and store this in <code>/scratchbox/users/<your username>/home/<your username>/</code>. For the sake of simplicity, we'll name our project "plugin". |
Alternatively, create an empty folder using the terminal and add an empty plugin.pro file. | Alternatively, create an empty folder using the terminal and add an empty plugin.pro file. | ||
- | == The project file == | + | === The project file === |
Add the following to your plugin.pro file: | Add the following to your plugin.pro file: | ||
Line 42: | Line 42: | ||
TEMPLATE = lib | TEMPLATE = lib | ||
CONFIG += plugin | CONFIG += plugin | ||
- | INCLUDEPATH += ../.. | + | INCLUDEPATH += ../.. |
- | HEADERS = plugin.h | + | HEADERS = plugin.h \ |
- | SOURCES = plugin.cpp | + | showdialogaction.h |
- | TARGET = $$qtLibraryTarget( | + | SOURCES = plugin.cpp \ |
+ | showdialogaction.cpp | ||
+ | TARGET = $$qtLibraryTarget(mbarcode_myplugin) | ||
- | unix { | + | unix { |
# VARIABLES | # VARIABLES | ||
isEmpty(PREFIX):PREFIX = /usr | isEmpty(PREFIX):PREFIX = /usr | ||
Line 55: | Line 57: | ||
DEFINES += DATADIR=\"$$DATADIR\" \ | DEFINES += DATADIR=\"$$DATADIR\" \ | ||
PKGDATADIR=\"$$PKGDATADIR\" | PKGDATADIR=\"$$PKGDATADIR\" | ||
- | + | ||
# MAKE INSTALL | # MAKE INSTALL | ||
INSTALLS += target | INSTALLS += target | ||
Line 64: | Line 66: | ||
</pre> | </pre> | ||
- | == The plugin actions == | + | === The plugin actions === |
- | Let's add some actions for our plugin! In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it | + | Let's add some actions for our plugin! In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it <code>ShowDialogAction</code>. |
- | Open the showdialogaction.h file and overwrite it with the following: | + | Open the <code>showdialogaction.h</code> file and overwrite it with the following: |
- | < | + | <source lang="cpp"> |
#ifndef SHOWDIALOGACTION_H | #ifndef SHOWDIALOGACTION_H | ||
#define SHOWDIALOGACTION_H | #define SHOWDIALOGACTION_H | ||
+ | #include <QtCore> | ||
#include <mbarcode-qt/plugininterfaces.h> | #include <mbarcode-qt/plugininterfaces.h> | ||
#include <mbarcode-qt/pluginaction.h> | #include <mbarcode-qt/pluginaction.h> | ||
- | class ShowDialogAction : | + | class ShowDialogAction : public PluginAction |
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
public: | public: | ||
ShowDialogAction(const PluginInterface *interface); | ShowDialogAction(const PluginInterface *interface); | ||
- | + | QString getName() { return "EAN Local Analyser"; } // This name is just for convenience | |
- | QString getName() { return " | + | |
QString getText(); // This is the text that will show in the list | QString getText(); // This is the text that will show in the list | ||
- | |||
void clickAction(QWidget *parentWindow); // The action which is called whenever we are clicked | void clickAction(QWidget *parentWindow); // The action which is called whenever we are clicked | ||
- | + | signals: | |
+ | void isReady(); | ||
private slots: | private slots: | ||
void barcodeAnalysed(QString barcodeType, QString barcodeData); // The slot where all the processing goes down | void barcodeAnalysed(QString barcodeType, QString barcodeData); // The slot where all the processing goes down | ||
- | |||
private: | private: | ||
- | |||
QString barcodeType; | QString barcodeType; | ||
QString barcodeData; | QString barcodeData; | ||
}; | }; | ||
+ | |||
+ | //Q_DECLARE_METATYPE(ShowDialogAction *); | ||
#endif // SHOWDIALOGACTION_H | #endif // SHOWDIALOGACTION_H | ||
- | </ | + | </source> |
- | Open the showdialogaction.cpp file and overwrite it with the following: | + | Open the <code>showdialogaction.cpp</code> file and overwrite it with the following: |
- | < | + | <source lang="cpp"> |
#include "showdialogaction.h" | #include "showdialogaction.h" | ||
Line 110: | Line 112: | ||
{ | { | ||
// Do your initialization here. | // Do your initialization here. | ||
- | |||
- | |||
- | |||
- | |||
} | } | ||
- | |||
QString ShowDialogAction::getText() { | QString ShowDialogAction::getText() { | ||
// This is the text which will show in the list of results. | // This is the text which will show in the list of results. | ||
- | |||
return "Show Dialog"; | return "Show Dialog"; | ||
} | } | ||
- | |||
void ShowDialogAction::clickAction(QWidget* parentWindow) | void ShowDialogAction::clickAction(QWidget* parentWindow) | ||
{ | { | ||
// This is what happens when the user clicks our button. | // This is what happens when the user clicks our button. | ||
- | + | QMessageBox msg(parentWindow); | |
- | + | msg.setWindowTitle("You clicked the button!"); | |
- | + | msg.setText("A window should be a dynamic object. Otherwise it is removed at the end of this function. A message box, on the other hand, has the exec()."); | |
- | + | msg.exec(); | |
- | + | ||
- | + | ||
} | } | ||
- | |||
void ShowDialogAction::barcodeAnalysed(QString barcodeType, QString barcodeData) { | void ShowDialogAction::barcodeAnalysed(QString barcodeType, QString barcodeData) { | ||
- | |||
// In this function you process the data. This could be used for early web lookups or whatever. | // In this function you process the data. This could be used for early web lookups or whatever. | ||
// We just store the data in memory for now. | // We just store the data in memory for now. | ||
- | |||
this->barcodeType = barcodeType; | this->barcodeType = barcodeType; | ||
this->barcodeData = barcodeData; | this->barcodeData = barcodeData; | ||
- | |||
// We've done what we need to and are ready to show ourselves | // We've done what we need to and are ready to show ourselves | ||
- | + | emit isReady(); | |
- | + | ||
} | } | ||
- | </ | + | </source> |
You could add more classes like this if you want different buttons with different actions, but for now, this will do. | You could add more classes like this if you want different buttons with different actions, but for now, this will do. | ||
- | == Plugin interface == | + | === Plugin interface === |
- | Let's connect to mBarcode's plugin interface. In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it | + | Let's connect to mBarcode's plugin interface. In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it <code>Plugin</code>. |
- | Open your plugin.h file and overwrite it with the following: | + | Open your <code>plugin.h</code> file and overwrite it with the following: |
+ | |||
+ | <source lang="cpp"> | ||
+ | // This is a plugin used as an example in different tutorials. | ||
- | |||
#ifndef PLUGIN_H | #ifndef PLUGIN_H | ||
#define PLUGIN_H | #define PLUGIN_H | ||
Line 181: | Line 171: | ||
#endif // PLUGIN_H | #endif // PLUGIN_H | ||
- | </ | + | </source> |
- | Open your plugin.cpp file and add the following: | + | Open your <code>plugin.cpp</code> file and add the following: |
- | < | + | <source lang="cpp"> |
#include "plugin.h" | #include "plugin.h" | ||
#include "showdialogaction.h" | #include "showdialogaction.h" | ||
Line 216: | Line 206: | ||
Q_EXPORT_PLUGIN2(mbqt_myplugin, Plugin) // Please change this name | Q_EXPORT_PLUGIN2(mbqt_myplugin, Plugin) // Please change this name | ||
- | </ | + | </source> |
- | == Testing your fancy new plugin == | + | === Testing your fancy new plugin === |
- | There you go! You are now ready to build your plugin and test it out. Before we build a complete | + | There you go! You are now ready to build your plugin and test it out. Before we build a complete Debian package, let's see if everything works. |
Build your plugin by issuing this command in your project folder in Scratchbox: | Build your plugin by issuing this command in your project folder in Scratchbox: | ||
- | + | ||
- | qmake && make | + | qmake && make |
- | + | ||
- | Go to your directory and pick up the new file named | + | Go to your directory and pick up the new file named <code>libmbarcode_myplugin.so</code>. Copy this to your device and place it in <code>/home/user/.config/mbarcode/plugins</code>. (Note: final plugins are stored in <code>/usr/share/mbarcode/plugins</code>. The former path with .config didn't work for me). |
Start mBarcode and scan a barcode as usual. Your plugin should now show up. | Start mBarcode and scan a barcode as usual. Your plugin should now show up. | ||
- | === Troubleshooting === | + | ==== Troubleshooting ==== |
If your plugin doesn't load, please run mbarcode from terminal and look for error messages during the loading of the application. | If your plugin doesn't load, please run mbarcode from terminal and look for error messages during the loading of the application. | ||
- | = Creating a | + | == Creating a Debian package == |
To be continued... | To be continued... | ||
+ | |||
+ | [[Category:Development]] | ||
+ | [[Category:mBarcode]] |
Latest revision as of 09:58, 14 June 2011
This tutorial will give you a quick introduction to writing plugins for mBarcode. We will try to guide you from your very first steps all the way to a full fledged plugin.
The plugin we are going to create will show a quick dialog with info about the data contained in the barcode.
Contents |
[edit] Prerequisites
In this tutorial we'll be using Qt together with C++ to connect to the mBarcode plugin framework. To make the process very easy we are also using Qt Creator to write our code and Scratchbox to test and compile it. You don't have to use Scratchbox or Qt Creator if you don't want to, but this is not covered by this tutorial.
Heck, you can even get through this without Qt installed, but bare in mind that you are on your own then.
You must have:
- Some basic knowledge about C++ and Qt
- Scratchbox installed
- Qt
- Access to a terminal
- An OS with dpkg
You could use:
- Qt Creator
Using MADDE for development is currently a bit of a pain since it lacks an option for third party libraries. There is a tutorial that covers writing these plugins using the Nokia Qt SDK available here Mbarcode_Plugin_Tutorial_MADDE
[edit] Writing the plugin
Using Scratchbox is the easiest way to get started. Log in to Scratchbox by typing
/scratchbox/login
Install the mbarcode-dev
package
apt-get install mbarcode-dev
Next, create a new "Empty Qt project" using Qt Creator and store this in /scratchbox/users/<your username>/home/<your username>/
. For the sake of simplicity, we'll name our project "plugin".
Alternatively, create an empty folder using the terminal and add an empty plugin.pro file.
[edit] The project file
Add the following to your plugin.pro file:
TEMPLATE = lib CONFIG += plugin INCLUDEPATH += ../.. HEADERS = plugin.h \ showdialogaction.h SOURCES = plugin.cpp \ showdialogaction.cpp TARGET = $$qtLibraryTarget(mbarcode_myplugin) unix { # VARIABLES isEmpty(PREFIX):PREFIX = /usr BINDIR = $$PREFIX/bin DATADIR = $$PREFIX/share DEFINES += DATADIR=\"$$DATADIR\" \ PKGDATADIR=\"$$PKGDATADIR\" # MAKE INSTALL INSTALLS += target target.path = $$DATADIR/mbarcode/plugins } OTHER_FILES += debian/changelog \ debian/control
[edit] The plugin actions
Let's add some actions for our plugin! In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it ShowDialogAction
.
Open the showdialogaction.h
file and overwrite it with the following:
#ifndef SHOWDIALOGACTION_H #define SHOWDIALOGACTION_H #include <QtCore> #include <mbarcode-qt/plugininterfaces.h> #include <mbarcode-qt/pluginaction.h> class ShowDialogAction : public PluginAction { Q_OBJECT public: ShowDialogAction(const PluginInterface *interface); QString getName() { return "EAN Local Analyser"; } // This name is just for convenience QString getText(); // This is the text that will show in the list void clickAction(QWidget *parentWindow); // The action which is called whenever we are clicked signals: void isReady(); private slots: void barcodeAnalysed(QString barcodeType, QString barcodeData); // The slot where all the processing goes down private: QString barcodeType; QString barcodeData; }; //Q_DECLARE_METATYPE(ShowDialogAction *); #endif // SHOWDIALOGACTION_H
Open the showdialogaction.cpp
file and overwrite it with the following:
#include "showdialogaction.h" #include <QMessageBox> ShowDialogAction::ShowDialogAction(const PluginInterface *interface) : PluginAction(interface) { // Do your initialization here. } QString ShowDialogAction::getText() { // This is the text which will show in the list of results. return "Show Dialog"; } void ShowDialogAction::clickAction(QWidget* parentWindow) { // This is what happens when the user clicks our button. QMessageBox msg(parentWindow); msg.setWindowTitle("You clicked the button!"); msg.setText("A window should be a dynamic object. Otherwise it is removed at the end of this function. A message box, on the other hand, has the exec()."); msg.exec(); } void ShowDialogAction::barcodeAnalysed(QString barcodeType, QString barcodeData) { // In this function you process the data. This could be used for early web lookups or whatever. // We just store the data in memory for now. this->barcodeType = barcodeType; this->barcodeData = barcodeData; // We've done what we need to and are ready to show ourselves emit isReady(); }
You could add more classes like this if you want different buttons with different actions, but for now, this will do.
[edit] Plugin interface
Let's connect to mBarcode's plugin interface. In Qt Creator, right click on your project and select "Add New...". Choose "C++ Class" and name it Plugin
.
Open your plugin.h
file and overwrite it with the following:
// This is a plugin used as an example in different tutorials. #ifndef PLUGIN_H #define PLUGIN_H #include <QObject> #include <mbarcode-qt/plugininterfaces.h> #include <mbarcode-qt/pluginaction.h> class Plugin : public QObject, public PluginInterface { Q_OBJECT Q_INTERFACES(PluginInterface) public: void initInterface(QWidget *parent); QSet<PluginAction*> getPluginActions(); // returns a list of actions this plugin provides QString getName() { return "My Plugin"; } // a name of your choosing private: QWidget *parent; QSet<PluginAction*> pluginActions; }; #endif // PLUGIN_H
Open your plugin.cpp
file and add the following:
#include "plugin.h" #include "showdialogaction.h" void Plugin::initInterface(QWidget *parent) { this->parent = parent; // Create an action ShowDialogAction *showDialogAction = new ShowDialogAction(this); // Add our action to the list pluginActions.insert(showDialogAction); // Connect the signal from the MaemoBarcodeWindow to our Action classes. connect(parent, SIGNAL(barcodeAnalysedSignal(QString,QString)), showDialogAction, SLOT(barcodeAnalysed(QString,QString))); // If you like to, you can connect this to whatever you want. // This means that you could even connect it to a slot in this class. // We've only chosen to do it this way for convenience. } QSet<PluginAction*> Plugin::getPluginActions() { return pluginActions; } Q_EXPORT_PLUGIN2(mbqt_myplugin, Plugin) // Please change this name
[edit] Testing your fancy new plugin
There you go! You are now ready to build your plugin and test it out. Before we build a complete Debian package, let's see if everything works.
Build your plugin by issuing this command in your project folder in Scratchbox:
qmake && make
Go to your directory and pick up the new file named libmbarcode_myplugin.so
. Copy this to your device and place it in /home/user/.config/mbarcode/plugins
. (Note: final plugins are stored in /usr/share/mbarcode/plugins
. The former path with .config didn't work for me).
Start mBarcode and scan a barcode as usual. Your plugin should now show up.
[edit] Troubleshooting
If your plugin doesn't load, please run mbarcode from terminal and look for error messages during the loading of the application.
[edit] Creating a Debian package
To be continued...
- This page was last modified on 14 June 2011, at 09:58.
- This page has been accessed 14,493 times.