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