29 #include <tulip/TulipModel.h> 30 #include <tulip/TlpQtTools.h> 31 #include <tulip/PluginLister.h> 39 class TLP_QT_SCOPE SimplePluginListModel :
public tlp::TulipModel {
41 QList<std::string> _list;
44 SimplePluginListModel(
const std::list<std::string> &plugins, QObject *parent =
nullptr);
45 ~SimplePluginListModel()
override;
46 int columnCount(
const QModelIndex & = QModelIndex())
const override;
47 int rowCount(
const QModelIndex &parent = QModelIndex())
const override;
48 QModelIndex parent(
const QModelIndex &)
const override;
49 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override;
50 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const override;
51 QList<std::string> plugins()
const;
52 std::string pluginName(
const QModelIndex &index)
const;
55 template <
typename PLUGIN>
56 class PluginModel :
public tlp::TulipModel {
58 TreeItem(QString name, QString info =
"", TreeItem *parent =
nullptr)
59 : name(name), info(info), parent(parent) {}
63 TreeItem *addChild(QString name, QString info =
"") {
64 TreeItem *result =
new TreeItem(name, info,
this);
65 children.push_back(result);
72 QList<TreeItem *> children;
79 _root =
new TreeItem(
"root");
80 QMap<QString, QMap<QString, QStringList>> pluginTree;
81 std::list<std::string> plugins = PluginLister::availablePlugins<PLUGIN>();
83 for (std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
84 std::string name = *it;
91 for (
const QString &cat : pluginTree.keys()) {
92 TreeItem *catItem = _root->addChild(cat);
94 for (
const QString &group : pluginTree[cat].keys()) {
95 TreeItem *groupItem = catItem;
97 if ((!group.isEmpty()) && (pluginTree[cat].keys().size() > 1))
98 groupItem = catItem->addChild(group);
101 std::sort(pluginTree[cat][group].begin(), pluginTree[cat][group].end(),
QStringCaseCmp);
103 for (
const QString &alg : pluginTree[cat][group]) {
105 std::string info = plugin.info();
108 if (info.find(
' ') != std::string::npos)
111 groupItem->addChild(alg);
117 QList<int> indexHierarchy(TreeItem *item)
const {
119 TreeItem *parent = item->parent;
120 TreeItem *child = item;
122 while (child != _root) {
123 result.push_front(parent->children.indexOf(child));
124 parent = parent->parent;
125 child = child->parent;
132 explicit PluginModel(QObject *parent =
nullptr) : TulipModel(parent), _root(nullptr) {
135 ~PluginModel()
override {
139 int rowCount(
const QModelIndex &parent = QModelIndex())
const override {
140 TreeItem *item = _root;
142 if (parent.isValid())
143 item = static_cast<TreeItem *>(parent.internalPointer());
145 return item->children.size();
148 int columnCount(
const QModelIndex & = QModelIndex())
const override {
152 QModelIndex parent(
const QModelIndex &child)
const override {
153 if (!child.isValid())
154 return QModelIndex();
156 TreeItem *childItem =
static_cast<TreeItem *
>(child.internalPointer());
158 if (childItem->parent == _root)
159 return QModelIndex();
161 QList<int> indexes = indexHierarchy(childItem->parent);
162 int row = indexes[indexes.size() - 1];
163 return createIndex(row, child.column(), childItem->parent);
166 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override {
167 TreeItem *parentItem = _root;
169 if (parent.isValid()) {
170 parentItem =
static_cast<TreeItem *
>(parent.internalPointer());
173 if (row >= parentItem->children.size())
174 return QModelIndex();
176 return createIndex(row, column, parentItem->children[row]);
179 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const override {
180 TreeItem *item =
static_cast<TreeItem *
>(index.internalPointer());
182 if (role == Qt::DisplayRole)
184 else if (role == Qt::ToolTipRole) {
185 if (item->info.isEmpty())
188 return QString(
"<table><tr><td>%1</td></tr><tr><td><i>%2</i></td></tr></table>")
189 .arg(item->name +
":")
191 }
else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
195 }
else if (role == Qt::DecorationRole && item->children.isEmpty() &&
206 Qt::ItemFlags flags(
const QModelIndex &index)
const override {
207 Qt::ItemFlags result(QAbstractItemModel::flags(index));
209 if (index.isValid()) {
210 TreeItem *item =
static_cast<TreeItem *
>(index.internalPointer());
213 result = Qt::ItemIsEnabled;
221 #endif // PLUGINMODEL_H
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.
Top-level interface for plug-ins.
static bool pluginExists(const std::string &pluginName)
Checks if a plugin of a given type is loaded This method checks the plugin "pluginName" is currently ...
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.