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
28 static const int PageRole = Qt::UserRole + 1;
29
ThumbnailsDock(QWidget * parent)30 ThumbnailsDock::ThumbnailsDock(QWidget *parent) : AbstractInfoDock(parent)
31 {
32 m_list = new QListWidget(this);
33 setWidget(m_list);
34 setWindowTitle(tr("Thumbnails"));
35 m_list->setViewMode(QListView::ListMode);
36 m_list->setMovement(QListView::Static);
37 m_list->setVerticalScrollMode(QListView::ScrollPerPixel);
38 connect(m_list, &QListWidget::itemActivated, this, &ThumbnailsDock::slotItemActivated);
39 }
40
~ThumbnailsDock()41 ThumbnailsDock::~ThumbnailsDock() { }
42
fillInfo()43 void 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(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(Qt::DecorationRole, QPixmap::fromImage(image));
54 item->setData(PageRole, i);
55 m_list->addItem(item);
56 maxSize.setWidth(qMax(maxSize.width(), image.width()));
57 maxSize.setHeight(qMax(maxSize.height(), image.height()));
58 }
59 }
60 if (num > 0) {
61 m_list->setGridSize(maxSize);
62 m_list->setIconSize(maxSize);
63 }
64 }
65
documentClosed()66 void ThumbnailsDock::documentClosed()
67 {
68 m_list->clear();
69 AbstractInfoDock::documentClosed();
70 }
71
slotItemActivated(QListWidgetItem * item)72 void ThumbnailsDock::slotItemActivated(QListWidgetItem *item)
73 {
74 if (!item) {
75 return;
76 }
77
78 setPage(item->data(PageRole).toInt());
79 }
80