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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2015-06-12 22:41:36 +00:00
|
|
|
#include <cstdint>
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QSet>
|
|
|
|
|
|
|
|
class FileTreeItem;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class FileTreeModel : public QAbstractItemModel
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
2015-06-10 21:27:11 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
COL_NAME,
|
|
|
|
COL_SIZE,
|
|
|
|
COL_PROGRESS,
|
|
|
|
COL_WANTED,
|
|
|
|
COL_PRIORITY,
|
|
|
|
//
|
|
|
|
NUM_COLUMNS
|
2015-06-10 21:27:11 +00:00
|
|
|
};
|
|
|
|
|
2015-06-15 21:07:46 +00:00
|
|
|
enum Role
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
SortRole = Qt::UserRole,
|
|
|
|
FileIndexRole,
|
|
|
|
WantedRole,
|
|
|
|
CompleteRole
|
2015-06-15 21:07:46 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
|
|
|
FileTreeModel(QObject* parent = nullptr, bool isEditable = true);
|
|
|
|
virtual ~FileTreeModel();
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void setEditable(bool editable);
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void clear();
|
2017-04-20 16:02:19 +00:00
|
|
|
void addFile(int index, QString const& filename, bool wanted, int priority, uint64_t size, uint64_t have,
|
2017-04-19 12:04:45 +00:00
|
|
|
bool torrentChanged);
|
2015-06-12 22:12:12 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool openFile(QModelIndex const& index);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void twiddleWanted(QModelIndexList const& indices);
|
|
|
|
void twiddlePriority(QModelIndexList const& indices);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void setWanted(QModelIndexList const& indices, bool wanted);
|
|
|
|
void setPriority(QModelIndexList const& indices, int priority);
|
2015-08-10 19:40:58 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QModelIndex parent(QModelIndex const& child, int column) const;
|
2015-06-12 22:12:12 +00:00
|
|
|
|
|
|
|
// QAbstractItemModel
|
2017-04-20 16:02:19 +00:00
|
|
|
virtual QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const;
|
|
|
|
virtual Qt::ItemFlags flags(QModelIndex const& index) const;
|
2017-04-19 12:04:45 +00:00
|
|
|
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
2017-04-20 16:02:19 +00:00
|
|
|
virtual QModelIndex index(int row, int column, QModelIndex const& parent = QModelIndex()) const;
|
|
|
|
virtual QModelIndex parent(QModelIndex const& child) const;
|
|
|
|
virtual int rowCount(QModelIndex const& parent = QModelIndex()) const;
|
|
|
|
virtual int columnCount(QModelIndex const& parent = QModelIndex()) const;
|
|
|
|
virtual bool setData(QModelIndex const& index, QVariant const& value, int role = Qt::EditRole);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
signals:
|
2017-04-20 16:02:19 +00:00
|
|
|
void priorityChanged(QSet<int> const& fileIndices, int);
|
|
|
|
void wantedChanged(QSet<int> const& fileIndices, bool);
|
|
|
|
void pathEdited(QString const& oldpath, QString const& newname);
|
|
|
|
void openRequested(QString const& path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-20 16:02:19 +00:00
|
|
|
void clearSubtree(QModelIndex const&);
|
2017-04-19 12:04:45 +00:00
|
|
|
QModelIndex indexOf(FileTreeItem*, int column) const;
|
2017-04-20 16:02:19 +00:00
|
|
|
void emitParentsChanged(QModelIndex const&, int firstColumn, int lastColumn,
|
2017-04-19 12:04:45 +00:00
|
|
|
QSet<QModelIndex>* visitedParentIndices = nullptr);
|
2017-04-20 16:02:19 +00:00
|
|
|
void emitSubtreeChanged(QModelIndex const&, int firstColumn, int lastColumn);
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* findItemForFileIndex(int fileIndex) const;
|
2017-04-20 16:02:19 +00:00
|
|
|
FileTreeItem* itemFromIndex(QModelIndex const&) const;
|
|
|
|
QModelIndexList getOrphanIndices(QModelIndexList const& indices) const;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
private:
|
2015-06-10 21:27:11 +00:00
|
|
|
bool myIsEditable;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FileTreeItem* myRootItem;
|
|
|
|
QMap<int, FileTreeItem*> myIndexCache;
|
2015-06-10 21:27:11 +00:00
|
|
|
};
|