]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToc.cpp
compile fix (missing forward declaration)
[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 "FloatList.h"
29 #include "FuncRequest.h"
30 #include "TextClass.h"
31
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/gettext.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, &parent);
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         return toc_models_[type]->modelIndex(currentTocItem(type));
100 }
101
102
103 void GuiToc::goTo(int type, QModelIndex const & index)
104 {
105         if (type < 0 || !index.isValid()
106                 || index.model() != toc_models_[type]) {
107                 LYXERR(Debug::GUI, "GuiToc::goTo(): QModelIndex is invalid!");
108                 return;
109         }
110
111         BOOST_ASSERT(type >= 0 && type < int(toc_models_.size()));
112
113         TocIterator const it = toc_models_[type]->tocIterator(index);
114
115         LYXERR(Debug::GUI, "GuiToc::goTo " << to_utf8(it->str()));
116
117         string const tmp = convert<string>(it->id());
118         dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
119 }
120
121
122 void GuiToc::updateView()
123 {
124         widget_->updateView();
125 }
126
127
128 TocList const & GuiToc::tocs() const
129 {
130         return buffer().masterBuffer()->tocBackend().tocs();
131 }
132
133
134 bool GuiToc::initialiseParams(string const & data)
135 {
136         LYXERR(Debug::GUI, data);
137         QString str = QString::fromUtf8(data.c_str());
138         QString new_type;
139         if (str.contains("tableofcontents"))
140                 new_type = "tableofcontents";
141         else if (str.contains("floatlist")) {
142                 if (str.contains("\"figure"))
143                         new_type = "figure";
144                 else if (str.contains("\"table"))
145                         new_type = "table";
146                 else if (str.contains("\"algorithm"))
147                         new_type = "algorithm";
148         } else if (!data.empty())
149                 new_type = toqstr(data);
150         else
151                 // Default to Outliner.
152                 new_type = "tableofcontents";
153
154         types_.clear();
155         type_names_.clear();
156         clearTocModels();
157         TocList const & tocs = buffer().masterBuffer()->tocBackend().tocs();
158         TocList::const_iterator it = tocs.begin();
159         TocList::const_iterator end = tocs.end();
160         for (; it != end; ++it) {
161                 types_.push_back(toqstr(it->first));
162                 type_names_.push_back(toqstr(guiName(it->first)));
163                 toc_models_.push_back(new TocModel(it->second));
164         }
165
166         int selected_type = -1;
167         for (int i = 0; i != types_.size(); ++i) {
168                 if (new_type == types_[i]) {
169                         selected_type = i;
170                         break;
171                 }
172         }
173         widget_->updateGui(selected_type);
174
175         return true;
176 }
177
178
179 bool GuiToc::canOutline(int type) const
180 {
181         return types_[type] == "tableofcontents";
182 }
183
184
185 void GuiToc::outlineUp()
186 {
187         dispatch(FuncRequest(LFUN_OUTLINE_UP));
188 }
189
190
191 void GuiToc::outlineDown()
192 {
193         dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
194 }
195
196
197 void GuiToc::outlineIn()
198 {
199         dispatch(FuncRequest(LFUN_OUTLINE_IN));
200 }
201
202
203 void GuiToc::outlineOut()
204 {
205         dispatch(FuncRequest(LFUN_OUTLINE_OUT));
206 }
207
208
209 void GuiToc::updateBackend()
210 {
211         buffer().masterBuffer()->tocBackend().update();
212         buffer().structureChanged();
213 }
214
215
216 TocIterator GuiToc::currentTocItem(int type) const
217 {
218         BOOST_ASSERT(bufferview());
219         ParConstIterator it(bufferview()->cursor());
220         return buffer().masterBuffer()->tocBackend().item(fromqstr(types_[type]), it);
221 }
222
223
224 docstring GuiToc::guiName(string const & type) const
225 {
226         if (type == "tableofcontents")
227                 return _("Table of Contents");
228         if (type == "child")
229                 return _("Child Documents");
230         if (type == "embedded")
231                 return _("Embedded Files");
232         if (type == "graphics")
233                 return _("List of Graphics");
234         if (type == "equation")
235                 return _("List of Equations");
236         if (type == "footnote")
237                 return _("List of Foot notes");
238         if (type == "listing")
239                 return _("List of Listings");
240         if (type == "index")
241                 return _("List of Indexes");
242         if (type == "marginalnote")
243                 return _("List of Marginal notes");
244         if (type == "note")
245                 return _("List of Notes");
246         if (type == "citation")
247                 return _("List of Citations");
248         if (type == "label")
249                 return _("Labels and References");
250
251         FloatList const & floats = buffer().params().documentClass().floats();
252         if (floats.typeExist(type))
253                 return _(floats.getType(type).listName());
254
255         return _(type);
256 }
257
258
259 void GuiToc::dispatchParams()
260 {
261 }
262
263
264 Dialog * createGuiToc(GuiView & lv)
265 {
266         GuiView & guiview = static_cast<GuiView &>(lv);
267 #ifdef Q_WS_MACX
268         // On Mac show as a drawer at the right
269         return new GuiToc(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
270 #else
271         return new GuiToc(guiview);
272 #endif
273 }
274
275
276 } // namespace frontend
277 } // namespace lyx
278
279 #include "GuiToc_moc.cpp"