diff options
Diffstat (limited to 'fileinfomodel.cpp')
-rw-r--r-- | fileinfomodel.cpp | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/fileinfomodel.cpp b/fileinfomodel.cpp new file mode 100644 index 0000000..681f1a1 --- /dev/null +++ b/fileinfomodel.cpp @@ -0,0 +1,185 @@ +/* + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version + 2 of the License, or (at your option) any later version. +*/ + +#include <QIcon> +#include <QColor> +#include <QPalette> +#include <QApplication> + +#include "fileinfomodel.h" +#include "fileinfoitem.h" + +FileInfoModel::FileInfoModel(QObject *parent) : QAbstractItemModel(parent) { + QList<QVariant> rootData; + rootData << QVariant() << QVariant(); + mRootItem = new FileInfoItem(rootData, 0); +} + +FileInfoModel::~FileInfoModel() { + delete mRootItem; +} + +QModelIndex FileInfoModel::index(int row, int column, const QModelIndex &parent) const{ + if(!hasIndex(row, column, parent)){ + return QModelIndex(); + } + FileInfoItem *parentItem; + if(!parent.isValid()){ + parentItem = mRootItem; + }else{ + parentItem = static_cast<FileInfoItem*>(parent.internalPointer()); + } + FileInfoItem *childItem = parentItem->child(row); + if(childItem){ + return createIndex(row, column, childItem); + } + return QModelIndex(); +} + +QModelIndex FileInfoModel::parent(const QModelIndex &index) const { + if(!index.isValid()){ + return QModelIndex(); + } + FileInfoItem *childItem = static_cast<FileInfoItem*>(index.internalPointer()); + Q_ASSERT(childItem); + if(!childItem){ + return QModelIndex(); + } + FileInfoItem *parentItem = childItem->parent(); + if(parentItem == mRootItem){ + return QModelIndex(); + } + if(parentItem->row() == -1){ + return QModelIndex(); + } + return createIndex(parentItem->row(), 0, parentItem); +} + +int FileInfoModel::rowCount(const QModelIndex &parent) const { + FileInfoItem *parentItem; + if(parent.column() > 0){ + return 0; + } + if(!parent.isValid()){ + parentItem = mRootItem; + }else{ + parentItem = static_cast<FileInfoItem*>(parent.internalPointer()); + } + return parentItem->childCount(); +} + +int FileInfoModel::columnCount(const QModelIndex &parent) const { + if(!parent.isValid()){ + return mRootItem->columnCount(); + } + FileInfoItem *item = static_cast<FileInfoItem*>(parent.internalPointer()); + return item->columnCount(); +} + +QVariant FileInfoModel::data(const QModelIndex &index, int role) const { + if(!index.isValid()){ + return QVariant(); + } + FileInfoItem *item = static_cast<FileInfoItem*>(index.internalPointer()); + switch(role) { + case Qt::DisplayRole: + return item->data(index.column()); + break; + case Qt::DecorationRole: { + if((item->parent() == mRootItem) && (index.column() == 0)){ + return QIcon(":/dildo.png"); + } + break; + } + case Qt::ForegroundRole: + if((item->parent() == mRootItem) && (index.column() == 0)){ + qApp->palette().color(QPalette::WindowText); + } + return Qt::blue; + break; + } + return QVariant(); +} + +Qt::ItemFlags FileInfoModel::flags(const QModelIndex &) const { + return Qt::ItemIsSelectable | Qt::ItemIsEnabled; +} + +QVariant FileInfoModel::headerData(int, Qt::Orientation, int) const { + return QVariant(); +} + +void FileInfoModel::addFiles(const QStringList &files){ + mCurrentIndex = QModelIndex(); + mTitle = QString(); + mMode = File; + mCurrentFiles = files; + insertRows(0, files.count(), createIndex(0, 0, mRootItem)); +} + +void FileInfoModel::addIndex(const QString &title, const QModelIndex &idx){ + mCurrentFiles.clear(); + mMode = Index; + mCurrentIndex = idx; + mTitle = title; + insertRows(0, 1, createIndex(0, 0, mRootItem)); +} + +bool FileInfoModel::insertRows(int row, int count, const QModelIndex &parent){ + if(mMode == File){ + if(count != mCurrentFiles.count()){ + return false; + } + }else{ + if((count != 1) || !mCurrentIndex.isValid()){ + return false; + } + } + beginInsertRows(parent, row, row + count - 1); + int i(0), j(0); + FileInfoItem *p = static_cast<FileInfoItem*>(parent.internalPointer()); + if(!p){ + p = mRootItem; + } + for(i = row, j = 0; i < (row + count); ++i, ++j){ + FileInfoItem *item = 0; + if(mMode == File){ + item = new FileInfoItem(mCurrentFiles.at(j), p); + item->populate(); + }else{ + item = new FileInfoItem(mTitle, mCurrentIndex, p); + item->populateFromIndex(); + } + p->appendChild(item); + } + endInsertRows(); + emit layoutChanged(); + mCurrentFiles.clear(); + return true; +} + +bool FileInfoModel::removeRows(int row, int count, const QModelIndex &parent){ + FileInfoItem *item = static_cast<FileInfoItem*>(parent.internalPointer()); + if(!item){ + item = mRootItem; + } + if(((row + count - 1) >= item->childCount()) || (count == 0)) { + return false; + } + beginRemoveRows(parent, row, row + (count - 1)); + for(int i = row + count; i > row; --i){ + item->removeChild(i - 1); + } + endRemoveRows(); + emit layoutChanged(); + return true; +} + +void FileInfoModel::clear(){ + removeRows(0, mRootItem->childCount(), QModelIndex()); +} + |