tageditor/renamingutility/filesystemitemmodel.cpp

252 lines
7.8 KiB
C++
Raw Normal View History

2015-09-06 20:20:00 +02:00
#include "./filesystemitemmodel.h"
#include "./filesystemitem.h"
2015-04-22 19:33:53 +02:00
#include <QApplication>
#include <QBrush>
2018-03-07 01:18:01 +01:00
#include <QFont>
#include <QStyle>
2015-04-22 19:33:53 +02:00
#include <forward_list>
using namespace std;
namespace RenamingUtility {
/*
TRANSLATOR RenamingUtility::FileSystemItemModel
Necessary for lupdate.
*/
2018-03-07 01:18:01 +01:00
FileSystemItemModel::FileSystemItemModel(FileSystemItem *rootItem, QObject *parent)
: QAbstractItemModel(parent)
, m_rootItem(rootItem)
{
}
2015-04-22 19:33:53 +02:00
FileSystemItemModel::~FileSystemItemModel()
2018-03-07 01:18:01 +01:00
{
}
2015-04-22 19:33:53 +02:00
void FileSystemItemModel::setRootItem(FileSystemItem *rootItem)
{
2018-03-07 01:18:01 +01:00
if (m_rootItem != rootItem) {
2015-04-22 19:33:53 +02:00
beginResetModel();
m_rootItem = rootItem;
endResetModel();
}
}
QVariant FileSystemItemModel::data(const QModelIndex &index, int role) const
{
2018-03-07 01:18:01 +01:00
if (index.isValid()) {
if (FileSystemItem *item = reinterpret_cast<FileSystemItem *>(index.internalPointer())) {
switch (role) {
2015-04-22 19:33:53 +02:00
case Qt::DisplayRole:
2018-03-07 01:18:01 +01:00
switch (index.column()) {
2015-04-22 19:33:53 +02:00
case 0:
2018-03-07 01:18:01 +01:00
switch (item->status()) {
2015-04-22 19:33:53 +02:00
case ItemStatus::Current:
return item->name();
case ItemStatus::New:
2018-03-07 01:18:01 +01:00
return item->counterpart() ? item->counterpart()->name() : item->name();
2015-04-22 19:33:53 +02:00
}
break;
case 1:
2018-03-07 01:18:01 +01:00
switch (item->status()) {
2015-04-22 19:33:53 +02:00
case ItemStatus::Current:
2018-03-07 01:18:01 +01:00
return item->counterpart() ? item->counterpart()->name() : item->name();
2015-04-22 19:33:53 +02:00
case ItemStatus::New:
return item->name();
}
break;
2018-03-07 01:18:01 +01:00
case 2:
return item->note();
default:;
2015-04-22 19:33:53 +02:00
}
break;
case Qt::DecorationRole:
2018-03-07 01:18:01 +01:00
switch (index.column()) {
2015-04-22 19:33:53 +02:00
case 0:
case 1:
2018-03-07 01:18:01 +01:00
switch (item->type()) {
case ItemType::Dir:
return QApplication::style()->standardIcon(QStyle::SP_DirIcon);
default:;
2015-04-22 19:33:53 +02:00
}
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
break;
case Qt::FontRole: {
QFont font;
2018-03-07 01:18:01 +01:00
if ((index.column() == 0 && item->status() == ItemStatus::New && !item->counterpart())
|| (index.column() == 1 && item->status() == ItemStatus::Current && !item->counterpart())) {
2015-04-22 19:33:53 +02:00
font.setItalic(true);
}
return font;
2018-03-07 01:18:01 +01:00
}
case Qt::ForegroundRole:
if (item->errorOccured()) {
2015-04-22 19:33:53 +02:00
return QBrush(Qt::red);
2018-03-07 01:18:01 +01:00
} else if (item->applied()) {
2015-04-22 19:33:53 +02:00
return QBrush(Qt::darkGreen);
2018-03-07 01:18:01 +01:00
} else if ((index.column() == 0 && item->status() == ItemStatus::New && !item->counterpart())
|| (index.column() == 1 && item->status() == ItemStatus::Current && !item->counterpart())) {
2015-04-22 19:33:53 +02:00
return QBrush(Qt::gray);
}
break;
case ErrorStatusRole:
return item->errorOccured();
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
}
}
return QVariant();
}
bool FileSystemItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
2018-03-07 01:18:01 +01:00
if (index.isValid()) {
if (FileSystemItem *item = reinterpret_cast<FileSystemItem *>(index.internalPointer())) {
switch (role) {
2015-04-22 19:33:53 +02:00
case Qt::DisplayRole:
2018-03-07 01:18:01 +01:00
switch (index.column()) {
2015-04-22 19:33:53 +02:00
case 0:
2018-03-07 01:18:01 +01:00
if (item->setCurrentName(value.toString())) {
2015-04-22 19:33:53 +02:00
emit dataChanged(index, index, QVector<int>() << role);
return true;
} else {
return false;
}
case 1:
item->setNewName(value.toString());
emit dataChanged(index, index, QVector<int>() << role);
return true;
case 2:
item->setNote(value.toString());
emit dataChanged(index, index, QVector<int>() << role);
return true;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
break;
case ErrorStatusRole:
item->setErrorOccured(value.toBool());
emit dataChanged(index, index, QVector<int>() << role << Qt::DecorationRole);
return true;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
}
}
return false;
}
QVariant FileSystemItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
2018-03-07 01:18:01 +01:00
switch (orientation) {
2015-04-22 19:33:53 +02:00
case Qt::Horizontal:
2018-03-07 01:18:01 +01:00
switch (role) {
2015-04-22 19:33:53 +02:00
case Qt::DisplayRole:
2018-03-07 01:18:01 +01:00
switch (section) {
case 0:
return tr("Current name");
case 1:
return tr("New name");
case 2:
return tr("Notes");
default:;
2015-04-22 19:33:53 +02:00
}
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
break;
2018-03-07 01:18:01 +01:00
default:;
2015-04-22 19:33:53 +02:00
}
return QVariant();
}
QModelIndex FileSystemItemModel::index(int row, int column, const QModelIndex &parent) const
{
2018-03-07 01:18:01 +01:00
if (FileSystemItem *parentItem = parent.isValid() ? reinterpret_cast<FileSystemItem *>(parent.internalPointer()) : m_rootItem) {
2015-04-22 19:33:53 +02:00
const QList<FileSystemItem *> &children = parentItem->children();
2018-03-07 01:18:01 +01:00
if (row < children.length()) {
2015-04-22 19:33:53 +02:00
return createIndex(row, column, children.at(row));
}
}
return QModelIndex();
}
QModelIndex FileSystemItemModel::index(FileSystemItem *item, int column) const
{
forward_list<FileSystemItem *> path;
path.push_front(item);
FileSystemItem *parent = item->parent();
2018-03-07 01:18:01 +01:00
while (parent) {
2015-04-22 19:33:53 +02:00
path.push_front(parent);
parent = parent->parent();
}
2018-03-07 01:18:01 +01:00
if (path.front() == m_rootItem) {
2015-04-22 19:33:53 +02:00
path.pop_front();
QModelIndex index;
2018-03-07 01:18:01 +01:00
for (FileSystemItem *pathItem : path) {
2015-04-22 19:33:53 +02:00
index = this->index(pathItem->row(), column, index);
2018-03-07 01:18:01 +01:00
if (pathItem == item || !index.isValid()) {
2015-04-22 19:33:53 +02:00
return index;
}
}
}
return QModelIndex();
}
QModelIndex FileSystemItemModel::parent(const QModelIndex &index) const
{
2018-03-07 01:18:01 +01:00
if (index.isValid()) {
if (FileSystemItem *item = reinterpret_cast<FileSystemItem *>(index.internalPointer())) {
2015-04-22 19:33:53 +02:00
FileSystemItem *parent = item->parent();
2018-03-07 01:18:01 +01:00
if (parent && (index.row() < parent->children().length())) {
2015-04-22 19:33:53 +02:00
return createIndex(parent->row(), index.column(), parent);
}
}
}
return QModelIndex();
}
QModelIndex FileSystemItemModel::counterpart(const QModelIndex &index, int column = -1)
{
2018-03-07 01:18:01 +01:00
if (index.isValid()) {
if (column < 0) {
2015-04-22 19:33:53 +02:00
column = index.column();
}
2018-03-07 01:18:01 +01:00
if (FileSystemItem *item = reinterpret_cast<FileSystemItem *>(index.internalPointer())) {
if (item->counterpart()) {
2015-04-22 19:33:53 +02:00
return this->index(item->counterpart(), column);
}
}
}
return QModelIndex();
}
int FileSystemItemModel::rowCount(const QModelIndex &parent) const
{
2018-03-07 01:18:01 +01:00
if (const FileSystemItem *parentItem = (parent.isValid() ? reinterpret_cast<FileSystemItem *>(parent.internalPointer()) : m_rootItem)) {
2017-01-06 22:15:39 +01:00
return parentItem->children().size();
2015-04-22 19:33:53 +02:00
} else {
return 0;
}
}
bool FileSystemItemModel::hasChildren(const QModelIndex &parent) const
{
2018-03-07 01:18:01 +01:00
if (const FileSystemItem *parentItem = (parent.isValid() ? reinterpret_cast<const FileSystemItem *>(parent.internalPointer()) : m_rootItem)) {
2017-01-06 22:15:39 +01:00
return parentItem->children().size() > 0;
2015-04-22 19:33:53 +02:00
} else {
return false;
}
}
int FileSystemItemModel::columnCount(const QModelIndex &) const
{
return 3;
}
2018-03-07 01:18:01 +01:00
} // namespace RenamingUtility