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