]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
Don't be so picky here.
[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 method.
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         ///
46         void reset()
47         {
48                 QStandardItemModel::reset();
49         }
50         ///
51         void beginResetModel()
52         { 
53         #if QT_VERSION >= 0x040600
54                 QStandardItemModel::beginResetModel(); 
55         #endif
56         }
57         ///
58         void endResetModel()
59         { 
60         #if QT_VERSION >= 0x040600
61                 QStandardItemModel::endResetModel(); 
62         #else
63                 reset();
64         #endif
65         }
66 };
67
68
69 ///////////////////////////////////////////////////////////////////////////////
70 //
71 // TocModel
72 //
73 ///////////////////////////////////////////////////////////////////////////////
74
75 TocModel::TocModel(QObject * parent)
76         : model_(new TocTypeModel(parent)),
77         sorted_model_(new QSortFilterProxyModel(parent)),
78         is_sorted_(false), maxdepth_(0), mindepth_(0)
79 {
80 #if QT_VERSION >= 0x040300
81         sorted_model_->setSortLocaleAware(true);
82 #endif
83         sorted_model_->setSourceModel(model_);
84 }
85
86
87 QAbstractItemModel * TocModel::model()
88 {
89         if (is_sorted_)
90                 return sorted_model_;
91         return model_;
92 }
93
94
95 QAbstractItemModel const * TocModel::model() const
96 {
97         if (is_sorted_)
98                 return sorted_model_;
99         return model_;
100 }
101
102
103 void TocModel::clear()
104 {
105         model_->blockSignals(true);
106         model_->clear();
107         model_->blockSignals(false);
108 }
109
110
111 void TocModel::sort(bool sort_it)
112 {
113         is_sorted_ = sort_it;
114         if (is_sorted_)
115                 sorted_model_->sort(0);
116 }
117
118
119 TocItem const & TocModel::tocItem(QModelIndex const & index) const
120 {
121         return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
122 }
123
124
125 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
126 {
127         if (toc_->empty())
128                 return QModelIndex();
129
130         unsigned int const toc_index = toc_->item(dit) - 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::updateItem(DocIterator const & dit)
148 {
149         QModelIndex index = modelIndex(dit);
150         TocItem const & toc_item = tocItem(index);
151         model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
152 }
153
154
155 void TocModel::reset(Toc const & toc)
156 {
157         toc_ = &toc;
158         if (toc_->empty()) {
159                 maxdepth_ = 0;
160                 mindepth_ = 0;
161                 reset();
162                 return;
163         }
164
165         model_->blockSignals(true);
166         model_->insertColumns(0, 1);
167         maxdepth_ = 0;
168         mindepth_ = INT_MAX;
169
170         size_t end = toc_->size();
171         for (unsigned int index = 0; index != end; ++index) {
172                 TocItem const & item = (*toc_)[index];
173                 maxdepth_ = max(maxdepth_, item.depth());
174                 mindepth_ = min(mindepth_, item.depth());
175                 int current_row = model_->rowCount();
176                 model_->insertRows(current_row, 1);
177                 QModelIndex top_level_item = model_->index(current_row, 0);
178                 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
179                 model_->setData(top_level_item, index, Qt::UserRole);
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         if (is_sorted_)
191                 sorted_model_->sort(0);
192         model_->blockSignals(false);
193         reset();
194 //      emit headerDataChanged();
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                 populate(index, child_item);
222                 if (index >= end)
223                         break;
224         }
225 }
226
227
228 int TocModel::modelDepth() const
229 {
230         int const d = maxdepth_ - mindepth_;
231         LASSERT(d >= 0 && d <= 100, return 0);
232         return d;
233 }
234
235
236 ///////////////////////////////////////////////////////////////////////////////
237 //
238 // TocModels
239 //
240 ///////////////////////////////////////////////////////////////////////////////
241
242 TocModels::TocModels()
243         : bv_(0)
244 {
245         names_ = new TocTypeModel(this);
246         names_sorted_ = new QSortFilterProxyModel(this);
247         names_sorted_->setSourceModel(names_);
248 #if QT_VERSION >= 0x040300
249         names_sorted_->setSortLocaleAware(true);
250 #endif
251         names_sorted_->sort(0);
252 }
253
254
255 void TocModels::clear()
256 {
257         names_->blockSignals(true);
258         names_->clear();
259         names_->blockSignals(false);
260         iterator end = models_.end();
261         for (iterator it = models_.begin(); it != end;  ++it)
262                 it.value()->clear();
263 }
264
265
266 int TocModels::depth(QString const & type)
267 {
268         const_iterator it = models_.find(type);
269         if (!bv_ || it == models_.end())
270                 return 0;
271         return it.value()->modelDepth();
272 }
273
274
275 QAbstractItemModel * TocModels::model(QString const & type)
276 {
277         if (!bv_)
278                 return 0;
279         iterator it = models_.find(type);
280         if (it != models_.end())
281                 return it.value()->model();
282         LYXERR0("type not found: " << type);
283         return 0;
284 }
285
286
287 QAbstractItemModel * TocModels::nameModel()
288 {
289         return names_sorted_;
290 }
291
292
293 QModelIndex TocModels::currentIndex(QString const & type) const
294 {
295         const_iterator it = models_.find(type);
296         if (!bv_ || it == models_.end())
297                 return QModelIndex();
298         return it.value()->modelIndex(bv_->cursor());
299 }
300
301
302 void TocModels::goTo(QString const & type, QModelIndex const & index) const
303 {
304         const_iterator it = models_.find(type);
305         if (it == models_.end() || !index.isValid()) {
306                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
307                 return;
308         }
309         LASSERT(index.model() == it.value()->model(), return);
310         TocItem const item = it.value()->tocItem(index);
311         LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
312         dispatch(item.action());
313 }
314
315
316 TocItem const TocModels::currentItem(QString const & type,
317         QModelIndex const & index) const
318 {
319         const_iterator it = models_.find(type);
320         if (it == models_.end() || !index.isValid()) {
321                 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
322                 return TocItem();
323         }
324         LASSERT(index.model() == it.value()->model(), return TocItem());
325         
326         return it.value()->tocItem(index);
327 }
328  
329
330 void TocModels::updateBackend() const
331 {
332         bv_->buffer().masterBuffer()->tocBackend().update();
333         bv_->buffer().structureChanged();
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"