Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "projecttreemodel.h"
- #include <QFile>
- #include <QPointF>
- #include <QDebug>
- ProjectTreeModel::ProjectTreeModel(QString &filename, QDomDocument *&projectXml, QObject *parent) :
- QAbstractItemModel(parent),projectInfo(filename)
- {
- if(projectXml)
- {
- delete projectXml;
- projectXml=0;
- }
- projectXml = new QDomDocument("WHC");
- QFile file(filename);
- project = projectXml;
- if (!file.open(QIODevice::ReadOnly))
- return;
- if (!project->setContent(&file)){
- file.close();
- return;
- }
- file.close();
- qDebug() << "Pointer inside" << projectXml;
- rootItem = new ProjectTreeItem(*project,0);
- }
- ProjectTreeModel::~ProjectTreeModel()
- {
- delete rootItem;
- }
- QModelIndex ProjectTreeModel::index(int row, int column, const QModelIndex &parent) const
- {
- if (!hasIndex(row, column, parent))
- return QModelIndex();
- ProjectTreeItem *parentItem;
- if (!parent.isValid())
- parentItem = rootItem;
- else
- parentItem = static_cast<ProjectTreeItem*>(parent.internalPointer());
- ProjectTreeItem *childItem = parentItem->child(row);
- if (childItem){
- return createIndex(row, column, childItem);
- } else {
- return QModelIndex();
- }
- }
- QModelIndex ProjectTreeModel::parent(const QModelIndex &child) const
- {
- if (!child.isValid())
- return QModelIndex();
- ProjectTreeItem *childItem = getItem(child);
- ProjectTreeItem *parentItem = childItem->parent();
- if (!parentItem || parentItem == rootItem)
- return QModelIndex();
- return createIndex(parentItem->row(), 0, parentItem);
- }
- int ProjectTreeModel::rowCount(const QModelIndex &parent) const
- {
- if (parent.column() > 0)
- return 0;
- ProjectTreeItem *parentItem;
- if (!parent.isValid())
- parentItem = rootItem;
- else
- parentItem = getItem(parent);
- return parentItem->childCount();
- }
- int ProjectTreeModel::columnCount(const QModelIndex &parent) const
- {
- Q_UNUSED(parent);
- return 1;
- }
- QVariant ProjectTreeModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- ProjectTreeItem *item = getItem(index);
- if (index.column() > 0)
- return QVariant();
- if (role == FileNameRole){
- QString fileName(item->data(role).toString());
- if (fileName.isEmpty())
- return QString();
- else
- return projectInfo.path() + fileName;
- }
- return item->data(role);
- }
- bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
- {
- if (!index.isValid())
- return false;
- ProjectTreeItem *item = getItem(index);
- if (item->setData(value, role)){
- emit dataChanged(index, index);
- return true;
- }
- return false;
- }
- Qt::ItemFlags ProjectTreeModel::flags(const QModelIndex &index) const
- {
- if (!index.isValid())
- return 0;
- return Qt::ItemIsEditable |Qt::ItemIsEnabled | Qt::ItemIsSelectable;
- }
- QVariant ProjectTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
- {
- Q_UNUSED(section);
- if (orientation == Qt::Horizontal && role == Qt::DisplayRole){
- return tr("Project");
- }
- return QVariant();
- }
- ProjectTreeItem *ProjectTreeModel::getItem(const QModelIndex &index) const
- {
- if (index.isValid()) {
- ProjectTreeItem *item = static_cast<ProjectTreeItem*>(index.internalPointer());
- if (item) return item;
- }
- return rootItem;
- }
- QModelIndex ProjectTreeModel::tasksIndex() const
- {
- QModelIndex root = index(0, 0);
- return index(0, 0, root);
- }
- QModelIndex ProjectTreeModel::dataIndex() const
- {
- QModelIndex root = index(0, 0);
- return index(1, 0, root);
- }
- QFileInfo ProjectTreeModel::file() const
- {
- return projectInfo;
- }
Advertisement
Add Comment
Please, Sign In to add comment