Skip to content

Commit

Permalink
Enabled picking metadata files with custom prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Dec 12, 2020
1 parent 63e6a8c commit 6f801fb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
34 changes: 27 additions & 7 deletions src/backend/utils/FolderListModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


namespace {
void remove_if(QFileInfoList& list, const std::function<bool(const QFileInfo&)>& predicate)
void erase_if(QFileInfoList& list, const std::function<bool(const QFileInfo&)>& predicate)
{
const auto start_it = std::remove_if(list.begin(), list.end(), predicate);
list.erase(start_it, list.end());
Expand Down Expand Up @@ -86,7 +86,7 @@ FolderListModel::FolderListModel(QObject* parent)
{
m_dir.setSorting(QDir::Name | QDir::DirsFirst | QDir::IgnoreCase);
m_dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Readable);
setNameFilters(m_name_filters);
cd(QStringLiteral("."));
}

int FolderListModel::rowCount(const QModelIndex&) const
Expand Down Expand Up @@ -135,8 +135,21 @@ void FolderListModel::cd(const QString& dirName)
m_dir_path = QDir::toNativeSeparators(m_dir.absolutePath());

auto filist = m_dir.entryInfoList();
remove_if(filist, [this](const QFileInfo& fi){
return !fi.isDir() && !m_name_filters.contains(fi.fileName());
erase_if(filist, [this](const QFileInfo& fi){
if (fi.isDir())
return false;

if (m_filenames.contains(fi.fileName()))
return false;

const bool has_matching_ext = std::any_of(
m_extensions.cbegin(),
m_extensions.cend(),
[&fi](const QString& suffix){ return fi.fileName().endsWith(suffix); });
if (has_matching_ext)
return false;

return true;
});

// adding dotdot manually to avoid getting stuck in the file system
Expand All @@ -150,9 +163,16 @@ void FolderListModel::cd(const QString& dirName)
emit folderChanged();
}

void FolderListModel::setNameFilters(QStringList nameFilters)
void FolderListModel::setFilenames(QStringList list)
{
m_filenames = std::move(list);
emit filenamesChanged();
cd(QStringLiteral("."));
}

void FolderListModel::setExtensions(QStringList list)
{
m_name_filters = std::move(nameFilters);
emit nameFiltersChanged();
m_extensions = std::move(list);
emit extensionsChanged();
cd(QStringLiteral("."));
}
21 changes: 14 additions & 7 deletions src/backend/utils/FolderListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ struct FolderListEntry {
class FolderListModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QString folder READ folder NOTIFY folderChanged)
Q_PROPERTY(QStringList nameFilters
READ nameFilters WRITE setNameFilters
NOTIFY nameFiltersChanged)
Q_PROPERTY(QStringList files
READ filenames WRITE setFilenames
NOTIFY filenamesChanged)
Q_PROPERTY(QStringList extensions
READ extensions WRITE setExtensions
NOTIFY extensionsChanged)

public:
explicit FolderListModel(QObject* parent = nullptr);
Expand All @@ -54,18 +57,22 @@ class FolderListModel : public QAbstractListModel {
Q_INVOKABLE void cd(const QString&);

QString folder() const { return m_dir_path; }
const QStringList& nameFilters() const { return m_name_filters; }
void setNameFilters(QStringList);
const QStringList& filenames() const { return m_filenames; }
const QStringList& extensions() const { return m_extensions; }
void setFilenames(QStringList);
void setExtensions(QStringList);

signals:
void folderChanged();
void nameFiltersChanged();
void filenamesChanged();
void extensionsChanged();

private:
QDir m_dir;
QString m_dir_path;
std::vector<FolderListEntry> m_files;
QStringList m_name_filters;
QStringList m_filenames;
QStringList m_extensions;

const std::vector<QString> m_drives_cache;
const QHash<int, QByteArray> m_role_names;
Expand Down
10 changes: 6 additions & 4 deletions src/frontend/menu/settings/gamedireditor/FilePicker.qml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ FocusScope {

FolderListModel {
id: folderModel
nameFilters: [
"collections.pegasus.txt",
"collections.txt",
files: [
"metadata.pegasus.txt",
"metadata.txt",
]
extensions: [
".metadata.pegasus.txt",
".metadata.txt",
]
}


Expand Down Expand Up @@ -150,7 +152,7 @@ FocusScope {
readonly property bool highlighted: ListView.isCurrentItem || mouseArea.containsMouse

color: highlighted ? "#566" : "transparent"
width: parent.width
width: ListView.view.width
height: label.height

function pickItem() {
Expand Down

0 comments on commit 6f801fb

Please sign in to comment.