source: trunk/qt/file-tree.h @ 14185

Last change on this file since 14185 was 14185, checked in by jordan, 10 years ago

revert quint64/quint32/qint64/qint32 use to inttypes to match libtransmission's API

  • Property svn:keywords set to Date Rev Author Id
File size: 5.3 KB
Line 
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 14185 2013-08-29 00:37:37Z 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
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
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
72  private:
73    void setSubtreePriority (int priority, QSet<int>& fileIds);
74    void setSubtreeWanted (bool, QSet<int>& fileIds);
75    QString priorityString () const;
76    QString sizeString () const;
77    void getSubtreeWantedSize (uint64_t& have, uint64_t& total) const;
78    double progress () const;
79    int priority () const;
80    int isSubtreeWanted () const;
81
82    const int myFileIndex;
83    FileTreeItem * myParent;
84    QList<FileTreeItem*> myChildren;
85    QHash<QString,int> myChildRows;
86    const QHash<QString,int>& getMyChildRows();
87    QString myName;
88    int myPriority;
89    bool myIsWanted;
90    uint64_t myHaveSize;
91    const uint64_t myTotalSize;
92    size_t myFirstUnhashedRow;
93};
94
95class FileTreeModel: public QAbstractItemModel
96{
97    Q_OBJECT
98
99  public:
100    FileTreeModel (QObject *parent = 0, bool isEditable = true);
101    ~FileTreeModel ();
102
103  public:
104    QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const;
105    Qt::ItemFlags flags (const QModelIndex& index) const;
106    QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
107    QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const;
108    QModelIndex parent (const QModelIndex& child) const;
109    QModelIndex parent (const QModelIndex& child, int column) const;
110    int rowCount (const QModelIndex& parent = QModelIndex()) const;
111    int columnCount (const QModelIndex &parent = QModelIndex()) const;
112    virtual bool setData (const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
113
114  signals:
115    void priorityChanged (const QSet<int>& fileIndices, int);
116    void wantedChanged (const QSet<int>& fileIndices, bool);
117    void pathEdited (const QString& oldpath, const QString& newname);
118
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);
126
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;
134
135  private:
136    FileTreeItem * myRootItem;
137    const bool myIsEditable;
138
139  public slots:
140    void clicked (const QModelIndex & index);
141};
142
143class FileTreeDelegate: public QItemDelegate
144{
145    Q_OBJECT
146
147  public:
148    FileTreeDelegate (QObject * parent=0): QItemDelegate(parent) { }
149    virtual ~FileTreeDelegate() { }
150
151  public:
152    virtual QSize sizeHint (const QStyleOptionViewItem&, const QModelIndex&) const;
153    virtual void paint (QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
154};
155
156class FileTreeView: public QTreeView
157{
158    Q_OBJECT
159
160  public:
161    FileTreeView (QWidget * parent=0, bool editable=true);
162    virtual ~FileTreeView ();
163    void clear ();
164    void update (const FileList& files, bool updateProperties=true);
165
166  signals:
167    void priorityChanged (const QSet<int>& fileIndices, int priority);
168    void wantedChanged (const QSet<int>& fileIndices, bool wanted);
169    void pathEdited (const QString& oldpath, const QString& newname);
170
171  protected:
172    bool eventFilter (QObject *, QEvent *);
173
174  private:
175    FileTreeModel myModel;
176    QSortFilterProxyModel * myProxy;
177    FileTreeDelegate myDelegate;
178
179  public slots:
180    void onClicked (const QModelIndex& index);
181};
182
183#endif
Note: See TracBrowser for help on using the repository browser.