]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
On Linux show in crash message box the backtrace
[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), maxdepth_(0), mindepth_(0)
85 {
86         sorted_model_->setSortLocaleAware(true);
87         sorted_model_->setSourceModel(model_);
88 }
89
90
91 QAbstractItemModel * TocModel::model()
92 {
93         if (is_sorted_)
94                 return sorted_model_;
95         return model_;
96 }
97
98
99 QAbstractItemModel const * TocModel::model() const
100 {
101         if (is_sorted_)
102                 return sorted_model_;
103         return model_;
104 }
105
106
107 void TocModel::clear()
108 {
109         model_->blockSignals(true);
110         model_->clear();
111         model_->blockSignals(false);
112 }
113
114
115 void TocModel::sort(bool sort_it)
116 {
117         is_sorted_ = sort_it;
118         if (is_sorted_)
119                 sorted_model_->sort(0);
120 }
121
122
123 TocItem const & TocModel::tocItem(QModelIndex const & index) const
124 {
125         return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
126 }
127
128
129 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
130 {
131         if (toc_->empty())
132                 return QModelIndex();
133
134         unsigned int const toc_index = toc_->item(dit) - toc_->begin();
135
136         QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
137                 QVariant(toc_index), 1,
138                 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
139
140         LASSERT(!list.isEmpty(), return QModelIndex());
141         return list[0];
142 }
143
144
145 void TocModel::reset()
146 {
147         model_->reset();
148 }
149
150
151 void TocModel::updateItem(DocIterator const & dit)
152 {
153         QModelIndex index = modelIndex(dit);
154         TocItem const & toc_item = tocItem(index);
155         model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
156         model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
157 }
158
159
160 void TocModel::reset(Toc const & toc)
161 {
162         toc_ = &toc;
163         if (toc_->empty()) {
164                 maxdepth_ = 0;
165                 mindepth_ = 0;
166                 reset();
167                 return;
168         }
169
170         model_->blockSignals(true);
171         model_->beginResetModel();
172         model_->insertColumns(0, 1);
173         maxdepth_ = 0;
174         mindepth_ = INT_MAX;
175
176         size_t end = toc_->size();
177         for (unsigned int index = 0; index != end; ++index) {
178                 TocItem const & item = (*toc_)[index];
179                 maxdepth_ = max(maxdepth_, item.depth());
180                 mindepth_ = min(mindepth_, item.depth());
181                 int current_row = model_->rowCount();
182                 model_->insertRows(current_row, 1);
183                 QModelIndex top_level_item = model_->index(current_row, 0);
184                 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
185                 model_->setData(top_level_item, index, Qt::UserRole);
186                 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
187
188                 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
189                         << ", added item " << item.str());
190
191                 populate(index, top_level_item);
192                 if (index >= end)
193                         break;
194         }
195
196         model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
197         sorted_model_->setSourceModel(model_);
198         if (is_sorted_)
199                 sorted_model_->sort(0);
200         model_->blockSignals(false);
201         model_->endResetModel();
202 }
203
204
205 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
206 {
207         int curdepth = (*toc_)[index].depth() + 1;
208
209         int current_row;
210         QModelIndex child_item;
211         model_->insertColumns(0, 1, parent);
212
213         size_t end = toc_->size();
214         ++index;
215         for (; index != end; ++index) {
216                 TocItem const & item = (*toc_)[index];
217                 if (item.depth() < curdepth) {
218                         --index;
219                         return;
220                 }
221                 maxdepth_ = max(maxdepth_, item.depth());
222                 mindepth_ = min(mindepth_, item.depth());
223                 current_row = model_->rowCount(parent);
224                 model_->insertRows(current_row, 1, parent);
225                 child_item = model_->index(current_row, 0, parent);
226                 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
227                 model_->setData(child_item, index, Qt::UserRole);
228                 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
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.str());
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"