1 /*
2  * Copyright (C) 2008, Pino Toscano <pino@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "toc.h"
20 
21 #include <poppler-qt4.h>
22 
23 #include <QtGui/QHeaderView>
24 #include <QtGui/QTreeWidget>
25 
fillToc(const QDomNode & parent,QTreeWidget * tree,QTreeWidgetItem * parentItem)26 static void fillToc(const QDomNode &parent, QTreeWidget *tree, QTreeWidgetItem *parentItem)
27 {
28     QTreeWidgetItem *newitem = 0;
29     for (QDomNode node = parent.firstChild(); !node.isNull(); node = node.nextSibling()) {
30         QDomElement e = node.toElement();
31 
32         if (!parentItem) {
33             newitem = new QTreeWidgetItem(tree, newitem);
34         } else {
35             newitem = new QTreeWidgetItem(parentItem, newitem);
36         }
37         newitem->setText(0, e.tagName());
38 
39         bool isOpen = false;
40         if (e.hasAttribute(QString::fromLatin1("Open"))) {
41             isOpen = QVariant(e.attribute(QString::fromLatin1("Open"))).toBool();
42         }
43         if (isOpen) {
44             tree->expandItem(newitem);
45         }
46 
47         if (e.hasChildNodes()) {
48             fillToc(node, tree, newitem);
49         }
50     }
51 }
52 
53 
TocDock(QWidget * parent)54 TocDock::TocDock(QWidget *parent)
55     : AbstractInfoDock(parent)
56 {
57     m_tree = new QTreeWidget(this);
58     setWidget(m_tree);
59     m_tree->setAlternatingRowColors(true);
60     m_tree->header()->hide();
61     setWindowTitle(tr("TOC"));
62     m_tree->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
63 }
64 
~TocDock()65 TocDock::~TocDock()
66 {
67 }
68 
fillInfo()69 void TocDock::fillInfo()
70 {
71     const QDomDocument *toc = document()->toc();
72     if (toc) {
73         fillToc(*toc, m_tree, 0);
74     } else {
75         QTreeWidgetItem *item = new QTreeWidgetItem();
76         item->setText(0, tr("No TOC"));
77         item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
78         m_tree->addTopLevelItem(item);
79     }
80 }
81 
documentClosed()82 void TocDock::documentClosed()
83 {
84     m_tree->clear();
85     AbstractInfoDock::documentClosed();
86 }
87 
88 #include "toc.moc"
89