ctkQtResourcesTreeModel.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 "ctkQtResourcesTreeModel.h"
00023 
00024 
00025 class ctkQtResourceTreeNode;
00026 
00027 class ctkQtResourceTreeItem
00028 {
00029 public:
00030 
00031   ctkQtResourceTreeItem(const QFileInfo& fileInfo, ctkQtResourceTreeNode* parent = 0);
00032 
00033   virtual ~ctkQtResourceTreeItem();
00034 
00035   virtual ctkQtResourceTreeItem* child(int row);
00036 
00037   virtual int childCount() const;
00038 
00039   int row();
00040 
00041   QVariant data(int role) const;
00042 
00043   ctkQtResourceTreeNode* parent() const;
00044 
00045 protected:
00046   QFileInfo entry;
00047   ctkQtResourceTreeNode* parentNode;
00048 };
00049 
00050 class ctkQtResourceTreeNode : public ctkQtResourceTreeItem
00051 {
00052 public:
00053 
00054   ctkQtResourceTreeNode(const QFileInfo& dirInfo, ctkQtResourceTreeNode* parent = 0);
00055 
00056   ~ctkQtResourceTreeNode();
00057 
00058   ctkQtResourceTreeItem* child(int row);
00059 
00060   int childCount() const;
00061 
00062   int indexOf(ctkQtResourceTreeItem* child) const;
00063 
00064 private:
00065 
00066   QList<ctkQtResourceTreeItem*> children;
00067 
00068 };
00069 
00070 
00071 ctkQtResourceTreeItem::ctkQtResourceTreeItem(const QFileInfo& fileInfo, ctkQtResourceTreeNode* parent)
00072   : entry(fileInfo), parentNode(parent)
00073 {
00074 
00075 }
00076 
00077 ctkQtResourceTreeItem::~ctkQtResourceTreeItem()
00078 {
00079 
00080 }
00081 
00082 ctkQtResourceTreeItem* ctkQtResourceTreeItem::child(int row)
00083 {
00084   return 0;
00085 }
00086 
00087 int ctkQtResourceTreeItem::childCount() const
00088 {
00089   return 0;
00090 }
00091 
00092 int ctkQtResourceTreeItem::row()
00093 {
00094   if (parentNode)
00095   {
00096     return parentNode->indexOf(this);
00097   }
00098 
00099   return 0;
00100 }
00101 
00102 QVariant ctkQtResourceTreeItem::data(int role) const
00103 {
00104   if (role == Qt::DisplayRole)
00105   {
00106     if (entry.isFile())
00107       return entry.fileName();
00108 
00109     QString lastDir = entry.absoluteFilePath();
00110     int i = lastDir.lastIndexOf('/');
00111 
00112     if (i == lastDir.size()-1) return lastDir;
00113     return lastDir.mid(i+1);
00114   }
00115 
00116   return QVariant();
00117 }
00118 
00119 ctkQtResourceTreeNode* ctkQtResourceTreeItem::parent() const
00120 {
00121   return parentNode;
00122 }
00123 
00124 
00125 ctkQtResourceTreeNode::ctkQtResourceTreeNode(const QFileInfo& dirInfo, ctkQtResourceTreeNode* parent)
00126   : ctkQtResourceTreeItem(dirInfo, parent)
00127 {
00128   QFileInfoList infoList = QDir(dirInfo.absoluteFilePath()).entryInfoList();
00129   QListIterator<QFileInfo> it(infoList);
00130   while (it.hasNext())
00131   {
00132     const QFileInfo& info = it.next();
00133     if (info.isFile())
00134     {
00135       children.push_back(new ctkQtResourceTreeItem(info, this));
00136     }
00137     else
00138     {
00139       children.push_back(new ctkQtResourceTreeNode(info, this));
00140     }
00141   }
00142 }
00143 
00144 ctkQtResourceTreeNode::~ctkQtResourceTreeNode()
00145 {
00146   qDeleteAll(children);
00147 }
00148 
00149 ctkQtResourceTreeItem* ctkQtResourceTreeNode::child(int row)
00150 {
00151   return children.value(row);
00152 }
00153 
00154 int ctkQtResourceTreeNode::childCount() const
00155 {
00156   return children.size();
00157 }
00158 
00159 int ctkQtResourceTreeNode::indexOf(ctkQtResourceTreeItem* child) const
00160 {
00161   return children.indexOf(child);
00162 }
00163 
00164 
00165 ctkQtResourcesTreeModel::ctkQtResourcesTreeModel(QObject* parent)
00166   : QAbstractItemModel(parent)
00167 {
00168   rootItem = new ctkQtResourceTreeNode(QFileInfo(":/"));
00169 }
00170 
00171 ctkQtResourcesTreeModel::~ctkQtResourcesTreeModel()
00172 {
00173   delete rootItem;
00174 }
00175 
00176 QVariant ctkQtResourcesTreeModel::data(const QModelIndex &index, int role) const
00177 {
00178   if (!index.isValid())
00179     return QVariant();
00180 
00181   if (role == Qt::DisplayRole)
00182   {
00183     ctkQtResourceTreeItem* item = static_cast<ctkQtResourceTreeItem*>(index.internalPointer());
00184     return item->data(role);
00185   }
00186 
00187   return QVariant();
00188 }
00189 
00190 Qt::ItemFlags ctkQtResourcesTreeModel::flags(const QModelIndex &index) const
00191 {
00192   if (!index.isValid())
00193     return 0;
00194 
00195   return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00196 }
00197 
00198 QVariant ctkQtResourcesTreeModel::headerData(int section, Qt::Orientation orientation,
00199                    int role) const
00200 {
00201   return QVariant();
00202 }
00203 
00204 QModelIndex ctkQtResourcesTreeModel::index(int row, int column,
00205                  const QModelIndex &parent) const
00206 {
00207   if (!hasIndex(row, column, parent))
00208     return QModelIndex();
00209 
00210   if (!parent.isValid())
00211     return createIndex(row, column, rootItem);
00212 
00213   ctkQtResourceTreeItem* parentItem = static_cast<ctkQtResourceTreeItem*>(parent.internalPointer());
00214   ctkQtResourceTreeItem* childItem = parentItem->child(row);
00215 
00216   if (childItem)
00217     return createIndex(row, column, childItem);
00218   else
00219     return QModelIndex();
00220 }
00221 
00222 QModelIndex ctkQtResourcesTreeModel::parent(const QModelIndex &index) const
00223 {
00224   if (!index.isValid())
00225     return QModelIndex();
00226 
00227   ctkQtResourceTreeItem* childItem = static_cast<ctkQtResourceTreeItem*>(index.internalPointer());
00228   ctkQtResourceTreeItem* parentItem = childItem->parent();
00229 
00230   if (parentItem)
00231     return createIndex(parentItem->row(), 0, parentItem);
00232 
00233   return QModelIndex();
00234 }
00235 
00236 int ctkQtResourcesTreeModel::rowCount(const QModelIndex &parent) const
00237 {
00238   if (parent.column() > 0) return 0;
00239 
00240   if (!parent.isValid())
00241   {
00242     return 1;
00243   }
00244   else
00245   {
00246     ctkQtResourceTreeItem* parentItem = static_cast<ctkQtResourceTreeItem*>(parent.internalPointer());
00247     return parentItem->childCount();
00248   }
00249 }
00250 
00251 int ctkQtResourcesTreeModel::columnCount(const QModelIndex &parent) const
00252 {
00253   return 1;
00254 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines