]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QToc.C
ws change
[lyx.git] / src / frontends / qt2 / QToc.C
1 /**
2  * \file QToc.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <stack>
16
17 #include <qslider.h>
18 #include <qlistview.h>
19 #include <qpushbutton.h>
20 #include <qcombobox.h>
21
22 #include "QTocDialog.h"
23 #include "QToc.h"
24 #include "Qt2BC.h"
25 #include "gettext.h"
26 #include "support/lstrings.h"
27 #include "debug.h"
28
29 #include "QtLyXView.h"
30
31 using std::endl;
32 using std::pair;
33 using std::stack;
34 using std::vector;
35
36 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
37
38 QToc::QToc(ControlToc & c)
39         : base_class(c, _("Table of contents"))
40 {}
41
42
43 void QToc::build_dialog()
44 {
45         dialog_.reset(new QTocDialog(this));
46
47         // Manage the cancel/close button
48         bc().setCancel(dialog_->closePB);
49 }
50
51
52 void QToc::updateType()
53 {
54         dialog_->typeCO->clear();
55
56         vector<string> const & choice = controller().getTypes();
57         string const & type = toc::getType(controller().params().getCmdName());
58
59         for (vector<string>::const_iterator it = choice.begin();
60                 it != choice.end(); ++it) {
61                 dialog_->typeCO->insertItem(it->c_str());
62                 if (*it == type) {
63                         dialog_->typeCO->setCurrentItem(it - choice.begin());
64                         dialog_->setCaption(type.c_str());
65                 }
66         }
67 }
68
69
70 void QToc::update_contents()
71 {
72         updateType();
73         updateToc(depth_);
74 }
75
76
77 void QToc::updateToc(int newdepth)
78 {
79         char const * str = dialog_->typeCO->currentText().latin1();
80         string type (str ? str : "");
81
82         Buffer::SingleList const & contents = controller().getContents(type);
83
84         // Check if all elements are the same.
85         if (newdepth == depth_ && toclist == contents) {
86                 return;
87         }
88
89         dialog_->tocLV->clear();
90
91         depth_ = newdepth;
92
93         toclist = contents;
94
95         if (toclist.empty())
96                 return;
97
98         dialog_->tocLV->setUpdatesEnabled(false);
99
100         int curdepth = 0;
101         stack< pair< QListViewItem *, QListViewItem *> > istack;
102         QListViewItem *last = 0;
103         QListViewItem *parent = 0;
104         QListViewItem *item;
105
106         // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
107         // rather than QListViewItem; and the TOC can move in and out an arbitrary number
108         // of levels
109
110         for (Buffer::SingleList::const_iterator iter = toclist.begin();
111                 iter != toclist.end(); ++iter) {
112                 if (iter->depth == curdepth) {
113                         // insert it after the last one we processed
114                         if (!parent)
115                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
116                         else
117                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
118                 } else if (iter->depth > curdepth) {
119                         int diff = iter->depth - curdepth;
120                         // first save old parent and last
121                         while (diff--)
122                                 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
123                         item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
124                         parent = last;
125                 } else {
126                         int diff = curdepth - iter->depth;
127                         pair< QListViewItem *, QListViewItem * > top;
128                         // restore context
129                         while (diff--) {
130                                 top = istack.top();
131                                 istack.pop();
132                         }
133                         parent = top.first;
134                         last = top.second;
135                         // insert it after the last one we processed
136                         if (!parent)
137                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
138                         else
139                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
140                 }
141
142                 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
143                         << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
144                         << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
145                 item->setText(0,iter->str.c_str());
146                 item->setOpen(iter->depth < depth_);
147                 curdepth = iter->depth;
148                 last = item;
149         }
150
151         dialog_->tocLV->setUpdatesEnabled(true);
152         dialog_->tocLV->update();
153 }
154
155
156 void QToc::select(string const & text)
157 {
158         Buffer::SingleList::const_iterator iter = toclist.begin();
159
160         for (; iter != toclist.end(); ++iter) {
161                 if (iter->str == text)
162                         break;
163         }
164
165         if (iter == toclist.end()) {
166                 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
167                         << text << endl;
168                 return;
169         }
170
171         controller().Goto(iter->par->id());
172 }
173
174
175 void QToc::set_depth(int depth)
176 {
177         if (depth != depth_)
178                 updateToc(depth);
179 }