2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2015-06-10 21:27:11 +00:00
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
#include <cstdint> // uint64_t
|
2022-08-16 14:30:05 +00:00
|
|
|
#include <map>
|
2021-11-28 01:58:35 +00:00
|
|
|
#include <memory>
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QSet>
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
#include <libtransmission/tr-macros.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
class FileTreeItem;
|
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
class FileTreeModel final : public QAbstractItemModel
|
2015-06-10 21:27:11 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(FileTreeModel)
|
2015-06-10 21:27:11 +00:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
FileTreeModel(QObject* parent = nullptr, bool is_editable = true);
|
2020-09-09 14:24:39 +00:00
|
|
|
~FileTreeModel() override;
|
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();
|
2021-08-15 09:41:48 +00:00
|
|
|
void addFile(
|
|
|
|
int index,
|
|
|
|
QString const& filename,
|
|
|
|
bool wanted,
|
|
|
|
int priority,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t have,
|
2020-05-27 21:53:12 +00:00
|
|
|
bool torrent_changed);
|
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
|
2019-11-12 01:37:05 +00:00
|
|
|
QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const override;
|
|
|
|
Qt::ItemFlags flags(QModelIndex const& index) const override;
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
2020-05-27 21:53:12 +00:00
|
|
|
QModelIndex index(int row, int column, QModelIndex const& parent = {}) const override;
|
2019-11-12 01:37:05 +00:00
|
|
|
QModelIndex parent(QModelIndex const& child) const override;
|
2020-05-27 21:53:12 +00:00
|
|
|
int rowCount(QModelIndex const& parent = {}) const override;
|
|
|
|
int columnCount(QModelIndex const& parent = {}) const override;
|
2019-11-12 01:37:05 +00:00
|
|
|
bool setData(QModelIndex const& index, QVariant const& value, int role = Qt::EditRole) override;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
signals:
|
2020-05-27 21:53:12 +00:00
|
|
|
void priorityChanged(QSet<int> const& file_indices, int);
|
|
|
|
void wantedChanged(QSet<int> const& file_indices, bool);
|
|
|
|
void pathEdited(QString const& oldpath, QString const& new_name);
|
2017-04-20 16:02:19 +00:00
|
|
|
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;
|
2021-08-15 09:41:48 +00:00
|
|
|
void emitParentsChanged(
|
|
|
|
QModelIndex const&,
|
|
|
|
int first_column,
|
|
|
|
int last_column,
|
2020-05-27 21:53:12 +00:00
|
|
|
QSet<QModelIndex>* visited_parent_indices = nullptr);
|
|
|
|
void emitSubtreeChanged(QModelIndex const&, int first_column, int last_column);
|
|
|
|
FileTreeItem* findItemForFileIndex(int file_index) 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
|
|
|
|
2022-08-16 14:30:05 +00:00
|
|
|
std::map<int, FileTreeItem*> index_cache_;
|
2021-11-28 01:58:35 +00:00
|
|
|
std::unique_ptr<FileTreeItem> root_item_;
|
2020-05-27 21:53:12 +00:00
|
|
|
bool is_editable_ = {};
|
2015-06-10 21:27:11 +00:00
|
|
|
};
|