1 /*
2     Scan Tailor - Interactive post-processing tool for scanned pages.
3     Copyright (C)  Joseph Artsimovich <joseph.artsimovich@gmail.com>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "ThumbnailFactory.h"
20 #include <QGraphicsItem>
21 #include <utility>
22 #include "CompositeCacheDrivenTask.h"
23 #include "filter_dc/ThumbnailCollector.h"
24 
25 class ThumbnailFactory::Collector : public ThumbnailCollector {
26  public:
27   Collector(intrusive_ptr<ThumbnailPixmapCache> cache, const QSizeF& max_size);
28 
29   void processThumbnail(std::unique_ptr<QGraphicsItem> thumbnail) override;
30 
31   intrusive_ptr<ThumbnailPixmapCache> thumbnailCache() override;
32 
33   QSizeF maxLogicalThumbSize() const override;
34 
retrieveThumbnail()35   std::unique_ptr<QGraphicsItem> retrieveThumbnail() { return std::move(m_thumbnail); }
36 
37  private:
38   intrusive_ptr<ThumbnailPixmapCache> m_cache;
39   QSizeF m_maxSize;
40   std::unique_ptr<QGraphicsItem> m_thumbnail;
41 };
42 
43 
ThumbnailFactory(intrusive_ptr<ThumbnailPixmapCache> pixmap_cache,const QSizeF & max_size,intrusive_ptr<CompositeCacheDrivenTask> task)44 ThumbnailFactory::ThumbnailFactory(intrusive_ptr<ThumbnailPixmapCache> pixmap_cache,
45                                    const QSizeF& max_size,
46                                    intrusive_ptr<CompositeCacheDrivenTask> task)
47     : m_pixmapCache(std::move(pixmap_cache)), m_maxSize(max_size), m_task(std::move(task)) {}
48 
49 ThumbnailFactory::~ThumbnailFactory() = default;
50 
get(const PageInfo & page_info)51 std::unique_ptr<QGraphicsItem> ThumbnailFactory::get(const PageInfo& page_info) {
52   Collector collector(m_pixmapCache, m_maxSize);
53   m_task->process(page_info, &collector);
54 
55   return collector.retrieveThumbnail();
56 }
57 
58 /*======================= ThumbnailFactory::Collector ======================*/
59 
Collector(intrusive_ptr<ThumbnailPixmapCache> cache,const QSizeF & max_size)60 ThumbnailFactory::Collector::Collector(intrusive_ptr<ThumbnailPixmapCache> cache, const QSizeF& max_size)
61     : m_cache(std::move(cache)), m_maxSize(max_size) {}
62 
processThumbnail(std::unique_ptr<QGraphicsItem> thumbnail)63 void ThumbnailFactory::Collector::processThumbnail(std::unique_ptr<QGraphicsItem> thumbnail) {
64   m_thumbnail = std::move(thumbnail);
65 }
66 
thumbnailCache()67 intrusive_ptr<ThumbnailPixmapCache> ThumbnailFactory::Collector::thumbnailCache() {
68   return m_cache;
69 }
70 
maxLogicalThumbSize() const71 QSizeF ThumbnailFactory::Collector::maxLogicalThumbSize() const {
72   return m_maxSize;
73 }
74