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