Commit 43b84683 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

continued with plugin tools

parent b17bcd59
......@@ -37,7 +37,9 @@
# External libraries
ext/**
plugins/**
!ext/.dir
!plugins/.dir
# Temporary files
tmp/**
......
Directory containing plugin binaries.
......@@ -12,9 +12,16 @@
#include "funcs.h"
#include "libs/file.h"
#include "libs/algos.h"
#include "libs/helper.h"
#include "libs/str.h"
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QDialog>
#include <iostream>
#include <memory>
#include <boost/dll/shared_library.hpp>
using t_real = double;
......@@ -32,7 +39,7 @@ MainWnd::MainWnd(QSettings* pSettings)
this->setObjectName("in20");
SetCurrentFile("");
this->resize(800, 600);
LoadPlugins();
// ------------------------------------------------------------------------
......@@ -134,6 +141,8 @@ MainWnd::MainWnd(QSettings* pSettings)
// restore window state
if(m_pSettings->contains("mainwnd/geo"))
this->restoreGeometry(m_pSettings->value("mainwnd/geo").toByteArray());
else
this->resize(800, 600);
if(m_pSettings->contains("mainwnd/state"))
this->restoreState(m_pSettings->value("mainwnd/state").toByteArray());
}
......@@ -385,3 +394,65 @@ void MainWnd::SetCurrentFile(const QString &file)
else
this->setWindowTitle(title + " -- " + m_curFile);
}
/**
* looks for and loads plugin tools
*/
void MainWnd::LoadPlugins()
{
QDirIterator iter("./plugins", QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
while(iter.hasNext())
{
std::string dllfile = iter.next().toStdString();
if(dllfile == "." || dllfile == "..")
continue;
try
{
auto dll = std::make_shared<boost::dll::shared_library>(dllfile);
if(!dll || !dll->is_loaded())
{
print_err("Could not load plugin \"", dllfile, "\".");
continue;
}
if(!dll->has("tl_descr") || !dll->has("tl_init") || !dll->has("tl_create") || !dll->has("tl_destroy"))
{
print_err("Not a valid plugin: \"", dllfile, "\".");
continue;
}
PluginDlg plugin;
// plugin functions
plugin.f_descr = dll->get<PluginDlg::t_descr>("tl_descr");
plugin.f_init = dll->get<PluginDlg::t_init>("tl_init");
plugin.f_create = dll->get<PluginDlg::t_create>("tl_create");
plugin.f_destroy = dll->get<PluginDlg::t_destroy>("tl_destroy");
// plugin descriptors
{
std::vector<std::string> vecdescr;
tl2::get_tokens<std::string, std::string>(plugin.f_descr(), ";", vecdescr);
plugin.ty = vecdescr[0];
plugin.name = vecdescr[1];
plugin.descr = vecdescr[2];
print_out("Plugin \"", dll->location(), "\" loaded. ",
"Module type: \"", plugin.ty, "\", ",
"name: \"", plugin.name, "\", ",
"descr: \"", plugin.descr, "\".");
}
m_plugin_dlgs.emplace_back(std::move(plugin));
}
catch(const std::exception& ex)
{
print_err("Error loading plugin \"", dllfile, "\": ", ex.what(), ".");
}
}
}
......@@ -21,6 +21,30 @@
#include "plot.h"
/**
* dialog plugins
*/
struct PluginDlg
{
std::string ty, name, descr;
using t_descr = const char*(*)();
using t_init = bool(*)();
using t_create = QDialog*(*)(QWidget*);
using t_destroy = void(*)(QDialog*);
t_descr f_descr = nullptr;
t_init f_init = nullptr;
t_create f_create = nullptr;
t_destroy f_destroy = nullptr;
};
/**
* main dialog
*/
class MainWnd : public QMainWindow
{
private:
......@@ -39,6 +63,8 @@ private:
QStringList m_recentFiles;
QString m_curFile;
std::vector<PluginDlg> m_plugin_dlgs;
protected:
virtual void showEvent(QShowEvent *pEvt) override;
virtual void closeEvent(QCloseEvent *pEvt) override;
......@@ -48,6 +74,8 @@ protected:
void AddRecentFile(const QString &file);
void RebuildRecentFiles();
void LoadPlugins();
public:
MainWnd(QSettings* pSettings = nullptr);
virtual ~MainWnd();
......
......@@ -549,27 +549,38 @@ bool init()
/**
* plugin descriptor
* type, title, description
* type; title; description
*/
std::tuple<std::string, std::string, std::string> descr()
const char* descr()
{
return std::make_tuple("dlg", "Polarisation Vectors", "Calculates polarisation vectors.");
return "dlg;Polarisation Vectors;Calculates polarisation vectors.";
}
/**
* create the plugin main dialog
*/
std::shared_ptr<QDialog> create(QWidget *pParent)
QDialog* create(QWidget *pParent)
{
//std::cout << "In " << __FUNCTION__ << std::endl;
return std::make_shared<PolDlg>(pParent);
return new PolDlg(pParent);
}
/**
* destroy the plugin main dialog
*/
void destroy(QDialog* dlg)
{
//std::cout << "In " << __FUNCTION__ << std::endl;
if(dlg) delete dlg;
}
BOOST_DLL_ALIAS(init, tl_init);
BOOST_DLL_ALIAS(descr, tl_descr);
BOOST_DLL_ALIAS(create, tl_create);
BOOST_DLL_ALIAS(destroy, tl_destroy);
#endif
......
......@@ -14,49 +14,76 @@
#include <memory>
#include <boost/dll/shared_library.hpp>
#include "libs/helper.h"
#include "libs/str.h"
int main(int argc, char** argv)
{
tl2::set_locales();
if(argc <= 1)
try
{
std::cerr << "Specify a plugin library." << std::endl;
return -1;
}
tl2::set_locales();
const char *dllfile = argv[1];
auto dll = std::make_shared<boost::dll::shared_library>(dllfile);
if(!dll || !dll->is_loaded())
{
std::cerr << "Could not load plugin." << std::endl;
return -1;
}
std::cerr << "Plugin " << dll->location() << " loaded." << std::endl;
if(argc <= 1)
{
std::cerr << "Specify a plugin library." << std::endl;
return -1;
}
const char *dllfile = argv[1];
auto dll = std::make_shared<boost::dll::shared_library>(dllfile);
if(!dll || !dll->is_loaded())
{
std::cerr << "Could not load plugin." << std::endl;
return -1;
}
std::cerr << "Plugin " << dll->location() << " loaded." << std::endl;
if(!dll->has("tl_init") || !dll->has("tl_create"))
{
std::cerr << "Plugin does not have the \"tl_init\" or \"tl_create\" functions." << std::endl;
return -1;
}
if(!dll->has("tl_descr") || !dll->has("tl_init") || !dll->has("tl_create") || !dll->has("tl_destroy"))
{
std::cerr << "Not a valid plugin" << std::endl;
return -1;
}
if(auto descr = dll->get<const char*(*)()>("tl_descr"); descr)
{
std::vector<std::string> vecdescr;
tl2::get_tokens<std::string, std::string>(descr(), ";", vecdescr);
std::cout << "Module type: \"" << vecdescr[0] << "\", "
<< "Name: \"" << vecdescr[1] << "\", "
<< "Descr: \"" << vecdescr[2] << "\"" << std::endl;
}
if(auto initDlg = dll->get<bool(*)()>("tl_init"); initDlg)
initDlg();
if(auto initDlg = dll->get<bool(*)()>("tl_init"); initDlg)
initDlg();
auto app = std::make_unique<QApplication>(argc, argv);
//if(auto createDlg = dll->get<QDialog*(*)(QWidget*)>("tl_create"); createDlg)
if(auto createDlg = dll->get<std::shared_ptr<QDialog>(*)(QWidget*)>("tl_create"); createDlg)
{
if(auto dlg = createDlg(nullptr); dlg)
auto app = std::make_unique<QApplication>(argc, argv);
QDialog *dlg = nullptr;
if(auto createDlg = dll->get<QDialog*(*)(QWidget*)>("tl_create"); createDlg)
//if(auto createDlg = dll->get<std::shared_ptr<QDialog>(*)(QWidget*)>("tl_create"); createDlg)
{
dlg->show();
dlg->activateWindow();
if(dlg = createDlg(nullptr); dlg)
{
dlg->show();
dlg->activateWindow();
}
}
int ret = app->exec();
if(auto destroyDlg = dll->get<void(*)(QDialog*)>("tl_destroy"); destroyDlg)
destroyDlg(dlg);
return ret;
}
catch(const std::exception& ex)
{
std::cerr << "Error: " << ex.what() << std::endl;
}
return app->exec();
return -1;
}
......@@ -1132,29 +1132,40 @@ bool init()
/**
* plugin descriptor
* type, title, description
* type; title; description
*/
std::tuple<std::string, std::string, std::string> descr()
const char* descr()
{
return std::make_tuple("dlg", "Structure Factors", "Calculates nuclear structure factors.");
return "dlg;Structure Factors;Calculates nuclear structure factors.";
}
/**
* create the plugin main dialog
*/
std::shared_ptr<QDialog> create(QWidget *pParent)
//QDialog* create(QWidget *pParent)
//std::shared_ptr<QDialog> create(QWidget *pParent)
QDialog* create(QWidget *pParent)
{
//std::cout << "In " << __FUNCTION__ << std::endl;
return std::make_shared<StructFactDlg>(pParent);
//return new StructFactDlg(pParent);
//return std::make_shared<StructFactDlg>(pParent);
return new StructFactDlg(pParent);
}
/**
* destroy the plugin main dialog
*/
void destroy(QDialog* dlg)
{
//std::cout << "In " << __FUNCTION__ << std::endl;
if(dlg) delete dlg;
}
BOOST_DLL_ALIAS(init, tl_init);
BOOST_DLL_ALIAS(descr, tl_descr);
BOOST_DLL_ALIAS(create, tl_create);
BOOST_DLL_ALIAS(destroy, tl_destroy);
#endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment