Skip to content

Commit

Permalink
[MainPage] Fixup filters
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Jan 1, 2025
1 parent f05f3a6 commit 7f90b9a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 27 deletions.
59 changes: 51 additions & 8 deletions src/plugin/gallerymodel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Chupligin Sergey <[email protected]>
* Copyright (C) 2024-2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand All @@ -26,7 +26,8 @@ GalleryModel::GalleryModel(QObject* parent)
: QAbstractListModel { parent }
, m_loading(false)
, m_error(false)
, m_filter(FilterMode::All)
, m_filter(FilterMode::AllFiles)
, m_sortMode(SortMode::SortByTime)
, m_fileSystemWatcher(new QFileSystemWatcher)
{
m_hash.insert(Qt::UserRole, QByteArray("url"));
Expand Down Expand Up @@ -84,6 +85,7 @@ void GalleryModel::setFilter(FilterMode newFilter)
if (m_filter == newFilter)
return;
m_filter = newFilter;

formatMimeTypes();
emit filterChanged();
}
Expand Down Expand Up @@ -120,16 +122,35 @@ void GalleryModel::onUrlsChanged()

void GalleryModel::formatFileList()
{
beginResetModel();
QMimeDatabase db;

if (m_urls.empty()) {
addPath();
}

m_files.clear();

foreach (const QString& dirString, m_urls) {
QDir dir(dirString);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
dir.setSorting(QDir::Time | QDir::Reversed);
switch (m_sortMode) {
case SortByName:
dir.setSorting(QDir::Name);
break;
case SortByTime:
dir.setSorting(QDir::Time);
break;
case SortBySize:
dir.setSorting(QDir::Size);
break;
case SortByType:
dir.setSorting(QDir::Type);
break;
default:
dir.setSorting(QDir::Unsorted);
break;
}

QFileInfoList filelistinfo = dir.entryInfoList();
foreach (const QFileInfo& fileinfo, filelistinfo) {
Expand All @@ -138,6 +159,7 @@ void GalleryModel::formatFileList()
}
}
}
endResetModel();
}

void GalleryModel::onFileSystemChanged(QString path)
Expand All @@ -150,17 +172,38 @@ void GalleryModel::formatMimeTypes()
QMimeDatabase db;
QList<QMimeType> mimeList = db.allMimeTypes();

m_mimeTypes.clear();
m_mimeTypes << "inode/directory";

for (const QMimeType& mime : std::as_const(mimeList)) {
if (m_filter == FilterMode::All) {
if (m_filter == FilterMode::AllFiles) {
if (mime.name().startsWith(QStringLiteral("image/")) || mime.name().startsWith(QStringLiteral("video/"))) {
m_mimeTypes << mime.name();
}
} else if (m_filter == FilterMode::Images && mime.name().startsWith(QStringLiteral("image/"))) {
m_mimeTypes << mime.name();
} else if (m_filter == FilterMode::Video && mime.name().startsWith(QStringLiteral("video/"))) {
m_mimeTypes << mime.name();
} else if (m_filter == FilterMode::OnlyImages) {
if (mime.name().startsWith(QStringLiteral("image/"))) {
m_mimeTypes << mime.name();
}
} else if (m_filter == FilterMode::OnlyVideo) {
if (mime.name().startsWith(QStringLiteral("video/"))) {
m_mimeTypes << mime.name();
}
}
}
formatFileList();
}

GalleryModel::SortMode GalleryModel::sortMode() const
{
return m_sortMode;
}

void GalleryModel::setSortMode(const GalleryModel::SortMode& newSort)
{
if (m_sortMode == newSort)
return;
m_sortMode = newSort;
emit sortModeChanged();

formatFileList();
}
27 changes: 21 additions & 6 deletions src/plugin/gallerymodel.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Chupligin Sergey <[email protected]>
* Copyright (C) 2024-2025 Chupligin Sergey <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
Expand All @@ -25,17 +25,28 @@

class GalleryModel : public QAbstractListModel {
Q_OBJECT

Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged FINAL)
Q_PROPERTY(bool error READ error NOTIFY errorChanged FINAL)
Q_PROPERTY(GalleryModel::FilterMode filter READ filter WRITE setFilter NOTIFY filterChanged FINAL)
Q_PROPERTY(GalleryModel::SortMode sortMode READ sortMode WRITE setSortMode NOTIFY sortModeChanged FINAL)

public:
enum FilterMode {
All,
Images,
Video
AllFiles,
OnlyImages,
OnlyVideo
};

enum SortMode {
SortByName = 0,
SortByTime,
SortBySize,
SortByType,
Unsorted = 255
};
Q_ENUMS(Filter)
Q_ENUMS(FilterMode)
Q_ENUMS(SortMode)

explicit GalleryModel(QObject* parent = nullptr);
virtual ~GalleryModel();
Expand All @@ -53,13 +64,16 @@ class GalleryModel : public QAbstractListModel {
void addPath(QString url = "");
void removePatch(QString url = "");

GalleryModel::SortMode sortMode() const;
void setSortMode(const GalleryModel::SortMode& newSort);

signals:
void sortPropertiesChanged();
void loadingChanged();
void errorChanged();
void filterChanged();

void urlsChanged();
void sortModeChanged();

private slots:
void onUrlsChanged();
Expand All @@ -71,6 +85,7 @@ private slots:
bool m_loading;
bool m_error;
GalleryModel::FilterMode m_filter;
GalleryModel::SortMode m_sortMode;

QStringList m_mimeTypes;
QStringList m_urls;
Expand Down
45 changes: 32 additions & 13 deletions src/qml/pages/MainPage.qml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2012 Andrea Bernabei <[email protected]>
* Copyright (C) 2017-2024 Chupligin Sergey <[email protected]>
* Copyright (C) 2017-2025 Chupligin Sergey <[email protected]>
*
* You may use this file under the terms of the BSD license as follows:
*
Expand Down Expand Up @@ -81,9 +81,22 @@ Page {
property int currentSort: -1
ListModel {
id: sortModel
ListElement { name: qsTr("None"); sortProperty: ""; ascending: false } // dummy
ListElement { name: qsTr("Name"); sortProperty: "fileName"; ascending: true }
ListElement { name: qsTr("Modified"); sortProperty: "lastModified"; ascending: true }
ListElement {
name: qsTr("None");
sortProperty: "none";
}
ListElement {
name: qsTr("Name");
sortProperty: "name";
}
ListElement {
name: qsTr("Modified");
sortProperty: "time";
}
ListElement {
name: qsTr("Size");
sortProperty: "size";
}
}

HeaderToolsLayout {
Expand Down Expand Up @@ -117,18 +130,13 @@ Page {
onCurrentIndexChanged: {
switch (filterButtons.currentIndex) {
case 0:
var videoFilter = gallery.createFilter(gallery, "videosfilter", "GalleryStartsWithFilter", "mimeType", "video/")
var imageFilter = gallery.createFilter(gallery, "imagesfilter", "GalleryStartsWithFilter", "mimeType", "image/")
var bothFilter = gallery.createFiltersArray(gallery, "arraysFilter", "GalleryFilterUnion", [videoFilter, imageFilter])
gallery.assignNewDestroyCurrent(bothFilter)
gallery.filter = GalleryModel.AllFiles
break
case 1:
var vidFilter = gallery.createFilter(gallery, "videosfilter", "GalleryStartsWithFilter", "mimeType", "video/")
gallery.assignNewDestroyCurrent(vidFilter)
gallery.filter = GalleryModel.OnlyVideo
break
case 2:
var imgFilter = gallery.createFilter(gallery, "imagesfilter", "GalleryStartsWithFilter", "mimeType", "image/")
gallery.assignNewDestroyCurrent(imgFilter)
gallery.filter = GalleryModel.OnlyImages
break
}
}
Expand All @@ -152,7 +160,18 @@ Page {
}

onCurrentIndexChanged: {
gallery.sortProperties = [ sortModel.get(sortButtons.currentIndex).sortProperty ];
if(sortModel.get(sortButtons.currentIndex).sortProperty == "none") {
gallery.sortMode = GalleryModel.Unsorted;
}
if(sortModel.get(sortButtons.currentIndex).sortProperty == "name") {
gallery.sortMode = GalleryModel.SortByName;
}
if(sortModel.get(sortButtons.currentIndex).sortProperty == "size") {
gallery.sortMode = GalleryModel.SortBySize;
}
if(sortModel.get(sortButtons.currentIndex).sortProperty == "time") {
gallery.sortMode = GalleryModel.SortByTime;
}
}
}
}
Expand Down

0 comments on commit 7f90b9a

Please sign in to comment.