1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License version 2 |
---|
6 | * as published by the Free Software Foundation. |
---|
7 | * |
---|
8 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | * |
---|
10 | * $Id: file-tree.h 14196 2013-09-08 19:03:25Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef QTR_FILE_TREE |
---|
14 | #define QTR_FILE_TREE |
---|
15 | |
---|
16 | #include <QAbstractItemModel> |
---|
17 | #include <QObject> |
---|
18 | #include <QItemDelegate> |
---|
19 | #include <QList> |
---|
20 | #include <QHash> |
---|
21 | #include <QSet> |
---|
22 | #include <QSize> |
---|
23 | #include <QString> |
---|
24 | #include <QTreeView> |
---|
25 | #include <QVariant> |
---|
26 | |
---|
27 | class QSortFilterProxyModel; |
---|
28 | class QStyle; |
---|
29 | |
---|
30 | #include "torrent.h" // FileList |
---|
31 | |
---|
32 | /**** |
---|
33 | ***** |
---|
34 | ****/ |
---|
35 | |
---|
36 | class FileTreeItem: public QObject |
---|
37 | { |
---|
38 | Q_OBJECT; |
---|
39 | |
---|
40 | enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) }; |
---|
41 | |
---|
42 | public: |
---|
43 | |
---|
44 | virtual ~FileTreeItem(); |
---|
45 | |
---|
46 | FileTreeItem (const QString& name="", int fileIndex=-1, uint64_t size=0): |
---|
47 | myFileIndex (fileIndex), |
---|
48 | myParent (0), |
---|
49 | myName (name), |
---|
50 | myPriority (0), |
---|
51 | myIsWanted (0), |
---|
52 | myHaveSize (0), |
---|
53 | myTotalSize (size), |
---|
54 | myFirstUnhashedRow (0) { } |
---|
55 | |
---|
56 | public: |
---|
57 | void appendChild (FileTreeItem *child); |
---|
58 | FileTreeItem * child (const QString& filename); |
---|
59 | FileTreeItem * child (int row) { return myChildren.at(row); } |
---|
60 | int childCount () const { return myChildren.size(); } |
---|
61 | FileTreeItem * parent () { return myParent; } |
---|
62 | const FileTreeItem * parent () const { return myParent; } |
---|
63 | int row () const; |
---|
64 | const QString& name () const { return myName; } |
---|
65 | QVariant data (int column, int role) const; |
---|
66 | std::pair<int,int> update (const QString& name, bool want, int priority, uint64_t have, bool updateFields); |
---|
67 | void twiddleWanted (QSet<int>& fileIds, bool&); |
---|
68 | void twiddlePriority (QSet<int>& fileIds, int&); |
---|
69 | int fileIndex () const { return myFileIndex; } |
---|
70 | uint64_t totalSize () const { return myTotalSize; } |
---|
71 | QString path () const; |
---|
72 | bool isComplete () const; |
---|
73 | |
---|
74 | private: |
---|
75 | void setSubtreePriority (int priority, QSet<int>& fileIds); |
---|
76 | void setSubtreeWanted (bool, QSet<int>& fileIds); |
---|
77 | QString priorityString () const; |
---|
78 | QString sizeString () const; |
---|
79 | void getSubtreeWantedSize (uint64_t& have, uint64_t& total) const; |
---|
80 | double progress () const; |
---|
81 | int priority () const; |
---|
82 | int isSubtreeWanted () const; |
---|
83 | |
---|
84 | const int myFileIndex; |
---|
85 | FileTreeItem * myParent; |
---|
86 | QList<FileTreeItem*> myChildren; |
---|
87 | QHash<QString,int> myChildRows; |
---|
88 | const QHash<QString,int>& getMyChildRows(); |
---|
89 | QString myName; |
---|
90 | int myPriority; |
---|
91 | bool myIsWanted; |
---|
92 | uint64_t myHaveSize; |
---|
93 | const uint64_t myTotalSize; |
---|
94 | size_t myFirstUnhashedRow; |
---|
95 | }; |
---|
96 | |
---|
97 | class FileTreeModel: public QAbstractItemModel |
---|
98 | { |
---|
99 | Q_OBJECT |
---|
100 | |
---|
101 | public: |
---|
102 | FileTreeModel (QObject *parent = 0, bool isEditable = true); |
---|
103 | ~FileTreeModel (); |
---|
104 | |
---|
105 | public: |
---|
106 | QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const; |
---|
107 | Qt::ItemFlags flags (const QModelIndex& index) const; |
---|
108 | QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; |
---|
109 | QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const; |
---|
110 | QModelIndex parent (const QModelIndex& child) const; |
---|
111 | QModelIndex parent (const QModelIndex& child, int column) const; |
---|
112 | int rowCount (const QModelIndex& parent = QModelIndex()) const; |
---|
113 | int columnCount (const QModelIndex &parent = QModelIndex()) const; |
---|
114 | virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole); |
---|
115 | |
---|
116 | signals: |
---|
117 | void priorityChanged (const QSet<int>& fileIndices, int); |
---|
118 | void wantedChanged (const QSet<int>& fileIndices, bool); |
---|
119 | void pathEdited (const QString& oldpath, const QString& newname); |
---|
120 | void openRequested (const QString& path); |
---|
121 | |
---|
122 | public: |
---|
123 | void clear (); |
---|
124 | void addFile (int index, const QString& filename, |
---|
125 | bool wanted, int priority, |
---|
126 | uint64_t size, uint64_t have, |
---|
127 | QList<QModelIndex>& rowsAdded, |
---|
128 | bool torrentChanged); |
---|
129 | |
---|
130 | private: |
---|
131 | void clearSubtree (const QModelIndex &); |
---|
132 | QModelIndex indexOf (FileTreeItem *, int column) const; |
---|
133 | void parentsChanged (const QModelIndex &, int column); |
---|
134 | void subtreeChanged (const QModelIndex &, int column); |
---|
135 | FileTreeItem * findItemForFileIndex (int fileIndex) const; |
---|
136 | FileTreeItem * itemFromIndex (const QModelIndex&) const; |
---|
137 | |
---|
138 | private: |
---|
139 | FileTreeItem * myRootItem; |
---|
140 | const bool myIsEditable; |
---|
141 | |
---|
142 | public slots: |
---|
143 | void clicked (const QModelIndex & index); |
---|
144 | void doubleClicked (const QModelIndex & index); |
---|
145 | }; |
---|
146 | |
---|
147 | class FileTreeDelegate: public QItemDelegate |
---|
148 | { |
---|
149 | Q_OBJECT |
---|
150 | |
---|
151 | public: |
---|
152 | FileTreeDelegate (QObject * parent=0): QItemDelegate(parent) { } |
---|
153 | virtual ~FileTreeDelegate() { } |
---|
154 | |
---|
155 | public: |
---|
156 | virtual QSize sizeHint (const QStyleOptionViewItem&, const QModelIndex&) const; |
---|
157 | virtual void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const; |
---|
158 | }; |
---|
159 | |
---|
160 | class FileTreeView: public QTreeView |
---|
161 | { |
---|
162 | Q_OBJECT |
---|
163 | |
---|
164 | public: |
---|
165 | FileTreeView (QWidget * parent=0, bool editable=true); |
---|
166 | virtual ~FileTreeView (); |
---|
167 | void clear (); |
---|
168 | void update (const FileList& files, bool updateProperties=true); |
---|
169 | |
---|
170 | signals: |
---|
171 | void priorityChanged (const QSet<int>& fileIndices, int priority); |
---|
172 | void wantedChanged (const QSet<int>& fileIndices, bool wanted); |
---|
173 | void pathEdited (const QString& oldpath, const QString& newname); |
---|
174 | void openRequested (const QString& path); |
---|
175 | |
---|
176 | protected: |
---|
177 | bool eventFilter (QObject *, QEvent *); |
---|
178 | |
---|
179 | private: |
---|
180 | FileTreeModel myModel; |
---|
181 | QSortFilterProxyModel * myProxy; |
---|
182 | FileTreeDelegate myDelegate; |
---|
183 | |
---|
184 | public slots: |
---|
185 | void onClicked (const QModelIndex& index); |
---|
186 | void onDoubleClicked (const QModelIndex& index); |
---|
187 | void onOpenRequested (const QString& path); |
---|
188 | }; |
---|
189 | |
---|
190 | #endif |
---|