Commit cab736b2 authored by Fushan Wen's avatar Fushan Wen
Browse files

wallpapers/image: Move SortingMode enums to sortingmode.h

parent 956fdd0b
Pipeline #161592 passed with stage
in 5 minutes and 38 seconds
......@@ -6,6 +6,7 @@ set(image_SRCS
backgroundlistmodel.cpp
slidemodel.cpp
slidefiltermodel.cpp
sortingmode.h
)
ecm_qt_declare_logging_category(image_SRCS HEADER debug.h
......
......@@ -51,7 +51,7 @@ ImageBackend::ImageBackend(QObject *parent)
, m_delay(10)
, m_dirWatch(new KDirWatch(this))
, m_mode(SingleImage)
, m_slideshowMode(Random)
, m_slideshowMode(SortingMode::Random)
, m_slideshowFoldersFirst(false)
, m_currentSlide(-1)
, m_model(nullptr)
......@@ -144,12 +144,12 @@ void ImageBackend::setRenderingMode(RenderingMode mode)
}
}
ImageBackend::SlideshowMode ImageBackend::slideshowMode() const
SortingMode::Mode ImageBackend::slideshowMode() const
{
return m_slideshowMode;
}
void ImageBackend::setSlideshowMode(ImageBackend::SlideshowMode slideshowMode)
void ImageBackend::setSlideshowMode(SortingMode::Mode slideshowMode)
{
if (slideshowMode == m_slideshowMode) {
return;
......@@ -785,7 +785,7 @@ void ImageBackend::nextSlide()
m_currentSlide += 1;
}
// We are starting again - avoid having the same random order when we restart the slideshow
if (m_slideshowMode == Random && m_currentSlide == 0) {
if (m_slideshowMode == SortingMode::Random && m_currentSlide == 0) {
m_slideFilterModel->invalidate();
}
QUrl next = m_slideFilterModel->index(m_currentSlide, 0).data(BackgroundListModel::PathRole).toUrl();
......
......@@ -21,6 +21,8 @@
#include <KPackage/Package>
#include "sortingmode.h"
class QFileDialog;
class QQuickItem;
......@@ -30,13 +32,13 @@ class BackgroundListModel;
class SlideModel;
class SlideFilterModel;
class ImageBackend : public QObject, public QQmlParserStatus
class ImageBackend : public QObject, public QQmlParserStatus, public SortingMode
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(RenderingMode renderingMode READ renderingMode WRITE setRenderingMode NOTIFY renderingModeChanged)
Q_PROPERTY(SlideshowMode slideshowMode READ slideshowMode WRITE setSlideshowMode NOTIFY slideshowModeChanged)
Q_PROPERTY(SortingMode::Mode slideshowMode READ slideshowMode WRITE setSlideshowMode NOTIFY slideshowModeChanged)
Q_PROPERTY(bool slideshowFoldersFirst READ slideshowFoldersFirst WRITE setSlideshowFoldersFirst NOTIFY slideshowFoldersFirstChanged)
Q_PROPERTY(QUrl wallpaperPath READ wallpaperPath NOTIFY wallpaperPathChanged)
Q_PROPERTY(QAbstractItemModel *wallpaperModel READ wallpaperModel CONSTANT)
......@@ -55,15 +57,6 @@ public:
};
Q_ENUM(RenderingMode)
enum SlideshowMode {
Random,
Alphabetical,
AlphabeticalReversed,
Modified,
ModifiedReversed,
};
Q_ENUM(SlideshowMode)
explicit ImageBackend(QObject *parent = nullptr);
~ImageBackend() override;
......@@ -87,8 +80,8 @@ public:
RenderingMode renderingMode() const;
void setRenderingMode(RenderingMode mode);
SlideshowMode slideshowMode() const;
void setSlideshowMode(SlideshowMode slideshowMode);
SortingMode::Mode slideshowMode() const;
void setSlideshowMode(SortingMode::Mode slideshowMode);
bool slideshowFoldersFirst() const;
void setSlideshowFoldersFirst(bool slideshowFoldersFirst);
......@@ -175,7 +168,7 @@ private:
QSize m_targetSize;
RenderingMode m_mode;
SlideshowMode m_slideshowMode;
SortingMode::Mode m_slideshowMode;
bool m_slideshowFoldersFirst;
KPackage::Package m_wallpaperPackage;
......
......@@ -191,23 +191,23 @@ ColumnLayout {
model: [
{
'label': i18nd("plasma_wallpaper_org.kde.image", "Random"),
'slideshowMode': Wallpaper.ImageBackend.Random
'slideshowMode': Wallpaper.SortingMode.Random
},
{
'label': i18nd("plasma_wallpaper_org.kde.image", "A to Z"),
'slideshowMode': Wallpaper.ImageBackend.Alphabetical
'slideshowMode': Wallpaper.SortingMode.Alphabetical
},
{
'label': i18nd("plasma_wallpaper_org.kde.image", "Z to A"),
'slideshowMode': Wallpaper.ImageBackend.AlphabeticalReversed
'slideshowMode': Wallpaper.SortingMode.AlphabeticalReversed
},
{
'label': i18nd("plasma_wallpaper_org.kde.image", "Date modified (newest first)"),
'slideshowMode': Wallpaper.ImageBackend.ModifiedReversed
'slideshowMode': Wallpaper.SortingMode.ModifiedReversed
},
{
'label': i18nd("plasma_wallpaper_org.kde.image", "Date modified (oldest first)"),
'slideshowMode': Wallpaper.ImageBackend.Modified
'slideshowMode': Wallpaper.SortingMode.Modified
}
]
textRole: "label"
......
......@@ -8,10 +8,13 @@
#include "imagebackend.h"
#include <QQmlContext>
#include "sortingmode.h"
void ImagePlugin::registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.plasma.wallpapers.image"));
qmlRegisterType<ImageBackend>(uri, 2, 0, "ImageBackend");
qmlRegisterAnonymousType<QAbstractItemModel>("QAbstractItemModel",1);
qmlRegisterUncreatableType<SortingMode>(uri, 2, 0, "SortingMode", "error: only enums");
}
......@@ -17,7 +17,7 @@
SlideFilterModel::SlideFilterModel(QObject *parent)
: QSortFilterProxyModel{parent}
, m_SortingMode{ImageBackend::Random}
, m_SortingMode{SortingMode::Random}
, m_SortingFoldersFirst{false}
, m_usedInConfig{false}
, m_random(m_randomDevice())
......@@ -39,13 +39,13 @@ void SlideFilterModel::setSourceModel(QAbstractItemModel *sourceModel)
disconnect(this->sourceModel(), nullptr, this, nullptr);
}
QSortFilterProxyModel::setSourceModel(sourceModel);
if (m_SortingMode == ImageBackend::Random && !m_usedInConfig) {
if (m_SortingMode == SortingMode::Random && !m_usedInConfig) {
buildRandomOrder();
}
if (sourceModel) {
connect(sourceModel, &QAbstractItemModel::modelReset, this, &SlideFilterModel::buildRandomOrder);
connect(sourceModel, &QAbstractItemModel::rowsInserted, this, [this] {
if (m_SortingMode != ImageBackend::Random || m_usedInConfig) {
if (m_SortingMode != SortingMode::Random || m_usedInConfig) {
return;
}
const int old_count = m_randomOrder.size();
......@@ -54,7 +54,7 @@ void SlideFilterModel::setSourceModel(QAbstractItemModel *sourceModel)
std::shuffle(m_randomOrder.begin() + old_count, m_randomOrder.end(), m_random);
});
connect(sourceModel, &QAbstractItemModel::rowsRemoved, this, [this] {
if (m_SortingMode != ImageBackend::Random || m_usedInConfig) {
if (m_SortingMode != SortingMode::Random || m_usedInConfig) {
return;
}
m_randomOrder.erase(std::remove_if(m_randomOrder.begin(),
......@@ -72,12 +72,12 @@ bool SlideFilterModel::lessThan(const QModelIndex &source_left, const QModelInde
Qt::CaseSensitivity cs = Qt::CaseInsensitive;
switch (m_SortingMode) {
case ImageBackend::Random:
case SortingMode::Random:
if (m_usedInConfig) {
return source_left.row() < source_right.row();
}
return m_randomOrder.indexOf(source_left.row()) < m_randomOrder.indexOf(source_right.row());
case ImageBackend::Alphabetical:
case SortingMode::Alphabetical:
if (m_SortingFoldersFirst) {
QFileInfo leftFile(getLocalFilePath(source_left));
QFileInfo rightFile(getLocalFilePath(source_right));
......@@ -98,7 +98,7 @@ bool SlideFilterModel::lessThan(const QModelIndex &source_left, const QModelInde
QFileInfo rightFile(getLocalFilePath(source_right));
return QString::compare(leftFile.fileName(), rightFile.fileName(), cs) < 0;
}
case ImageBackend::AlphabeticalReversed:
case SortingMode::AlphabeticalReversed:
if (m_SortingFoldersFirst) {
QFileInfo leftFile(getLocalFilePath(source_left));
QFileInfo rightFile(getLocalFilePath(source_right));
......@@ -119,13 +119,13 @@ bool SlideFilterModel::lessThan(const QModelIndex &source_left, const QModelInde
QFileInfo rightFile(getLocalFilePath(source_right));
return QString::compare(leftFile.fileName(), rightFile.fileName(), cs) > 0;
}
case ImageBackend::Modified: // oldest first
case SortingMode::Modified: // oldest first
{
QFileInfo leftFile(getLocalFilePath(source_left));
QFileInfo rightFile(getLocalFilePath(source_right));
return leftFile.lastModified() < rightFile.lastModified();
}
case ImageBackend::ModifiedReversed: // newest first
case SortingMode::ModifiedReversed: // newest first
{
QFileInfo leftFile(getLocalFilePath(source_left));
QFileInfo rightFile(getLocalFilePath(source_right));
......@@ -135,11 +135,11 @@ bool SlideFilterModel::lessThan(const QModelIndex &source_left, const QModelInde
Q_UNREACHABLE();
}
void SlideFilterModel::setSortingMode(ImageBackend::SlideshowMode slideshowMode, bool slideshowFoldersFirst)
void SlideFilterModel::setSortingMode(SortingMode::Mode slideshowMode, bool slideshowFoldersFirst)
{
m_SortingMode = slideshowMode;
m_SortingFoldersFirst = slideshowFoldersFirst;
if (m_SortingMode == ImageBackend::Random && !m_usedInConfig) {
if (m_SortingMode == SortingMode::Random && !m_usedInConfig) {
buildRandomOrder();
}
QSortFilterProxyModel::invalidate();
......@@ -147,7 +147,7 @@ void SlideFilterModel::setSortingMode(ImageBackend::SlideshowMode slideshowMode,
void SlideFilterModel::invalidate()
{
if (m_SortingMode == ImageBackend::Random && !m_usedInConfig) {
if (m_SortingMode == SortingMode::Random && !m_usedInConfig) {
std::shuffle(m_randomOrder.begin(), m_randomOrder.end(), m_random);
}
QSortFilterProxyModel::invalidate();
......
......@@ -6,14 +6,14 @@
#pragma once
#include "imagebackend.h"
#include <QSortFilterProxyModel>
#include <QVector>
#include <QFileInfo>
#include <random>
#include "sortingmode.h"
class SlideFilterModel : public QSortFilterProxyModel
{
Q_OBJECT
......@@ -25,7 +25,7 @@ public:
bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
void setSourceModel(QAbstractItemModel *sourceModel) override;
void setSortingMode(ImageBackend::SlideshowMode slideshowMode, bool slideshowFoldersFirst);
void setSortingMode(SortingMode::Mode slideshowMode, bool slideshowFoldersFirst);
void invalidate();
void invalidateFilter();
......@@ -42,7 +42,7 @@ private:
QString getFilePathWithDir(const QFileInfo& fileInfo) const;
QVector<int> m_randomOrder;
ImageBackend::SlideshowMode m_SortingMode;
SortingMode::Mode m_SortingMode;
bool m_SortingFoldersFirst;
bool m_usedInConfig;
std::random_device m_randomDevice;
......
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef SORTINGMODE_H
#define SORTINGMODE_H
class SortingMode
{
Q_GADGET
public:
enum Mode {
Random,
Alphabetical,
AlphabeticalReversed,
Modified,
ModifiedReversed,
};
Q_ENUM(Mode)
};
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment