pgLab/pglab/Module.h

121 lines
2.5 KiB
C
Raw Normal View History

#ifndef MODULE_H
#define MODULE_H
#include "AbstractCommand.h"
#include <QKeySequence>
#include <QString>
#include <boost/container/small_vector.hpp>
#include <memory>
#include <unordered_set>
#include <vector>
class QAction;
namespace Leon {
class Module;
class ModuleRegistry {
public:
using ModuleContainer = std::unordered_set<Module*>;
using Iterator = ModuleContainer::iterator;
Iterator begin() { return modules.begin(); }
Iterator end() { return modules.end(); }
void registerModule(Module *module);
private:
ModuleContainer modules;
};
ModuleRegistry& GetModuleRegistry();
template <typename Func>
class Command: public AbstractCommand {
public:
Command(QString caption, QString iconpath, Func f);
Command& setHint(QString hint);
Command& setDescription(QString description);
Command& setShortCut(QKeySequence shortcut);
Command& addOnToolbar(QString toolbar, QString group);
Command& setMenuPath(QString menu);
// action = new QAction(QIcon(":/icons/script_save.png"), tr("Save SQL"), this);
// action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_S));
// connect(action, &QAction::triggered, this, &QueryTab::save);
// actions.push_back(action);
private:
mutable QAction *action = nullptr;
};
/// Abstract interface for retrieving information about a subsystem
///
/// Each module should derive a class From ModuleInstance and implement
/// the abstract methods declared in this interface. Don't do to much in your constructor
/// as it might be called very early in the startup of the program
class Module
{
public:
using AbstractCommandSptr = std::shared_ptr<AbstractCommand>;
using CommandContainer = std::vector<AbstractCommand>;
Module();
// getGlobalActions
virtual QString getName() const = 0;
virtual CommandContainer getCommands() const = 0;
};
/// Adds auto registration to Module so not each
/// module has to implement registration
class ModuleInstance: public Module {
protected:
ModuleInstance();
};
class Path {
public:
explicit Path(QString p)
{
int ofs = 0;
if (p.isEmpty())
return;
if (p[0] == '/')
++ofs;
while (true) {
int i = p.indexOf('/', ofs);
if (i >= ofs) {
path.push_back(p.mid(ofs, i - ofs));
ofs = i+1;
}
else {
path.push_back(p.right(ofs));
break;
}
}
}
int getDepth() const { return path.size(); }
QString getElem(int i) const { return path.at(i); }
private:
using PathContainer = boost::container::small_vector<QString, 3>;
PathContainer path;
};
}
#endif // MODULE_H