Fix regressions
[kphotoalbum.git] / ThumbnailView / ThumbnailCache.h
blobfaa35b5b3bee43e9b20f64a8da1f5aa9a972bd4f
1 /* Copyright (C) 2008-2009 Henner Zeller <h.zeller@acm.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
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 GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
18 #ifndef THUMBNAILVIEW_THUMBNAILCACHE_H
19 #define THUMBNAILVIEW_THUMBNAILCACHE_H
21 #include <QHash>
22 #include <QMutex>
23 #include <QObject>
24 #include <QSize>
25 #include <QString>
27 #include "DB/Result.h"
28 #include "ImageManager/ImageClient.h"
30 class QPixmap;
31 class QTimer;
33 namespace DB {
34 class ResultId;
36 namespace ThumbnailView {
38 /**
39 * A cache for thumbnails that supports automatic pre-warming.
41 * This is a wrapper around QPixmapCache for the raw store/retrieve
42 * functionality. In addition to that, given a current displayed page
43 * out of a list of images, it attempts to pre-warm the cache with the
44 * thumbnails of the surrounding pages if possible. The sequence of pre-loading
45 * is scheduled according to the observed scroll direction.
47 class ThumbnailCache : public QObject, public ImageManager::ImageClient {
48 Q_OBJECT
49 public:
50 ThumbnailCache();
52 /**
53 * Find the pixmap for the given ResultId. If found, return 'true' and
54 * insert found pixmap into result. Result must not be NULL.
56 bool find(const DB::ResultId& id, QPixmap *result) const;
58 /** insert the pixmap for the given Media ID */
59 void insert(const DB::ResultId& id, const QPixmap& pix);
61 /** clear the cache */
62 void clear();
64 /**
65 * set the hot area of thumbnails currently displayed. The values
66 * denote a range in the display list.
67 * The Cache uses this to decide what images not to throw away and
68 * what images to preload.
70 void setHotArea(int from, int to);
72 /** Set information about the thumbnails to be displayed.
73 * TODO: view and cache should share a model */
74 void setDisplayList(const DB::Result& displayList);
75 void setThumbnailSize(const QSize& thumbSize);
77 public:
78 bool thumbnailStillNeeded(const QString& fileName) const;
80 protected:
81 // ImageManager interface. The callback just puts the resulting image
82 // into the cache.
83 virtual void pixmapLoaded( const QString& fileName,
84 const QSize& size, const QSize& fullSize,
85 int angle, const QImage& image,
86 const bool loadedOK);
88 protected slots:
89 void slotAsyncCacheWarming();
91 private:
92 static QString thumbnailPixmapCacheKey(const DB::ResultId& id);
94 /**
95 * Warm the cache for the given range by requesting it.
96 * Including 'from', exlcuding 'to'.
98 void requestRange(int from, int to);
100 DB::Result _displayList;
101 QSize _thumbSize;
102 int _hotFrom, _hotTo;
103 int _lastHotFrom;
104 QTimer* _asyncWarmingTimer;
106 mutable QMutex _requestedImagesLock;
107 typedef QHash<QString, DB::ResultId> RequestedMap;
108 RequestedMap _requestedImages;
112 #endif /* THUMBNAILVIEW_THUMBNAILCACHE_H */