move LyXerr QString specialisation to support/qstring_helpers
[lyx.git] / src / TocBackend.cpp
blobfd588e97930e231ed13b28cce970781c7e41f0be
1 /**
2 * \file TocBackend.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
13 #include <config.h>
15 #include "TocBackend.h"
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Layout.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "ParIterator.h"
26 #include "TextClass.h"
28 #include "insets/InsetOptArg.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
34 #include "support/lassert.h"
36 using namespace std;
39 namespace lyx {
41 ///////////////////////////////////////////////////////////////////////////
43 // TocItem implementation
45 ///////////////////////////////////////////////////////////////////////////
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s)
48 : dit_(dit), depth_(d), str_(s)
53 int TocItem::id() const
55 return dit_.paragraph().id();
59 int TocItem::depth() const
61 return depth_;
65 docstring const & TocItem::str() const
67 return str_;
71 docstring const TocItem::asString() const
73 return docstring(4 * depth_, ' ') + str_;
77 FuncRequest TocItem::action() const
79 string const arg = convert<string>(dit_.paragraph().id())
80 + ' ' + convert<string>(dit_.pos());
81 return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
85 ///////////////////////////////////////////////////////////////////////////
87 // TocBackend implementation
89 ///////////////////////////////////////////////////////////////////////////
91 Toc const & TocBackend::toc(string const & type) const
93 // Is the type already supported?
94 TocList::const_iterator it = tocs_.find(type);
95 LASSERT(it != tocs_.end(), /**/);
97 return it->second;
101 Toc & TocBackend::toc(string const & type)
103 return tocs_[type];
107 void TocBackend::updateItem(DocIterator const & dit)
109 if (toc("tableofcontents").empty()) {
110 // FIXME: should not happen,
111 // a call to TocBackend::update() is missing somewhere
112 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
113 return;
116 BufferParams const & bufparams = buffer_->params();
117 const int min_toclevel = bufparams.documentClass().min_toclevel();
119 TocIterator toc_item = item("tableofcontents", dit);
121 docstring tocstring;
123 // For each paragraph, traverse its insets and let them add
124 // their toc items
125 Paragraph & par = toc_item->dit_.paragraph();
126 InsetList::const_iterator it = par.insetList().begin();
127 InsetList::const_iterator end = par.insetList().end();
128 for (; it != end; ++it) {
129 Inset & inset = *it->inset;
130 if (inset.lyxCode() == OPTARG_CODE) {
131 if (!tocstring.empty())
132 break;
133 Paragraph const & inset_par =
134 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
135 if (!par.labelString().empty())
136 tocstring = par.labelString() + ' ';
137 tocstring += inset_par.asString();
138 break;
142 int const toclevel = par.layout().toclevel;
143 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
144 && tocstring.empty())
145 tocstring = par.asString(AS_STR_LABEL);
147 const_cast<TocItem &>(*toc_item).str_ = tocstring;
151 void TocBackend::update()
153 tocs_.clear();
154 DocIterator dit;
155 buffer_->inset().addToToc(dit);
159 TocIterator TocBackend::item(string const & type,
160 DocIterator const & dit) const
162 TocList::const_iterator toclist_it = tocs_.find(type);
163 // Is the type supported?
164 LASSERT(toclist_it != tocs_.end(), /**/);
165 return toclist_it->second.item(dit);
169 TocIterator Toc::item(DocIterator const & dit) const
171 TocIterator last = begin();
172 TocIterator it = end();
173 if (it == last)
174 return it;
176 --it;
178 DocIterator dit_text = dit;
179 if (dit_text.inMathed()) {
180 // We are only interested in text so remove the math CursorSlice.
181 while (dit_text.inMathed())
182 dit_text.pop_back();
185 for (; it != last; --it) {
186 // We verify that we don't compare contents of two
187 // different document. This happens when you
188 // have parent and child documents.
189 if (&it->dit_[0].inset() != &dit_text[0].inset())
190 continue;
191 if (it->dit_ <= dit_text)
192 return it;
195 // We are before the first Toc Item:
196 return last;
200 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
202 TocList::const_iterator cit = tocs_.find(type);
203 if (cit != tocs_.end()) {
204 TocIterator ccit = cit->second.begin();
205 TocIterator end = cit->second.end();
206 for (; ccit != end; ++ccit)
207 os << ccit->asString() << from_utf8("\n");
212 } // namespace lyx