]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QToc.C
Two ways toc navigation:
[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 QToc::QToc(Dialog & parent)
35         : ControlToc(parent)
36 {
37         update();
38 }
39
40
41 bool QToc::canOutline()
42 {
43         vector<string> const & types = getTypes();
44
45         BOOST_ASSERT(type_ >= 0 && type_ < int(types.size()));
46         return ControlToc::canOutline(types[type_]);
47 }
48
49
50 QStandardItemModel * QToc::tocModel()
51 {
52         lyxerr[Debug::GUI]
53                 << "QToc: type_ " << type_
54                 << "  toc_models_.size() " << toc_models_.size()
55                 << endl;
56
57         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
58         return toc_models_[type_];
59 }
60
61
62 QStandardItemModel * QToc::setTocModel(int type)
63 {
64         type_ = type;
65
66         lyxerr[Debug::GUI]
67                 << "QToc: type_ " << type_
68                 << "  toc_models_.size() " << toc_models_.size()
69                 << endl;
70
71         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
72         return toc_models_[type_];
73 }
74
75
76 QModelIndex const QToc::getCurrentIndex()
77 {
78         vector<string> const & types = getTypes();
79         toc::TocItem const item = getCurrentTocItem(types[type_]);
80         if (item.id_ == -1) {
81                 lyxerr[Debug::GUI]
82                         << "QToc::getCurrentIndex(): TocItem is invalid!" << endl;
83                 return QModelIndex();
84         }
85
86         string toc_str = item.str;
87         toc_str.erase(0, toc_str.find(' ') + 1);
88
89         return toc_models_[type_]->index(toc_str);
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         lyxerr[Debug::GUI]
103                 << "QToc::goTo " << toc_models_[type_]->item(index).str
104                 << endl;
105
106         ControlToc::goTo(toc_models_[type_]->item(index));
107 }
108
109
110 void QToc::update()
111 {
112         toc_models_.clear();
113
114         QStringList type_list;
115
116         type_ = 0;
117
118         vector<string> const & types = getTypes();
119         string const & selected_type = toc::getType(params().getCmdName());
120         lyxerr[Debug::GUI] << "selected_type " << selected_type << endl;
121
122         QString gui_names_;
123         for (size_t i = 0; i != types.size(); ++i) {
124                 string const & type_str = types[i];
125                 type_list.append(toqstr(getGuiName(type_str)));
126                 if (type_str == selected_type)
127                         type_ = i;
128                 
129                 lyxerr[Debug::GUI]
130                         << "QToc: new type " << type_str
131                         << "\ttoc_models_.size() " << toc_models_.size()
132                         << endl;
133
134                 toc_models_.push_back(new TocModel(getContents(types[i])));
135         }
136         type_model_.setStringList(type_list);
137 }
138
139
140 void QToc::updateToc(int type)
141 {
142         vector<string> const & choice = getTypes();
143
144         toc_models_[type] = new TocModel(getContents(choice[type]));
145 }
146
147
148 void QToc::move(toc::OutlineOp const operation, QModelIndex & index)
149 {
150         int toc_id = toc_models_[type_]->item(index).id_;
151         string toc_str = toc_models_[type_]->item(index).str;
152         toc_str.erase(0, toc_str.find(' ') + 1);
153
154         outline(operation);
155         updateToc(type_);
156
157         lyxerr[Debug::GUI]
158                 << "Toc id " << toc_id
159                 << "  Toc str " << toc_str
160                 << endl;
161
162         index = toc_models_[type_]->index(toc_str);
163 }
164
165 } // namespace frontend
166 } // namespace lyx