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