ctkPluginResourcesTreeModel.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkPluginResourcesTreeModel.h"
00023 
00024 #include <ctkPlugin.h>
00025 
00026 class ctkPluginResourceTreeNode;
00027 
00028 class ctkPluginResourceTreeItem
00029 {
00030 public:
00031 
00032   ctkPluginResourceTreeItem(const QString& path, ctkPluginResourceTreeNode* parent = 0);
00033 
00034   virtual ~ctkPluginResourceTreeItem();
00035 
00036   virtual ctkPluginResourceTreeItem* child(int row);
00037 
00038   virtual int childCount() const;
00039 
00040   int row();
00041 
00042   QVariant data(int role) const;
00043 
00044   ctkPluginResourceTreeNode* parent() const;
00045 
00046 protected:
00047   const QString path;
00048   ctkPluginResourceTreeNode* parentNode;
00049 };
00050 
00051 class ctkPluginResourceTreeNode : public ctkPluginResourceTreeItem
00052 {
00053 public:
00054 
00055   ctkPluginResourceTreeNode(const ctkPlugin* plugin, const QString& path, ctkPluginResourceTreeNode* parent = 0);
00056 
00057   ~ctkPluginResourceTreeNode();
00058 
00059   ctkPluginResourceTreeItem* child(int row);
00060 
00061   int childCount() const;
00062 
00063   int indexOf(ctkPluginResourceTreeItem* child) const;
00064 
00065 private:
00066 
00067   const ctkPlugin* plugin;
00068   QList<ctkPluginResourceTreeItem*> children;
00069 
00070 };
00071 
00072 
00073 ctkPluginResourceTreeItem::ctkPluginResourceTreeItem(const QString& path,
00074                                                ctkPluginResourceTreeNode* parent)
00075   : path(path), parentNode(parent)
00076 {
00077 
00078 }
00079 
00080 ctkPluginResourceTreeItem::~ctkPluginResourceTreeItem()
00081 {
00082 
00083 }
00084 
00085 ctkPluginResourceTreeItem* ctkPluginResourceTreeItem::child(int row)
00086 {
00087   return 0;
00088 }
00089 
00090 int ctkPluginResourceTreeItem::childCount() const
00091 {
00092   return 0;
00093 }
00094 
00095 int ctkPluginResourceTreeItem::row()
00096 {
00097   if (parentNode)
00098   {
00099     return parentNode->indexOf(this);
00100   }
00101 
00102   return 0;
00103 }
00104 
00105 QVariant ctkPluginResourceTreeItem::data(int role) const
00106 {
00107   if (role == Qt::DisplayRole)
00108   {
00109     QString p = path;
00110     if (p.endsWith('/')) p = p.left(p.size()-1);
00111 
00112     int i = p.lastIndexOf('/');
00113 
00114     return p.mid(i+1);
00115   }
00116   else if (role == Qt::UserRole)
00117   {
00118     if (this->parent())
00119     {
00120       return this->parent()->data(role).toString() + path;
00121     }
00122     else
00123     {
00124       return path;
00125     }
00126   }
00127 
00128   return QVariant();
00129 }
00130 
00131 ctkPluginResourceTreeNode* ctkPluginResourceTreeItem::parent() const
00132 {
00133   return parentNode;
00134 }
00135 
00136 
00137 ctkPluginResourceTreeNode::ctkPluginResourceTreeNode(const ctkPlugin* plugin, const QString& path, ctkPluginResourceTreeNode* parent)
00138   : ctkPluginResourceTreeItem(path, parent), plugin(plugin)
00139 {
00140   QStringList pathEntries = plugin->getResourceList(path);
00141   QStringListIterator it(pathEntries);
00142   while (it.hasNext())
00143   {
00144     const QString& entry = it.next();
00145     if (entry.endsWith('/'))
00146     {
00147       children.push_back(new ctkPluginResourceTreeNode(plugin, entry, this));
00148     }
00149     else
00150     {
00151       children.push_back(new ctkPluginResourceTreeItem(entry, this));
00152     }
00153   }
00154 }
00155 
00156 ctkPluginResourceTreeNode::~ctkPluginResourceTreeNode()
00157 {
00158   qDeleteAll(children);
00159 }
00160 
00161 ctkPluginResourceTreeItem* ctkPluginResourceTreeNode::child(int row)
00162 {
00163   return children.value(row);
00164 }
00165 
00166 int ctkPluginResourceTreeNode::childCount() const
00167 {
00168   return children.size();
00169 }
00170 
00171 int ctkPluginResourceTreeNode::indexOf(ctkPluginResourceTreeItem* child) const
00172 {
00173   return children.indexOf(child);
00174 }
00175 
00176 
00177 ctkPluginResourcesTreeModel::ctkPluginResourcesTreeModel(const ctkPlugin* plugin, QObject* parent)
00178   : QAbstractItemModel(parent), plugin(plugin)
00179 {
00180   rootItem = new ctkPluginResourceTreeNode(plugin, "/");
00181 }
00182 
00183 ctkPluginResourcesTreeModel::~ctkPluginResourcesTreeModel()
00184 {
00185   delete rootItem;
00186 }
00187 
00188 QVariant ctkPluginResourcesTreeModel::data(const QModelIndex &index, int role) const
00189 {
00190   if (!index.isValid())
00191     return QVariant();
00192 
00193   if (role == Qt::DisplayRole | role == Qt::UserRole)
00194   {
00195     ctkPluginResourceTreeItem* item = static_cast<ctkPluginResourceTreeItem*>(index.internalPointer());
00196     return item->data(role);
00197   }
00198 
00199   return QVariant();
00200 }
00201 
00202 Qt::ItemFlags ctkPluginResourcesTreeModel::flags(const QModelIndex &index) const
00203 {
00204   if (!index.isValid())
00205     return 0;
00206 
00207   return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00208 }
00209 
00210 QVariant ctkPluginResourcesTreeModel::headerData(int section, Qt::Orientation orientation,
00211                    int role) const
00212 {
00213   return QVariant();
00214 }
00215 
00216 QModelIndex ctkPluginResourcesTreeModel::index(int row, int column,
00217                  const QModelIndex &parent) const
00218 {
00219   if (!hasIndex(row, column, parent))
00220     return QModelIndex();
00221 
00222   ctkPluginResourceTreeItem* parentItem;
00223 
00224   if (!parent.isValid())
00225     parentItem = rootItem;
00226   else
00227     parentItem = static_cast<ctkPluginResourceTreeItem*>(parent.internalPointer());
00228 
00229   ctkPluginResourceTreeItem* childItem = parentItem->child(row);
00230 
00231   if (childItem)
00232     return createIndex(row, column, childItem);
00233   else
00234     return QModelIndex();
00235 }
00236 
00237 QModelIndex ctkPluginResourcesTreeModel::parent(const QModelIndex &index) const
00238 {
00239   if (!index.isValid())
00240     return QModelIndex();
00241 
00242   ctkPluginResourceTreeItem* childItem = static_cast<ctkPluginResourceTreeItem*>(index.internalPointer());
00243   ctkPluginResourceTreeItem* parentItem = childItem->parent();
00244 
00245   if (parentItem == rootItem)
00246     return QModelIndex();
00247 
00248   return createIndex(parentItem->row(), 0, parentItem);
00249 }
00250 
00251 int ctkPluginResourcesTreeModel::rowCount(const QModelIndex &parent) const
00252 {
00253   if (parent.column() > 0) return 0;
00254 
00255   ctkPluginResourceTreeItem* parentItem;
00256   if (!parent.isValid())
00257   {
00258     parentItem = rootItem;
00259   }
00260   else
00261   {
00262     parentItem = static_cast<ctkPluginResourceTreeItem*>(parent.internalPointer());
00263   }
00264 
00265   return parentItem->childCount();
00266 }
00267 
00268 int ctkPluginResourcesTreeModel::columnCount(const QModelIndex &parent) const
00269 {
00270   return 1;
00271 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines