4 #include <QAbstractListModel>
6 #include <QFileSystemWatcher>
8 // struct to hold data for a single file
13 bool operator==(const FileData
&other
) const {
14 return other
.info
== info
;
19 * @brief The FileModel class can be used as a model in a ListView to display a list of files
20 * in the current directory. It has methods to change the current directory and to access
22 * It also actively monitors the directory. If the directory changes, then the model is
23 * updated automatically if active is true. If active is false, then the directory is
24 * updated when active becomes true.
26 class FileModel
: public QAbstractListModel
29 Q_PROPERTY(QString dir READ
dir() WRITE
setDir(QString
) NOTIFY
dirChanged())
30 Q_PROPERTY(int fileCount READ
fileCount() NOTIFY
fileCountChanged())
31 Q_PROPERTY(QString errorMessage READ
errorMessage() NOTIFY
errorMessageChanged())
32 Q_PROPERTY(bool active READ
active() WRITE
setActive() NOTIFY
activeChanged())
33 Q_PROPERTY(bool showAll READ
showAll() WRITE
setShowAll() NOTIFY
showAllChanged())
36 explicit FileModel(QObject
*parent
= 0);
39 // methods needed by ListView
40 int rowCount(const QModelIndex
&parent
= QModelIndex()) const;
41 QVariant
data(const QModelIndex
&index
, int role
= Qt::DisplayRole
) const;
42 QHash
<int, QByteArray
> roleNames() const;
45 QString
dir() const { return m_dir
; }
46 void setDir(QString dir
);
47 int fileCount() const;
48 QString
errorMessage() const;
49 bool active() const { return m_active
; }
50 void setActive(bool active
);
51 bool showAll() const { return m_showAll
; }
52 void setShowAll(bool showAll
);
54 // methods accessible from QML
55 Q_INVOKABLE QString
appendPath(QString dirName
);
56 Q_INVOKABLE QString
parentPath();
57 Q_INVOKABLE QString
fileNameAt(int fileIndex
);
60 // reads the directory and inserts/removes model items as needed
61 Q_INVOKABLE
void refresh();
62 // reads the directory and sets all model items
63 Q_INVOKABLE
void refreshFull();
67 void fileCountChanged();
68 void errorMessageChanged();
70 void showAllChanged();
77 void refreshEntries();
80 QList
<FileData
> m_files
;
81 QString m_errorMessage
;
84 QFileSystemWatcher
*m_watcher
;