-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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")); | ||
|
@@ -84,6 +85,7 @@ void GalleryModel::setFilter(FilterMode newFilter) | |
if (m_filter == newFilter) | ||
return; | ||
m_filter = newFilter; | ||
|
||
formatMimeTypes(); | ||
emit filterChanged(); | ||
} | ||
|
@@ -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) { | ||
|
@@ -138,6 +159,7 @@ void GalleryModel::formatFileList() | |
} | ||
} | ||
} | ||
endResetModel(); | ||
} | ||
|
||
void GalleryModel::onFileSystemChanged(QString path) | ||
|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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(); | ||
|
@@ -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(); | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
* | ||
|
@@ -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 { | ||
|
@@ -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 | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
} | ||
} | ||
} | ||
|