]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QToc.C
a85e961741b92b09a20edf5685f4a874c3e875ab
[features.git] / src / frontends / qt4 / QToc.C
1 /**
2  * \file QToc.C
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 "QToc.h"
15 #include "TocModel.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "debug.h"
20
21 #include "controllers/ControlToc.h"
22
23 #include <algorithm>
24
25 using std::endl;
26
27 using std::pair;
28 using std::vector;
29 using std::string;
30
31 namespace lyx {
32 namespace frontend {
33
34
35 QToc::QToc(Dialog & parent)
36         : ControlToc(parent)
37 {
38         update();
39 }
40
41
42 bool QToc::canOutline()
43 {
44         vector<string> const & types = getTypes();
45
46         if (types.empty())
47                 return false;
48
49         BOOST_ASSERT(type_ >= 0 && type_ < int(types.size()));
50         return ControlToc::canOutline(types[type_]);
51 }
52
53
54 QStandardItemModel * QToc::tocModel()
55 {
56         lyxerr[Debug::GUI]
57                 << "QToc: type_ " << type_
58                 << "  toc_models_.size() " << toc_models_.size()
59                 << endl;
60
61         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
62         return toc_models_[type_];
63 }
64
65
66 QStandardItemModel * QToc::setTocModel(int type)
67 {
68         type_ = type;
69
70         lyxerr[Debug::GUI]
71                 << "QToc: type_ " << type_
72                 << "  toc_models_.size() " << toc_models_.size()
73                 << endl;
74
75         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
76         return toc_models_[type_];
77 }
78
79
80 QModelIndex const QToc::getCurrentIndex()
81 {
82         vector<string> const & types = getTypes();
83         TocIterator const it = getCurrentTocItem(types[type_]);
84         if (!it->isValid()) {
85                 lyxerr[Debug::GUI] << "QToc::getCurrentIndex(): TocItem is invalid!" << endl;
86                 return QModelIndex();
87         }
88
89         return toc_models_[type_]->modelIndex(it);
90 }
91
92
93 void QToc::goTo(QModelIndex const & index)
94 {
95         if (!index.isValid()) {
96                 lyxerr[Debug::GUI]
97                         << "QToc::goTo(): QModelIndex is invalid!"
98                         << endl;
99                 return;
100         }
101
102         TocIterator const it = toc_models_[type_]->tocIterator(index);
103         
104         lyxerr[Debug::GUI]
105                 << "QToc::goTo " << lyx::to_utf8(it->str())
106                 << endl;
107
108         it->goTo(kernel().lyxview());
109 }
110
111
112 void QToc::update()
113 {
114         toc_models_.clear();
115
116         QStringList type_list;
117
118         type_ = 0;
119
120         vector<string> const & types = getTypes();
121         if (types.empty())
122                 return;
123
124         string const & selected_type = toc::getType(params().getCmdName());
125         lyxerr[Debug::GUI] << "selected_type " << selected_type << endl;
126
127         QString gui_names_;
128         for (size_t i = 0; i != types.size(); ++i) {
129                 string const & type_str = types[i];
130                 type_list.append(toqstr(getGuiName(type_str)));
131                 if (type_str == selected_type)
132                         type_ = i;
133                 
134                 lyxerr[Debug::GUI]
135                         << "QToc: new type " << type_str
136                         << "\ttoc_models_.size() " << toc_models_.size()
137                         << endl;
138
139                 toc_models_.push_back(new TocModel(getContents(types[i])));
140         }
141         type_model_.setStringList(type_list);
142 }
143
144
145 void QToc::updateToc(int type)
146 {
147         toc_models_[type] = new TocModel(getContents(getTypes()[type]));
148 }
149
150
151 } // namespace frontend
152 } // namespace lyx