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;
 
   86       pluginTree[plugin.category().c_str()][plugin.group().c_str()].append(name.c_str());
 
   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)
 
  109             groupItem->addChild(alg, infos.c_str());
 
  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()) {
 
  197       QIcon icon(p.
icon().c_str());
 
  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