]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiToc.cpp
* GuiToc: make it work.
[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         widget_->updateView();
122 }
123
124
125 TocList const & GuiToc::tocs() const
126 {
127         return buffer().getMasterBuffer()->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().getMasterBuffer()->
139                 tocBackend().tocs();
140         TocList::const_iterator it = tocs.begin();
141         TocList::const_iterator end = tocs.end();
142         for (; it != end; ++it) {
143                 types_.push_back(it->first);
144                 type_names_.push_back(guiName(it->first));
145                 toc_models_.push_back(new TocModel(it->second));
146         }
147
148         string selected_type ;
149         if (params_["type"].empty()) //Then plain toc...
150                 selected_type = params_.getCmdName();
151         else
152                 selected_type = to_ascii(params_["type"]);
153         selected_type_ = -1;
154         for (size_t i = 0;  i != types_.size(); ++i) {
155                 if (selected_type == types_[i]) {
156                         selected_type_ = i;
157                         break;
158                 }
159         }
160
161         modelReset();
162         return true;
163 }
164
165
166 bool GuiToc::canOutline(int type) const
167 {
168         return types_[type] == "tableofcontents";
169 }
170
171
172 void GuiToc::outlineUp()
173 {
174         dispatch(FuncRequest(LFUN_OUTLINE_UP));
175 }
176
177
178 void GuiToc::outlineDown()
179 {
180         dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
181 }
182
183
184 void GuiToc::outlineIn()
185 {
186         dispatch(FuncRequest(LFUN_OUTLINE_IN));
187 }
188
189
190 void GuiToc::outlineOut()
191 {
192         dispatch(FuncRequest(LFUN_OUTLINE_OUT));
193 }
194
195
196 void GuiToc::updateBackend()
197 {
198         buffer().getMasterBuffer()->tocBackend().update();
199         buffer().structureChanged();
200 }
201
202
203 TocIterator GuiToc::currentTocItem(int type) const
204 {
205         BOOST_ASSERT(bufferview());
206         ParConstIterator it(bufferview()->cursor());
207         Buffer const * master = buffer().getMasterBuffer();
208         return master->tocBackend().item(types_[type], it);
209 }
210
211
212 docstring GuiToc::guiName(string const & type) const
213 {
214         if (type == "tableofcontents")
215                 return _("Table of Contents");
216
217         FloatList const & floats = buffer().params().getTextClass().floats();
218         if (floats.typeExist(type))
219                 return _(floats.getType(type).listName());
220
221         return _(type);
222 }
223
224
225 void GuiToc::dispatchParams()
226 {
227         string const lfun = 
228                 InsetCommandMailer::params2string("toc", params_);
229         dispatch(FuncRequest(getLfun(), lfun));
230 }
231
232
233 Dialog * createGuiToc(LyXView & lv)
234 {
235         GuiViewBase & guiview = static_cast<GuiViewBase &>(lv);
236 #ifdef Q_WS_MACX
237         // On Mac show as a drawer at the right
238         return new GuiToc(guiview, Qt::RightDockWidgetArea, Qt::Drawer);
239 #else
240         return new GuiToc(guiview);
241 #endif
242 }
243
244
245 } // namespace frontend
246 } // namespace lyx
247
248 #include "GuiToc_moc.cpp"