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