transmission 2.83
[tomato.git] / release / src-rt-6.x.4708 / router / transmission / qt / file-tree.h
blob265a63d5f4ab51b275a06922bd1f0c83287fa0e8
1 /*
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 $
8 */
10 #ifndef QTR_FILE_TREE
11 #define QTR_FILE_TREE
13 #include <QAbstractItemModel>
14 #include <QObject>
15 #include <QItemDelegate>
16 #include <QList>
17 #include <QHash>
18 #include <QSet>
19 #include <QSize>
20 #include <QString>
21 #include <QTreeView>
22 #include <QVariant>
24 class QSortFilterProxyModel;
25 class QStyle;
27 #include "torrent.h" // FileList
29 /****
30 *****
31 ****/
33 class FileTreeItem: public QObject
35 Q_OBJECT;
37 enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) };
39 public:
41 virtual ~FileTreeItem();
43 FileTreeItem (const QString& name="", int fileIndex=-1, uint64_t size=0):
44 myFileIndex (fileIndex),
45 myParent (0),
46 myName (name),
47 myPriority (0),
48 myIsWanted (0),
49 myHaveSize (0),
50 myTotalSize (size),
51 myFirstUnhashedRow (0) {}
53 public:
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; }
60 int row () const;
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;
71 private:
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();
86 QString myName;
87 int myPriority;
88 bool myIsWanted;
89 uint64_t myHaveSize;
90 const uint64_t myTotalSize;
91 size_t myFirstUnhashedRow;
94 class FileTreeModel: public QAbstractItemModel
96 Q_OBJECT
98 public:
99 FileTreeModel (QObject *parent = 0, bool isEditable = true);
100 ~FileTreeModel ();
102 public:
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);
113 signals:
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);
119 public:
120 void clear ();
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);
127 private:
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;
135 private:
136 FileTreeItem * myRootItem;
137 const bool myIsEditable;
139 public slots:
140 void clicked (const QModelIndex & index);
141 void doubleClicked (const QModelIndex & index);
144 class FileTreeDelegate: public QItemDelegate
146 Q_OBJECT
148 public:
149 FileTreeDelegate (QObject * parent=0): QItemDelegate(parent) {}
150 virtual ~FileTreeDelegate() {}
152 public:
153 virtual QSize sizeHint (const QStyleOptionViewItem&, const QModelIndex&) const;
154 virtual void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
157 class FileTreeView: public QTreeView
159 Q_OBJECT
161 public:
162 FileTreeView (QWidget * parent=0, bool editable=true);
163 virtual ~FileTreeView ();
164 void clear ();
165 void update (const FileList& files, bool updateProperties=true);
167 signals:
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);
173 protected:
174 bool eventFilter (QObject *, QEvent *);
176 private:
177 FileTreeModel myModel;
178 QSortFilterProxyModel * myProxy;
179 FileTreeDelegate myDelegate;
181 public slots:
182 void onClicked (const QModelIndex& index);
183 void onDoubleClicked (const QModelIndex& index);
184 void onOpenRequested (const QString& path);
187 #endif