]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
From c7899a30a0b5975bf599a69ecd11ab25e1cdf1a4 Mon Sep 17 00:00:00 2001
[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 "qt_helpers.h"
23 #include "TocBackend.h"
24
25 #include "support/debug.h"
26 #include "support/lassert.h"
27
28 #include <QSortFilterProxyModel>
29 #include <QStandardItemModel>
30
31
32 #include <climits>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39 /// A QStandardItemModel that gives access to the reset methods.
40 /// This is needed in order to fix http://www.lyx.org/trac/ticket/3740
41 // FIXME: Better appropriately subclass QStandardItemModel and implement
42 // the toc-specific reset methods there.
43 class TocTypeModel : public QStandardItemModel
44 {
45 public:
46         ///
47         TocTypeModel(QObject * parent) : QStandardItemModel(parent)
48         {}
49         ///
50         void reset()
51         {
52                 QStandardItemModel::beginResetModel();
53                 QStandardItemModel::endResetModel();
54         }
55         ///
56         void beginResetModel()
57         {
58                 QStandardItemModel::beginResetModel();
59         }
60         ///
61         void endResetModel()
62         {
63                 QStandardItemModel::endResetModel();
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), toc_(new Toc()),
78           maxdepth_(0), mindepth_(0)
79 {
80         sorted_model_->setSortLocaleAware(true);
81         sorted_model_->setSourceModel(model_);
82 }
83
84
85 QAbstractItemModel * TocModel::model()
86 {
87         if (is_sorted_)
88                 return sorted_model_;
89         return model_;
90 }
91
92
93 QAbstractItemModel const * TocModel::model() const
94 {
95         if (is_sorted_)
96                 return sorted_model_;
97         return model_;
98 }
99
100
101 void TocModel::clear()
102 {
103         model_->blockSignals(true);
104         model_->clear();
105         toc_ = make_shared<Toc>();
106         model_->blockSignals(false);
107 }
108
109
110 void TocModel::sort(bool sort_it)
111 {
112         is_sorted_ = sort_it;
113         if (is_sorted_)
114                 sorted_model_->sort(0);
115 }
116
117
118 TocItem const & TocModel::tocItem(QModelIndex const & index) const
119 {
120         return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
121 }
122
123
124 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
125 {
126         if (toc_->empty())
127                 return QModelIndex();
128
129         unsigned int const toc_index = TocBackend::findItem(*toc_, dit) -
130                                        toc_->begin();
131
132         QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
133                 QVariant(toc_index), 1,
134                 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
135
136         LASSERT(!list.isEmpty(), return QModelIndex());
137         return list[0];
138 }
139
140
141 void TocModel::reset()
142 {
143         model_->reset();
144 }
145
146
147 void TocModel::setString(TocItem const & item, QModelIndex index)
148 {
149         // Use implicit sharing of QStrings
150         QString str = toqstr(item.asString());
151         model_->setData(index, str, Qt::DisplayRole);
152         model_->setData(index, str, Qt::ToolTipRole);
153 }
154
155
156 void TocModel::updateItem(DocIterator const & dit)
157 {
158         QModelIndex const index = modelIndex(dit);
159         setString(tocItem(index), index);
160 }
161
162
163 void TocModel::reset(shared_ptr<Toc const> toc)
164 {
165         toc_ = toc;
166         if (toc_->empty()) {
167                 maxdepth_ = 0;
168                 mindepth_ = 0;
169                 reset();
170                 return;
171         }
172
173         model_->blockSignals(true);
174         model_->beginResetModel();
175         model_->insertColumns(0, 1);
176         maxdepth_ = 0;
177         mindepth_ = INT_MAX;
178
179         size_t end = toc_->size();
180         for (unsigned int index = 0; index != end; ++index) {
181                 TocItem const & item = (*toc_)[index];
182                 maxdepth_ = max(maxdepth_, item.depth());
183                 mindepth_ = min(mindepth_, item.depth());
184                 int current_row = model_->rowCount();
185                 model_->insertRows(current_row, 1);
186                 QModelIndex top_level_item = model_->index(current_row, 0);
187                 setString(item, top_level_item);
188                 model_->setData(top_level_item, index, Qt::UserRole);
189
190                 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
191                         << ", added item " << item.asString());
192
193                 populate(index, top_level_item);
194                 if (index >= end)
195                         break;
196         }
197
198         model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
199         sorted_model_->setSourceModel(model_);
200         if (is_sorted_)
201                 sorted_model_->sort(0);
202         model_->blockSignals(false);
203         model_->endResetModel();
204 }
205
206
207 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
208 {
209         int curdepth = (*toc_)[index].depth() + 1;
210
211         QModelIndex child_item;
212         model_->insertColumns(0, 1, parent);
213
214         size_t end = toc_->size();
215         ++index;
216         for (; index != end; ++index) {
217                 TocItem const & item = (*toc_)[index];
218                 if (item.depth() < curdepth) {
219                         --index;
220                         return;
221                 }
222                 maxdepth_ = max(maxdepth_, item.depth());
223                 mindepth_ = min(mindepth_, item.depth());
224                 int current_row = model_->rowCount(parent);
225                 model_->insertRows(current_row, 1, parent);
226                 child_item = model_->index(current_row, 0, parent);
227                 setString(item, child_item);
228                 model_->setData(child_item, index, Qt::UserRole);
229                 populate(index, child_item);
230                 if (index >= end)
231                         break;
232         }
233 }
234
235
236 int TocModel::modelDepth() const
237 {
238         int const d = maxdepth_ - mindepth_;
239         LASSERT(d >= 0 && d <= 100, return 0);
240         return d;
241 }
242
243
244 ///////////////////////////////////////////////////////////////////////////////
245 //
246 // TocModels
247 //
248 ///////////////////////////////////////////////////////////////////////////////
249
250 TocModels::TocModels()
251         : bv_(0)
252 {
253         names_ = new TocTypeModel(this);
254         names_sorted_ = new TocModelSortProxyModel(this);
255         names_sorted_->setSourceModel(names_);
256         names_sorted_->setSortLocaleAware(true);
257         names_sorted_->sort(0);
258 }
259
260
261 void TocModels::clear()
262 {
263         names_->blockSignals(true);
264         names_->clear();
265         names_->blockSignals(false);
266         iterator end = models_.end();
267         for (iterator it = models_.begin(); it != end;  ++it)
268                 it.value()->clear();
269 }
270
271
272 int TocModels::depth(QString const & type)
273 {
274         const_iterator it = models_.find(type);
275         if (!bv_ || it == models_.end())
276                 return 0;
277         return it.value()->modelDepth();
278 }
279
280
281 QAbstractItemModel * TocModels::model(QString const & type)
282 {
283         if (!bv_)
284                 return 0;
285         iterator it = models_.find(type);
286         if (it != models_.end())
287                 return it.value()->model();
288         LYXERR0("type not found: " << type);
289         return 0;
290 }
291
292
293 QAbstractItemModel * TocModels::nameModel()
294 {
295         return names_sorted_;
296 }
297
298
299 QModelIndex TocModels::currentIndex(QString const & type) const
300 {
301         const_iterator it = models_.find(type);
302         if (!bv_ || it == models_.end())
303                 return QModelIndex();
304         return it.value()->modelIndex(bv_->cursor());
305 }
306
307
308 void TocModels::goTo(QString const & type, QModelIndex const & index) const
309 {
310         const_iterator it = models_.find(type);
311         if (it == models_.end() || !index.isValid()) {
312                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
313                 return;
314         }
315         LASSERT(index.model() == it.value()->model(), return);
316         TocItem const item = it.value()->tocItem(index);
317         LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
318         dispatch(item.action());
319 }
320
321
322 TocItem const TocModels::currentItem(QString const & type,
323         QModelIndex const & index) const
324 {
325         const_iterator it = models_.find(type);
326         if (it == models_.end() || !index.isValid()) {
327                 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
328                 return TocItem();
329         }
330         LASSERT(index.model() == it.value()->model(), return TocItem());
331
332         return it.value()->tocItem(index);
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"