]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
c2cda38c490c2ef0f9fc8abdac0bfa74e0a40276
[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_(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_ = 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.str()), 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.str()), 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.str());
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         int current_row;
212         QModelIndex child_item;
213         model_->insertColumns(0, 1, parent);
214
215         size_t end = toc_->size();
216         ++index;
217         for (; index != end; ++index) {
218                 TocItem const & item = (*toc_)[index];
219                 if (item.depth() < curdepth) {
220                         --index;
221                         return;
222                 }
223                 maxdepth_ = max(maxdepth_, item.depth());
224                 mindepth_ = min(mindepth_, item.depth());
225                 current_row = model_->rowCount(parent);
226                 model_->insertRows(current_row, 1, parent);
227                 child_item = model_->index(current_row, 0, parent);
228                 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
229                 model_->setData(child_item, index, Qt::UserRole);
230                 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
231                 populate(index, child_item);
232                 if (index >= end)
233                         break;
234         }
235 }
236
237
238 int TocModel::modelDepth() const
239 {
240         int const d = maxdepth_ - mindepth_;
241         LASSERT(d >= 0 && d <= 100, return 0);
242         return d;
243 }
244
245
246 ///////////////////////////////////////////////////////////////////////////////
247 //
248 // TocModels
249 //
250 ///////////////////////////////////////////////////////////////////////////////
251
252 TocModels::TocModels()
253         : bv_(0)
254 {
255         names_ = new TocTypeModel(this);
256         names_sorted_ = new TocModelSortProxyModel(this);
257         names_sorted_->setSourceModel(names_);
258         names_sorted_->setSortLocaleAware(true);
259         names_sorted_->sort(0);
260 }
261
262
263 void TocModels::clear()
264 {
265         names_->blockSignals(true);
266         names_->clear();
267         names_->blockSignals(false);
268         iterator end = models_.end();
269         for (iterator it = models_.begin(); it != end;  ++it)
270                 it.value()->clear();
271 }
272
273
274 int TocModels::depth(QString const & type)
275 {
276         const_iterator it = models_.find(type);
277         if (!bv_ || it == models_.end())
278                 return 0;
279         return it.value()->modelDepth();
280 }
281
282
283 QAbstractItemModel * TocModels::model(QString const & type)
284 {
285         if (!bv_)
286                 return 0;
287         iterator it = models_.find(type);
288         if (it != models_.end())
289                 return it.value()->model();
290         LYXERR0("type not found: " << type);
291         return 0;
292 }
293
294
295 QAbstractItemModel * TocModels::nameModel()
296 {
297         return names_sorted_;
298 }
299
300
301 QModelIndex TocModels::currentIndex(QString const & type) const
302 {
303         const_iterator it = models_.find(type);
304         if (!bv_ || it == models_.end())
305                 return QModelIndex();
306         return it.value()->modelIndex(bv_->cursor());
307 }
308
309
310 void TocModels::goTo(QString const & type, QModelIndex const & index) const
311 {
312         const_iterator it = models_.find(type);
313         if (it == models_.end() || !index.isValid()) {
314                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
315                 return;
316         }
317         LASSERT(index.model() == it.value()->model(), return);
318         TocItem const item = it.value()->tocItem(index);
319         LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
320         dispatch(item.action());
321 }
322
323
324 TocItem const TocModels::currentItem(QString const & type,
325         QModelIndex const & index) const
326 {
327         const_iterator it = models_.find(type);
328         if (it == models_.end() || !index.isValid()) {
329                 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
330                 return TocItem();
331         }
332         LASSERT(index.model() == it.value()->model(), return TocItem());
333         
334         return it.value()->tocItem(index);
335 }
336  
337
338 void TocModels::updateItem(QString const & type, DocIterator const & dit)
339 {
340         models_[type]->updateItem(dit);
341 }
342
343
344 void TocModels::reset(BufferView const * bv)
345 {
346         bv_ = bv;
347         clear();
348         if (!bv_) {
349                 iterator end = models_.end();
350                 for (iterator it = models_.begin(); it != end;  ++it)
351                         it.value()->reset();
352                 names_->reset();
353                 return;
354         }
355
356         names_->blockSignals(true);
357         names_->beginResetModel();
358         names_->insertColumns(0, 1);
359         TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
360         TocList::const_iterator it = tocs.begin();
361         TocList::const_iterator toc_end = tocs.end();
362         for (; it != toc_end; ++it) {
363                 QString const type = toqstr(it->first);
364
365                 // First, fill in the toc models.
366                 iterator mod_it = models_.find(type);
367                 if (mod_it == models_.end())
368                         mod_it = models_.insert(type, new TocModel(this));
369                 mod_it.value()->reset(it->second);
370
371                 // Fill in the names_ model.
372                 QString const gui_name = guiName(it->first, bv->buffer().params());
373                 int const current_row = names_->rowCount();
374                 names_->insertRows(current_row, 1);
375                 QModelIndex const index = names_->index(current_row, 0);
376                 names_->setData(index, gui_name, Qt::DisplayRole);
377                 names_->setData(index, type, Qt::UserRole);
378         }
379         names_->blockSignals(false);
380         names_->endResetModel();
381 }
382
383
384 bool TocModels::isSorted(QString const & type) const
385 {
386         const_iterator it = models_.find(type);
387         if (it == models_.end()) {
388                 LYXERR0("type not found: " << type);
389                 return false;
390         }
391         return it.value()->isSorted();
392 }
393
394
395 void TocModels::sort(QString const & type, bool sort_it)
396 {
397         iterator it = models_.find(type);
398         if (it == models_.end())
399                 LYXERR0("type not found: " << type);
400         else
401                 it.value()->sort(sort_it);
402 }
403
404 } // namespace frontend
405 } // namespace lyx
406
407 #include "moc_TocModel.cpp"