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 11092 2010-08-01 20:36:13Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef QTR_TREE_FILE_MODEL |
---|
14 | #define QTR_TREE_FILE_MODEL |
---|
15 | |
---|
16 | #include <QAbstractItemModel> |
---|
17 | #include <QObject> |
---|
18 | #include <QItemDelegate> |
---|
19 | #include <QList> |
---|
20 | #include <QSet> |
---|
21 | #include <QSize> |
---|
22 | #include <QString> |
---|
23 | #include <QTreeView> |
---|
24 | #include <QVariant> |
---|
25 | |
---|
26 | class QSortFilterProxyModel; |
---|
27 | class QStyle; |
---|
28 | |
---|
29 | #include "torrent.h" // FileList |
---|
30 | |
---|
31 | /**** |
---|
32 | ***** |
---|
33 | ****/ |
---|
34 | |
---|
35 | class FileTreeItem: public QObject |
---|
36 | { |
---|
37 | Q_OBJECT; |
---|
38 | |
---|
39 | enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) }; |
---|
40 | |
---|
41 | public: |
---|
42 | virtual ~FileTreeItem( ); |
---|
43 | FileTreeItem( int fileIndex, const QString& name="" ): |
---|
44 | myIndex(fileIndex), myParent(0), myName(name), |
---|
45 | myPriority(0), myIsWanted(0), |
---|
46 | myHaveSize(0), myTotalSize(0) { } |
---|
47 | |
---|
48 | public: |
---|
49 | void appendChild( FileTreeItem *child ); |
---|
50 | FileTreeItem * child( const QString& filename ); |
---|
51 | FileTreeItem * child( int row ) { return myChildren.at( row ); } |
---|
52 | int childCount( ) const { return myChildren.size( ); } |
---|
53 | FileTreeItem * parent( ) { return myParent; } |
---|
54 | const FileTreeItem * parent( ) const { return myParent; } |
---|
55 | int row( ) const; |
---|
56 | const QString& name( ) const { return myName; } |
---|
57 | QVariant data( int column ) const; |
---|
58 | bool update( int index, bool want, int priority, uint64_t total, uint64_t have, bool torrentChanged ); |
---|
59 | void twiddleWanted( QSet<int>& fileIds, bool& ); |
---|
60 | void twiddlePriority( QSet<int>& fileIds, int& ); |
---|
61 | |
---|
62 | private: |
---|
63 | void setSubtreePriority( int priority, QSet<int>& fileIds ); |
---|
64 | void setSubtreeWanted( bool, QSet<int>& fileIds ); |
---|
65 | QString priorityString( ) const; |
---|
66 | void getSubtreeSize( uint64_t& have, uint64_t& total ) const; |
---|
67 | QString fileSizeName( ) const; |
---|
68 | double progress( ) const; |
---|
69 | int priority( ) const; |
---|
70 | int isSubtreeWanted( ) const; |
---|
71 | |
---|
72 | int myIndex; |
---|
73 | FileTreeItem * myParent; |
---|
74 | QList<FileTreeItem*> myChildren; |
---|
75 | const QString myName; |
---|
76 | int myPriority; |
---|
77 | bool myIsWanted; |
---|
78 | uint64_t myHaveSize; |
---|
79 | uint64_t myTotalSize; |
---|
80 | }; |
---|
81 | |
---|
82 | class FileTreeModel: public QAbstractItemModel |
---|
83 | { |
---|
84 | Q_OBJECT |
---|
85 | |
---|
86 | public: |
---|
87 | FileTreeModel( QObject *parent = 0); |
---|
88 | ~FileTreeModel( ); |
---|
89 | |
---|
90 | public: |
---|
91 | QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; |
---|
92 | Qt::ItemFlags flags( const QModelIndex& index ) const; |
---|
93 | QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; |
---|
94 | QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const; |
---|
95 | QModelIndex parent( const QModelIndex& child ) const; |
---|
96 | QModelIndex parent( const QModelIndex& child, int column ) const; |
---|
97 | int rowCount( const QModelIndex& parent = QModelIndex( ) ) const; |
---|
98 | int columnCount( const QModelIndex &parent = QModelIndex( ) ) const; |
---|
99 | |
---|
100 | signals: |
---|
101 | void priorityChanged( const QSet<int>& fileIndices, int ); |
---|
102 | void wantedChanged( const QSet<int>& fileIndices, bool ); |
---|
103 | |
---|
104 | public: |
---|
105 | void clear( ); |
---|
106 | void addFile( int index, const QString& filename, |
---|
107 | bool wanted, int priority, |
---|
108 | uint64_t size, uint64_t have, |
---|
109 | QList<QModelIndex>& rowsAdded, |
---|
110 | bool torrentChanged ); |
---|
111 | |
---|
112 | private: |
---|
113 | void clearSubtree( const QModelIndex & ); |
---|
114 | QModelIndex indexOf( FileTreeItem *, int column ) const; |
---|
115 | void parentsChanged( const QModelIndex &, int column ); |
---|
116 | void subtreeChanged( const QModelIndex &, int column ); |
---|
117 | |
---|
118 | private: |
---|
119 | FileTreeItem * rootItem; |
---|
120 | |
---|
121 | public slots: |
---|
122 | void clicked ( const QModelIndex & index ); |
---|
123 | }; |
---|
124 | |
---|
125 | class FileTreeDelegate: public QItemDelegate |
---|
126 | { |
---|
127 | Q_OBJECT |
---|
128 | |
---|
129 | public: |
---|
130 | FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { } |
---|
131 | virtual ~FileTreeDelegate( ) { } |
---|
132 | |
---|
133 | public: |
---|
134 | virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const; |
---|
135 | virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const; |
---|
136 | }; |
---|
137 | |
---|
138 | class FileTreeView: public QTreeView |
---|
139 | { |
---|
140 | Q_OBJECT |
---|
141 | |
---|
142 | public: |
---|
143 | FileTreeView( QWidget * parent=0 ); |
---|
144 | virtual ~FileTreeView( ); |
---|
145 | void clear( ); |
---|
146 | void update( const FileList& files ); |
---|
147 | void update( const FileList& files, bool torrentChanged ); |
---|
148 | |
---|
149 | signals: |
---|
150 | void priorityChanged( const QSet<int>& fileIndices, int ); |
---|
151 | void wantedChanged( const QSet<int>& fileIndices, bool ); |
---|
152 | |
---|
153 | protected: |
---|
154 | bool eventFilter( QObject *, QEvent * ); |
---|
155 | |
---|
156 | private: |
---|
157 | FileTreeModel myModel; |
---|
158 | QSortFilterProxyModel * myProxy; |
---|
159 | FileTreeDelegate myDelegate; |
---|
160 | |
---|
161 | public slots: |
---|
162 | void onClicked ( const QModelIndex & index ); |
---|
163 | }; |
---|
164 | |
---|
165 | #endif |
---|