forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemodel.h
89 lines (76 loc) · 2.85 KB
/
filemodel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef FILEMODEL_H
#define FILEMODEL_H
#include <QAbstractListModel>
#include <QDir>
#include <QFileSystemWatcher>
#include "statfileinfo.h"
/**
* @brief The FileModel class can be used as a model in a ListView to display a list of files
* in the current directory. It has methods to change the current directory and to access
* file info.
* It also actively monitors the directory. If the directory changes, then the model is
* updated automatically if active is true. If active is false, then the directory is
* updated when active becomes true.
*
* Original code by karip in filebrowser for Sailfish OS
*/
class FileModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString dir READ dir() WRITE setDir(QString) NOTIFY dirChanged())
Q_PROPERTY(int fileCount READ fileCount() NOTIFY fileCountChanged())
Q_PROPERTY(QString errorMessage READ errorMessage() NOTIFY errorMessageChanged())
Q_PROPERTY(bool active READ active() WRITE setActive(bool) NOTIFY activeChanged())
Q_PROPERTY(int selectedFileCount READ selectedFileCount() NOTIFY selectedFileCountChanged())
public:
explicit FileModel(QObject *parent = 0);
~FileModel();
// methods needed by ListView
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
// property accessors
QString dir() const { return m_dir; }
void setDir(QString dir);
int fileCount() const;
QString errorMessage() const;
bool active() const { return m_active; }
void setActive(bool active);
int selectedFileCount() const { return m_selectedFileCount; }
// methods accessible from QML
Q_INVOKABLE QString appendPath(QString dirName);
Q_INVOKABLE QString parentPath();
Q_INVOKABLE QString fileNameAt(int fileIndex);
// file selection
Q_INVOKABLE void toggleSelectedFile(int fileIndex);
Q_INVOKABLE void clearSelectedFiles();
Q_INVOKABLE void selectAllFiles();
Q_INVOKABLE QStringList selectedFiles() const;
public slots:
// reads the directory and inserts/removes model items as needed
Q_INVOKABLE void refresh();
// reads the directory and sets all model items
Q_INVOKABLE void refreshFull();
signals:
void dirChanged();
void fileCountChanged();
void errorMessageChanged();
void activeChanged();
void selectedFileCountChanged();
private slots:
void readDirectory();
private:
void recountSelectedFiles();
void readAllEntries();
void refreshEntries();
void clearModel();
bool filesContains(const QList<StatFileInfo> &files, const StatFileInfo &fileData) const;
QString m_dir;
QList<StatFileInfo> m_files;
int m_selectedFileCount;
QString m_errorMessage;
bool m_active;
bool m_dirty;
QFileSystemWatcher *m_watcher;
};
#endif // FIELMODEL_H