]> git.lyx.org Git - features.git/blob - src/frontends/qt2/QToc.C
Angus's compile patch.
[features.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 #include <stack>
12
13 #include <qslider.h>
14 #include "QTocDialog.h"
15 #include "QtLyXView.h"
16
17 #include "Dialogs.h"
18 #include "BufferView.h" 
19 #include "QToc.h"
20 #include "gettext.h"
21 #include "buffer.h"
22 #include "support/lstrings.h"
23 #include "lyxfunc.h"
24 #include "debug.h"
25
26 #include <qlistview.h>
27 #include <qcombobox.h>
28
29 using std::vector;
30 using std::pair;
31 using std::stack;
32 using std::endl;
33 using SigC::slot;
34
35
36 QToc::QToc(LyXView *v, Dialogs *d)
37         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
38         /*toclist(0),*/ depth(1)
39 {
40         d->showTOC.connect(slot(this, &QToc::showTOC));
41         d->createTOC.connect(slot(this, &QToc::createTOC));
42 }
43
44  
45 QToc::~QToc()
46 {
47         delete dialog_;
48 }
49
50
51 void QToc::showTOC(InsetCommand * const inset)
52 {
53         // FIXME: when could inset be 0 here ?
54         if (inset == 0)
55                 return;
56
57         inset_ = inset;
58         //FIXME ih_ = inset_->hide.connect(slot(this,&QToc::hide));
59         params = inset->params();
60         
61         show();
62 }
63
64
65 void QToc::createTOC(string const & arg)
66 {
67         if (inset_)
68                 close();
69
70         params.setFromString(arg);
71         show();
72 }
73
74
75 void QToc::updateToc(int newdepth)
76 {
77         if (!lv_->view()->available()) {
78                 //toclist.clear();
79                 dialog_->tocLV->clear();
80                 return;
81         }
82
83 #if 0 
84         vector< vector<Buffer::TocItem> > tmp =
85                 lv_->view()->buffer()->getTocList();
86
87         // Check if all elements are the same.
88         if (newdepth==depth && toclist.size() == tmp[type].size()) {
89                 unsigned int i = 0;
90                 for (; i < toclist.size(); ++i) {
91                         if (toclist[i] !=  tmp[type][i])
92                                 break;
93                 }
94                 if (i >= toclist.size())
95                         return;
96         }
97
98         depth=newdepth;
99
100         toclist = tmp[type];
101
102         dialog_->tocLV->clear();
103         if (toclist.empty()) 
104                 return;
105
106         dialog_->tocLV->setUpdatesEnabled(false);
107
108         int curdepth = 0;
109         stack< pair< QListViewItem *, QListViewItem *> > istack;
110         QListViewItem *last = 0;
111         QListViewItem *parent = 0;
112         QListViewItem *item;
113
114         // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
115         // rather than QListViewItem; and the TOC can move in and out an arbitrary number
116         // of levels
117
118         for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
119                 iter != toclist.end(); ++iter) {
120                 if (iter->depth == curdepth) {
121                         // insert it after the last one we processed
122                         if (!parent)
123                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
124                         else
125                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
126                 } else if (iter->depth > curdepth) {
127                         int diff = iter->depth - curdepth;
128                         // first save old parent and last
129                         while (diff--)
130                                 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
131                         item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
132                         parent = last;
133                 } else {
134                         int diff = curdepth - iter->depth;
135                         pair< QListViewItem *, QListViewItem * > top;
136                         // restore context
137                         while (diff--) {
138                                 top = istack.top();
139                                 istack.pop();
140                         }
141                         parent = top.first;
142                         last = top.second;
143                         // insert it after the last one we processed
144                         if (!parent)
145                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
146                         else
147                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
148                 }
149                 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
150                         << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
151                         << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
152                 item->setText(0,iter->str.c_str());
153                 item->setOpen(iter->depth < depth);
154                 curdepth = iter->depth;
155                 last = item;
156         }
157
158         dialog_->tocLV->setUpdatesEnabled(true);
159         dialog_->tocLV->update();
160 }
161
162
163 void QToc::setType(Buffer::TocType toctype)
164 {
165         type = toctype;
166         switch (type) {
167                 case Buffer::TOC_TOC:
168                         dialog_->setCaption(_("Table of Contents"));
169                         dialog_->tocLV->setColumnText(0,_("Table of Contents"));
170                         dialog_->depthSL->setEnabled(true);
171                         break;
172                 case Buffer::TOC_LOF:
173                         dialog_->setCaption(_("List of Figures"));
174                         dialog_->tocLV->setColumnText(0,_("List of Figures"));
175                         dialog_->depthSL->setEnabled(false);
176                         break;
177                 case Buffer::TOC_LOT:
178                         dialog_->setCaption(_("List of Tables"));
179                         dialog_->tocLV->setColumnText(0,_("List of Tables"));
180                         dialog_->depthSL->setEnabled(false);
181                         break;
182                 case Buffer::TOC_LOA:
183                         dialog_->setCaption(_("List of Algorithms"));
184                         dialog_->tocLV->setColumnText(0,_("List of Algorithms"));
185                         dialog_->depthSL->setEnabled(false);
186                         break;
187         }
188 }
189
190
191 void QToc::set_depth(int newdepth)
192 {
193         if (newdepth!=depth)
194                 updateToc(newdepth);
195 #endif 
196 }
197
198
199 void QToc::update()
200 {
201 #if 0 
202         if (params.getCmdName()=="tableofcontents") {
203                 setType(Buffer::TOC_TOC);
204                 dialog_->typeCO->setCurrentItem(0);
205         } else if (params.getCmdName()=="listoffigures") {
206                 setType(Buffer::TOC_LOF);
207                 dialog_->typeCO->setCurrentItem(1);
208         } else if (params.getCmdName()=="listoftables") {
209                 setType(Buffer::TOC_LOT);
210                 dialog_->typeCO->setCurrentItem(2);
211         } else {
212                 setType(Buffer::TOC_LOA);
213                 dialog_->typeCO->setCurrentItem(3);
214         }
215
216         updateToc(depth);
217 }
218
219
220 void QToc::select(const char *text)
221 {
222         if (!lv_->view()->available())
223                 return;
224
225         vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
226         for (; iter != toclist.end(); ++iter) {
227                 if (iter->str==text)
228                         break;
229         }
230         
231         if (iter==toclist.end()) {
232                 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
233                 return;
234         }
235
236         lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
237 }
238
239
240 void QToc::set_type(Buffer::TocType toctype)
241 {
242         if (toctype==type)
243                 return;
244
245         setType(toctype);
246         updateToc(depth);
247 #endif 
248 }
249
250
251 void QToc::show()
252 {
253         if (!dialog_)
254                 dialog_ = new QTocDialog(this, 0, _("LyX: Table of Contents"), false);
255
256         if (!dialog_->isVisible()) {
257                 h_ = d_->hideBufferDependent.connect(slot(this, &QToc::hide));
258                 //u_ = d_->updateBufferDependent.connect(slot(this, &QToc::update));
259         }
260
261         dialog_->raise();
262         dialog_->setActiveWindow();
263
264         update();
265         dialog_->show();
266 }
267
268
269 void QToc::close()
270 {
271         h_.disconnect();
272         u_.disconnect();
273         ih_.disconnect();
274         inset_ = 0;
275 }
276
277
278 void QToc::hide()
279 {
280         dialog_->hide();
281         close();
282 }