1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 23:03:22 +02:00
GPXSee/src/GUI/filebrowser.cpp

110 lines
2.2 KiB
C++
Raw Normal View History

2015-10-20 22:18:41 +02:00
#include <QFileSystemWatcher>
#include <QDir>
#include "filebrowser.h"
FileBrowser::FileBrowser(QObject *parent) : QObject(parent)
{
2022-05-28 14:05:14 +02:00
#ifndef Q_OS_ANDROID
2015-10-20 22:18:41 +02:00
_watcher = new QFileSystemWatcher(this);
2021-04-28 00:01:07 +02:00
connect(_watcher, &QFileSystemWatcher::directoryChanged, this,
&FileBrowser::reloadDirectory);
2022-05-28 14:05:14 +02:00
#endif // Q_OS_ANDROID
2015-10-20 22:18:41 +02:00
_index = -1;
}
2022-05-28 14:05:14 +02:00
#ifdef Q_OS_ANDROID
void FileBrowser::setCurrentDir(const QString &path)
{
QDir dir(path);
_files = dir.entryInfoList(_filter, QDir::Files);
_index = _files.empty() ? -1 : 0;
emit listChanged();
}
#else // Q_OS_ANDROID
2015-10-20 22:18:41 +02:00
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);
}
2022-05-28 14:05:14 +02:00
#endif // Q_OS_ANDROID
2015-10-20 22:18:41 +02:00
void FileBrowser::setFilter(const QStringList &filter)
{
_filter = filter;
if (!_files.isEmpty())
reloadDirectory(_files.last().canonicalPath());
}
2016-03-19 17:24:53 +01:00
bool FileBrowser::isLast() const
2015-12-18 22:21:11 +01:00
{
return (_files.size() > 0 && _index == _files.size() - 1);
}
2016-03-19 17:24:53 +01:00
bool FileBrowser::isFirst() const
2015-12-18 22:21:11 +01:00
{
return (_files.size() > 0 && _index == 0);
}
2022-05-28 14:05:14 +02:00
QString FileBrowser::current()
{
return (_index >= 0) ? _files.at(_index).absoluteFilePath() : QString();
}
2015-10-20 22:18:41 +02:00
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();
}
2015-12-18 22:21:11 +01:00
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();
}
2015-10-20 22:18:41 +02:00
void FileBrowser::reloadDirectory(const QString &path)
{
QDir dir(path);
QFileInfo current = (_index >= 0) ? _files.at(_index) : QFileInfo();
2015-10-20 22:18:41 +02:00
_files = dir.entryInfoList(_filter, QDir::Files);
_index = _files.empty() ? -1 : _files.indexOf(current);
emit listChanged();
2015-10-20 22:18:41 +02:00
}