1 #include <QFileSystemWatcher>
3 #include "filebrowser.h"
6 FileBrowser::FileBrowser(QObject
*parent
) : QObject(parent
)
9 _watcher
= new QFileSystemWatcher(this);
10 connect(_watcher
, &QFileSystemWatcher::directoryChanged
, this,
11 &FileBrowser::reloadDirectory
);
12 #endif // Q_OS_ANDROID
18 void FileBrowser::setCurrentDir(const QString
&path
)
21 _files
= dir
.entryInfoList(_filter
, QDir::Files
);
22 _index
= _files
.empty() ? -1 : 0;
27 void FileBrowser::setCurrent(const QString
&path
)
30 QDir dir
= file
.absoluteDir();
32 if (_files
.isEmpty() || _files
.last().canonicalPath()
33 != dir
.canonicalPath()) {
34 if (!_watcher
->directories().isEmpty())
35 _watcher
->removePaths(_watcher
->directories());
36 _watcher
->addPath(dir
.canonicalPath());
37 _files
= dir
.entryInfoList(_filter
, QDir::Files
);
40 _index
= _files
.empty() ? -1 : _files
.indexOf(file
);
42 #endif // Q_OS_ANDROID
44 void FileBrowser::setFilter(const QStringList
&filter
)
47 if (!_files
.isEmpty())
48 reloadDirectory(_files
.last().canonicalPath());
51 bool FileBrowser::isLast() const
53 return (_files
.size() > 0 && _index
== _files
.size() - 1);
56 bool FileBrowser::isFirst() const
58 return (_files
.size() > 0 && _index
== 0);
61 QString
FileBrowser::current()
63 return (_index
>= 0) ? _files
.at(_index
).absoluteFilePath() : QString();
66 QString
FileBrowser::next()
68 if (_index
< 0 || _index
== _files
.size() - 1)
71 return _files
.at(++_index
).absoluteFilePath();
74 QString
FileBrowser::prev()
79 return _files
.at(--_index
).absoluteFilePath();
82 QString
FileBrowser::last()
87 _index
= _files
.size() - 1;
88 return _files
.last().absoluteFilePath();
91 QString
FileBrowser::first()
97 return _files
.first().absoluteFilePath();
100 void FileBrowser::reloadDirectory(const QString
&path
)
103 QFileInfo current
= (_index
>= 0) ? _files
.at(_index
) : QFileInfo();
105 _files
= dir
.entryInfoList(_filter
, QDir::Files
);
106 _index
= _files
.empty() ? -1 : _files
.indexOf(current
);