]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiToc.cpp
reduce line noise
[features.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 ");
75                 return 0;
76         }
77
78         LYXERR(Debug::GUI, "GuiToc: type " << type
79                 << "  toc_models_.size() " << toc_models_.size());
80
81         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
82         return toc_models_[type];
83 }
84
85
86 QModelIndex GuiToc::currentIndex(int type) const
87 {
88         if (type < 0)
89                 return QModelIndex();
90
91         // FIXME: The TocBackend infrastructure is not ready for LOF and LOT
92         // This is because a proper ParConstIterator is not constructed in
93         // InsetCaption::addToToc()
94         if(!canOutline(type))
95                 return QModelIndex();
96
97         return toc_models_[type]->modelIndex(currentTocItem(type));
98 }
99
100
101 void GuiToc::goTo(int type, QModelIndex const & index)
102 {
103         if (type < 0 || !index.isValid()
104                 || index.model() != toc_models_[type]) {
105                 LYXERR(Debug::GUI, "GuiToc::goTo(): QModelIndex is invalid!");
106                 return;
107         }
108
109         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
110
111         TocIterator const it = toc_models_[type]->tocIterator(index);
112
113         LYXERR(Debug::GUI, "GuiToc::goTo " << to_utf8(it->str()));
114
115         string const tmp = convert<string>(it->id());
116         lyxview().dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
117 }
118
119
120 void GuiToc::updateView()
121 {
122         widget_->updateView();
123 }
124
125
126 TocList const & GuiToc::tocs() const
127 {
128         return buffer().masterBuffer()->tocBackend().tocs();
129 }
130
131
132 bool GuiToc::initialiseParams(string const & data)
133 {
134         InsetCommandMailer::string2params("toc", data, params_);
135
136         types_.clear();
137         type_names_.clear();
138         toc_models_.clear();
139         TocList const & tocs = buffer().masterBuffer()->tocBackend().tocs();
140         TocList::const_iterator it = tocs.begin();
141         TocList::const_iterator end = tocs.end();
142         for (; it != end; ++it) {
143                 types_.push_back(it->first);
144                 type_names_.push_back(guiName(it->first));
145                 toc_models_.push_back(new TocModel(it->second));
146         }
147
148         string selected_type ;
149         if (params_["type"].empty()) //Then plain toc...
150                 selected_type = params_.getCmdName();
151         else
152                 selected_type = to_ascii(params_["type"]);
153         selected_type_ = -1;
154         for (size_t i = 0;  i != types_.size(); ++i) {
155                 if (selected_type == types_[i]) {
156                         selected_type_ = i;
157                         break;
158                 }
159         }
160
161         modelReset();
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().masterBuffer()->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         return buffer().masterBuffer()->tocBackend().item(types_[type], it);
208 }
209
210
211 docstring GuiToc::guiName(string const & type) const
212 {
213         if (type == "tableofcontents")
214                 return _("Table of Contents");
215
216         FloatList const & floats = buffer().params().getTextClass().floats();
217         if (floats.typeExist(type))
218                 return _(floats.getType(type).listName());
219
220         return _(type);
221 }
222
223
224 void GuiToc::dispatchParams()
225 {
226         string const lfun = 
227                 InsetCommandMailer::params2string("toc", params_);
228         dispatch(FuncRequest(getLfun(), lfun));
229 }
230
231
232 Dialog * createGuiToc(LyXView & lv)
233 {
234         GuiView & guiview = static_cast<GuiView &>(lv);
235 #ifdef Q_WS_MACX
236         // On Mac show as a drawer at the right
237         return new GuiToc(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
238 #else
239         return new GuiToc(guiview);
240 #endif
241 }
242
243
244 } // namespace frontend
245 } // namespace lyx
246
247 #include "GuiToc_moc.cpp"