]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/TocModel.cpp
Fix a strange warning in QtCreator:
[lyx.git] / src / frontends / qt / 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 // FIXME: Better appropriately subclass QStandardItemModel and implement
42 // the toc-specific reset methods there.
43 class TocTypeModel : public QStandardItemModel
44 {
45 public:
46         ///
47         TocTypeModel(QObject * parent) : QStandardItemModel(parent)
48         {}
49         ///
50         void reset()
51         {
52                 QStandardItemModel::beginResetModel();
53                 QStandardItemModel::endResetModel();
54         }
55         ///
56         void beginResetModel()
57         {
58                 QStandardItemModel::beginResetModel();
59         }
60         ///
61         void endResetModel()
62         {
63                 QStandardItemModel::endResetModel();
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), toc_(new Toc()),
78           maxdepth_(0), mindepth_(0)
79 {
80         sorted_model_->setSortLocaleAware(true);
81         sorted_model_->setSourceModel(model_);
82 }
83
84
85 QAbstractItemModel * TocModel::model()
86 {
87         if (is_sorted_)
88                 return sorted_model_;
89         return model_;
90 }
91
92
93 QAbstractItemModel const * TocModel::model() const
94 {
95         if (is_sorted_)
96                 return sorted_model_;
97         return model_;
98 }
99
100
101 void TocModel::clear()
102 {
103         model_->blockSignals(true);
104         model_->clear();
105         toc_ = make_shared<Toc>();
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 = TocBackend::findItem(*toc_, dit) -
130                                        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::setString(TocItem const & item, QModelIndex index)
148 {
149         // Use implicit sharing of QStrings
150         QString str = toqstr(item.asString());
151         model_->setData(index, str, Qt::DisplayRole);
152         model_->setData(index, str, Qt::ToolTipRole);
153 }
154
155
156 void TocModel::updateItem(DocIterator const & dit)
157 {
158         QModelIndex const index = modelIndex(dit);
159         setString(tocItem(index), index);
160 }
161
162
163 void TocModel::reset(shared_ptr<Toc const> toc)
164 {
165         toc_ = toc;
166         if (toc_->empty()) {
167                 maxdepth_ = 0;
168                 mindepth_ = 0;
169                 reset();
170                 return;
171         }
172
173         model_->blockSignals(true);
174         model_->beginResetModel();
175         model_->insertColumns(0, 1);
176         maxdepth_ = 0;
177         mindepth_ = INT_MAX;
178
179         size_t end = toc_->size();
180         for (unsigned int index = 0; index != end; ++index) {
181                 TocItem const & item = (*toc_)[index];
182                 maxdepth_ = max(maxdepth_, item.depth());
183                 mindepth_ = min(mindepth_, item.depth());
184                 int current_row = model_->rowCount();
185                 model_->insertRows(current_row, 1);
186                 QModelIndex top_level_item = model_->index(current_row, 0);
187                 setString(item, top_level_item);
188                 model_->setData(top_level_item, index, Qt::UserRole);
189
190                 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
191                         << ", added item " << item.asString());
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         QModelIndex child_item;
212         model_->insertColumns(0, 1, parent);
213
214         size_t end = toc_->size();
215         ++index;
216         for (; index != end; ++index) {
217                 TocItem const & item = (*toc_)[index];
218                 if (item.depth() < curdepth) {
219                         --index;
220                         return;
221                 }
222                 maxdepth_ = max(maxdepth_, item.depth());
223                 mindepth_ = min(mindepth_, item.depth());
224                 int current_row = model_->rowCount(parent);
225                 model_->insertRows(current_row, 1, parent);
226                 child_item = model_->index(current_row, 0, parent);
227                 setString(item, child_item);
228                 model_->setData(child_item, index, Qt::UserRole);
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 {
252         names_ = new TocTypeModel(this);
253         names_sorted_ = new TocModelSortProxyModel(this);
254         names_sorted_->setSourceModel(names_);
255         names_sorted_->setSortLocaleAware(true);
256         names_sorted_->sort(0);
257 }
258
259
260 void TocModels::clear()
261 {
262         names_->blockSignals(true);
263         names_->clear();
264         names_->blockSignals(false);
265         iterator end = models_.end();
266         for (iterator it = models_.begin(); it != end;  ++it)
267                 it.value()->clear();
268 }
269
270
271 int TocModels::depth(QString const & type)
272 {
273         const_iterator it = models_.find(type);
274         if (it == models_.end())
275                 return 0;
276         return it.value()->modelDepth();
277 }
278
279
280 QAbstractItemModel * TocModels::model(QString const & type)
281 {
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,
297                                     DocIterator const & dit) const
298 {
299         const_iterator it = models_.find(type);
300         if (it == models_.end())
301                 return QModelIndex();
302         return it.value()->modelIndex(dit);
303 }
304
305
306 FuncRequest TocModels::goTo(QString const & type, QModelIndex const & index) const
307 {
308         const_iterator it = models_.find(type);
309         if (it == models_.end() || !index.isValid()) {
310                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
311                 return FuncRequest(LFUN_NOACTION);
312         }
313         LASSERT(index.model() == it.value()->model(), return FuncRequest(LFUN_NOACTION));
314         TocItem const item = it.value()->tocItem(index);
315         LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
316         return item.action();
317 }
318
319
320 TocItem const TocModels::currentItem(QString const & type,
321         QModelIndex const & index) const
322 {
323         const_iterator it = models_.find(type);
324         if (it == models_.end() || !index.isValid()) {
325                 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
326                 return TocItem();
327         }
328         LASSERT(index.model() == it.value()->model(), return TocItem());
329
330         return it.value()->tocItem(index);
331 }
332
333
334 void TocModels::updateItem(QString const & type, DocIterator const & dit)
335 {
336         models_[type]->updateItem(dit);
337 }
338
339
340 void TocModels::reset(BufferView const * bv)
341 {
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         // In the outliner, add Tocs from the master document
355         TocBackend const & backend = bv->buffer().masterBuffer()->tocBackend();
356         for (auto const & toc : backend.tocs()) {
357                 QString const type = toqstr(toc.first);
358
359                 // First, fill in the toc models.
360                 iterator mod_it = models_.find(type);
361                 if (mod_it == models_.end())
362                         mod_it = models_.insert(type, new TocModel(this));
363                 mod_it.value()->reset(toc.second);
364
365                 // Fill in the names_ model.
366                 QString const gui_name = toqstr(backend.outlinerName(toc.first));
367                 int const current_row = names_->rowCount();
368                 names_->insertRows(current_row, 1);
369                 QModelIndex const index = names_->index(current_row, 0);
370                 names_->setData(index, gui_name, Qt::DisplayRole);
371                 names_->setData(index, type, Qt::UserRole);
372         }
373         names_->blockSignals(false);
374         names_->endResetModel();
375 }
376
377
378 bool TocModels::isSorted(QString const & type) const
379 {
380         const_iterator it = models_.find(type);
381         if (it == models_.end()) {
382                 LYXERR0("type not found: " << type);
383                 return false;
384         }
385         return it.value()->isSorted();
386 }
387
388
389 void TocModels::sort(QString const & type, bool sort_it)
390 {
391         iterator it = models_.find(type);
392         if (it == models_.end())
393                 LYXERR0("type not found: " << type);
394         else
395                 it.value()->sort(sort_it);
396 }
397
398 } // namespace frontend
399 } // namespace lyx
400
401 #include "moc_TocModel.cpp"