]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToc.cpp
math stuff
[lyx.git] / src / frontends / qt4 / GuiToc.cpp
1 /**
2  * \file GuiToc.cpp
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  * \author Abdelrazak Younes
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiToc.h"
16 #include "GuiView.h"
17 #include "DockView.h"
18 #include "TocWidget.h"
19 #include "FuncRequest.h"
20 #include "insets/InsetCommand.h"
21
22 #include "TocModel.h"
23 #include "qt_helpers.h"
24
25 #include "Buffer.h"
26 #include "BufferView.h"
27 #include "BufferParams.h"
28 #include "debug.h"
29 #include "FloatList.h"
30 #include "FuncRequest.h"
31 #include "gettext.h"
32
33 #include "frontends/LyXView.h"
34
35 #include "support/convert.h"
36
37 #include <algorithm>
38
39 using std::endl;
40 using std::string;
41
42
43 namespace lyx {
44 namespace frontend {
45
46 GuiToc::GuiToc(Dialog & dialog)
47         : Controller(dialog), params_("toc")
48 {}
49
50
51 int GuiToc::getTocDepth(int type)
52 {
53         if (type < 0)
54                 return 0;
55         return toc_models_[type]->modelDepth();
56 }
57
58
59 QStandardItemModel * GuiToc::tocModel(int type)
60 {
61         if (type < 0)
62                 return 0;
63
64         if (toc_models_.empty()) {
65                 LYXERR(Debug::GUI) << "GuiToc::tocModel(): no types available " << endl;
66                 return 0;
67         }
68
69         LYXERR(Debug::GUI)
70                 << "GuiToc: type " << type
71                 << "  toc_models_.size() " << toc_models_.size()
72                 << endl;
73
74         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
75         return toc_models_[type];
76 }
77
78
79 QModelIndex GuiToc::currentIndex(int type) const
80 {
81         if (type < 0)
82                 return QModelIndex();
83
84         // FIXME: The TocBackend infrastructure is not ready for LOF and LOT
85         // This is because a proper ParConstIterator is not constructed in
86         // InsetCaption::addToToc()
87         if(!canOutline(type))
88                 return QModelIndex();
89
90         return toc_models_[type]->modelIndex(currentTocItem(type));
91 }
92
93
94 void GuiToc::goTo(int type, QModelIndex const & index)
95 {
96         if (type < 0 || !index.isValid()
97                 || index.model() != toc_models_[type]) {
98                 LYXERR(Debug::GUI)
99                         << "GuiToc::goTo(): QModelIndex is invalid!"
100                         << endl;
101                 return;
102         }
103
104         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
105
106         TocIterator const it = toc_models_[type]->tocIterator(index);
107
108         LYXERR(Debug::GUI) << "GuiToc::goTo " << to_utf8(it->str()) << endl;
109
110         string const tmp = convert<string>(it->id());
111         lyxview().dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
112 }
113
114
115 void GuiToc::updateView()
116 {
117         toc_models_.clear();
118         TocList::const_iterator it = tocs().begin();
119         TocList::const_iterator end = tocs().end();
120         for (; it != end; ++it)
121                 toc_models_.push_back(new TocModel(it->second));
122 }
123
124
125 TocList const & GuiToc::tocs() const
126 {
127         return buffer().getMasterBuffer()->tocBackend().tocs();
128 }
129
130
131 bool GuiToc::initialiseParams(string const & data)
132 {
133         InsetCommandMailer::string2params("toc", data, params_);
134
135         updateView();
136         modelReset();
137
138         types_.clear();
139         type_names_.clear();
140         TocList const & tocs = buffer().getMasterBuffer()->
141                 tocBackend().tocs();
142         TocList::const_iterator it = tocs.begin();
143         TocList::const_iterator end = tocs.end();
144         for (; it != end; ++it) {
145                 types_.push_back(it->first);
146                 type_names_.push_back(guiName(it->first));
147         }
148
149         string selected_type ;
150         if (params_["type"].empty()) //Then plain toc...
151                 selected_type = params_.getCmdName();
152         else
153                 selected_type = to_ascii(params_["type"]);
154         selected_type_ = -1;
155         for (size_t i = 0;  i != types_.size(); ++i) {
156                 if (selected_type == types_[i]) {
157                         selected_type_ = i;
158                         break;
159                 }
160         }
161
162         return true;
163 }
164
165
166 bool GuiToc::canOutline(int type) const
167 {
168         return types_[type] == "tableofcontents";
169 }
170
171
172 void GuiToc::outlineUp()
173 {
174         dispatch(FuncRequest(LFUN_OUTLINE_UP));
175 }
176
177
178 void GuiToc::outlineDown()
179 {
180         dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
181 }
182
183
184 void GuiToc::outlineIn()
185 {
186         dispatch(FuncRequest(LFUN_OUTLINE_IN));
187 }
188
189
190 void GuiToc::outlineOut()
191 {
192         dispatch(FuncRequest(LFUN_OUTLINE_OUT));
193 }
194
195
196 void GuiToc::updateBackend()
197 {
198         buffer().getMasterBuffer()->tocBackend().update();
199         buffer().structureChanged();
200 }
201
202
203 TocIterator GuiToc::currentTocItem(int type) const
204 {
205         BOOST_ASSERT(bufferview());
206         ParConstIterator it(bufferview()->cursor());
207         Buffer const * master = buffer().getMasterBuffer();
208         return master->tocBackend().item(types_[type], it);
209 }
210
211
212 docstring GuiToc::guiName(string const & type) const
213 {
214         if (type == "tableofcontents")
215                 return _("Table of Contents");
216
217         FloatList const & floats = buffer().params().getTextClass().floats();
218         if (floats.typeExist(type))
219                 return _(floats.getType(type).listName());
220
221         return _(type);
222 }
223
224
225 void GuiToc::dispatchParams()
226 {
227         string const lfun = 
228                 InsetCommandMailer::params2string("toc", params_);
229         dispatch(FuncRequest(getLfun(), lfun));
230 }
231
232
233 Dialog * createGuiToc(LyXView & lv)
234 {
235         GuiViewBase & guiview = static_cast<GuiViewBase &>(lv);
236 #ifdef Q_WS_MACX
237         // On Mac show as a drawer at the right
238         return new DockView<GuiToc, TocWidget>(guiview, "toc",
239                 Qt::RightDockWidgetArea, Qt::Drawer);
240 #else
241         return new DockView<GuiToc, TocWidget>(guiview, "toc");
242 #endif
243 }
244
245
246 } // namespace frontend
247 } // namespace lyx
248
249 #include "GuiToc_moc.cpp"