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