]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToc.cpp
next one
[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
20 #include "TocModel.h"
21 #include "qt_helpers.h"
22
23 #include "Buffer.h"
24 #include "BufferView.h"
25 #include "BufferParams.h"
26 #include "debug.h"
27 #include "FloatList.h"
28 #include "FuncRequest.h"
29 #include "gettext.h"
30
31 #include "frontends/LyXView.h"
32
33 #include "support/convert.h"
34
35 #include <algorithm>
36
37 using std::endl;
38 using std::string;
39
40
41 namespace lyx {
42 namespace frontend {
43
44 GuiToc::GuiToc(Dialog & dialog)
45         : ControlCommand(dialog, "toc")
46 {
47 }
48
49
50 int GuiToc::getTocDepth(int type)
51 {
52         if (type < 0)
53                 return 0;
54         return toc_models_[type]->modelDepth();
55 }
56
57
58 QStandardItemModel * GuiToc::tocModel(int type)
59 {
60         if (type < 0)
61                 return 0;
62
63         if (toc_models_.empty()) {
64                 LYXERR(Debug::GUI) << "GuiToc::tocModel(): no types available " << endl;
65                 return 0;
66         }
67
68         LYXERR(Debug::GUI)
69                 << "GuiToc: type " << type
70                 << "  toc_models_.size() " << toc_models_.size()
71                 << endl;
72
73         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
74         return toc_models_[type];
75 }
76
77
78 QModelIndex GuiToc::currentIndex(int type) const
79 {
80         if (type < 0)
81                 return QModelIndex();
82
83         // FIXME: The TocBackend infrastructure is not ready for LOF and LOT
84         // This is because a proper ParConstIterator is not constructed in
85         // InsetCaption::addToToc()
86         if(!canOutline(type))
87                 return QModelIndex();
88
89         return toc_models_[type]->modelIndex(currentTocItem(type));
90 }
91
92
93 void GuiToc::goTo(int type, QModelIndex const & index)
94 {
95         if (type < 0 || !index.isValid()
96                 || index.model() != toc_models_[type]) {
97                 LYXERR(Debug::GUI)
98                         << "GuiToc::goTo(): QModelIndex is invalid!"
99                         << endl;
100                 return;
101         }
102
103         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
104
105         TocIterator const it = toc_models_[type]->tocIterator(index);
106
107         LYXERR(Debug::GUI) << "GuiToc::goTo " << to_utf8(it->str()) << endl;
108
109         string const tmp = convert<string>(it->id());
110         lyxview().dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
111 }
112
113
114 void GuiToc::updateView()
115 {
116         toc_models_.clear();
117         TocList::const_iterator it = tocs().begin();
118         TocList::const_iterator end = tocs().end();
119         for (; it != end; ++it)
120                 toc_models_.push_back(new TocModel(it->second));
121 }
122
123
124 TocList const & GuiToc::tocs() const
125 {
126         return buffer().getMasterBuffer()->tocBackend().tocs();
127 }
128
129
130 bool GuiToc::initialiseParams(string const & data)
131 {
132         if (!ControlCommand::initialiseParams(data))
133                 return false;
134
135         updateView();
136         modelReset();
137
138         types_.clear();
139         type_names_.clear();
140         TocList const & tocs = buffer().getMasterBuffer()->
141                 tocBackend().tocs();
142         TocList::const_iterator it = tocs.begin();
143         TocList::const_iterator end = tocs.end();
144         for (; it != end; ++it) {
145                 types_.push_back(it->first);
146                 type_names_.push_back(guiName(it->first));
147         }
148
149         string selected_type ;
150         if(params()["type"].empty()) //Then plain toc...
151                 selected_type = params().getCmdName();
152         else
153                 selected_type = to_ascii(params()["type"]);
154         selected_type_ = -1;
155         for (size_t i = 0;  i != types_.size(); ++i) {
156                 if (selected_type == types_[i]) {
157                         selected_type_ = i;
158                         break;
159                 }
160         }
161
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().getMasterBuffer()->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         Buffer const * master = buffer().getMasterBuffer();
208         return master->tocBackend().item(types_[type], it);
209 }
210
211
212 docstring GuiToc::guiName(string const & type) const
213 {
214         if (type == "tableofcontents")
215                 return _("Table of Contents");
216
217         FloatList const & floats = buffer().params().getTextClass().floats();
218         if (floats.typeExist(type))
219                 return _(floats.getType(type).listName());
220
221         return _(type);
222 }
223
224
225 Dialog * createGuiToc(LyXView & lv)
226 {
227         GuiViewBase & guiview = static_cast<GuiViewBase &>(lv);
228 #ifdef Q_WS_MACX
229         // On Mac show as a drawer at the right
230         return new DockView<GuiToc, TocWidget>(guiview, "toc",
231                 Qt::RightDockWidgetArea, Qt::Drawer);
232 #else
233         return new DockView<GuiToc, TocWidget>(guiview, "toc");
234 #endif
235 }
236
237
238 } // namespace frontend
239 } // namespace lyx
240
241 #include "GuiToc_moc.cpp"