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