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;
43 SimplePluginListModel(
const QList<std::string>& plugins,QObject *parent = NULL);
44 virtual ~SimplePluginListModel();
45 int columnCount (
const QModelIndex& = QModelIndex() )
const;
46 int rowCount(
const QModelIndex &parent = QModelIndex())
const;
47 QModelIndex parent(
const QModelIndex &)
const;
48 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const;
49 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const;
50 QList<std::string> plugins()
const;
51 std::string pluginName(
const QModelIndex& index)
const;
54 template<
typename PLUGIN>
55 class PluginModel :
public tlp::TulipModel {
57 TreeItem(QString name, QString infos = QString::null,
58 TreeItem* parent = NULL): name(name), infos(infos), parent(parent) {}
60 foreach(TreeItem* c, children)
63 TreeItem* addChild(QString name, QString infos = QString::null) {
64 TreeItem* result =
new TreeItem(name, infos,
this);
65 children.push_back(result);
72 QList<TreeItem*> children;
79 _root =
new TreeItem(
"root");
80 QMap<QString,QMap<QString,QStringList > > pluginTree;
83 for(std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
84 std::string name = *it;
89 foreach(QString cat, pluginTree.keys()) {
90 TreeItem* catItem = _root->addChild(cat);
92 foreach(QString group, pluginTree[cat].keys()) {
93 TreeItem* groupItem = catItem;
95 if ((group !=
"") && (pluginTree[cat].keys().size() > 1))
96 groupItem = catItem->addChild(group);
99 std::sort(pluginTree[cat][group].begin(),
102 foreach(QString alg, pluginTree[cat][group]) {
103 const Plugin& plugin =
105 std::string infos = plugin.
info();
108 if (infos.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 = NULL): TulipModel(parent), _root(NULL) {
135 virtual ~PluginModel() {
139 int rowCount(
const QModelIndex &parent = QModelIndex())
const {
140 TreeItem* item = _root;
142 if (parent.isValid())
143 item = (TreeItem*)parent.internalPointer();
145 return item->children.size();
148 int columnCount(
const QModelIndex & = QModelIndex())
const {
152 QModelIndex parent(
const QModelIndex &child)
const {
153 if (!child.isValid())
154 return QModelIndex();
156 TreeItem* childItem = (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 {
167 TreeItem* parentItem = _root;
169 if (parent.isValid()) {
170 parentItem = (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 {
180 TreeItem* item = (TreeItem*)index.internalPointer();
182 if (role == Qt::DisplayRole)
184 else if (role == Qt::ToolTipRole) {
185 if (item->infos.isNull())
188 return QString(
"<table><tr><td>%1</td></tr><tr><td><i>%2</i></td></tr></table>").arg(item->name +
":").arg(item->infos);
190 else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
204 virtual Qt::ItemFlags flags (
const QModelIndex& index )
const {
205 Qt::ItemFlags result(QAbstractItemModel::flags(index));
207 if(index.isValid()) {
208 TreeItem* item = (TreeItem*)index.internalPointer();
211 result = Qt::ItemIsEnabled;
219 #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.
virtual std::string icon() const
The icon (preferably a thumbnail) of the plugin.
std::string QStringToTlpString(const QString &toConvert)
Convert a string from Tulip to QString.
static const Plugin & pluginInformation(const std::string &name)
Gets more detailed information about one specific plug-in.
#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 QString to tulip string.
bool pluginExists(const std::string &pluginName)
Checks if a plugin of a given type is loaded This method checks the plugin "pluginName" is currentl...
static tlp::PluginLister * instance()
Gets the static instance of this class. If not already done, creates it beforehand.