3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
18 #include "TocWidget.h"
19 #include "FuncRequest.h"
20 #include "insets/InsetCommand.h"
23 #include "qt_helpers.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"
34 #include "support/convert.h"
43 GuiToc::GuiToc(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
44 : DockView(parent, "toc", area, flags), params_(TOC_CODE)
46 widget_ = new TocWidget(*this);
48 setWindowTitle(widget_->windowTitle());
59 void GuiToc::clearTocModels()
61 const unsigned int size = toc_models_.size();
62 for (unsigned int i = 0; i < size; ++i) {
63 delete toc_models_[i];
69 int GuiToc::getTocDepth(int type)
73 return toc_models_[type]->modelDepth();
77 QStandardItemModel * GuiToc::tocModel(int type)
82 if (toc_models_.empty()) {
83 LYXERR(Debug::GUI, "GuiToc::tocModel(): no types available ");
87 LYXERR(Debug::GUI, "GuiToc: type " << type
88 << " toc_models_.size() " << toc_models_.size());
90 BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
91 return toc_models_[type];
95 QModelIndex GuiToc::currentIndex(int type) const
100 // FIXME: The TocBackend infrastructure is not ready for LOF and LOT
101 // This is because a proper ParConstIterator is not constructed in
102 // InsetCaption::addToToc()
103 if(!canOutline(type))
104 return QModelIndex();
106 return toc_models_[type]->modelIndex(currentTocItem(type));
110 void GuiToc::goTo(int type, QModelIndex const & index)
112 if (type < 0 || !index.isValid()
113 || index.model() != toc_models_[type]) {
114 LYXERR(Debug::GUI, "GuiToc::goTo(): QModelIndex is invalid!");
118 BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
120 TocIterator const it = toc_models_[type]->tocIterator(index);
122 LYXERR(Debug::GUI, "GuiToc::goTo " << to_utf8(it->str()));
124 string const tmp = convert<string>(it->id());
125 dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
129 void GuiToc::updateView()
131 widget_->updateView();
135 TocList const & GuiToc::tocs() const
137 return buffer().masterBuffer()->tocBackend().tocs();
141 bool GuiToc::initialiseParams(string const & data)
143 InsetCommandMailer::string2params("toc", data, params_);
148 TocList const & tocs = buffer().masterBuffer()->tocBackend().tocs();
149 TocList::const_iterator it = tocs.begin();
150 TocList::const_iterator end = tocs.end();
151 for (; it != end; ++it) {
152 types_.push_back(it->first);
153 type_names_.push_back(guiName(it->first));
154 toc_models_.push_back(new TocModel(it->second));
157 string selected_type ;
158 if (params_["type"].empty()) //Then plain toc...
159 selected_type = params_.getCmdName();
161 selected_type = to_ascii(params_["type"]);
163 for (size_t i = 0; i != types_.size(); ++i) {
164 if (selected_type == types_[i]) {
175 bool GuiToc::canOutline(int type) const
177 return types_[type] == "tableofcontents";
181 void GuiToc::outlineUp()
183 dispatch(FuncRequest(LFUN_OUTLINE_UP));
187 void GuiToc::outlineDown()
189 dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
193 void GuiToc::outlineIn()
195 dispatch(FuncRequest(LFUN_OUTLINE_IN));
199 void GuiToc::outlineOut()
201 dispatch(FuncRequest(LFUN_OUTLINE_OUT));
205 void GuiToc::updateBackend()
207 buffer().masterBuffer()->tocBackend().update();
208 buffer().structureChanged();
212 TocIterator GuiToc::currentTocItem(int type) const
214 BOOST_ASSERT(bufferview());
215 ParConstIterator it(bufferview()->cursor());
216 return buffer().masterBuffer()->tocBackend().item(types_[type], it);
220 docstring GuiToc::guiName(string const & type) const
222 if (type == "tableofcontents")
223 return _("Table of Contents");
225 FloatList const & floats = buffer().params().getTextClass().floats();
226 if (floats.typeExist(type))
227 return _(floats.getType(type).listName());
233 void GuiToc::dispatchParams()
236 InsetCommandMailer::params2string("toc", params_);
237 dispatch(FuncRequest(getLfun(), lfun));
241 Dialog * createGuiToc(GuiView & lv)
243 GuiView & guiview = static_cast<GuiView &>(lv);
245 // On Mac show as a drawer at the right
246 return new GuiToc(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
248 return new GuiToc(guiview);
253 } // namespace frontend
256 #include "GuiToc_moc.cpp"