LibreCAD Plugin Development

From LibreCAD wiki
Jump to: navigation, search

Introduction.

You can personalize / extend LibreCAD writing your own plugin. The requisites are:

  • Qt SDK
  • librecad/src/plugins/document_interface.h
  • librecad/src/plugins/qc_plugininterface.h

The LibreCAD header are from your LibreCAD version because plugin support are in development state and can change into versions.

To use the plugin install the so or dll file in ~/.librecad/plugins

LibreCAD Plugin Development.

To create a plugin for librecad it require at least three files:

myplugin.pro

myplugin.h

myplugin.cpp


myplugin.pro

The first is a qmake project file (myplugin.pro) contain at least the following:

    TEMPLATE = lib   
    CONFIG += plugin   
    INCLUDEPATH    += ../librecad/src/plugins   
    TARGET=myplugin   
    VERSION = 1.0.0   
    SOURCES += myplugin.cpp   
    HEADERS += myplugin.h   

TEMPLATE: Specifies that will be build a library

CONFIG: Add the configuration necessary to indicate that the library is a plugin

INCLUDEPATH: Sets the search path for LibreCAD header files (document_interface.h and qc_plugininterface.h)

TARGET: the name of the plugin, qmake adds the correct extension for the platform (. dll or. so)

SOURCES and HEADERS: list of source files

For more information consult the documentation of qmake.

myplugin.h

In the header file must declare a class that inherits from QObject and QC_PluginInterface and implement virtual functions of the QC_PluginInterface base class. This class is used by LibreCAD for charge, communicate and execute the plugin.

   #include "qc_plugininterface.h"
 
   class LC_myPlugin : public QObject, QC_PluginInterface {
       Q_OBJECT
       Q_INTERFACES(QC_PluginInterface)
 
    public:
       virtual QString name() const;
       virtual PluginCapabilities getCapabilities() const;
       virtual void execComm(Document_Interface *doc, QWidget *parent, QString cmd);
   };

The Q_INTERFACES and Q_OBJECT macros are mandatory for the Qt meta-object system.

In this class you can declare its own functions and properties.

myplugin.cpp

Finally, in the source file functions are implemented:

   #include <QMessageBox>
   #include "document_interface.h"
   #include "myplugin.h"
   
   QString LC_myPlugin::name() const {
        return (tr("My first plugin"));
   }
   QString LC_myPlugin::menu() const {
        return ("Help");
   }
   void LC_myPlugin::execComm(Document_Interface *doc, QWidget *parent) {
       Q_UNUSED(doc);
       QMessageBox::information ( parent, "LibreCAD rules", "This is my first plugin");
   }
   Q_EXPORT_PLUGIN2(myplugin, LC_myPlugin);


LC_myPlugin::name()

This function is called by the program during the loading of the module and returns the module name, in this example "My first plugin"

LC_myPlugin::menu()

This function is also called when loading the module and returns the menu where you have to install the module, in this example, in the 'Help' menu would appear a new entry called "My first plugin".

To install on a submenu, path should be separated with a "/". For example "Draw/Points " would go into the menu "Draw" submenu "Points".

If the returned menu does not exist, Creates one called "Plugins" and installed in it.


void LC_myPlugin::execComm(Document_Interface *doc, QWidget *parent)

This function is called when you run the module. The parameter "parent" is a pointer to QMainWidget (see Qt documentation) useful for show modal dialogs.

The parameter "doc" allows the module to communicate with the current drawing.

See documentation for Document_Interface class for more information.


Q_EXPORT_PLUGIN2(myplugin, LC_myPlugin);

From Qt doc: Q_EXPORT_PLUGIN2 ( PluginName, ClassName ) This macro exports the plugin class ClassName for the plugin specified by PluginName. The value of PluginName should correspond to the TARGET specified in the plugin's project file (myplugin.pro). There should be exactly one occurrence of this macro in the source code for a Qt plugin, and it should be used where the implementation is written rather than in a header file.

In the version 1.0.0 possible menus are:

  • File
  • File/Import
  • Edit
  • View
  • View/Toolbars
  • Select
  • Draw
  • Draw/Point
  • Draw/Line
  • Draw/Arc
  • Draw/Circle
  • Draw/Ellipse
  • Draw/Spline
  • Draw/Polyline
  • Dimension
  • Modify
  • Snap
  • Info
  • Layer
  • Block
  • Window
  • Help

more...

See also:

Document_Interface class

Plug_Entity class