Use the canonical file name also for the "already open" check
[GPXSee.git] / src / GUI / filebrowser.cpp
blob5946a9e50811beb15e05187d5f3e0c5fc9821a85
1 #include <QFileSystemWatcher>
2 #include <QDir>
3 #include "filebrowser.h"
6 FileBrowser::FileBrowser(QObject *parent) : QObject(parent)
8 #ifndef Q_OS_ANDROID
9 _watcher = new QFileSystemWatcher(this);
10 connect(_watcher, &QFileSystemWatcher::directoryChanged, this,
11 &FileBrowser::reloadDirectory);
12 #endif // Q_OS_ANDROID
14 _index = -1;
17 #ifdef Q_OS_ANDROID
18 void FileBrowser::setCurrentDir(const QString &path)
20 QDir dir(path);
21 _files = dir.entryInfoList(_filter, QDir::Files);
22 _index = _files.empty() ? -1 : 0;
24 emit listChanged();
26 #else // Q_OS_ANDROID
27 void FileBrowser::setCurrent(const QString &path)
29 QFileInfo file(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)
46 _filter = 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)
69 return QString();
71 return _files.at(++_index).absoluteFilePath();
74 QString FileBrowser::prev()
76 if (_index <= 0)
77 return QString();
79 return _files.at(--_index).absoluteFilePath();
82 QString FileBrowser::last()
84 if (_files.empty())
85 return QString();
87 _index = _files.size() - 1;
88 return _files.last().absoluteFilePath();
91 QString FileBrowser::first()
93 if (_files.empty())
94 return QString();
96 _index = 0;
97 return _files.first().absoluteFilePath();
100 void FileBrowser::reloadDirectory(const QString &path)
102 QDir dir(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);
108 emit listChanged();