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