]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToc.cpp
9f51e3d7f7165cf69fb53c0b885546503a6e078b
[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
21 #include "insets/InsetCommand.h"
22
23 #include "TocModel.h"
24 #include "qt_helpers.h"
25
26 #include "Buffer.h"
27 #include "BufferView.h"
28 #include "BufferParams.h"
29 #include "FloatList.h"
30 #include "FuncRequest.h"
31 #include "TextClass.h"
32
33 #include "support/convert.h"
34 #include "support/debug.h"
35 #include "support/gettext.h"
36
37 #include "support/assert.h"
38
39 #include <algorithm>
40
41 using namespace std;
42
43 namespace lyx {
44 namespace frontend {
45
46 GuiToc::GuiToc(GuiView & parent, Qt::DockWidgetArea area, Qt::WindowFlags flags)
47         : DockView(parent, "toc", qt_("Outline"), area, flags)
48 {
49         widget_ = new TocWidget(*this, &parent);
50         setWidget(widget_);
51 }
52
53
54 GuiToc::~GuiToc()
55 {
56         clearTocModels();
57         delete widget_;
58 }
59
60
61 void GuiToc::clearTocModels()
62 {
63         const unsigned int size = toc_models_.size();
64         for (unsigned int i = 0; i < size; ++i) {
65                 delete toc_models_[i];
66         }
67         toc_models_.clear();
68 }
69
70
71 int GuiToc::getTocDepth(int type)
72 {
73         if (type < 0)
74                 return 0;
75         return toc_models_[type]->modelDepth();
76 }
77
78
79 QStandardItemModel * GuiToc::tocModel(int type)
80 {
81         if (type < 0)
82                 return 0;
83
84         if (toc_models_.empty()) {
85                 LYXERR(Debug::GUI, "GuiToc::tocModel(): no types available ");
86                 return 0;
87         }
88
89         LYXERR(Debug::GUI, "GuiToc: type " << type
90                 << "  toc_models_.size() " << toc_models_.size());
91
92         LASSERT(type >= 0 && type < int(toc_models_.size()), /**/);
93         return toc_models_[type];
94 }
95
96
97 QModelIndex GuiToc::currentIndex(int type) const
98 {
99         if (type < 0)
100                 return QModelIndex();
101
102         return toc_models_[type]->modelIndex(currentTocItem(type));
103 }
104
105
106 void GuiToc::goTo(int type, QModelIndex const & index)
107 {
108         if (type < 0 || !index.isValid()
109                 || index.model() != toc_models_[type]) {
110                 LYXERR(Debug::GUI, "GuiToc::goTo(): QModelIndex is invalid!");
111                 return;
112         }
113
114         LASSERT(type >= 0 && type < int(toc_models_.size()), /**/);
115
116         TocIterator const it = toc_models_[type]->tocIterator(index);
117
118         LYXERR(Debug::GUI, "GuiToc::goTo " << to_utf8(it->str()));
119
120         string const tmp = convert<string>(it->id());
121         dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
122 }
123
124
125 void GuiToc::updateView()
126 {
127         widget_->updateView();
128 }
129
130
131 TocList const & GuiToc::tocs() const
132 {
133         return buffer().masterBuffer()->tocBackend().tocs();
134 }
135
136
137 bool GuiToc::initialiseParams(string const & data)
138 {
139         LYXERR(Debug::GUI, data);
140         types_.clear();
141         type_names_.clear();
142         clearTocModels();
143         if (!bufferview())
144                 // Nothing to show here.
145                 return true;
146
147         QString str = toqstr(data);
148         QString new_type;
149         if (str.contains("tableofcontents")) {
150                 new_type = "tableofcontents";
151         } else if (str.contains("floatlist")) {
152                 if (str.contains("\"figure"))
153                         new_type = "figure";
154                 else if (str.contains("\"table"))
155                         new_type = "table";
156                 else if (str.contains("\"algorithm"))
157                         new_type = "algorithm";
158         } else if (!str.isEmpty()) {
159                 new_type = str;
160         } else {
161                 // Default to Outliner.
162                 new_type = "tableofcontents";
163         }
164
165         TocList const & tocs = buffer().masterBuffer()->tocBackend().tocs();
166         TocList::const_iterator it = tocs.begin();
167         TocList::const_iterator end = tocs.end();
168         for (; it != end; ++it) {
169                 types_.push_back(toqstr(it->first));
170                 type_names_.push_back(toqstr(guiName(it->first)));
171                 toc_models_.push_back(new TocModel(it->second));
172         }
173
174         widget_->updateGui(types_.indexOf(new_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         LASSERT(bufferview(), /**/);
220         ParConstIterator it(bufferview()->cursor());
221         return buffer().masterBuffer()->tocBackend().item(fromqstr(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         if (type == "child")
230                 return _("Child Documents");
231         if (type == "graphics")
232                 return _("List of Graphics");
233         if (type == "equation")
234                 return _("List of Equations");
235         if (type == "footnote")
236                 return _("List of Footnotes");
237         if (type == "listing")
238                 return _("List of Listings");
239         if (type == "index")
240                 return _("List of Indexes");
241         if (type == "marginalnote")
242                 return _("List of Marginal notes");
243         if (type == "note")
244                 return _("List of Notes");
245         if (type == "citation")
246                 return _("List of Citations");
247         if (type == "label")
248                 return _("Labels and References");
249
250         FloatList const & floats = buffer().params().documentClass().floats();
251         if (floats.typeExist(type))
252                 return _(floats.getType(type).listName());
253
254         return _(type);
255 }
256
257
258 void GuiToc::dispatchParams()
259 {
260 }
261
262
263 Dialog * createGuiToc(GuiView & lv)
264 {
265         GuiView & guiview = static_cast<GuiView &>(lv);
266 #ifdef Q_WS_MACX
267         // On Mac show as a drawer at the right
268         return new GuiToc(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
269 #else
270         return new GuiToc(guiview);
271 #endif
272 }
273
274
275 } // namespace frontend
276 } // namespace lyx
277
278 #include "GuiToc_moc.cpp"