~hendrik-grewe/transmission/private-patch

« back to all changes in this revision

Viewing changes to qt/file-tree.h

  • Committer: charles
  • Date: 2009-04-09 17:55:47 UTC
  • Revision ID: svn-v4:f4695dd4-2c0a-0410-b89c-da849a56a58e:trunk:8188
(trunk) add the Qt beta into svn 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
 
3
 *
 
4
 * This file is licensed by the GPL version 2.  Works owned by the
 
5
 * Transmission project are granted a special exemption to clause 2(b)
 
6
 * so that the bulk of its code can remain under the MIT license.
 
7
 * This exemption does not extend to derived works not owned by
 
8
 * the Transmission project.
 
9
 *
 
10
 * $Id:$
 
11
 */
 
12
 
 
13
#ifndef QTR_TREE_FILE_MODEL
 
14
#define QTR_TREE_FILE_MODEL
 
15
 
 
16
#include <QAbstractItemModel>
 
17
#include <QObject>
 
18
#include <QList>
 
19
#include <QString>
 
20
#include <QSet>
 
21
#include <QTreeView>
 
22
#include <QVariant>
 
23
#include <QItemDelegate>
 
24
 
 
25
#include "torrent.h" // FileList
 
26
 
 
27
/****
 
28
*****
 
29
****/
 
30
 
 
31
class FileTreeItem: public QObject
 
32
{
 
33
        Q_OBJECT;
 
34
 
 
35
        enum { LOW=(1<<0), NORMAL=(1<<1), HIGH=(1<<2) };
 
36
 
 
37
    public:
 
38
        virtual ~FileTreeItem( );
 
39
        FileTreeItem( int fileIndex, const QString& name="" ):
 
40
            myIndex(fileIndex), myParent(0), myName(name),
 
41
            myPriority(0), myIsWanted(0),
 
42
            myHaveSize(0), myTotalSize(0) { }
 
43
 
 
44
    public:
 
45
        void appendChild( FileTreeItem *child );
 
46
        FileTreeItem * child( const QString& filename );
 
47
        FileTreeItem * child( int row ) { return myChildren.at( row ); }
 
48
        int childCount( ) const { return myChildren.size( ); }
 
49
        FileTreeItem * parent( ) { return myParent; }
 
50
        const FileTreeItem * parent( ) const { return myParent; }
 
51
        int row( ) const;
 
52
        const QString& name( ) const { return myName; }
 
53
        QVariant data( int column ) const;
 
54
        bool update( int index, bool want, int priority, uint64_t total, uint64_t have );
 
55
        void twiddleWanted( QSet<int>& fileIds, bool& );
 
56
        void twiddlePriority( QSet<int>& fileIds, int& );
 
57
 
 
58
    private:
 
59
        void setSubtreePriority( int priority, QSet<int>& fileIds );
 
60
        void setSubtreeWanted( bool, QSet<int>& fileIds );
 
61
        QString priorityString( ) const;
 
62
        void getSubtreeSize( uint64_t& have, uint64_t& total ) const;
 
63
        double progress( ) const;
 
64
        int priority( ) const;
 
65
        int isSubtreeWanted( ) const;
 
66
 
 
67
        int myIndex;
 
68
        FileTreeItem * myParent;
 
69
        QList<FileTreeItem*> myChildren;
 
70
        const QString myName;
 
71
        int myPriority;
 
72
        bool myIsWanted;
 
73
        uint64_t myHaveSize;
 
74
        uint64_t myTotalSize;
 
75
};
 
76
 
 
77
class FileTreeModel: public QAbstractItemModel
 
78
{
 
79
        Q_OBJECT
 
80
 
 
81
    public:
 
82
        FileTreeModel( QObject *parent = 0);
 
83
        ~FileTreeModel( );
 
84
 
 
85
    public:
 
86
        QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
 
87
        Qt::ItemFlags flags( const QModelIndex& index ) const;
 
88
        QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
 
89
        QModelIndex index( int row, int column, const QModelIndex& parent = QModelIndex() ) const;
 
90
        QModelIndex parent( const QModelIndex& child ) const;
 
91
        QModelIndex parent( const QModelIndex& child, int column ) const;
 
92
        int rowCount( const QModelIndex& parent = QModelIndex( ) ) const;
 
93
        int columnCount( const QModelIndex &parent = QModelIndex( ) ) const;
 
94
 
 
95
    signals:
 
96
        void priorityChanged( const QSet<int>& fileIndices, int );
 
97
        void wantedChanged( const QSet<int>& fileIndices, bool );
 
98
 
 
99
    public:
 
100
        void clear( );
 
101
        void addFile( int index, const QString& filename,
 
102
                      bool wanted, int priority,
 
103
                      uint64_t size, uint64_t have,
 
104
                      QList<QModelIndex>& rowsAdded );
 
105
 
 
106
    private:
 
107
        void clearSubtree( const QModelIndex & );
 
108
        QModelIndex indexOf( FileTreeItem *, int column ) const;
 
109
        void parentsChanged( const QModelIndex &, int column );
 
110
        void subtreeChanged( const QModelIndex &, int column );
 
111
 
 
112
    private:
 
113
        FileTreeItem * rootItem;
 
114
 
 
115
    public slots:
 
116
        void clicked ( const QModelIndex & index );
 
117
};
 
118
 
 
119
class FileTreeDelegate: public QItemDelegate
 
120
{
 
121
        Q_OBJECT
 
122
 
 
123
    public:
 
124
 
 
125
        FileTreeDelegate( QObject * parent=0 ): QItemDelegate( parent ) { }
 
126
        virtual ~FileTreeDelegate( ) { }
 
127
 
 
128
        void paint( QPainter                   * painter,
 
129
                    const QStyleOptionViewItem & option,
 
130
                    const QModelIndex          & index ) const;
 
131
};
 
132
 
 
133
class FileTreeView: public QTreeView
 
134
{
 
135
        Q_OBJECT
 
136
 
 
137
    public:
 
138
        FileTreeView( QWidget * parent=0 );
 
139
        virtual ~FileTreeView( ) { }
 
140
        void clear( );
 
141
        void update( const FileList& files );
 
142
 
 
143
    signals:
 
144
        void priorityChanged( const QSet<int>& fileIndices, int );
 
145
        void wantedChanged( const QSet<int>& fileIndices, bool );
 
146
 
 
147
    protected:
 
148
        bool eventFilter( QObject *, QEvent * );
 
149
 
 
150
    private:
 
151
        FileTreeModel myModel;
 
152
        FileTreeDelegate myDelegate;
 
153
        
 
154
};
 
155
 
 
156
#endif