backgroundlistmodel.h 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/***************************************************************************
 *   Copyright 2007 Paolo Capriotti <p.capriotti@gmail.com>                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#ifndef BACKGROUNDLISTMODEL_H
#define BACKGROUNDLISTMODEL_H

#include "image.h"

#include <QAbstractListModel>
26
#include <QCache>
27 28 29
#include <QPixmap>
#include <QRunnable>
#include <QThread>
30
#include <QMutex>
31
#include <QSet>
32 33 34 35

#include <KDirWatch>
#include <KFileItem>

36
#include <KPackage/PackageStructure>
37 38 39 40 41 42 43 44 45 46

class QEventLoop;
class KProgressDialog;

class Image;

class ImageSizeFinder : public QObject, public QRunnable
{
    Q_OBJECT
    public:
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
47
        explicit ImageSizeFinder(const QString &path, QObject *parent = nullptr);
48
        void run() override;
49 50 51 52 53 54 55 56 57 58 59

    Q_SIGNALS:
        void sizeFound(const QString &path, const QSize &size);

    private:
        QString m_path;
};

class BackgroundListModel : public QAbstractListModel
{
    Q_OBJECT
60
    Q_PROPERTY(int count READ count NOTIFY countChanged)
61 62 63 64 65 66

public:
    enum {
        AuthorRole = Qt::UserRole,
        ScreenshotRole,
        ResolutionRole,
67
        PathRole,
68
        PackageNameRole,
69 70
        RemovableRole,
        PendingDeletionRole
71
    };
Marco Martin's avatar
Marco Martin committed
72

73 74 75 76
    static const int BLUR_INCREMENT = 9;
    static const int MARGIN = 6;

    BackgroundListModel(Image *listener, QObject *parent);
77
    ~BackgroundListModel() override;
78

Laurent Montel's avatar
Laurent Montel committed
79 80 81 82
    QHash<int, QByteArray> roleNames() const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override ;
    bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
83
    KPackage::Package package(int index) const;
84 85 86 87

    void reload();
    void reload(const QStringList &selected);
    void addBackground(const QString &path);
88
    Q_INVOKABLE int indexOf(const QString &path) const;
89 90
    virtual bool contains(const QString &bg) const;

Laurent Montel's avatar
Laurent Montel committed
91
    int count() const {return m_packages.size();}
92

93
    Q_INVOKABLE void openContainingFolder(int rowIndex);
94 95 96
    Q_INVOKABLE void setPendingDeletion(int rowIndex, bool pendingDeletion);
    const QStringList wallpapersAwaitingDeletion();

97 98 99
Q_SIGNALS:
    void countChanged();

100 101 102 103 104 105 106 107 108
protected Q_SLOTS:
    void removeBackground(const QString &path);
    void showPreview(const KFileItem &item, const QPixmap &preview);
    void previewFailed(const KFileItem &item);
    void sizeFound(const QString &path, const QSize &s);
    void backgroundsFound(const QStringList &paths, const QString &token);
    void processPaths(const QStringList &paths);

private:
109
    QSize bestSize(const KPackage::Package &package) const;
110

111
    QPointer<Image> m_wallpaper;
112
    QList<KPackage::Package> m_packages;
113
    QSet<QString> m_removableWallpapers;
114 115 116
    QHash<QString, QSize> m_sizeCache;
    QHash<QUrl, QPersistentModelIndex> m_previewJobs;
    KDirWatch m_dirwatch;
117
    QCache<QString, QPixmap> m_imageCache;
118 119

    QString m_findToken;
Marco Martin's avatar
Marco Martin committed
120
    int m_screenshotSize;
121
    QHash<QString, int> m_pendingDeletion;
122 123 124 125 126 127 128
};

class BackgroundFinder : public QThread
{
    Q_OBJECT

public:
129
    BackgroundFinder(Image *wallpaper, const QStringList &p);
130
    ~BackgroundFinder() override;
131 132 133

    QString token() const;

134
    static QStringList suffixes();
135
    static bool isAcceptableSuffix(const QString &suffix);
136 137 138 139 140

Q_SIGNALS:
    void backgroundsFound(const QStringList &paths, const QString &token);

protected:
141
    void run() override;
142 143 144 145 146

private:
    QStringList m_paths;
    QString m_token;

147 148
    static QMutex s_suffixMutex;
    static QStringList s_suffixes;
149 150 151
};

#endif // BACKGROUNDLISTMODEL_H