]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
Replace auto_ptr with unique_ptr
[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 // 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_(lyx::make_shared<Toc const>()),
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_ = lyx::make_shared<Toc const>();
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::updateItem(DocIterator const & dit)
148 {
149         QModelIndex index = modelIndex(dit);
150         TocItem const & toc_item = tocItem(index);
151         model_->setData(index, toqstr(toc_item.asString()), Qt::DisplayRole);
152         model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
153 }
154
155
156 void TocModel::reset(shared_ptr<Toc const> toc)
157 {
158         toc_ = toc;
159         if (toc_->empty()) {
160                 maxdepth_ = 0;
161                 mindepth_ = 0;
162                 reset();
163                 return;
164         }
165
166         model_->blockSignals(true);
167         model_->beginResetModel();
168         model_->insertColumns(0, 1);
169         maxdepth_ = 0;
170         mindepth_ = INT_MAX;
171
172         size_t end = toc_->size();
173         for (unsigned int index = 0; index != end; ++index) {
174                 TocItem const & item = (*toc_)[index];
175                 maxdepth_ = max(maxdepth_, item.depth());
176                 mindepth_ = min(mindepth_, item.depth());
177                 int current_row = model_->rowCount();
178                 model_->insertRows(current_row, 1);
179                 QModelIndex top_level_item = model_->index(current_row, 0);
180                 model_->setData(top_level_item, toqstr(item.asString()), Qt::DisplayRole);
181                 model_->setData(top_level_item, index, Qt::UserRole);
182                 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
183
184                 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
185                         << ", added item " << item.asString());
186
187                 populate(index, top_level_item);
188                 if (index >= end)
189                         break;
190         }
191
192         model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
193         sorted_model_->setSourceModel(model_);
194         if (is_sorted_)
195                 sorted_model_->sort(0);
196         model_->blockSignals(false);
197         model_->endResetModel();
198 }
199
200
201 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
202 {
203         int curdepth = (*toc_)[index].depth() + 1;
204
205         QModelIndex child_item;
206         model_->insertColumns(0, 1, parent);
207
208         size_t end = toc_->size();
209         ++index;
210         for (; index != end; ++index) {
211                 TocItem const & item = (*toc_)[index];
212                 if (item.depth() < curdepth) {
213                         --index;
214                         return;
215                 }
216                 maxdepth_ = max(maxdepth_, item.depth());
217                 mindepth_ = min(mindepth_, item.depth());
218                 int current_row = model_->rowCount(parent);
219                 model_->insertRows(current_row, 1, parent);
220                 child_item = model_->index(current_row, 0, parent);
221                 model_->setData(child_item, toqstr(item.asString()), Qt::DisplayRole);
222                 model_->setData(child_item, index, Qt::UserRole);
223                 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
224                 populate(index, child_item);
225                 if (index >= end)
226                         break;
227         }
228 }
229
230
231 int TocModel::modelDepth() const
232 {
233         int const d = maxdepth_ - mindepth_;
234         LASSERT(d >= 0 && d <= 100, return 0);
235         return d;
236 }
237
238
239 ///////////////////////////////////////////////////////////////////////////////
240 //
241 // TocModels
242 //
243 ///////////////////////////////////////////////////////////////////////////////
244
245 TocModels::TocModels()
246         : bv_(0)
247 {
248         names_ = new TocTypeModel(this);
249         names_sorted_ = new TocModelSortProxyModel(this);
250         names_sorted_->setSourceModel(names_);
251         names_sorted_->setSortLocaleAware(true);
252         names_sorted_->sort(0);
253 }
254
255
256 void TocModels::clear()
257 {
258         names_->blockSignals(true);
259         names_->clear();
260         names_->blockSignals(false);
261         iterator end = models_.end();
262         for (iterator it = models_.begin(); it != end;  ++it)
263                 it.value()->clear();
264 }
265
266
267 int TocModels::depth(QString const & type)
268 {
269         const_iterator it = models_.find(type);
270         if (!bv_ || it == models_.end())
271                 return 0;
272         return it.value()->modelDepth();
273 }
274
275
276 QAbstractItemModel * TocModels::model(QString const & type)
277 {
278         if (!bv_)
279                 return 0;
280         iterator it = models_.find(type);
281         if (it != models_.end())
282                 return it.value()->model();
283         LYXERR0("type not found: " << type);
284         return 0;
285 }
286
287
288 QAbstractItemModel * TocModels::nameModel()
289 {
290         return names_sorted_;
291 }
292
293
294 QModelIndex TocModels::currentIndex(QString const & type) const
295 {
296         const_iterator it = models_.find(type);
297         if (!bv_ || it == models_.end())
298                 return QModelIndex();
299         return it.value()->modelIndex(bv_->cursor());
300 }
301
302
303 void TocModels::goTo(QString const & type, QModelIndex const & index) const
304 {
305         const_iterator it = models_.find(type);
306         if (it == models_.end() || !index.isValid()) {
307                 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
308                 return;
309         }
310         LASSERT(index.model() == it.value()->model(), return);
311         TocItem const item = it.value()->tocItem(index);
312         LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
313         dispatch(item.action());
314 }
315
316
317 TocItem const TocModels::currentItem(QString const & type,
318         QModelIndex const & index) const
319 {
320         const_iterator it = models_.find(type);
321         if (it == models_.end() || !index.isValid()) {
322                 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
323                 return TocItem();
324         }
325         LASSERT(index.model() == it.value()->model(), return TocItem());
326
327         return it.value()->tocItem(index);
328 }
329
330
331 void TocModels::updateItem(QString const & type, DocIterator const & dit)
332 {
333         models_[type]->updateItem(dit);
334 }
335
336
337 void TocModels::reset(BufferView const * bv)
338 {
339         bv_ = bv;
340         clear();
341         if (!bv_) {
342                 iterator end = models_.end();
343                 for (iterator it = models_.begin(); it != end;  ++it)
344                         it.value()->reset();
345                 names_->reset();
346                 return;
347         }
348
349         names_->blockSignals(true);
350         names_->beginResetModel();
351         names_->insertColumns(0, 1);
352         TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
353         TocList::const_iterator it = tocs.begin();
354         TocList::const_iterator toc_end = tocs.end();
355         for (; it != toc_end; ++it) {
356                 QString const type = toqstr(it->first);
357
358                 // First, fill in the toc models.
359                 iterator mod_it = models_.find(type);
360                 if (mod_it == models_.end())
361                         mod_it = models_.insert(type, new TocModel(this));
362                 mod_it.value()->reset(it->second);
363
364                 // Fill in the names_ model.
365                 QString const gui_name = guiName(it->first, bv->buffer().params());
366                 int const current_row = names_->rowCount();
367                 names_->insertRows(current_row, 1);
368                 QModelIndex const index = names_->index(current_row, 0);
369                 names_->setData(index, gui_name, Qt::DisplayRole);
370                 names_->setData(index, type, Qt::UserRole);
371         }
372         names_->blockSignals(false);
373         names_->endResetModel();
374 }
375
376
377 bool TocModels::isSorted(QString const & type) const
378 {
379         const_iterator it = models_.find(type);
380         if (it == models_.end()) {
381                 LYXERR0("type not found: " << type);
382                 return false;
383         }
384         return it.value()->isSorted();
385 }
386
387
388 void TocModels::sort(QString const & type, bool sort_it)
389 {
390         iterator it = models_.find(type);
391         if (it == models_.end())
392                 LYXERR0("type not found: " << type);
393         else
394                 it.value()->sort(sort_it);
395 }
396
397 } // namespace frontend
398 } // namespace lyx
399
400 #include "moc_TocModel.cpp"