1 // vim: set tabstop=4 shiftwidth=4 expandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2012 Aurélien Gâteau <agateau@kde.org>
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
19 
20 */
21 #ifndef THUMBNAILGENERATOR_H
22 #define THUMBNAILGENERATOR_H
23 
24 // Local
25 #include <lib/thumbnailgroup.h>
26 
27 // KF
28 #include <KFileItem>
29 
30 // Qt
31 #include <QImage>
32 #include <QMutex>
33 #include <QThread>
34 #include <QWaitCondition>
35 
36 namespace Gwenview
37 {
38 struct ThumbnailContext {
39     QImage mImage;
40     int mOriginalWidth;
41     int mOriginalHeight;
42     bool mNeedCaching;
43 
44     bool load(const QString &pixPath, int pixelSize);
45 };
46 
47 class ThumbnailGenerator : public QThread
48 {
49     Q_OBJECT
50 public:
51     ThumbnailGenerator();
52 
53     // Because we override run(), like you're not really supposed to do, we
54     // can't trust isRunning()
55     bool isStopped();
56 
57     void load(const QString &originalUri,
58               time_t originalTime,
59               KIO::filesize_t originalFileSize,
60               const QString &originalMimeType,
61               const QString &pixPath,
62               const QString &thumbnailPath,
63               ThumbnailGroup::Enum group);
64 
65     void cancel();
66 
67     QString originalUri() const;
68     time_t originalTime() const;
69     KIO::filesize_t originalFileSize() const;
70     QString originalMimeType() const;
71 
72 protected:
73     void run() override;
74 
75 Q_SIGNALS:
76     void done(const QImage &, const QSize &);
77     void thumbnailReadyToBeCached(const QString &thumbnailPath, const QImage &);
78 
79 private:
80     bool testCancel();
81     void cacheThumbnail();
82     QImage mImage;
83     QString mPixPath;
84     QString mThumbnailPath;
85     QString mOriginalUri;
86     time_t mOriginalTime;
87     KIO::filesize_t mOriginalFileSize;
88     QString mOriginalMimeType;
89     int mOriginalWidth;
90     int mOriginalHeight;
91     QMutex mMutex;
92     QWaitCondition mCond;
93     ThumbnailGroup::Enum mThumbnailGroup;
94     bool mCancel;
95     bool mStopped = false;
96 };
97 
98 } // namespace
99 
100 #endif /* THUMBNAILGENERATOR_H */
101