forked from tumic0/GPXSee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilebrowser.cpp
90 lines (70 loc) · 1.81 KB
/
filebrowser.cpp
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
90
#include <QFileSystemWatcher>
#include <QDir>
#include "filebrowser.h"
FileBrowser::FileBrowser(QObject *parent) : QObject(parent)
{
_watcher = new QFileSystemWatcher(this);
connect(_watcher, SIGNAL(directoryChanged(const QString &)), this,
SLOT(reloadDirectory(const QString &)));
_index = -1;
}
void FileBrowser::setCurrent(const QString &path)
{
QFileInfo file(path);
QDir dir = file.absoluteDir();
if (_files.isEmpty() || _files.last().canonicalPath()
!= dir.canonicalPath()) {
if (!_watcher->directories().isEmpty())
_watcher->removePaths(_watcher->directories());
_watcher->addPath(dir.canonicalPath());
_files = dir.entryInfoList(_filter, QDir::Files);
}
_index = _files.empty() ? -1 : _files.indexOf(file);
}
void FileBrowser::setFilter(const QStringList &filter)
{
_filter = filter;
if (!_files.isEmpty())
reloadDirectory(_files.last().canonicalPath());
}
bool FileBrowser::isLast() const
{
return (_files.size() > 0 && _index == _files.size() - 1);
}
bool FileBrowser::isFirst() const
{
return (_files.size() > 0 && _index == 0);
}
QString FileBrowser::next()
{
if (_index < 0 || _index == _files.size() - 1)
return QString();
return _files.at(++_index).absoluteFilePath();
}
QString FileBrowser::prev()
{
if (_index <= 0)
return QString();
return _files.at(--_index).absoluteFilePath();
}
QString FileBrowser::last()
{
if (_files.empty())
return QString();
_index = _files.size() - 1;
return _files.last().absoluteFilePath();
}
QString FileBrowser::first()
{
if (_files.empty())
return QString();
_index = 0;
return _files.first().absoluteFilePath();
}
void FileBrowser::reloadDirectory(const QString &path)
{
QDir dir(path);
QFileInfo current = _files.at(_index);
_files = dir.entryInfoList(_filter, QDir::Files);
_index = _files.empty() ? -1 : _files.indexOf(current);
}