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 info =
"",
58 TreeItem* parent = NULL): name(name), info(info), parent(parent) {}
62 TreeItem* addChild(QString name, QString info =
"") {
63 TreeItem* result =
new TreeItem(name, info,
this);
64 children.push_back(result);
71 QList<TreeItem*> children;
78 _root =
new TreeItem(
"root");
79 QMap<QString,QMap<QString,QStringList > > pluginTree;
82 for(std::list<std::string>::iterator it = plugins.begin(); it != plugins.end(); ++it) {
83 std::string name = *it;
88 foreach(
const QString& cat, pluginTree.keys()) {
89 TreeItem* catItem = _root->addChild(cat);
91 foreach(
const QString& group, pluginTree[cat].keys()) {
92 TreeItem* groupItem = catItem;
94 if ((!group.isEmpty()) && (pluginTree[cat].keys().size() > 1))
95 groupItem = catItem->addChild(group);
98 qSort(pluginTree[cat][group].begin(),
101 foreach(
const QString& alg, pluginTree[cat][group]) {
102 const Plugin& plugin =
104 std::string info = plugin.
info();
107 if (info.find(
' ') != std::string::npos)
110 groupItem->addChild(alg);
116 QList<int> indexHierarchy(TreeItem* item)
const {
118 TreeItem* parent = item->parent;
119 TreeItem* child = item;
121 while (child != _root) {
122 result.push_front(parent->children.indexOf(child));
123 parent = parent->parent;
124 child = child->parent;
131 explicit PluginModel(QObject *parent = NULL): TulipModel(parent), _root(NULL) {
134 virtual ~PluginModel() {
138 int rowCount(
const QModelIndex &parent = QModelIndex())
const {
139 TreeItem* item = _root;
141 if (parent.isValid())
142 item = (TreeItem*)parent.internalPointer();
144 return item->children.size();
147 int columnCount(
const QModelIndex & = QModelIndex())
const {
151 QModelIndex parent(
const QModelIndex &child)
const {
152 if (!child.isValid())
153 return QModelIndex();
155 TreeItem* childItem = (TreeItem*)child.internalPointer();
157 if (childItem->parent == _root)
158 return QModelIndex();
160 QList<int> indexes = indexHierarchy(childItem->parent);
161 int row = indexes[indexes.size()-1];
162 return createIndex(row,child.column(),childItem->parent);
165 QModelIndex index(
int row,
int column,
const QModelIndex &parent = QModelIndex())
const {
166 TreeItem* parentItem = _root;
168 if (parent.isValid()) {
169 parentItem = (TreeItem*)parent.internalPointer();
172 if (row >= parentItem->children.size())
173 return QModelIndex();
175 return createIndex(row,column,parentItem->children[row]);
178 QVariant data(
const QModelIndex &index,
int role = Qt::DisplayRole)
const {
179 TreeItem* item = (TreeItem*)index.internalPointer();
181 if (role == Qt::DisplayRole)
183 else if (role == Qt::ToolTipRole) {
184 if (item->info.isEmpty())
187 return QString(
"<table><tr><td>%1</td></tr><tr><td><i>%2</i></td></tr></table>").arg(item->name +
":").arg(item->info);
189 else if (role == Qt::FontRole && !index.parent().parent().isValid()) {
203 virtual Qt::ItemFlags flags (
const QModelIndex& index )
const {
204 Qt::ItemFlags result(QAbstractItemModel::flags(index));
206 if(index.isValid()) {
207 TreeItem* item = (TreeItem*)index.internalPointer();
210 result = Qt::ItemIsEnabled;
218 #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 QString to a Tulip UTF-8 encoded std::string.
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 Tulip UTF-8 encoded std::string to a QString.
bool pluginExists(const std::string &pluginName)
Checks if a plugin of a given type is loaded This method checks the plugin "pluginName" is currently ...
static tlp::PluginLister * instance()
Gets the static instance of this class. If not already done, creates it beforehand.