2015-06-10 21:27:11 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-08-10 19:40:58 +00:00
|
|
|
#include <libtransmission/transmission.h> // priorities
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "FileTreeItem.h"
|
|
|
|
#include "FileTreeModel.h"
|
|
|
|
|
2015-07-12 20:48:54 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class PathIteratorBase
|
|
|
|
{
|
|
|
|
protected:
|
2017-04-20 16:02:19 +00:00
|
|
|
PathIteratorBase(QString const& path, int slashIndex) :
|
2017-04-19 12:04:45 +00:00
|
|
|
myPath(path),
|
|
|
|
mySlashIndex(slashIndex),
|
|
|
|
myToken()
|
|
|
|
{
|
|
|
|
myToken.reserve(path.size() / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& myPath;
|
2017-04-19 12:04:45 +00:00
|
|
|
int mySlashIndex;
|
|
|
|
QString myToken;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static QChar const SlashChar;
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QChar const PathIteratorBase::SlashChar = QLatin1Char('/');
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
class ForwardPathIterator : public PathIteratorBase
|
|
|
|
{
|
|
|
|
public:
|
2017-04-20 16:02:19 +00:00
|
|
|
ForwardPathIterator(QString const& path) :
|
2017-04-19 12:04:45 +00:00
|
|
|
PathIteratorBase(path, path.size() - 1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasNext() const
|
|
|
|
{
|
2015-07-12 20:48:54 +00:00
|
|
|
return mySlashIndex > 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& next()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int newSlashIndex = myPath.lastIndexOf(SlashChar, mySlashIndex);
|
|
|
|
myToken.truncate(0);
|
|
|
|
myToken += myPath.midRef(newSlashIndex + 1, mySlashIndex - newSlashIndex);
|
2015-07-12 20:48:54 +00:00
|
|
|
mySlashIndex = newSlashIndex - 1;
|
|
|
|
return myToken;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BackwardPathIterator : public PathIteratorBase
|
|
|
|
{
|
|
|
|
public:
|
2017-04-20 16:02:19 +00:00
|
|
|
BackwardPathIterator(QString const& path) :
|
2017-04-19 12:04:45 +00:00
|
|
|
PathIteratorBase(path, 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasNext() const
|
|
|
|
{
|
|
|
|
return mySlashIndex < myPath.size();
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& next()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
int newSlashIndex = myPath.indexOf(SlashChar, mySlashIndex);
|
|
|
|
|
2015-07-12 20:48:54 +00:00
|
|
|
if (newSlashIndex == -1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
newSlashIndex = myPath.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
myToken.truncate(0);
|
|
|
|
myToken += myPath.midRef(mySlashIndex, newSlashIndex - mySlashIndex);
|
2015-07-12 20:48:54 +00:00
|
|
|
mySlashIndex = newSlashIndex + 1;
|
|
|
|
return myToken;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2015-07-12 20:48:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeModel::FileTreeModel(QObject* parent, bool isEditable) :
|
|
|
|
QAbstractItemModel(parent),
|
|
|
|
myIsEditable(isEditable),
|
|
|
|
myRootItem(new FileTreeItem),
|
|
|
|
myIndexCache()
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeModel::~FileTreeModel()
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
clear();
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
delete myRootItem;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeModel::setEditable(bool editable)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myIsEditable = editable;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
FileTreeItem* FileTreeModel::itemFromIndex(QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(index.model() == this);
|
|
|
|
return static_cast<FileTreeItem*>(index.internalPointer());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndexList FileTreeModel::getOrphanIndices(QModelIndexList const& indices) const
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndexList orphanIndices = indices;
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
qSort(orphanIndices);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (QMutableListIterator<QModelIndex> it(orphanIndices); it.hasNext();)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex walk = it.next();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
walk = parent(walk, walk.column());
|
|
|
|
|
|
|
|
if (!walk.isValid())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (qBinaryFind(orphanIndices, walk) != orphanIndices.end())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
it.remove();
|
|
|
|
break;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return orphanIndices;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QVariant FileTreeModel::data(QModelIndex const& index, int role) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariant value;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (index.isValid())
|
|
|
|
{
|
|
|
|
value = itemFromIndex(index)->data(index.column(), role);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return value;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
Qt::ItemFlags FileTreeModel::flags(QModelIndex const& index) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int i(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (myIsEditable && index.column() == COL_NAME)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
i |= Qt::ItemIsEditable;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (index.column() == COL_WANTED)
|
|
|
|
{
|
|
|
|
i |= Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return Qt::ItemFlags(i);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool FileTreeModel::setData(QModelIndex const& index, QVariant const& newname, int role)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (role == Qt::EditRole)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* item = itemFromIndex(index);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit pathEdited(item->path(), newname.toString());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false; // don't update the view until the session confirms the change
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariant FileTreeModel::headerData(int column, Qt::Orientation orientation, int role) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariant data;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (column)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case COL_NAME:
|
|
|
|
data.setValue(tr("File"));
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case COL_SIZE:
|
|
|
|
data.setValue(tr("Size"));
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case COL_PROGRESS:
|
|
|
|
data.setValue(tr("Progress"));
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case COL_WANTED:
|
|
|
|
data.setValue(tr("Download"));
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case COL_PRIORITY:
|
|
|
|
data.setValue(tr("Priority"));
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2015-06-10 21:27:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return data;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex FileTreeModel::index(int row, int column, QModelIndex const& parent) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex i;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (hasIndex(row, column, parent))
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* parentItem;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!parent.isValid())
|
|
|
|
{
|
|
|
|
parentItem = myRootItem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parentItem = itemFromIndex(parent);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* childItem = parentItem->child(row);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (childItem != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
i = createIndex(row, column, childItem);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return i;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex FileTreeModel::parent(QModelIndex const& child) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return parent(child, 0); // QAbstractItemModel::parent() wants col 0
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex FileTreeModel::parent(QModelIndex const& child, int column) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex parent;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (child.isValid())
|
|
|
|
{
|
|
|
|
parent = indexOf(itemFromIndex(child)->parent(), column);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return parent;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int FileTreeModel::rowCount(QModelIndex const& parent) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* parentItem;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (parent.isValid())
|
|
|
|
{
|
|
|
|
parentItem = itemFromIndex(parent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parentItem = myRootItem;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return parentItem->childCount();
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int FileTreeModel::columnCount(QModelIndex const& parent) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_UNUSED(parent);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NUM_COLUMNS;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex FileTreeModel::indexOf(FileTreeItem* item, int column) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (item == nullptr || item == myRootItem)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return createIndex(item->row(), column, item);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::clearSubtree(QModelIndex const& top)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t i = rowCount(top);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
clearSubtree(index(--i, 0, top));
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* const item = itemFromIndex(top);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 09:29:58 +00:00
|
|
|
if (item == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->fileIndex() != -1)
|
|
|
|
{
|
|
|
|
myIndexCache.remove(item->fileIndex());
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
delete item;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FileTreeModel::clear()
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
beginResetModel();
|
|
|
|
clearSubtree(QModelIndex());
|
|
|
|
endResetModel();
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(myIndexCache.isEmpty());
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* FileTreeModel::findItemForFileIndex(int fileIndex) const
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-30 09:29:58 +00:00
|
|
|
return myIndexCache.value(fileIndex, nullptr);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::addFile(int fileIndex, QString const& filename, bool wanted, int priority, uint64_t totalSize,
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t have, bool updateFields)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* item;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
item = findItemForFileIndex(fileIndex);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (item != nullptr) // this file is already in the tree, we've added this
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex indexWithChangedParents;
|
|
|
|
ForwardPathIterator filenameIt(filename);
|
|
|
|
|
|
|
|
while (filenameIt.hasNext())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& token = filenameIt.next();
|
|
|
|
std::pair<int, int> const changed = item->update(token, wanted, priority, have, updateFields);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (changed.first >= 0)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
emit dataChanged(indexOf(item, changed.first), indexOf(item, changed.second));
|
|
|
|
|
|
|
|
if (!indexWithChangedParents.isValid() && changed.first <= COL_PRIORITY && changed.second >= COL_SIZE)
|
|
|
|
{
|
|
|
|
indexWithChangedParents = indexOf(item, 0);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
item = item->parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(item == myRootItem);
|
|
|
|
|
|
|
|
if (indexWithChangedParents.isValid())
|
|
|
|
{
|
|
|
|
emitParentsChanged(indexWithChangedParents, COL_SIZE, COL_PRIORITY);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // we haven't build the FileTreeItems for these tokens yet
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool added = false;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
item = myRootItem;
|
|
|
|
BackwardPathIterator filenameIt(filename);
|
|
|
|
|
|
|
|
while (filenameIt.hasNext())
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const& token = filenameIt.next();
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* child(item->child(token));
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (child == nullptr)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
added = true;
|
|
|
|
QModelIndex parentIndex(indexOf(item, 0));
|
2017-04-20 16:02:19 +00:00
|
|
|
int const n(item->childCount());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
beginInsertRows(parentIndex, n, n);
|
|
|
|
|
|
|
|
if (!filenameIt.hasNext())
|
|
|
|
{
|
|
|
|
child = new FileTreeItem(token, fileIndex, totalSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child = new FileTreeItem(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
item->appendChild(child);
|
|
|
|
endInsertRows();
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
item = child;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (item != myRootItem)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(item->fileIndex() == fileIndex);
|
|
|
|
assert(item->totalSize() == totalSize);
|
|
|
|
|
|
|
|
myIndexCache[fileIndex] = item;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
std::pair<int, int> const changed = item->update(item->name(), wanted, priority, have, added || updateFields);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (changed.first >= 0)
|
|
|
|
{
|
|
|
|
emit dataChanged(indexOf(item, changed.first), indexOf(item, changed.second));
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::emitParentsChanged(QModelIndex const& index, int firstColumn, int lastColumn,
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<QModelIndex>* visitedParentIndices)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(firstColumn <= lastColumn);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex walk = index;
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
walk = parent(walk, firstColumn);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!walk.isValid())
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (visitedParentIndices != nullptr)
|
|
|
|
{
|
|
|
|
if (visitedParentIndices->contains(walk))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedParentIndices->insert(walk);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit dataChanged(walk, walk.sibling(walk.row(), lastColumn));
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::emitSubtreeChanged(QModelIndex const& index, int firstColumn, int lastColumn)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assert(firstColumn <= lastColumn);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int const childCount = rowCount(index);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (childCount == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// tell everyone that this item changed
|
|
|
|
emit dataChanged(index.child(0, firstColumn), index.child(childCount - 1, lastColumn));
|
|
|
|
|
|
|
|
// walk the subitems
|
|
|
|
for (int i = 0; i < childCount; ++i)
|
|
|
|
{
|
|
|
|
emitSubtreeChanged(index.child(i, 0), firstColumn, lastColumn);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::twiddleWanted(QModelIndexList const& indices)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QMap<bool, QModelIndexList> wantedIndices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : getOrphanIndices(indices))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
FileTreeItem const* const item = itemFromIndex(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
wantedIndices[item->isSubtreeWanted() != Qt::Checked] << i;
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = 0; i <= 1; ++i)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wantedIndices.contains(i))
|
|
|
|
{
|
|
|
|
setWanted(wantedIndices[i], i != 0);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::twiddlePriority(QModelIndexList const& indices)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QMap<int, QModelIndexList> priorityIndices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : getOrphanIndices(indices))
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
FileTreeItem const* const item = itemFromIndex(i);
|
2017-04-19 12:04:45 +00:00
|
|
|
int priority = item->priority();
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// ... -> normal -> high -> low -> normal -> ...; mixed -> normal
|
|
|
|
if (priority == FileTreeItem::NORMAL)
|
|
|
|
{
|
|
|
|
priority = TR_PRI_HIGH;
|
|
|
|
}
|
|
|
|
else if (priority == FileTreeItem::HIGH)
|
|
|
|
{
|
|
|
|
priority = TR_PRI_LOW;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priority = TR_PRI_NORMAL;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
priorityIndices[priority] << i;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = TR_PRI_LOW; i <= TR_PRI_HIGH; ++i)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (priorityIndices.contains(i))
|
|
|
|
{
|
|
|
|
setPriority(priorityIndices[i], i);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::setWanted(QModelIndexList const& indices, bool wanted)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (indices.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndexList const orphanIndices = getOrphanIndices(indices);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<int> fileIds;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : orphanIndices)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* const item = itemFromIndex(i);
|
|
|
|
item->setSubtreeWanted(wanted, fileIds);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit dataChanged(i, i);
|
|
|
|
emitSubtreeChanged(i, COL_WANTED, COL_WANTED);
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// emit parent changes separately to avoid multiple updates for same items
|
|
|
|
QSet<QModelIndex> parentIndices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : orphanIndices)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
emitParentsChanged(i, COL_SIZE, COL_WANTED, &parentIndices);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!fileIds.isEmpty())
|
|
|
|
{
|
|
|
|
emit wantedChanged(fileIds, wanted);
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FileTreeModel::setPriority(QModelIndexList const& indices, int priority)
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (indices.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndexList const orphanIndices = getOrphanIndices(indices);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<int> fileIds;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : orphanIndices)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* const item = itemFromIndex(i);
|
|
|
|
item->setSubtreePriority(priority, fileIds);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit dataChanged(i, i);
|
|
|
|
emitSubtreeChanged(i, COL_PRIORITY, COL_PRIORITY);
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// emit parent changes separately to avoid multiple updates for same items
|
|
|
|
QSet<QModelIndex> parentIndices;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QModelIndex const& i : orphanIndices)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
emitParentsChanged(i, COL_PRIORITY, COL_PRIORITY, &parentIndices);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!fileIds.isEmpty())
|
|
|
|
{
|
|
|
|
emit priorityChanged(fileIds, priority);
|
|
|
|
}
|
2015-08-10 19:40:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool FileTreeModel::openFile(QModelIndex const& index)
|
2015-08-10 19:40:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* const item = itemFromIndex(index);
|
|
|
|
|
|
|
|
if (item->fileIndex() < 0 || !item->isComplete())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit openRequested(item->path());
|
|
|
|
return true;
|
2015-06-10 21:27:11 +00:00
|
|
|
}
|