-
Notifications
You must be signed in to change notification settings - Fork 0
/
tocdock.cpp
290 lines (240 loc) · 9.44 KB
/
tocdock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright (C) 2008, Pino Toscano <pino@kde.org>
* Copyright (C) 2016, Marc Langenbach <mlangen@absint.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tocdock.h"
#include "pageview.h"
#include "viewer.h"
#include <poppler-qt6.h>
#include <QHeaderView>
#include <QLineEdit>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QTreeView>
#include <QVBoxLayout>
#define DestinationRole (Qt::UserRole + 1)
#define FilterRole (Qt::UserRole + 2)
#define UnicodeOr QChar(0x22c1)
class MySortFilterProxyModel : public QSortFilterProxyModel
{
public:
MySortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const
{
if (Qt::BackgroundRole == role && !filterRegularExpression().pattern().isEmpty()) {
if (-1 != proxyIndex.data().toString().indexOf(filterRegularExpression()))
return QVariant::fromValue(PageView::matchColor());
}
return QSortFilterProxyModel::data(proxyIndex, role);
}
};
TocDock::TocDock(QWidget *parent)
: QDockWidget(parent)
{
setWindowTitle(tr("Table of contents"));
setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
// for state saving
setObjectName(QStringLiteral("toc_info_dock"));
m_model = new QStandardItemModel(this);
m_proxyModel = new MySortFilterProxyModel(this);
m_proxyModel->setSourceModel(m_model);
m_proxyModel->setFilterRole(FilterRole);
QWidget *container = new QWidget(this);
QVBoxLayout *vbl = new QVBoxLayout(container);
vbl->setContentsMargins(0, 0, 0, 0);
setWidget(container);
m_tree = new QTreeView(this);
m_tree->setAlternatingRowColors(true);
m_tree->header()->hide();
m_tree->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
vbl->addWidget(m_tree);
m_filter = new QLineEdit(this);
m_filter->setPlaceholderText(tr("Filter"));
m_filter->setClearButtonEnabled(true);
vbl->addWidget(m_filter);
connect(this, &TocDock::visibilityChanged, this, &TocDock::slotVisibilityChanged);
connect(m_tree, &QTreeView::clicked, this, &TocDock::indexClicked);
connect(PdfViewer::document(), &Document::documentChanged, this, &TocDock::documentChanged);
connect(PdfViewer::view(), &PageView::pageChanged, this, &TocDock::pageChanged);
connect(PdfViewer::view(), &PageView::pageRequested, this, &TocDock::pageChanged);
m_findStartTimer = new QTimer(this);
m_findStartTimer->setSingleShot(true);
m_findStartTimer->setInterval(1000);
connect(m_findStartTimer, &QTimer::timeout, this, &TocDock::setFilter);
connect(m_filter, &QLineEdit::textChanged, m_findStartTimer, qOverload<>(&QTimer::start));
connect(m_proxyModel, &MySortFilterProxyModel::layoutChanged, this, &TocDock::expand);
}
TocDock::~TocDock()
{
}
void TocDock::fillInfo()
{
const auto toc = PdfViewer::document()->toc();
if (!toc.isEmpty()) {
QSet<QModelIndex> openIndices = fillToc(toc);
// inform tree about new model
m_tree->setModel(m_proxyModel);
m_tree->header()->setStretchLastSection(false);
m_tree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
m_tree->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
// expand open indices
for (const QModelIndex &index : openIndices)
m_tree->setExpanded(m_proxyModel->mapFromSource(index), true);
} else {
// tell we found no toc
QStandardItem *item = new QStandardItem(tr("No table of contents available."));
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
m_model->appendRow(item);
m_tree->setModel(m_model);
}
}
QSet<QModelIndex> TocDock::fillToc(const QVector<Poppler::OutlineItem> &items, QStandardItem *parentItem)
{
QSet<QModelIndex> openIndices;
for (const auto &item : items) {
// tag name == link name, strange enough
QStandardItem *labelItem = new QStandardItem(item.name());
labelItem->setFlags(labelItem->flags() & ~Qt::ItemIsEditable);
/**
* use raw string for destination or convert the named one
*/
QString destination;
if (item.destination()) {
if (item.destination()->pageNumber() > 0)
destination = item.destination()->toString();
}
// skip destination building if not there
int pageNumber = 0;
if (!destination.isEmpty()) {
Poppler::LinkDestination link(destination);
pageNumber = link.pageNumber();
// remember link string representation
labelItem->setData(link.toString(), DestinationRole);
}
QStandardItem *pageItem = new QStandardItem(QString::number(pageNumber));
pageItem->setFlags(pageItem->flags() & ~Qt::ItemIsEditable);
pageItem->setData(Qt::AlignRight, Qt::TextAlignmentRole);
if (!parentItem)
m_model->appendRow(QList<QStandardItem *>() << labelItem << pageItem);
else
parentItem->appendRow(QList<QStandardItem *>() << labelItem << pageItem);
m_pageToIndexMap.insert(pageNumber, labelItem->index());
if (item.isOpen())
openIndices << labelItem->index();
if (item.hasChildren())
openIndices += fillToc(item.children(), labelItem);
// adjust filter role
QStringList filterRoles;
for (int c = 0; c < labelItem->rowCount(); c++)
filterRoles << labelItem->child(c, 0)->data(FilterRole).toString();
filterRoles.prepend(labelItem->text());
labelItem->setData(filterRoles.join(UnicodeOr), FilterRole);
}
return openIndices;
}
/*
* protected slots
*/
void TocDock::documentChanged()
{
// reset old data
m_model->clear();
m_proxyModel->setFilterRegularExpression(QRegularExpression());
m_tree->setModel(nullptr);
m_filter->clear();
m_pageToIndexMap.clear();
m_markedIndex = QModelIndex();
m_filled = false;
// try to fill toc if visible
if (!isHidden()) {
fillInfo();
m_filled = true;
}
}
void TocDock::pageChanged(int page)
{
while (m_markedIndex.isValid()) {
m_model->setData(m_markedIndex, QVariant(), Qt::FontRole);
m_model->setData(m_markedIndex, QVariant(), Qt::FontRole);
m_markedIndex = m_markedIndex.parent();
}
// init new marked item
QList<QModelIndex> indices = m_pageToIndexMap.values(1 + page);
m_markedIndex = (indices.count() > 0 ? indices.last() : QModelIndex());
// special test for double page layout: if left page is not in toc check right page first
if (!m_markedIndex.isValid() && PdfViewer::document()->doubleSided() && page > 0 && (page % 2) == 1) {
indices = m_pageToIndexMap.values(2 + page);
m_markedIndex = (indices.count() > 0 ? indices.last() : QModelIndex());
}
// still no item found? check previous pages
while (!m_markedIndex.isValid() && page >= 0) {
indices = m_pageToIndexMap.values(1 + page--);
m_markedIndex = (indices.count() > 0 ? indices.first() : QModelIndex());
}
// if there is a selected index with the same page use this index instead
if (m_tree->selectionModel()) {
QModelIndexList selected = m_tree->selectionModel()->selectedIndexes();
for (const QModelIndex &idx : selected) {
QModelIndex sourceIndex = m_proxyModel->mapToSource(idx);
if (indices.contains(sourceIndex)) {
m_markedIndex = sourceIndex;
break;
}
}
}
if (m_markedIndex.isValid()) {
QFont font = m_tree->font();
font.setBold(true);
m_model->setData(m_markedIndex, font, Qt::FontRole);
m_model->setData(m_markedIndex, font, Qt::FontRole);
QModelIndex index = m_markedIndex.parent();
while (index.isValid()) {
m_model->setData(index, font, Qt::FontRole);
m_model->setData(index, font, Qt::FontRole);
index = index.parent();
}
}
}
void TocDock::slotVisibilityChanged(bool visible)
{
if (visible && !m_filled) {
fillInfo();
m_filled = true;
}
}
void TocDock::indexClicked(const QModelIndex &index)
{
QModelIndex firstColumnIndex = index.sibling(index.row(), 0);
QString dest = firstColumnIndex.data(DestinationRole).toString();
if (!dest.isEmpty())
PdfViewer::view()->gotoDestination(dest);
}
void TocDock::setFilter()
{
m_findStartTimer->stop();
m_proxyModel->setFilterRegularExpression(QRegularExpression(m_filter->text(), QRegularExpression::CaseInsensitiveOption));
m_proxyModel->invalidate();
}
void TocDock::expand()
{
if (!m_filter->text().isEmpty())
m_tree->expandAll();
}