25 #include <tulip/TulipModel.h>
26 #include <tulip/PluginLister.h>
27 #include <tulip/TlpQtTools.h>
28 #include <QtCore/QMap>
29 #include <QtCore/QSet>
30 #include <QtCore/QString>
31 #include <QtGui/QPixmap>
32 #include <QtGui/QFont>
36 template<
typename PLUGIN>
37 class PluginListModel :
public tlp::TulipModel {
39 QList<std::string> _list;
41 explicit PluginListModel(QObject *parent = NULL): TulipModel(parent), _list(QList<std::string>::fromStdList(PluginLister::instance()->availablePlugins<
PLUGIN>())) {
43 virtual ~PluginListModel() {
46 virtual int columnCount (
const QModelIndex& = QModelIndex() )
const {
50 int rowCount(
const QModelIndex &parent = QModelIndex())
const {
57 QModelIndex parent(
const QModelIndex &)
const {
61 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const {
65 return createIndex(row, column);
68 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const {
69 if(index.row() < _list.size()) {
70 std::string name(_list[index.row()]);
72 if(role == Qt::DisplayRole) {
75 else if (role == Qt::DecorationRole) {
77 QPixmap pix(p->icon().c_str());
87 template<
typename PLUGIN>
88 class PluginModel :
public tlp::TulipModel {
90 TreeItem(QString name, TreeItem* parent = NULL): name(name), parent(parent) {}
92 foreach(TreeItem* c, children)
95 TreeItem* addChild(QString name) {
96 TreeItem* result =
new TreeItem(name,
this);
97 children.push_back(result);
103 QList<TreeItem*> children;
110 _root =
new TreeItem(
"root");
111 QMap<QString,QMap<QString,QStringList > > pluginTree;
114 for(std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
115 std::string name = *it;
117 pluginTree[plugin->category().c_str()][plugin->group().c_str()].append(name.c_str());
120 foreach(QString cat, pluginTree.keys()) {
121 TreeItem* catItem = _root->addChild(cat);
123 if (pluginTree[cat].keys().size() > 1) {
124 foreach(QString group, pluginTree[cat].keys()) {
125 TreeItem* groupItem = catItem;
128 groupItem = catItem->addChild(group);
131 std::sort(pluginTree[cat][group].begin(),
132 pluginTree[cat][group].end(), QStringCaseCmp);
134 foreach(QString alg, pluginTree[cat][group])
135 groupItem->addChild(alg);
139 foreach(QString alg, pluginTree[cat][pluginTree[cat].keys()[0]])
140 catItem->addChild(alg);
145 QList<int> indexHierarchy(TreeItem* item)
const {
147 TreeItem* parent = item->parent;
148 TreeItem* child = item;
150 while (child != _root) {
151 result.push_front(parent->children.indexOf(child));
152 parent = parent->parent;
153 child = child->parent;
160 explicit PluginModel(QObject *parent = NULL): TulipModel(parent), _root(NULL) {
163 virtual ~PluginModel() {
167 int rowCount(
const QModelIndex &parent = QModelIndex())
const {
168 TreeItem* item = _root;
170 if (parent.isValid())
171 item = (TreeItem*)parent.internalPointer();
173 return item->children.size();
176 int columnCount(
const QModelIndex & = QModelIndex())
const {
180 QModelIndex parent(
const QModelIndex &child)
const {
181 if (!child.isValid())
182 return QModelIndex();
184 TreeItem* childItem = (TreeItem*)child.internalPointer();
186 if (childItem->parent == _root)
187 return QModelIndex();
189 QList<int> indexes = indexHierarchy(childItem->parent);
190 int row = indexes[indexes.size()-1];
191 return createIndex(row,child.column(),childItem->parent);
194 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const {
195 TreeItem* parentItem = _root;
197 if (parent.isValid()) {
198 parentItem = (TreeItem*)parent.internalPointer();
201 if (row >= parentItem->children.size())
202 return QModelIndex();
204 return createIndex(row,column,parentItem->children[row]);
207 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const {
208 TreeItem* item = (TreeItem*)index.internalPointer();
210 if (role == Qt::DisplayRole)
212 else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
219 QIcon icon(p->
icon().c_str());
227 virtual Qt::ItemFlags flags (
const QModelIndex& index )
const {
228 Qt::ItemFlags result(QAbstractItemModel::flags(index));
230 if(index.isValid()) {
231 TreeItem* item = (TreeItem*)index.internalPointer();
234 result = Qt::ItemIsEnabled;
242 #endif // PLUGINMODEL_H