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