24 #include <tulip/TulipModel.h>
25 #include <tulip/TlpQtTools.h>
32 class TLP_QT_SCOPE SimplePluginListModel :
public tlp::TulipModel {
34 QList<std::string> _list;
38 SimplePluginListModel(
const QList<std::string>& plugins,QObject *parent = NULL);
39 virtual ~SimplePluginListModel();
40 int columnCount (
const QModelIndex& = QModelIndex() )
const;
41 int rowCount(
const QModelIndex &parent = QModelIndex())
const;
42 QModelIndex parent(
const QModelIndex &)
const;
43 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const;
44 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const;
45 QList<std::string> plugins()
const;
46 std::string pluginName(
const QModelIndex& index)
const;
49 template<
typename PLUGIN>
50 class PluginModel :
public tlp::TulipModel {
52 TreeItem(QString name, QString infos = QString::null,
53 TreeItem* parent = NULL): name(name), infos(infos), parent(parent) {}
55 foreach(TreeItem* c, children)
58 TreeItem* addChild(QString name, QString infos = QString::null) {
59 TreeItem* result =
new TreeItem(name, infos,
this);
60 children.push_back(result);
67 QList<TreeItem*> children;
74 _root =
new TreeItem(
"root");
75 QMap<QString,QMap<QString,QStringList > > pluginTree;
78 for(std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
79 std::string name = *it;
81 pluginTree[plugin.category().c_str()][plugin.group().c_str()].append(name.c_str());
84 foreach(QString cat, pluginTree.keys()) {
85 TreeItem* catItem = _root->addChild(cat);
87 foreach(QString group, pluginTree[cat].keys()) {
88 TreeItem* groupItem = catItem;
90 if ((group !=
"") && (pluginTree[cat].keys().size() > 1))
91 groupItem = catItem->addChild(group);
94 std::sort(pluginTree[cat][group].begin(),
97 foreach(QString alg, pluginTree[cat][group]) {
98 const Plugin& plugin =
100 std::string infos = plugin.
info();
103 if (infos.find(
' ') != std::string::npos)
104 groupItem->addChild(alg, infos.c_str());
106 groupItem->addChild(alg);
112 QList<int> indexHierarchy(TreeItem* item)
const {
114 TreeItem* parent = item->parent;
115 TreeItem* child = item;
117 while (child != _root) {
118 result.push_front(parent->children.indexOf(child));
119 parent = parent->parent;
120 child = child->parent;
127 explicit PluginModel(QObject *parent = NULL): TulipModel(parent), _root(NULL) {
130 virtual ~PluginModel() {
134 int rowCount(
const QModelIndex &parent = QModelIndex())
const {
135 TreeItem* item = _root;
137 if (parent.isValid())
138 item = (TreeItem*)parent.internalPointer();
140 return item->children.size();
143 int columnCount(
const QModelIndex & = QModelIndex())
const {
147 QModelIndex parent(
const QModelIndex &child)
const {
148 if (!child.isValid())
149 return QModelIndex();
151 TreeItem* childItem = (TreeItem*)child.internalPointer();
153 if (childItem->parent == _root)
154 return QModelIndex();
156 QList<int> indexes = indexHierarchy(childItem->parent);
157 int row = indexes[indexes.size()-1];
158 return createIndex(row,child.column(),childItem->parent);
161 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const {
162 TreeItem* parentItem = _root;
164 if (parent.isValid()) {
165 parentItem = (TreeItem*)parent.internalPointer();
168 if (row >= parentItem->children.size())
169 return QModelIndex();
171 return createIndex(row,column,parentItem->children[row]);
174 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const {
175 TreeItem* item = (TreeItem*)index.internalPointer();
177 if (role == Qt::DisplayRole)
179 else if (role == Qt::ToolTipRole) {
180 if (item->infos.isNull())
183 return QString(
"<table><tr><td>%1</td></tr><tr><td><i>%2</i></td></tr></table>").arg(item->name +
" :").arg(item->infos);
185 else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
192 QIcon icon(p.
icon().c_str());
199 virtual Qt::ItemFlags flags (
const QModelIndex& index )
const {
200 Qt::ItemFlags result(QAbstractItemModel::flags(index));
202 if(index.isValid()) {
203 TreeItem* item = (TreeItem*)index.internalPointer();
206 result = Qt::ItemIsEnabled;
214 #endif // PLUGINMODEL_H