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