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