forked from jonathanverner/comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc.cpp
223 lines (169 loc) · 5.6 KB
/
toc.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
/** This file is part of project comment
*
* File: toc.cpp
* Created: 2. 5. 2010
* Author: Jonathan Verner <[email protected]>
* License: GPL v2 or later
*
* Copyright (C) 2010 Jonathan Verner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "toc.h"
#include "linkLayer.h"
#include "pdfUtil.h"
#include <podofo/podofo.h>
using namespace PoDoFo;
void tocItem::appendChild ( tocItem* child ) {
children.append(child);
}
void tocItem::insertChild( tocItem* after, tocItem* child ) {
if ( ! after ) {
children.prepend( child );
return;
}
for( QList<tocItem *>::iterator it = children.begin(); it != children.end(); ++it ) {
if (*it == after ) {
children.insert( it, child );
break;
}
}
}
void tocItem::removeChild ( tocItem* child ) {
children.removeOne( child );
delete child;
}
tocItem::~tocItem() {
qDeleteAll(children);
};
toc::toc( linkLayer* Links, PoDoFo::PdfMemDocument* doc ): links(Links), root(NULL) {
load( doc );
};
toc::~toc() {
delete root;
}
void toc::load( PoDoFo::PdfMemDocument* doc ) {
if ( ! doc ) return;
PdfOutlineItem *toc_root = doc->GetOutlines();
if ( ! toc_root ) return;
PdfOutlineItem *child = toc_root->First();
delete root;
root = new tocItem( tr("Table of Contents") );
while( child ) {
tocItem *item = loadOutlineItem( child, root, "toc", doc );
root->appendChild( item );
child = child->Next();
}
}
tocItem* toc::loadOutlineItem( PdfOutlineItem* item, tocItem* parent, const QString& path, PoDoFo::PdfDocument *doc ) {
PdfDestination *dest = pdfUtil::getDestination(item, doc);
QString title = pdfUtil::pdfStringToQ( item->GetTitle() );
qDebug() << "Loading TOC item: " << path << "." << title;
targetItem *tgt = NULL;
QString nm = path+"."+title;
if ( dest ) tgt = links->addTarget( nm, dest, doc );
else tgt = NULL;
tocItem *me = new tocItem( title, parent, tgt );
PdfOutlineItem *child = item->First();
while( child ) {
tocItem *item = loadOutlineItem( child, me, path+"."+title, doc );
me->appendChild( item );
child = child->Next();
}
return me;
};
void toc::save ( PoDoFo::PdfMemDocument* doc )
{
}
QVariant tocItem::data(int column) const {
if ( column == 0 ) return getTitle();
else if ( column == 1 ) return getPage();
else return QVariant();
}
int tocItem::row() const {
if ( parent )
return parent->children.indexOf(const_cast<tocItem*>(this));
return 0;
}
tocItem* tocItem::child(int row) {
return children.value(row);
}
int tocItem::childCount() const {
return children.count();
}
int tocItem::getPage() const {
if ( tgt ) return tgt->getPage();
else return 0;
}
QModelIndex toc::index(int row, int column, const QModelIndex& parent) const {
if ( ! hasIndex( row, column, parent ) ) return QModelIndex();
tocItem *pItem;
if ( ! parent.isValid() ) pItem = root;
else pItem = static_cast<tocItem *>(parent.internalPointer());
tocItem *chItem = pItem->child(row);
if ( chItem ) return createIndex( row, column, chItem );
else return QModelIndex();
}
QModelIndex toc::parent(const QModelIndex& index) const {
if ( ! index.isValid() ) return QModelIndex();
tocItem *chItem = static_cast<tocItem *>(index.internalPointer());
tocItem *pItem = chItem->parentItem();
if ( pItem == root ) return QModelIndex();
return createIndex( pItem->row(), 0, pItem );
}
int toc::rowCount(const QModelIndex& parent) const {
tocItem * pItem;
if ( parent.column() > 0 ) return 0;
if ( ! parent.isValid() ) pItem = root;
else pItem = static_cast<tocItem *>(parent.internalPointer());
return pItem->childCount();
}
int toc::columnCount(const QModelIndex& parent) const {
if ( parent.isValid() ) return static_cast<tocItem *>(parent.internalPointer())->columnCount();
else return root->columnCount();
}
QVariant toc::data(const QModelIndex& index, int role) const {
if ( !index.isValid() ) return QVariant();
tocItem *item = static_cast<tocItem *>(index.internalPointer());
if ( role == Qt::ToolTipRole ) {
QString ret = "Page "+ QString::number(item->getPage()+1);
return ret;
}
if ( role == Qt::DisplayRole ) {
return item->data(index.column());
}
return QVariant();
}
tocItem* toc::getItem(const QModelIndex& index ) {
if ( !index.isValid() ) return NULL;
return static_cast<tocItem *>(index.internalPointer());
}
Qt::ItemFlags toc::flags(const QModelIndex& index) const {
if ( ! index.isValid() ) return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant toc::headerData(int section, Qt::Orientation orientation, int role) const {
if ( orientation != Qt::Horizontal || role != Qt::DisplayRole ) return QVariant();
switch (section) {
case 0:
return "Table of Contents";
break;
case 1:
return "Page";
break;
};
}
#include "toc.moc"