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