]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
8bb33f9258c8fb23f7f06c380023337d1448d45a
[lyx.git] / src / frontends / qt4 / TocModel.cpp
1 /**
2  * \file TocModel.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "TocModel.h"
15
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "DocIterator.h"
20 #include "FuncRequest.h"
21 #include "LyXFunc.h"
22 #include "TocBackend.h"
23
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/lassert.h"
27
28 #include <QSortFilterProxyModel>
29
30 #include <climits>
31
32 using namespace std;
33
34 namespace lyx {
35 namespace frontend {
36
37 TocTypeModel::TocTypeModel(QObject * parent): QStandardItemModel(parent)
38 {
39 }
40
41
42 void TocTypeModel::reset()
43 {
44         QStandardItemModel::reset();
45 }
46
47
48 TocModel::TocModel(QObject * parent)
49         : model_(new TocTypeModel(parent)),
50         sorted_model_(new QSortFilterProxyModel(parent)),
51         is_sorted_(false), maxdepth_(0), mindepth_(0)
52 {
53 #if QT_VERSION >= 0x040300
54         sorted_model_->setSortLocaleAware(true);
55 #endif
56         sorted_model_->setSourceModel(model_);
57 }
58
59
60 QAbstractItemModel * TocModel::model()
61 {
62         if (is_sorted_)
63                 return sorted_model_;
64         return model_;
65 }
66
67
68 QAbstractItemModel const * TocModel::model() const
69 {
70         if (is_sorted_)
71                 return sorted_model_;
72         return model_;
73 }
74
75
76 void TocModel::clear()
77 {
78         model_->blockSignals(true);
79         model_->clear();
80         model_->blockSignals(false);
81 }
82
83
84 void TocModel::sort(bool sort_it)
85 {
86         is_sorted_ = sort_it;
87         if (is_sorted_)
88                 sorted_model_->sort(0);
89 }
90
91 TocItem const & TocModel::tocItem(QModelIndex const & index) const
92 {
93         return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
94 }
95
96
97 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
98 {
99         if (toc_->empty())
100                 return QModelIndex();
101
102         unsigned int const toc_index = toc_->item(dit) - toc_->begin();
103
104         QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
105                 QVariant(toc_index), 1,
106                 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
107
108         LASSERT(!list.isEmpty(), return QModelIndex());
109         return list[0];
110 }
111
112
113 void TocModel::reset()
114 {
115         model_->reset();
116 }
117
118
119 void TocModel::reset(Toc const & toc)
120 {
121         toc_ = &toc;
122         if (toc_->empty()) {
123                 maxdepth_ = 0;
124                 mindepth_ = 0;
125                 reset();
126                 return;
127         }
128
129         model_->blockSignals(true);
130         int current_row;
131         QModelIndex top_level_item;
132         model_->insertColumns(0, 1);
133         maxdepth_ = 0;
134         mindepth_ = INT_MAX;
135
136         size_t end = toc_->size();
137         for (unsigned int index = 0; index != end; ++index) {
138                 TocItem const & item = (*toc_)[index];
139                 maxdepth_ = max(maxdepth_, item.depth());
140                 mindepth_ = min(mindepth_, item.depth());
141                 current_row = model_->rowCount();
142                 model_->insertRows(current_row, 1);
143                 top_level_item = model_->index(current_row, 0);
144                 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
145                 model_->setData(top_level_item, index, Qt::UserRole);
146
147                 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
148                         << ", added item " << item.str());
149
150                 populate(index, top_level_item);
151                 if (index >= end)
152                         break;
153         }
154
155         model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
156         if (is_sorted_)
157                 sorted_model_->sort(0);
158         model_->blockSignals(false);
159         reset();
160 //      emit headerDataChanged();
161 }
162
163
164 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
165 {
166         int curdepth = (*toc_)[index].depth() + 1;
167
168         int current_row;
169         QModelIndex child_item;
170         model_->insertColumns(0, 1, parent);
171
172         size_t end = toc_->size();
173         ++index;
174         for (; index != end; ++index) {
175                 TocItem const & item = (*toc_)[index];
176                 if (item.depth() < curdepth) {
177                         --index;
178                         return;
179                 }
180                 maxdepth_ = max(maxdepth_, item.depth());
181                 mindepth_ = min(mindepth_, item.depth());
182                 current_row = model_->rowCount(parent);
183                 model_->insertRows(current_row, 1, parent);
184                 child_item = model_->index(current_row, 0, parent);
185                 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
186                 model_->setData(child_item, index, Qt::UserRole);
187                 populate(index, child_item);
188                 if (index >= end)
189                         break;
190         }
191 }
192
193
194 int TocModel::modelDepth() const
195 {
196         int const d = maxdepth_ - mindepth_;
197         LASSERT(d >= 0 && d <= 100, /* */);
198         return d;
199 }
200
201
202 ///////////////////////////////////////////////////////////////////////////////
203 // TocModels implementation.
204 ///////////////////////////////////////////////////////////////////////////////
205
206 TocModels::TocModels(): bv_(0)
207 {
208         names_ = new TocTypeModel(this);
209         names_sorted_ = new QSortFilterProxyModel(this);
210         names_sorted_->setSourceModel(names_);
211 #if QT_VERSION >= 0x040300
212         names_sorted_->setSortLocaleAware(true);
213 #endif
214         names_sorted_->sort(0);
215 }
216
217
218 void TocModels::clear() 
219 {
220         names_->blockSignals(true);
221         names_->clear();
222         names_->blockSignals(false);
223         iterator end = models_.end();
224         for (iterator it = models_.begin(); it != end;  ++it)
225                 it.value()->clear();
226 }
227
228
229 int TocModels::depth(QString const & type)
230 {
231         const_iterator it = models_.find(type);
232         if (!bv_ || it == models_.end())
233                 return 0;
234         return it.value()->modelDepth();
235 }
236
237
238 QAbstractItemModel * TocModels::model(QString const & type)
239 {
240         if (!bv_)
241                 return 0;
242         iterator it = models_.find(type);
243         if (it != models_.end())
244                 return it.value()->model();
245         LYXERR0("type not found: " << type);
246         return 0;
247 }
248
249
250 QAbstractItemModel * TocModels::nameModel()
251 {
252         return names_sorted_;
253 }
254
255
256 QModelIndex TocModels::currentIndex(QString const & type) const
257 {
258         const_iterator it = models_.find(type);
259         if (!bv_ || it == models_.end())
260                 return QModelIndex();
261         return it.value()->modelIndex(bv_->cursor());
262 }
263
264
265 void TocModels::goTo(QString const & type, QModelIndex const & index) const
266 {
267         const_iterator it = models_.find(type);
268         if (it == models_.end() || !index.isValid()) {
269                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
270                 return;
271         }
272         LASSERT(index.model() == it.value()->model(), return);
273         TocItem const item = it.value()->tocItem(index);
274         LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
275         dispatch(item.action());
276 }
277
278
279 void TocModels::updateBackend() const
280 {
281         bv_->buffer().masterBuffer()->tocBackend().update();
282         bv_->buffer().structureChanged();
283 }
284
285
286 void TocModels::reset(BufferView const * bv)
287 {
288         bv_ = bv;
289         clear();
290         if (!bv_) {
291                 iterator end = models_.end();
292                 for (iterator it = models_.begin(); it != end;  ++it)
293                         it.value()->reset();
294                 names_->reset();
295                 return;
296         }
297
298         names_->blockSignals(true);
299         names_->insertColumns(0, 1);
300         TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
301         TocList::const_iterator it = tocs.begin();
302         TocList::const_iterator toc_end = tocs.end();
303         for (; it != toc_end; ++it) {
304                 QString const type = toqstr(it->first);
305
306                 // First, fill in the toc models.
307                 iterator mod_it = models_.find(type);
308                 if (mod_it == models_.end())
309                         mod_it = models_.insert(type, new TocModel(this));
310                 mod_it.value()->reset(it->second);
311
312                 // Fill in the names_ model.
313                 QString const gui_name = guiName(it->first, bv->buffer().params());
314                 int const current_row = names_->rowCount();
315                 names_->insertRows(current_row, 1);
316                 QModelIndex const index = names_->index(current_row, 0);
317                 names_->setData(index, gui_name, Qt::DisplayRole);
318                 names_->setData(index, type, Qt::UserRole);
319         }
320         names_->blockSignals(false);
321         names_->reset();
322 }
323
324
325 bool TocModels::isSorted(QString const & type) const
326 {
327         const_iterator it = models_.find(type);
328         if (it == models_.end()) {
329                 LYXERR0("type not found: " << type);
330                 return false;
331         }
332         return it.value()->isSorted();
333 }
334
335
336 void TocModels::sort(QString const & type, bool sort_it)
337 {
338         iterator it = models_.find(type);
339         if (it == models_.end())
340                 LYXERR0("type not found: " << type);
341         else
342                 it.value()->sort(sort_it);
343 }
344
345 } // namespace frontend
346 } // namespace lyx
347
348 #include "TocModel_moc.cpp"