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