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