Guest User

Untitled

a guest
Jul 21st, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. #include "projecttreemodel.h"
  2.  
  3. #include <QFile>
  4. #include <QPointF>
  5. #include <QDebug>
  6.  
  7. ProjectTreeModel::ProjectTreeModel(QString &filename, QDomDocument *&projectXml, QObject *parent) :
  8.      QAbstractItemModel(parent),projectInfo(filename)
  9. {
  10.  
  11.     if(projectXml)
  12.     {
  13.         delete projectXml;
  14.         projectXml=0;
  15.     }
  16.     projectXml =  new QDomDocument("WHC");
  17.     QFile file(filename);
  18.  
  19.     project = projectXml;
  20.  
  21.     if (!file.open(QIODevice::ReadOnly))
  22.         return;
  23.     if (!project->setContent(&file)){
  24.         file.close();
  25.         return;
  26.     }
  27.     file.close();
  28.  
  29.     qDebug() << "Pointer inside" << projectXml;
  30.  
  31.     rootItem = new ProjectTreeItem(*project,0);
  32.  
  33. }
  34.  
  35. ProjectTreeModel::~ProjectTreeModel()
  36. {
  37.     delete rootItem;
  38. }
  39.  
  40. QModelIndex ProjectTreeModel::index(int row, int column, const QModelIndex &parent) const
  41. {
  42.     if (!hasIndex(row, column, parent))
  43.         return QModelIndex();
  44.  
  45.     ProjectTreeItem *parentItem;
  46.  
  47.     if (!parent.isValid())
  48.         parentItem = rootItem;
  49.     else
  50.         parentItem = static_cast<ProjectTreeItem*>(parent.internalPointer());
  51.  
  52.     ProjectTreeItem *childItem = parentItem->child(row);
  53.  
  54.     if (childItem){
  55.         return createIndex(row, column, childItem);
  56.     } else {
  57.         return QModelIndex();
  58.     }
  59. }
  60.  
  61. QModelIndex ProjectTreeModel::parent(const QModelIndex &child) const
  62. {
  63.     if (!child.isValid())
  64.         return QModelIndex();
  65.  
  66.     ProjectTreeItem *childItem = getItem(child);
  67.     ProjectTreeItem *parentItem = childItem->parent();
  68.  
  69.     if (!parentItem || parentItem == rootItem)
  70.         return QModelIndex();
  71.  
  72.     return createIndex(parentItem->row(), 0, parentItem);
  73. }
  74.  
  75. int ProjectTreeModel::rowCount(const QModelIndex &parent) const
  76. {
  77.     if (parent.column() > 0)
  78.         return 0;
  79.  
  80.     ProjectTreeItem *parentItem;
  81.  
  82.     if (!parent.isValid())
  83.         parentItem = rootItem;
  84.     else
  85.         parentItem = getItem(parent);
  86.  
  87.     return parentItem->childCount();
  88. }
  89.  
  90. int ProjectTreeModel::columnCount(const QModelIndex &parent) const
  91. {
  92.     Q_UNUSED(parent);
  93.     return 1;
  94. }
  95.  
  96. QVariant ProjectTreeModel::data(const QModelIndex &index, int role) const
  97. {
  98.     if (!index.isValid())
  99.         return QVariant();
  100.  
  101.     ProjectTreeItem *item = getItem(index);
  102.  
  103.     if (index.column() > 0)
  104.         return QVariant();
  105.  
  106.     if (role == FileNameRole){
  107.         QString fileName(item->data(role).toString());
  108.         if (fileName.isEmpty())
  109.             return QString();
  110.         else
  111.             return projectInfo.path() + fileName;
  112.     }
  113.  
  114.     return item->data(role);
  115. }
  116.  
  117. bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
  118. {
  119.     if (!index.isValid())
  120.         return false;
  121.  
  122.     ProjectTreeItem *item = getItem(index);
  123.  
  124.     if (item->setData(value, role)){
  125.         emit dataChanged(index, index);
  126.         return true;
  127.     }
  128.     return false;
  129. }
  130.  
  131. Qt::ItemFlags ProjectTreeModel::flags(const QModelIndex &index) const
  132. {
  133.     if (!index.isValid())
  134.         return 0;
  135.  
  136.     return Qt::ItemIsEditable |Qt::ItemIsEnabled | Qt::ItemIsSelectable;
  137. }
  138.  
  139. QVariant ProjectTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
  140. {
  141.     Q_UNUSED(section);
  142.  
  143.     if (orientation == Qt::Horizontal && role == Qt::DisplayRole){
  144.         return tr("Project");
  145.     }
  146.     return QVariant();
  147. }
  148.  
  149. ProjectTreeItem *ProjectTreeModel::getItem(const QModelIndex &index) const
  150. {
  151.      if (index.isValid()) {
  152.          ProjectTreeItem *item = static_cast<ProjectTreeItem*>(index.internalPointer());
  153.          if (item) return item;
  154.      }
  155.      return rootItem;
  156. }
  157.  
  158. QModelIndex ProjectTreeModel::tasksIndex() const
  159. {
  160.     QModelIndex root = index(0, 0);
  161.     return index(0, 0, root);
  162. }
  163.  
  164. QModelIndex ProjectTreeModel::dataIndex() const
  165. {
  166.     QModelIndex root = index(0, 0);
  167.     return index(1, 0, root);
  168. }
  169.  
  170. QFileInfo ProjectTreeModel::file() const
  171. {
  172.     return projectInfo;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment