~artmello/ubuntu-ui-extras/ubuntu-ui-extras-fix_1524973

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/Extras/plugin/photoeditor/photo-image-provider.h

  • Committer: CI Train Bot
  • Author(s): Ugo Riboni
  • Date: 2015-02-04 20:33:44 UTC
  • mfrom: (65.1.46 ubuntu-ui-extras-photo-editor)
  • Revision ID: ci-train-bot@canonical.com-20150204203344-pfmu1ckhooy7oaca
Add a photo editor component, partially based on the Gallery photo editor Fixes: #1368787
Approved by: PS Jenkins bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011-2014 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 * Lucas Beeler <lucas@yorba.org>
 
18
 * Jim Nelson <jim@yorba.org>
 
19
 * Ugo Riboni <ugo.riboni@canonical.com>
 
20
*/
 
21
 
 
22
#ifndef PHOTO_IMAGE_PROVIDER_H_
 
23
#define PHOTO_IMAGE_PROVIDER_H_
 
24
 
 
25
// util
 
26
#include "orientation.h"
 
27
 
 
28
#include <QDateTime>
 
29
#include <QFileInfo>
 
30
#include <QImage>
 
31
#include <QMap>
 
32
#include <QMutex>
 
33
#include <QObject>
 
34
#include <QQuickImageProvider>
 
35
#include <QSize>
 
36
#include <QString>
 
37
#include <QUrl>
 
38
 
 
39
/*!
 
40
 * We use a custom image provider for the following reasons:
 
41
 *
 
42
 * 1. QML's image loader does not respect EXIF orientation.  This provider will
 
43
 *    rotate and mirror the image as necessary.
 
44
 *
 
45
 * 2. QML's image caching appears to be directly related to image size (scaling)
 
46
 *    which leads to thrashing when animating a thumbnail or loading images at
 
47
 *    various sizes.
 
48
 *    The strategy here is to cache the largest requested size of the image
 
49
 *    and downscale it if smaller versions are requested. This minimizes
 
50
 *    expensive JPEG load-and-decodes.
 
51
 *
 
52
 * 3. Logging allows for monitoring of all image I/O, useful when debugging and
 
53
 *    optimizing, and signals can be emitted to monitor and test cache operation.
 
54
 *
 
55
 * 4. The QML cache does not check if a file has been modified on disk after it
 
56
 *    was cached, but our cache does. You should always set Image.cache to false
 
57
 *    when loading images from this provider in QML.
 
58
 */
 
59
class PhotoImageProvider : public QObject, public QQuickImageProvider
 
60
{
 
61
    Q_OBJECT
 
62
 
 
63
public:
 
64
    static const char* PROVIDER_ID;
 
65
    static const char* PROVIDER_ID_SCHEME;
 
66
 
 
67
    PhotoImageProvider();
 
68
    virtual ~PhotoImageProvider();
 
69
 
 
70
    virtual QImage requestImage(const QString& id, QSize* size,
 
71
                                const QSize& requestedSize);
 
72
 
 
73
    void setLogging(bool enableLogging);
 
74
    void setEmitCacheSignals(bool emitCacheSignals);
 
75
 
 
76
Q_SIGNALS:
 
77
    void cacheHit(QString id, QSize size);
 
78
    void cacheMiss(QString id, QSize size, bool wasStale);
 
79
    void cacheAdd(QString id, QSize size, QSize cachedSize);
 
80
 
 
81
private:
 
82
    class CachedImage {
 
83
    public:
 
84
        const QString id;
 
85
        QMutex imageMutex;
 
86
 
 
87
        // these fields should only be accessed when imageMutex_ is locked
 
88
        QImage image;
 
89
        QSize fullSize;
 
90
        Orientation orientation;
 
91
        QDateTime cachedAt;
 
92
 
 
93
        // the following should only be accessed when cacheMutex_ is locked; the
 
94
        // counter controls removing a CachedImage entry from the cache table
 
95
        int cleanCount;
 
96
        uint byteCount;
 
97
 
 
98
        CachedImage(const QString& id);
 
99
 
 
100
        void storeImage(const QImage& newImage, const QSize& newFullSize,
 
101
                        Orientation newOrientation);
 
102
        bool isFullSized() const;
 
103
        bool isReady() const;
 
104
        bool isCacheHit(const QSize& requestedSize) const;
 
105
    };
 
106
 
 
107
    QMap<QString, CachedImage*> m_cache;
 
108
    QList<QString> m_cachingOrder;
 
109
    QMutex m_cacheMutex;
 
110
    long m_cachedBytes;
 
111
    bool m_logImageLoading;
 
112
    bool m_emitCacheSignals;
 
113
 
 
114
    static QSize orientSize(const QSize& size, Orientation orientation);
 
115
 
 
116
    CachedImage* claimCachedImageEntry(const QString& id, QString& loggingStr);
 
117
 
 
118
    QImage fetchCachedImage(CachedImage* cachedImage, const QSize& requestedSize,
 
119
                              uint* bytesLoaded, QString& loggingStr);
 
120
 
 
121
    void releaseCachedImageEntry(CachedImage* cachedImage, uint bytesLoaded);
 
122
};
 
123
 
 
124
#endif // PHOTO_IMAGE_PROVIDER_H_