File indexing completed on 2024-05-19 15:40:35

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QCoroQmlTask>
0008 #include <QList>
0009 #include <QQmlEngine>
0010 #include <QUrl>
0011 
0012 enum ImageType {
0013     PNG,
0014     JPEG,
0015     SVG,
0016     WEBP,
0017     UNSURPORTED,
0018 };
0019 
0020 struct ImageInfo {
0021     QUrl path;
0022     QUrl newPath;
0023     qint64 size = -1;
0024     qint64 oldSize = -1;
0025     ImageType imageType;
0026     bool processed = false;
0027     bool error = false;
0028 };
0029 
0030 class ImageModel : public QAbstractListModel
0031 {
0032     Q_OBJECT
0033     QML_ELEMENT
0034     Q_PROPERTY(bool running READ running NOTIFY runningChanged)
0035 
0036 public:
0037     enum Roles { FileNameRole = Qt::UserRole, SizeRole, NewSizeRole, SavingRole, AlreadyOptimizedRole, ProcessedRole };
0038 
0039 public:
0040     explicit ImageModel(QObject *parent = nullptr);
0041 
0042     QVariant data(const QModelIndex &index, int role) const override;
0043     int rowCount(const QModelIndex &parent = {}) const override;
0044     QHash<int, QByteArray> roleNames() const override;
0045     Q_INVOKABLE void addImages(const QList<QUrl> &paths);
0046 
0047     bool running() const;
0048 
0049     Q_INVOKABLE void open(const QString &url);
0050     Q_INVOKABLE void openProperties(const QString &url);
0051     Q_INVOKABLE void highlightInFileManager(const QString &url);
0052 
0053 Q_SIGNALS:
0054     void runningChanged();
0055 
0056 private:
0057     QCoro::Task<> optimize();
0058 
0059     QVector<ImageInfo> m_images;
0060     bool m_running = false;
0061 };