-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.cpp
More file actions
173 lines (143 loc) · 4.84 KB
/
toc.cpp
File metadata and controls
173 lines (143 loc) · 4.84 KB
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
/*
* Copyright (C) 2008, Pino Toscano <pino@kde.org>
*
* 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 "toc.h"
//#include <poppler-qt4.h>
#include <poppler-link.h>
#include <QtGui/QHeaderView>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QAction>
#include <QDebug>
void TocDock::fillToc(const QDomNode &parent, QTreeWidget *tree, QTreeWidgetItem *parentItem)
{
QTreeWidgetItem *newitem = 0;
for (QDomNode node = parent.firstChild(); !node.isNull(); node = node.nextSibling()) {
QDomElement e = node.toElement();
if (!parentItem) {
newitem = new QTreeWidgetItem(tree, newitem);
} else {
newitem = new QTreeWidgetItem(parentItem, newitem);
}
newitem->setText(0, e.tagName());
QString dest = e.attribute("Destination");
if(!dest.isEmpty())
{
Poppler::LinkDestination ld = Poppler::LinkDestination(dest);
newitem->setData(0, Qt::UserRole, QVariant(ld.pageNumber()-1));
} else
{
dest = e.attribute("DestinationName");
if (!dest.isEmpty())
{
Poppler::LinkDestination* ld = document()->linkDestination(dest);
newitem->setData(0, Qt::UserRole, QVariant(ld->pageNumber()-1));
}
else
newitem->setData(0, Qt::UserRole, QVariant(-1));
}
bool isOpen = false;
if (e.hasAttribute(QString::fromLatin1("Open"))) {
isOpen = QVariant(e.attribute(QString::fromLatin1("Open"))).toBool();
}
if (isOpen) {
tree->expandItem(newitem);
}
}
}
TocDock::TocDock(QWidget *parent)
: AbstractInfoDock(parent)
{
flickCharm = new FlickCharm(this);
setFeatures(QDockWidget::NoDockWidgetFeatures);
setTitleBarWidget(new QWidget());
setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding);
/*QWidget *mainWidget = new QWidget(this);
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setSizeConstraint(QLayout::SetNoConstraint);
layout->setAlignment(Qt::AlignHCenter);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
mainWidget->setLayout(layout);
mainWidget->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Minimum);*/
m_tree = new QTreeWidget(this);
m_tree->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Minimum);
m_tree->setAlternatingRowColors(true);
m_tree->header()->hide();
m_tree->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
m_tree->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
QFont treeFont = QFont();
treeFont.setPixelSize(13);
m_tree->setFont(treeFont);
flickCharm->activateOn(m_tree);
connect(m_tree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(itemClick(QTreeWidgetItem*, int)));
connect(m_tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(itemDoubleClick(QTreeWidgetItem*, int)));
/*QPushButton* TOCButton = new QPushButton("TOC");
TOCButton->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding);
TOCButton->setFlat(true);
TOCButton->setFocusPolicy(Qt::NoFocus);
TOCButton->setStyleSheet("padding: 6px; margin: 0px; border: none; background-color: #D0D8E1");
connect(TOCButton, SIGNAL (clicked()),this, SLOT (toggleTOC()));*/
//layout->addWidget(m_tree);
//layout->addWidget(TOCButton);
setWidget(m_tree);
hide();
}
TocDock::~TocDock()
{
}
void TocDock::fillInfo()
{
const QDomDocument *toc = document()->toc();
if (toc) {
fillToc(*toc, m_tree, 0);
} else {
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, tr("No TOC"));
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
m_tree->addTopLevelItem(item);
}
m_tree->resizeColumnToContents(0);
}
void TocDock::documentClosed()
{
m_tree->clear();
AbstractInfoDock::documentClosed();
}
void TocDock::documentLoaded()
{
fillInfo();
m_filled = true;
}
void TocDock::toggleTOC()
{
if(isVisible())
hide();
else
show();
}
void TocDock::itemClick(QTreeWidgetItem* qmi, int col)
{
qmi->setExpanded(!qmi->isExpanded());
m_tree->resizeColumnToContents(col);
int page = qmi->data(0, Qt::UserRole).toInt();
if(page >= 0)
setPage(page);
}
void TocDock::itemDoubleClick(QTreeWidgetItem* qmi, int col)
{
}