Editing Mbarcode Plugin Tutorial
Warning: You are not logged in.
Your IP address will be recorded in this page's edit history.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
- | This tutorial will give you a quick introduction to writing plugins for | + | 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 = | |
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 | + | Using MADDE for development is currently a bit of a pain since it lacks an option for third party libraries. This might be unnecessary for plugins, but MADDE is in any case not covered by this tutorial as of yet. |
- | + | = 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 | ||
- | + | <pre>/scratchbox/login</pre> | |
- | Install the | + | Install the mbarcode-dev package |
- | + | <pre>apt-get install mbarcode-dev</pre> | |
- | Next, create a new "Empty Qt project" using Qt Creator and store this in | + | 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. | Alternatively, create an empty folder using the terminal and add an empty plugin.pro file. | ||
- | + | == The project file == | |
Add the following to your plugin.pro file: | Add the following to your plugin.pro file: | ||
Line 66: | Line 66: | ||
</pre> | </pre> | ||
- | + | == 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 "ShowDialogAction". |
- | Open the | + | Open the showdialogaction.h file and overwrite it with the following: |
- | < | + | <pre> |
#ifndef SHOWDIALOGACTION_H | #ifndef SHOWDIALOGACTION_H | ||
#define SHOWDIALOGACTION_H | #define SHOWDIALOGACTION_H | ||
- | |||
#include <mbarcode-qt/plugininterfaces.h> | #include <mbarcode-qt/plugininterfaces.h> | ||
#include <mbarcode-qt/pluginaction.h> | #include <mbarcode-qt/pluginaction.h> | ||
- | class ShowDialogAction : public PluginAction | + | class ShowDialogAction : public QObject, public PluginAction |
{ | { | ||
Q_OBJECT | Q_OBJECT | ||
public: | public: | ||
ShowDialogAction(const PluginInterface *interface); | ShowDialogAction(const PluginInterface *interface); | ||
- | QString getName() { return " | + | |
+ | QString getName() { return "My Plugin Action"; } // This name is just for convenience | ||
QString getText(); // This is the text that will show in the list | QString getText(); // This is the text that will show in the list | ||
+ | bool isReady() { return ready; } // Tells the list wether this plugin is ready to be shown or not | ||
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 | ||
- | + | ||
- | + | ||
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: | ||
+ | bool ready; | ||
QString barcodeType; | QString barcodeType; | ||
QString barcodeData; | QString barcodeData; | ||
}; | }; | ||
- | |||
- | |||
#endif // SHOWDIALOGACTION_H | #endif // SHOWDIALOGACTION_H | ||
- | </ | + | </pre> |
- | Open the | + | Open the showdialogaction.cpp file and overwrite it with the following: |
- | < | + | <pre> |
#include "showdialogaction.h" | #include "showdialogaction.h" | ||
Line 112: | Line 112: | ||
{ | { | ||
// Do your initialization here. | // Do your initialization here. | ||
+ | // Remember to set ready to false. | ||
+ | // This keeps the plugin from appearing in the list before it is ready | ||
+ | |||
+ | ready = false; | ||
} | } | ||
+ | |||
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 | + | // In this example we only show a message box |
- | + | ||
- | + | QMessageBox *msgBox = new QMessageBox(parentWindow); | |
- | + | msgBox->setWindowTitle("My Plugin"); | |
+ | msgBox->setText("The barcode was of type " + barcodeType + " and contained the data " + barcodeData); | ||
+ | msgBox->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 | ||
- | + | ||
+ | ready = true; | ||
} | } | ||
- | </ | + | </pre> |
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 == | |
- | 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 "Plugin". |
- | Open your | + | Open your plugin.h file and overwrite it with the following: |
- | + | ||
- | + | ||
- | + | ||
+ | <pre> | ||
#ifndef PLUGIN_H | #ifndef PLUGIN_H | ||
#define PLUGIN_H | #define PLUGIN_H | ||
Line 171: | Line 183: | ||
#endif // PLUGIN_H | #endif // PLUGIN_H | ||
- | </ | + | </pre> |
- | Open your | + | Open your plugin.cpp file and add the following: |
- | < | + | <pre> |
#include "plugin.h" | #include "plugin.h" | ||
#include "showdialogaction.h" | #include "showdialogaction.h" | ||
Line 206: | Line 218: | ||
Q_EXPORT_PLUGIN2(mbqt_myplugin, Plugin) // Please change this name | Q_EXPORT_PLUGIN2(mbqt_myplugin, Plugin) // Please change this name | ||
- | </ | + | </pre> |
- | + | == 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: | ||
- | + | <pre> | |
- | + | qmake && make | |
- | + | </pre> | |
- | Go to your directory and pick up the new file named | + | 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. |
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 === | |
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 debian package = | |
To be continued... | To be continued... | ||
- | [[Category: | + | [[Category:Software]] |
[[Category:mBarcode]] | [[Category:mBarcode]] |
Learn more about Contributing to the wiki.