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