2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
2013-01-28 18:20:20 +00:00
|
|
|
#ifndef QTR_FILE_TREE
|
|
|
|
#define QTR_FILE_TREE
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QObject>
|
2010-07-28 18:53:25 +00:00
|
|
|
#include <QItemDelegate>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QList>
|
2012-07-12 23:37:04 +00:00
|
|
|
#include <QHash>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QSet>
|
2010-07-28 18:53:25 +00:00
|
|
|
#include <QSize>
|
|
|
|
#include <QString>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QTreeView>
|
|
|
|
#include <QVariant>
|
2010-07-28 18:53:25 +00:00
|
|
|
|
|
|
|
class QSortFilterProxyModel;
|
|
|
|
class QStyle;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include "torrent.h" // FileList
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
class FileTreeItem: public QObject
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
Q_OBJECT;
|
|
|
|
|
|
|
|
enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) };
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~FileTreeItem();
|
|
|
|
|
2013-08-29 00:37:37 +00:00
|
|
|
FileTreeItem (const QString& name="", int fileIndex=-1, uint64_t size=0):
|
2013-01-28 18:20:20 +00:00
|
|
|
myFileIndex (fileIndex),
|
2013-01-25 18:37:11 +00:00
|
|
|
myParent (0),
|
|
|
|
myName (name),
|
|
|
|
myPriority (0),
|
|
|
|
myIsWanted (0),
|
|
|
|
myHaveSize (0),
|
2013-01-28 07:01:58 +00:00
|
|
|
myTotalSize (size),
|
2013-09-14 22:45:04 +00:00
|
|
|
myFirstUnhashedRow (0) {}
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void appendChild (FileTreeItem *child);
|
|
|
|
FileTreeItem * child (const QString& filename);
|
|
|
|
FileTreeItem * child (int row) { return myChildren.at(row); }
|
|
|
|
int childCount () const { return myChildren.size(); }
|
|
|
|
FileTreeItem * parent () { return myParent; }
|
|
|
|
const FileTreeItem * parent () const { return myParent; }
|
|
|
|
int row () const;
|
|
|
|
const QString& name () const { return myName; }
|
|
|
|
QVariant data (int column, int role) const;
|
2013-08-29 00:37:37 +00:00
|
|
|
std::pair<int,int> update (const QString& name, bool want, int priority, uint64_t have, bool updateFields);
|
2013-01-25 18:37:11 +00:00
|
|
|
void twiddleWanted (QSet<int>& fileIds, bool&);
|
|
|
|
void twiddlePriority (QSet<int>& fileIds, int&);
|
2013-01-28 18:20:20 +00:00
|
|
|
int fileIndex () const { return myFileIndex; }
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t totalSize () const { return myTotalSize; }
|
2013-09-08 19:03:25 +00:00
|
|
|
QString path () const;
|
|
|
|
bool isComplete () const;
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void setSubtreePriority (int priority, QSet<int>& fileIds);
|
|
|
|
void setSubtreeWanted (bool, QSet<int>& fileIds);
|
|
|
|
QString priorityString () const;
|
2013-02-09 20:51:17 +00:00
|
|
|
QString sizeString () const;
|
2013-08-29 00:37:37 +00:00
|
|
|
void getSubtreeWantedSize (uint64_t& have, uint64_t& total) const;
|
2013-01-25 18:37:11 +00:00
|
|
|
double progress () const;
|
|
|
|
int priority () const;
|
|
|
|
int isSubtreeWanted () const;
|
|
|
|
|
2013-01-28 18:20:20 +00:00
|
|
|
const int myFileIndex;
|
2013-01-25 18:37:11 +00:00
|
|
|
FileTreeItem * myParent;
|
|
|
|
QList<FileTreeItem*> myChildren;
|
|
|
|
QHash<QString,int> myChildRows;
|
2013-01-28 07:01:58 +00:00
|
|
|
const QHash<QString,int>& getMyChildRows();
|
|
|
|
QString myName;
|
2013-01-25 18:37:11 +00:00
|
|
|
int myPriority;
|
|
|
|
bool myIsWanted;
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t myHaveSize;
|
|
|
|
const uint64_t myTotalSize;
|
2013-01-25 18:37:11 +00:00
|
|
|
size_t myFirstUnhashedRow;
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileTreeModel: public QAbstractItemModel
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeModel (QObject *parent = 0, bool isEditable = true);
|
2013-01-25 18:37:11 +00:00
|
|
|
~FileTreeModel ();
|
|
|
|
|
|
|
|
public:
|
|
|
|
QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const;
|
|
|
|
Qt::ItemFlags flags (const QModelIndex& index) const;
|
|
|
|
QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
|
|
|
QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const;
|
|
|
|
QModelIndex parent (const QModelIndex& child) const;
|
|
|
|
QModelIndex parent (const QModelIndex& child, int column) const;
|
|
|
|
int rowCount (const QModelIndex& parent = QModelIndex()) const;
|
|
|
|
int columnCount (const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void priorityChanged (const QSet<int>& fileIndices, int);
|
|
|
|
void wantedChanged (const QSet<int>& fileIndices, bool);
|
|
|
|
void pathEdited (const QString& oldpath, const QString& newname);
|
2013-09-08 19:03:25 +00:00
|
|
|
void openRequested (const QString& path);
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
void clear ();
|
|
|
|
void addFile (int index, const QString& filename,
|
|
|
|
bool wanted, int priority,
|
2013-08-29 00:37:37 +00:00
|
|
|
uint64_t size, uint64_t have,
|
2013-01-25 18:37:11 +00:00
|
|
|
QList<QModelIndex>& rowsAdded,
|
|
|
|
bool torrentChanged);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clearSubtree (const QModelIndex &);
|
|
|
|
QModelIndex indexOf (FileTreeItem *, int column) const;
|
|
|
|
void parentsChanged (const QModelIndex &, int column);
|
|
|
|
void subtreeChanged (const QModelIndex &, int column);
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeItem * findItemForFileIndex (int fileIndex) const;
|
2013-01-28 18:20:20 +00:00
|
|
|
FileTreeItem * itemFromIndex (const QModelIndex&) const;
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
private:
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeItem * myRootItem;
|
|
|
|
const bool myIsEditable;
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
void clicked (const QModelIndex & index);
|
2013-09-08 19:03:25 +00:00
|
|
|
void doubleClicked (const QModelIndex & index);
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileTreeDelegate: public QItemDelegate
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
Q_OBJECT
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
public:
|
2013-09-14 22:45:04 +00:00
|
|
|
FileTreeDelegate (QObject * parent=0): QItemDelegate(parent) {}
|
|
|
|
virtual ~FileTreeDelegate() {}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-01-25 18:37:11 +00:00
|
|
|
public:
|
|
|
|
virtual QSize sizeHint (const QStyleOptionViewItem&, const QModelIndex&) const;
|
|
|
|
virtual void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileTreeView: public QTreeView
|
|
|
|
{
|
2013-01-25 18:37:11 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2013-01-28 07:01:58 +00:00
|
|
|
FileTreeView (QWidget * parent=0, bool editable=true);
|
2013-01-25 18:37:11 +00:00
|
|
|
virtual ~FileTreeView ();
|
|
|
|
void clear ();
|
2013-01-28 18:20:20 +00:00
|
|
|
void update (const FileList& files, bool updateProperties=true);
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
signals:
|
2013-01-28 18:20:20 +00:00
|
|
|
void priorityChanged (const QSet<int>& fileIndices, int priority);
|
|
|
|
void wantedChanged (const QSet<int>& fileIndices, bool wanted);
|
2013-01-25 18:37:11 +00:00
|
|
|
void pathEdited (const QString& oldpath, const QString& newname);
|
2013-09-08 19:03:25 +00:00
|
|
|
void openRequested (const QString& path);
|
2013-01-25 18:37:11 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool eventFilter (QObject *, QEvent *);
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileTreeModel myModel;
|
|
|
|
QSortFilterProxyModel * myProxy;
|
|
|
|
FileTreeDelegate myDelegate;
|
|
|
|
|
|
|
|
public slots:
|
2013-01-28 18:20:20 +00:00
|
|
|
void onClicked (const QModelIndex& index);
|
2013-09-08 19:03:25 +00:00
|
|
|
void onDoubleClicked (const QModelIndex& index);
|
|
|
|
void onOpenRequested (const QString& path);
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|