]> git.lyx.org Git - lyx.git/blob - src/frontends/kde/FormToc.C
Applied John's KDE patch
[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 "formtocdialog.h"
19  
20 #include "Dialogs.h"
21 #include "FormToc.h"
22 #include "gettext.h"
23 #include "buffer.h"
24 #include "support/lstrings.h"
25 #include "QtLyXView.h"
26 #include "lyxfunc.h" 
27 #include "debug.h" 
28
29 using std::vector;
30 using std::pair;
31  
32 FormToc::FormToc(LyXView *v, Dialogs *d)
33         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
34         toclist(0), type(Buffer::TOC_TOC)
35 {
36         // let the dialog be shown
37         // This is a permanent connection so we won't bother
38         // storing a copy because we won't be disconnecting.
39         d->showTOC.connect(slot(this, &FormToc::showTOC));
40         d->createTOC.connect(slot(this, &FormToc::createTOC));
41 }
42
43 FormToc::~FormToc()
44 {
45         delete dialog_;
46 }
47
48 void FormToc::showTOC(InsetCommand * const inset)
49 {
50         // FIXME: when could inset be 0 here ?
51         if (inset==0)
52                 return;
53
54         inset_ = inset;
55         ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
56         params = inset->params();
57         
58         show();
59 }
60  
61 void FormToc::createTOC(string const & arg)
62 {
63         if (inset_)
64                 close();
65  
66         params.setFromString(arg);
67         show();
68 }
69  
70 void FormToc::updateToc()
71 {
72         if (!lv_->view()->available()) {
73                 toclist.clear();
74                 dialog_->tree->clear();
75                 return;
76         }
77
78         vector< vector<Buffer::TocItem> > tmp =
79                 lv_->view()->buffer()->getTocList();
80
81         // Check if all elements are the same.
82         if (toclist.size() == tmp[type].size()) {
83                 unsigned int i = 0;
84                 for (; i < toclist.size(); ++i) {
85                         if (toclist[i] !=  tmp[type][i])
86                                 break;
87                 }
88                 if (i >= toclist.size()) 
89                         return;
90         }
91  
92         toclist = tmp[type];
93
94         dialog_->tree->clear();
95
96          
97         // FIXME: should do hierarchically. at each point we need to know
98         // id of item we've just inserted, id of most recent sibling, and
99         // id of parent
100  
101         dialog_->tree->setAutoUpdate(false);
102         for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
103                 iter != toclist.end(); ++iter) {
104                 dialog_->tree->insertItem((string(4*(*iter).depth,' ')+(*iter).str).c_str(), 0, -1, false);
105         }
106         dialog_->tree->setAutoUpdate(true);
107         dialog_->tree->update();
108 }
109  
110 void FormToc::setType(Buffer::TocType toctype)
111 {
112         type = toctype;
113         switch (type) {
114                 case Buffer::TOC_TOC:
115                         dialog_->setCaption(_("Table of Contents"));
116                         break; 
117                 case Buffer::TOC_LOF:
118                         dialog_->setCaption(_("List of Figures"));
119                         break; 
120                 case Buffer::TOC_LOT:
121                         dialog_->setCaption(_("List of Tables"));
122                         break; 
123                 case Buffer::TOC_LOA:
124                         dialog_->setCaption(_("List of Algorithms"));
125                         break; 
126         }
127 }
128  
129 void FormToc::update()
130 {
131         if (params.getCmdName()=="tableofcontents") {
132                 setType(Buffer::TOC_TOC);
133                 dialog_->menu->setCurrentItem(0);
134         } else if (params.getCmdName()=="listoffigures") {
135                 setType(Buffer::TOC_LOF); 
136                 dialog_->menu->setCurrentItem(1); 
137         } else if (params.getCmdName()=="listoftables") {
138                 setType(Buffer::TOC_LOT); 
139                 dialog_->menu->setCurrentItem(2); 
140         } else {
141                 setType(Buffer::TOC_LOA); 
142                 dialog_->menu->setCurrentItem(3); 
143         } 
144
145         updateToc();
146 }
147  
148 void FormToc::highlight(int index)
149 {
150         if (!lv_->view()->available())
151                 return;
152  
153         // FIXME: frontStrip can go once it's hierarchical
154         string tmp(frontStrip(dialog_->tree->itemAt(index)->getText(),' '));
155  
156         vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
157         for (; iter != toclist.end(); ++iter) {
158                 if (iter->str==tmp)
159                         break;
160         } 
161         
162         if (iter==toclist.end()) {
163                 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << tmp << endl;
164                 return;
165         }
166
167         lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
168 }
169  
170 void FormToc::set_type(Buffer::TocType toctype)
171 {
172         if (toctype==type)
173                 return;
174
175         setType(toctype);
176         updateToc();
177 }
178  
179 void FormToc::show()
180 {
181         if (!dialog_)
182                 dialog_ = new FormTocDialog(this, 0, _("LyX: Table of Contents"), false);
183  
184         if (!dialog_->isVisible()) {
185                 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
186                 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
187         }
188
189         dialog_->raise();
190         dialog_->setActiveWindow();
191  
192         update();
193         dialog_->show();
194 }
195
196 void FormToc::close()
197 {
198         h_.disconnect();
199         u_.disconnect();
200         ih_.disconnect();
201         inset_ = 0;
202 }
203  
204 void FormToc::hide()
205 {
206         dialog_->hide();
207         close();
208 }