30 #include <tulip/TulipModel.h>
31 #include <tulip/TlpQtTools.h>
32 #include <tulip/PluginLister.h>
40 class TLP_QT_SCOPE SimplePluginListModel :
public tlp::TulipModel {
42 QList<std::string> _list;
45 SimplePluginListModel(
const std::list<std::string> &plugins, QObject *parent =
nullptr);
46 ~SimplePluginListModel()
override;
47 int columnCount(
const QModelIndex & = QModelIndex())
const override;
48 int rowCount(
const QModelIndex &parent = QModelIndex())
const override;
49 QModelIndex parent(
const QModelIndex &)
const override;
50 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const override;
51 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const override;
52 QList<std::string> plugins()
const;
53 std::string pluginName(
const QModelIndex &index)
const;
56 template <
typename PLUGIN>
57 class PluginModel :
public tlp::TulipModel {
59 TreeItem(QString name, QString info =
"", TreeItem *parent =
nullptr)
60 : name(name), info(info), parent(parent) {}
64 TreeItem *addChild(QString name, QString info =
"") {
65 TreeItem *result =
new TreeItem(name, info,
this);
66 children.push_back(result);
73 QList<TreeItem *> children;
80 _root =
new TreeItem(
"root");
81 QMap<QString, QMap<QString, QStringList>> pluginTree;
82 std::list<std::string> plugins = PluginLister::availablePlugins<PLUGIN>();
84 for (std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
85 std::string name = *it;
92 for (
const QString &cat : pluginTree.keys()) {
93 TreeItem *catItem = _root->addChild(cat);
95 for (
const QString &group : pluginTree[cat].keys()) {
96 TreeItem *groupItem = catItem;
98 if ((!group.isEmpty()) && (pluginTree[cat].keys().size() > 1))
99 groupItem = catItem->addChild(group);
102 std::sort(pluginTree[cat][group].begin(), pluginTree[cat][group].end(),
QStringCaseCmp);
104 for (
const QString &alg : pluginTree[cat][group]) {
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><b>%1</b></td></tr><tr><td><i>%2</i></td></tr></table>")
191 }
else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
193 QWidget *p =
dynamic_cast<QWidget *
>(QAbstractItemModel::parent());
199 }
else if (role == Qt::DecorationRole && item->children.isEmpty() &&
209 Qt::ItemFlags flags(
const QModelIndex &index)
const override {
210 Qt::ItemFlags result(QAbstractItemModel::flags(index));
212 if (index.isValid()) {
213 TreeItem *item =
static_cast<TreeItem *
>(index.internalPointer());
216 result = Qt::ItemIsEnabled;
virtual std::string info() const =0
Information about the plug-in, from the plug-in author. This information can contains anything,...
virtual std::string icon() const
The icon (preferably a thumbnail) of the plugin.
static const Plugin & pluginInformation(const std::string &name)
Gets more detailed information about one specific plug-in.
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 ...
std::string QStringToTlpString(const QString &toConvert)
Convert a QString to a Tulip UTF-8 encoded std::string.
bool QStringCaseCmp(const QString &s1, const QString &s2)
Case insensitive comparison of two QStrings.
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.