Skip to content

Commit

Permalink
Rework model
Browse files Browse the repository at this point in the history
  • Loading branch information
neochapay committed Jan 4, 2025
1 parent cc3b96c commit e3c1d79
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 74 deletions.
25 changes: 18 additions & 7 deletions src/plugin/filesystemworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,37 @@
#include <QImageReader>
#include <QMimeDatabase>

FileSystemWorker::FileSystemWorker(QStringList dirList, QStringList suffixes, QObject* parent)
FileSystemWorker::FileSystemWorker(QObject* parent)
: QObject { parent }
, m_dirs(dirList)
, m_suffixes(suffixes)
, m_busy(false)
{
}

void FileSystemWorker::setDirs(const QStringList dirs)
{
stop();
m_dirs = dirs;
}

void FileSystemWorker::setSuffixes(const QStringList suff)
{
stop();
m_suffixes = suff;
}

void FileSystemWorker::start()
{
if (m_busy) {
qWarning() << "Stop before run again!";
return;
}

QMimeDatabase db;

m_busy = true;
m_mutex.lock();
emit busyChanged();
foreach (const QString& dirString, m_dirs) {
if (m_mutex.tryLock()) {
if (!m_busy) { //STOP
break;
}
QDirIterator it(dirString, m_suffixes, QDir::Files, QDirIterator::Subdirectories);
Expand Down Expand Up @@ -76,12 +87,12 @@ void FileSystemWorker::start()
emit foundFile(file);
}
}
m_mutex.unlock();
m_busy = false;
emit busyChanged();
}

void FileSystemWorker::stop()
{
m_mutex.unlock();
m_busy = false;
emit busyChanged();
}
9 changes: 5 additions & 4 deletions src/plugin/filesystemworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <QDirIterator>
#include <QMimeType>
#include <QMutex>
#include <QObject>

struct MediaFile {
Expand All @@ -38,19 +37,21 @@ struct MediaFile {

class FileSystemWorker : public QObject {
Q_OBJECT
Q_PROPERTY(bool busy READ busy NOTIFY busyChanged FINAL)
public:
explicit FileSystemWorker(QStringList dirList, QStringList suffixes, QObject* parent = nullptr);
explicit FileSystemWorker(QObject* parent = nullptr);
void setDirs(QStringList dirs);
void setSuffixes(QStringList suff);

bool busy() { return m_busy; }

void start();
void stop();

signals:
void foundFile(MediaFile file);
void busyChanged();

private:
QMutex m_mutex;
QStringList m_dirs;
QStringList m_suffixes;
bool m_busy;
Expand Down
88 changes: 33 additions & 55 deletions src/plugin/gallerymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ GalleryModel::GalleryModel(QObject* parent)
, m_filter(FilterMode::AllFiles)
, m_sortMode(SortMode::SortByTime)
, m_fileSystemWatcher(new QFileSystemWatcher)
, m_work(new FileSystemWorker())
{
m_hash.insert(Qt::UserRole, QByteArray("url"));
m_hash.insert(Qt::UserRole + 1, QByteArray("mimeType"));
Expand All @@ -46,6 +47,18 @@ GalleryModel::GalleryModel(QObject* parent)
connect(m_fileSystemWatcher, &QFileSystemWatcher::fileChanged, this, &GalleryModel::onFileSystemChanged);
connect(m_fileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, &GalleryModel::onFileSystemChanged);

QThread* scanTread = new QThread;
connect(m_work, &FileSystemWorker::foundFile, this, &GalleryModel::appendFiles);
connect(m_work ,&FileSystemWorker::busyChanged, [=](){
if(m_work->busy() != m_loading) {
m_loading = m_work->busy();
emit loadingChanged();
}
});

m_work->moveToThread(scanTread);
scanTread->start();

formatFileList();
}

Expand All @@ -70,8 +83,21 @@ QVariant GalleryModel::data(const QModelIndex& index, int role) const
MediaFile file = m_files.at(index.row());
if (role == Qt::UserRole) {
return file.path;
} else if (role == Qt::UserRole+1) {
return file.mimeType.name();
} else if (role == Qt::UserRole+2) {
return file.width;
} else if (role == Qt::UserRole+3) {
return file.height;
} else if (role == Qt::UserRole+4) {
return file.modified;
} else if (role == Qt::UserRole+5) {
return file.created;
} else if (role == Qt::UserRole+6) {
return file.size;
}


return QVariant();
}

Expand Down Expand Up @@ -108,7 +134,7 @@ void GalleryModel::addPath(QString url)
}

if (url.isEmpty()) {
m_urls.append(QStandardPaths::standardLocations(QStandardPaths::HomeLocation));
m_urls.append(QStandardPaths::standardLocations(QStandardPaths::PicturesLocation));
emit urlsChanged();
}
}
Expand Down Expand Up @@ -140,6 +166,9 @@ void GalleryModel::formatFileList()
}

m_files.clear();
if(m_work != nullptr) {
m_work->stop();
}

QStringList suffixes;
foreach (const QMimeType mType, m_mimeTypes) {
Expand All @@ -148,43 +177,10 @@ void GalleryModel::formatFileList()
}
};

FileSystemWorker* work = new FileSystemWorker(m_urls, suffixes);
QThread* scanTread = new QThread;
connect(scanTread, &QThread::started, work, &FileSystemWorker::start);
connect(work, &FileSystemWorker::foundFile, this, &GalleryModel::appendFiles);

work->moveToThread(scanTread);
scanTread->start();

/*foreach (const QString& dirString, m_urls) {
QDir dir(dirString);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
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;
}
m_work->setDirs(m_urls);
m_work->setSuffixes(suffixes);
m_work->start();

/*QDirIterator it(dirString, suffixes, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}
}*/
endResetModel();
}

Expand Down Expand Up @@ -259,24 +255,6 @@ QString GalleryModel::sizeTotext(float size)
return QString().setNum(size, 'f', 2) + " " + unit;
}

bool GalleryModel::isVideo(int index)
{
QMimeDatabase db;
if (index < 0 || index >= m_files.count()) {
return false;
}
QString url = m_files.at(index).path;
if (url.isEmpty()) {
return false;
}

QFileInfo fileInfo(url);
if (db.mimeTypeForFile(fileInfo.absoluteFilePath()).name().startsWith("video/")) {
return true;
}
return false;
}

QVariant GalleryModel::get(const int idx)
{
if (idx >= m_files.size()) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/gallerymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class GalleryModel : public QAbstractListModel {
void setSortMode(const GalleryModel::SortMode& newSort);

Q_INVOKABLE QString sizeTotext(float size);
Q_INVOKABLE bool isVideo(int index);

public slots:
QVariant get(const int idx);
Expand Down Expand Up @@ -101,6 +100,7 @@ private slots:
QList<MediaFile> m_files;

QFileSystemWatcher* m_fileSystemWatcher;
FileSystemWorker* m_work;

void formatMimeTypes();
};
Expand Down
1 change: 1 addition & 0 deletions src/plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ void QQuickNemoControlsExtensionPlugin::registerTypes(const char* uri)
Q_ASSERT(uri == QLatin1String("Glacier.Gallery"));
qmlRegisterModule(uri, 1, 0);
//@uri Glacier.Gallery

qmlRegisterType<GalleryModel>(uri, 1, 0, "GalleryModel");
}
4 changes: 2 additions & 2 deletions src/qml/components/ImageContainer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Item {
id: imgContainer
property int index: -1
property variant pinchingController
property string source: ""
property string source
readonly property bool isVideo: gallery.isVideo(source) === 1
property alias flickableArea: flickImg
property int doubleClickInterval: 350
Expand Down Expand Up @@ -95,7 +95,7 @@ Item {
height: imgContainer.height
fillMode: Image.PreserveAspectFit

source: isVideo ? "file:///usr/share/glacier-gallery/images/DefaultVideoThumbnail.jpg" : "file://"+ imgContainer.source
source: imgContainer.source ? isVideo ? "file:///usr/share/glacier-gallery/images/DefaultVideoThumbnail.jpg" : "file://"+ imgContainer.source : undefined

MouseArea {
anchors.fill: parent
Expand Down
4 changes: 2 additions & 2 deletions src/qml/pages/ImagePage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ Page {
if(parameterIndex > 1) {
previosImage.source = galleryModel.get(parameterIndex-1).url;
} else {
previosImage.source = "";
previosImage.source = undefined;
}

if (parameterIndex+1 < galleryModel.count) {
nextImage.source = galleryModel.get(parameterIndex+1).url;
} else {
nextImage.source = "";
nextImage.source = undefined;
}

currentImage.x = 0
Expand Down
17 changes: 14 additions & 3 deletions src/qml/pages/MainPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@ import Nemo.Controls

import Glacier.Gallery

import org.nemomobile.sortfiltermodel 1.0

Page {
id: mainPage
headerTools: mainTools

GalleryView {
anchors.fill: parent
model: GalleryModel {
SortFilterModel{
id: gallerySorted
sourceModel: GalleryModel {
id: gallery
}
sortRole: "modified"
sortOrder: Qt.DescendingOrder
property alias loading: gallery.loading
}


GalleryView {
anchors.fill: parent
model: gallerySorted

delegate: GalleryDelegate {
MouseArea {
Expand Down

0 comments on commit e3c1d79

Please sign in to comment.