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