27 #include <tulip/TulipModel.h> 28 #include <tulip/TlpQtTools.h> 29 #include <tulip/PluginLister.h> 37 class TLP_QT_SCOPE SimplePluginListModel :
public tlp::TulipModel {
39 QList<std::string> _list;
42 SimplePluginListModel(
const QList<std::string> &plugins, QObject *parent =
nullptr);
43 ~SimplePluginListModel()
override;
44 int columnCount(
const QModelIndex & = QModelIndex())
const override;
45 int rowCount(
const QModelIndex &parent = QModelIndex())
const override;
46 QModelIndex parent(
const QModelIndex &)
const override;
47 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override;
48 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const override;
49 QList<std::string> plugins()
const;
50 std::string pluginName(
const QModelIndex &index)
const;
53 template <
typename PLUGIN>
54 class PluginModel :
public tlp::TulipModel {
56 TreeItem(QString name, QString info =
"", TreeItem *parent =
nullptr)
57 : name(name), info(info), parent(parent) {}
61 TreeItem *addChild(QString name, QString info =
"") {
62 TreeItem *result =
new TreeItem(name, info,
this);
63 children.push_back(result);
70 QList<TreeItem *> children;
77 _root =
new TreeItem(
"root");
78 QMap<QString, QMap<QString, QStringList>> pluginTree;
81 for (std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
82 std::string name = *it;
89 for (
const QString &cat : pluginTree.keys()) {
90 TreeItem *catItem = _root->addChild(cat);
92 for (
const QString &group : pluginTree[cat].keys()) {
93 TreeItem *groupItem = catItem;
95 if ((!group.isEmpty()) && (pluginTree[cat].keys().size() > 1))
96 groupItem = catItem->addChild(group);
99 qSort(pluginTree[cat][group].begin(), pluginTree[cat][group].end(),
QStringCaseCmp);
101 for (
const QString &alg : pluginTree[cat][group]) {
102 const Plugin &plugin =
104 std::string info = plugin.
info();
107 if (info.find(
' ') != std::string::npos)
110 groupItem->addChild(alg);
116 QList<int> indexHierarchy(TreeItem *item)
const {
118 TreeItem *parent = item->parent;
119 TreeItem *child = item;
121 while (child != _root) {
122 result.push_front(parent->children.indexOf(child));
123 parent = parent->parent;
124 child = child->parent;
131 explicit PluginModel(QObject *parent =
nullptr) : TulipModel(parent), _root(nullptr) {
134 ~PluginModel()
override {
138 int rowCount(
const QModelIndex &parent = QModelIndex())
const override {
139 TreeItem *item = _root;
141 if (parent.isValid())
142 item = static_cast<TreeItem *>(parent.internalPointer());
144 return item->children.size();
147 int columnCount(
const QModelIndex & = QModelIndex())
const override {
151 QModelIndex parent(
const QModelIndex &child)
const override {
152 if (!child.isValid())
153 return QModelIndex();
155 TreeItem *childItem =
static_cast<TreeItem *
>(child.internalPointer());
157 if (childItem->parent == _root)
158 return QModelIndex();
160 QList<int> indexes = indexHierarchy(childItem->parent);
161 int row = indexes[indexes.size() - 1];
162 return createIndex(row, child.column(), childItem->parent);
165 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override {
166 TreeItem *parentItem = _root;
168 if (parent.isValid()) {
169 parentItem =
static_cast<TreeItem *
>(parent.internalPointer());
172 if (row >= parentItem->children.size())
173 return QModelIndex();
175 return createIndex(row, column, parentItem->children[row]);
178 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const override {
179 TreeItem *item =
static_cast<TreeItem *
>(index.internalPointer());
181 if (role == Qt::DisplayRole)
183 else if (role == Qt::ToolTipRole) {
184 if (item->info.isEmpty())
187 return QString(
"<table><tr><td>%1</td></tr><tr><td><i>%2</i></td></tr></table>")
188 .arg(item->name +
":")
190 }
else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
194 }
else if (role == Qt::DecorationRole && item->children.isEmpty() &&
205 Qt::ItemFlags flags(
const QModelIndex &index)
const override {
206 Qt::ItemFlags result(QAbstractItemModel::flags(index));
208 if (index.isValid()) {
209 TreeItem *item =
static_cast<TreeItem *
>(index.internalPointer());
212 result = Qt::ItemIsEnabled;
220 #endif // PLUGINMODEL_H static std::list< std::string > availablePlugins()
Gets the list of plugins of a given type (template parameter).
virtual std::string info() const =0
Information about the plug-in, from the plug-in author. This information can contains anything...
bool QStringCaseCmp(const QString &s1, const QString &s2)
Case insensitive comparison of two QStrings.
std::string QStringToTlpString(const QString &toConvert)
Convert a QString to a Tulip UTF-8 encoded std::string.
static const Plugin & pluginInformation(const std::string &name)
Gets more detailed information about one specific plug-in.
virtual std::string icon() const
The icon (preferably a thumbnail) of the plugin.
#define PLUGIN(C)
Register a plugin into the plugin system. This macro is mandatory in order to register a plugin into ...
Top-level interface for plug-ins.
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.
bool pluginExists(const std::string &pluginName)
Checks if a plugin of a given type is loaded This method checks the plugin "pluginName" is currently ...
static tlp::PluginLister * instance()
Gets the static instance of this class. If not already done, creates it beforehand.