Debian Codegraph @

qt5/demos/thumbnails.cpp

raw
1 /*
2 * Copyright (C) 2009, Shawn Rutledge <[email protected]>
3 * Copyright (C) 2009, Pino Toscano <[email protected]>
4 * Copyright (C) 2020, Albert Astals Cid <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * 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, Boston, MA 02110-1301, USA.
19 */
20
21 #include "thumbnails.h"
22
23 #include <poppler-qt5.h>
24
25 #include <QtWidgets/QListWidget>
26
27 static const int PageRole = Qt::UserRole + 1;
28
29 ThumbnailsDock::ThumbnailsDock(QWidget *parent) : AbstractInfoDock(parent)
30 {
31 m_list = new QListWidget(this);
32 setWidget(m_list);
33 setWindowTitle(tr("Thumbnails"));
34 m_list->setViewMode(QListView::ListMode);
35 m_list->setMovement(QListView::Static);
36 m_list->setVerticalScrollMode(QListView::ScrollPerPixel);
37 connect(m_list, &QListWidget::itemActivated, this, &ThumbnailsDock::slotItemActivated);
38 }
39
40 ThumbnailsDock::~ThumbnailsDock() = default;
41
42 void ThumbnailsDock::fillInfo()
43 {
44 const int num = document()->numPages();
45 QSize maxSize;
46 for (int i = 0; i < num; ++i) {
47 const Poppler::Page *page = document()->page(i);
48 const QImage image = page ? page->thumbnail() : QImage();
49 if (!image.isNull()) {
50 QListWidgetItem *item = new QListWidgetItem();
51 item->setText(QString::number(i + 1));
52 item->setData(Qt::DecorationRole, QPixmap::fromImage(image));
53 item->setData(PageRole, i);
54 m_list->addItem(item);
55 maxSize.setWidth(qMax(maxSize.width(), image.width()));
56 maxSize.setHeight(qMax(maxSize.height(), image.height()));
57 }
58 delete page;
59 }
60 if (num > 0) {
61 m_list->setGridSize(maxSize);
62 m_list->setIconSize(maxSize);
63 }
64 }
65
66 void ThumbnailsDock::documentClosed()
67 {
68 m_list->clear();
69 AbstractInfoDock::documentClosed();
70 }
71
72 void ThumbnailsDock::slotItemActivated(QListWidgetItem *item)
73 {
74 if (!item) {
75 return;
76 }
77
78 setPage(item->data(PageRole).toInt());
79 }