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