1/*
2 * Copyright (C) 2009, Shawn Rutledge <shawn.t.rutledge@gmail.com>
3 * Copyright (C) 2009, Pino Toscano <pino@kde.org>
4 * Copyright (C) 2020, Albert Astals Cid <aacid@kde.org>
5 * Copyright (C) 2021, Oliver Sander <oliver.sander@tu-dresden.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "thumbnails.h"
23
24#include <poppler-qt6.h>
25
26#include <QtWidgets/QListWidget>
27
28static const int PageRole = Qt::UserRole + 1;
29
30ThumbnailsDock::ThumbnailsDock(QWidget *parent) : AbstractInfoDock(parent)
31{
32 m_list = new QListWidget(this);
33 setWidget(m_list);
34 setWindowTitle(tr(s: "Thumbnails"));
35 m_list->setViewMode(QListView::ListMode);
36 m_list->setMovement(QListView::Static);
37 m_list->setVerticalScrollMode(QListView::ScrollPerPixel);
38 connect(sender: m_list, signal: &QListWidget::itemActivated, context: this, slot: &ThumbnailsDock::slotItemActivated);
39}
40
41ThumbnailsDock::~ThumbnailsDock() { }
42
43void ThumbnailsDock::fillInfo()
44{
45 const int num = document()->numPages();
46 QSize maxSize;
47 for (int i = 0; i < num; ++i) {
48 const std::unique_ptr<Poppler::Page> page = document()->page(index: i);
49 const QImage image = page ? page->thumbnail() : QImage();
50 if (!image.isNull()) {
51 QListWidgetItem *item = new QListWidgetItem();
52 item->setText(QString::number(i + 1));
53 item->setData(role: Qt::DecorationRole, value: QPixmap::fromImage(image));
54 item->setData(role: PageRole, value: i);
55 m_list->addItem(aitem: item);
56 maxSize.setWidth(qMax(a: maxSize.width(), b: image.width()));
57 maxSize.setHeight(qMax(a: maxSize.height(), b: image.height()));
58 }
59 }
60 if (num > 0) {
61 m_list->setGridSize(maxSize);
62 m_list->setIconSize(maxSize);
63 }
64}
65
66void ThumbnailsDock::documentClosed()
67{
68 m_list->clear();
69 AbstractInfoDock::documentClosed();
70}
71
72void ThumbnailsDock::slotItemActivated(QListWidgetItem *item)
73{
74 if (!item) {
75 return;
76 }
77
78 setPage(item->data(role: PageRole).toInt());
79}
80

source code of poppler/qt6/demos/thumbnails.cpp