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