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