2 * This file Copyright (C) 2009-2014 Mnemosyne LLC
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
7 * $Id: file-tree.h 14241 2014-01-21 03:10:30Z jordan $
13 #include <QAbstractItemModel>
15 #include <QItemDelegate>
24 class QSortFilterProxyModel
;
27 #include "torrent.h" // FileList
33 class FileTreeItem
: public QObject
37 enum { LOW
=(1<<0), NORMAL
=(1<<1), HIGH
=(1<<2) };
41 virtual ~FileTreeItem();
43 FileTreeItem (const QString
& name
="", int fileIndex
=-1, uint64_t size
=0):
44 myFileIndex (fileIndex
),
51 myFirstUnhashedRow (0) {}
54 void appendChild (FileTreeItem
*child
);
55 FileTreeItem
* child (const QString
& filename
);
56 FileTreeItem
* child (int row
) { return myChildren
.at(row
); }
57 int childCount () const { return myChildren
.size(); }
58 FileTreeItem
* parent () { return myParent
; }
59 const FileTreeItem
* parent () const { return myParent
; }
61 const QString
& name () const { return myName
; }
62 QVariant
data (int column
, int role
) const;
63 std::pair
<int,int> update (const QString
& name
, bool want
, int priority
, uint64_t have
, bool updateFields
);
64 void twiddleWanted (QSet
<int>& fileIds
, bool&);
65 void twiddlePriority (QSet
<int>& fileIds
, int&);
66 int fileIndex () const { return myFileIndex
; }
67 uint64_t totalSize () const { return myTotalSize
; }
68 QString
path () const;
69 bool isComplete () const;
72 void setSubtreePriority (int priority
, QSet
<int>& fileIds
);
73 void setSubtreeWanted (bool, QSet
<int>& fileIds
);
74 QString
priorityString () const;
75 QString
sizeString () const;
76 void getSubtreeWantedSize (uint64_t& have
, uint64_t& total
) const;
77 double progress () const;
78 int priority () const;
79 int isSubtreeWanted () const;
81 const int myFileIndex
;
82 FileTreeItem
* myParent
;
83 QList
<FileTreeItem
*> myChildren
;
84 QHash
<QString
,int> myChildRows
;
85 const QHash
<QString
,int>& getMyChildRows();
90 const uint64_t myTotalSize
;
91 size_t myFirstUnhashedRow
;
94 class FileTreeModel
: public QAbstractItemModel
99 FileTreeModel (QObject
*parent
= 0, bool isEditable
= true);
103 QVariant
data (const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
104 Qt::ItemFlags
flags (const QModelIndex
& index
) const;
105 QVariant
headerData (int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const;
106 QModelIndex
index (int row
, int column
, const QModelIndex
& parent
= QModelIndex()) const;
107 QModelIndex
parent (const QModelIndex
& child
) const;
108 QModelIndex
parent (const QModelIndex
& child
, int column
) const;
109 int rowCount (const QModelIndex
& parent
= QModelIndex()) const;
110 int columnCount (const QModelIndex
&parent
= QModelIndex()) const;
111 virtual bool setData (const QModelIndex
& index
, const QVariant
& value
, int role
= Qt::EditRole
);
114 void priorityChanged (const QSet
<int>& fileIndices
, int);
115 void wantedChanged (const QSet
<int>& fileIndices
, bool);
116 void pathEdited (const QString
& oldpath
, const QString
& newname
);
117 void openRequested (const QString
& path
);
121 void addFile (int index
, const QString
& filename
,
122 bool wanted
, int priority
,
123 uint64_t size
, uint64_t have
,
124 QList
<QModelIndex
>& rowsAdded
,
125 bool torrentChanged
);
128 void clearSubtree (const QModelIndex
&);
129 QModelIndex
indexOf (FileTreeItem
*, int column
) const;
130 void parentsChanged (const QModelIndex
&, int column
);
131 void subtreeChanged (const QModelIndex
&, int column
);
132 FileTreeItem
* findItemForFileIndex (int fileIndex
) const;
133 FileTreeItem
* itemFromIndex (const QModelIndex
&) const;
136 FileTreeItem
* myRootItem
;
137 const bool myIsEditable
;
140 void clicked (const QModelIndex
& index
);
141 void doubleClicked (const QModelIndex
& index
);
144 class FileTreeDelegate
: public QItemDelegate
149 FileTreeDelegate (QObject
* parent
=0): QItemDelegate(parent
) {}
150 virtual ~FileTreeDelegate() {}
153 virtual QSize
sizeHint (const QStyleOptionViewItem
&, const QModelIndex
&) const;
154 virtual void paint (QPainter
*, const QStyleOptionViewItem
&, const QModelIndex
&) const;
157 class FileTreeView
: public QTreeView
162 FileTreeView (QWidget
* parent
=0, bool editable
=true);
163 virtual ~FileTreeView ();
165 void update (const FileList
& files
, bool updateProperties
=true);
168 void priorityChanged (const QSet
<int>& fileIndices
, int priority
);
169 void wantedChanged (const QSet
<int>& fileIndices
, bool wanted
);
170 void pathEdited (const QString
& oldpath
, const QString
& newname
);
171 void openRequested (const QString
& path
);
174 bool eventFilter (QObject
*, QEvent
*);
177 FileTreeModel myModel
;
178 QSortFilterProxyModel
* myProxy
;
179 FileTreeDelegate myDelegate
;
182 void onClicked (const QModelIndex
& index
);
183 void onDoubleClicked (const QModelIndex
& index
);
184 void onOpenRequested (const QString
& path
);