Unity 8
ImageCache.h
1 /*
2  * Copyright (C) 2016 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 as published by
6  * the Free Software Foundation; version 3.
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 
17 #pragma once
18 
19 #include <QFileInfo>
20 #include <QImageReader>
21 #include <QQuickImageProvider>
22 #include <QSize>
23 
40 class ImageCache: public QQuickImageProvider
41 {
42 public:
43  ImageCache();
44 
45  QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
46 
47 private:
48  static QString imageCacheRoot();
49  static QFileInfo imagePath(const QUrl &image);
50  static bool needsUpdate(const QUrl &image, const QFileInfo &cachePath, const QSize &imageSize, const QSize &requestedSize, QSize &finalSize);
51  static QSize calculateSize(const QSize &imageSize, const QSize &requestedSize);
52  static QImage loadAndCacheImage(QImageReader &reader, const QFileInfo &cachePath, const QSize &finalSize);
53 };