]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QTocDialog.C
This new citation dialog follows a new design similar to lyx-1.3:
[lyx.git] / src / frontends / qt4 / QTocDialog.C
1 /**
2  * \file QTocDialog.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 "QTocDialog.h"
15 #include "QToc.h"
16 #include "qt_helpers.h"
17 #include "controllers/ControlToc.h"
18
19 #include "debug.h"
20
21 #include <QTreeWidget>
22 #include <QTreeWidgetItem>
23 #include <QPushButton>
24 #include <QCloseEvent>
25
26 #include <vector>
27 #include <string>
28 #include <stack>
29
30 using std::endl;
31 using std::pair;
32 using std::stack;
33 using std::vector;
34 using std::string;
35
36 namespace lyx {
37 namespace frontend {
38
39 QTocDialog::QTocDialog(QToc * form)
40         : form_(form), depth_(2)
41 {
42         setupUi(this);
43
44         // disable sorting
45         tocTW->setSortingEnabled(false);
46         tocTW->setColumnCount(1);
47         // hide the pointless QHeader
48 //      QWidget * w = static_cast<QWidget*>(tocTW->child("list view header"));
49 //      if (w)
50 //              w->hide();
51
52         connect(closePB, SIGNAL(clicked()),
53                 form, SLOT(slotClose()));
54
55         connect( tocTW, SIGNAL(itemClicked(QTreeWidgetItem*)), this, SLOT(select_adaptor(QTreeWidgetItem*)));
56     connect( typeCO, SIGNAL( activated(int) ), this, SLOT( activate_adaptor(int) ) );
57     connect( updatePB, SIGNAL( clicked() ), this, SLOT( update_adaptor() ) );
58     connect( depthSL, SIGNAL( valueChanged(int) ), this, SLOT( depth_adaptor(int) ) );
59 }
60
61
62 QTocDialog::~QTocDialog()
63 {
64 }
65
66 void QTocDialog::updateType()
67 {
68         typeCO->clear();
69
70         vector<string> const & choice = form_->controller().getTypes();
71         string const & type = toc::getType(form_->controller().params().getCmdName());
72
73         for (vector<string>::const_iterator it = choice.begin();
74                 it != choice.end(); ++it) {
75                 string const & guiname = form_->controller().getGuiName(*it);
76                 typeCO->insertItem(toqstr(guiname));
77                 if (*it == type) {
78                         typeCO->setCurrentItem(it - choice.begin());
79                         form_->setTitle(guiname);
80                 }
81         }
82 }
83
84 void QTocDialog::updateToc(bool newdepth)
85 {
86         vector<string> const & choice = form_->controller().getTypes();
87         string type;
88         if (!choice.empty())
89                 type = choice[typeCO->currentItem()];
90
91         toc::Toc const & contents = form_->controller().getContents(type);
92
93         // Check if all elements are the same.
94         if (!newdepth && form_->get_toclist() == contents) {
95                 return;
96         }
97
98         tocTW->clear();
99
100         form_->get_toclist() = contents;
101
102         if (form_->get_toclist().empty())
103                 return;
104
105         tocTW->setUpdatesEnabled(false);
106
107         QTreeWidgetItem * topLevelItem;
108
109         toc::Toc::const_iterator iter = form_->get_toclist().begin();
110
111         while (iter != form_->get_toclist().end()) {
112
113                 if (iter->depth == 1) {
114                         topLevelItem = new QTreeWidgetItem(tocTW);
115 //                      tocTW->addTopLevelItem(topLevelItem);
116                         topLevelItem->setText(0, toqstr(iter->str));
117                         if (iter->depth < depth_) tocTW->collapseItem(topLevelItem);
118
119                         lyxerr[Debug::GUI]
120                                 << "Table of contents\n"
121                                 << "Added Top Level item " << iter->str
122                                 << " at depth " << iter->depth
123                                 << endl;
124
125                         populateItem(topLevelItem, iter);
126                 }
127
128                 if (iter == form_->get_toclist().end())
129                         break;
130
131                 ++iter;
132         }
133
134         tocTW->setUpdatesEnabled(true);
135         tocTW->update();
136         form_->setTitle(fromqstr(typeCO->currentText()));
137         tocTW->show();
138 }
139
140 void QTocDialog::populateItem(QTreeWidgetItem * parentItem, toc::Toc::const_iterator& iter)
141 {
142         int curdepth = iter->depth+1;
143         QTreeWidgetItem * item;
144
145         while (iter != form_->get_toclist().end()) {
146                 
147                 ++iter;
148
149                 if (iter == form_->get_toclist().end())
150                         break;
151
152                 if (iter->depth < curdepth) {
153                         --iter;
154                         return;
155                 }
156                 if (iter->depth > curdepth) {
157 //                      --iter;
158                         return;
159                 }
160
161                 item = new QTreeWidgetItem(parentItem);
162                 item->setText(0, toqstr(iter->str));
163 //              parentItem->addChild(item);
164
165                 if (iter->depth < depth_) tocTW->collapseItem(item);
166 //              else tocTW->expandItem(item);
167                 lyxerr[Debug::GUI]
168                         << "Table of contents: Added item " << iter->str
169                         << " at depth " << iter->depth
170                         << "  \", parent \""
171                         << fromqstr(parentItem->text(0)) << '"'
172                         << endl;
173
174                 populateItem(item, iter);
175         }
176 }
177
178 void QTocDialog::activate_adaptor(int)
179 {
180         updateToc();
181 }
182
183
184 void QTocDialog::depth_adaptor(int depth)
185 {
186         if (depth == depth_)
187                 return;
188
189         depth_ = depth;
190         updateToc(true);
191 }
192
193
194 void QTocDialog::select_adaptor(QTreeWidgetItem * item)
195 {
196         form_->select(fromqstr(item->text(0)));
197 }
198
199
200 void QTocDialog::update_adaptor()
201 {
202         form_->update();
203 }
204
205
206 void QTocDialog::closeEvent(QCloseEvent * e)
207 {
208         form_->slotWMHide();
209         e->accept();
210 }
211
212 } // namespace frontend
213 } // namespace lyx
214
215
216 /*
217         stack<pair<QTreeWidgetItem *, QTreeWidgetItem *> > istack;
218         QTreeWidgetItem * last = 0;
219         QTreeWidgetItem * parent = 0;
220         QTreeWidgetItem * item;
221
222         // Yes, it is this ugly. Two reasons - root items must have
223         // a QListView parent, rather than QListViewItem; and the
224         // TOC can move in and out an arbitrary number of levels
225
226         for (toc::Toc::const_iterator iter = form_->get_toclist().begin();
227              iter != form_->get_toclist().end(); ++iter) {
228
229                 if (iter->depth == 0) {
230                         TocTW
231
232                 if (iter->depth == curdepth) {
233                         // insert it after the last one we processed
234                         if (!parent)
235                                 item = (last ? new QTreeWidgetItem(tocTW,last) : new QTreeWidgetItem(tocTW));
236                         else
237                                 item = (last ? new QTreeWidgetItem(parent,last) : new QTreeWidgetItem(parent));
238                 } else if (iter->depth > curdepth) {
239                         int diff = iter->depth - curdepth;
240                         // first save old parent and last
241                         while (diff--)
242                                 istack.push(pair< QTreeWidgetItem *, QTreeWidgetItem * >(parent,last));
243                         item = (last ? new QTreeWidgetItem(last) : new QTreeWidgetItem(tocTW));
244                         parent = last;
245                 } else {
246                         int diff = curdepth - iter->depth;
247                         pair<QTreeWidgetItem *, QTreeWidgetItem * > top;
248                         // restore context
249                         while (diff--) {
250                                 top = istack.top();
251                                 istack.pop();
252                         }
253                         parent = top.first;
254                         last = top.second;
255                         // insert it after the last one we processed
256                         if (!parent)
257                                 item = (last ? new QTreeWidgetItem(tocTW,last) : new QTreeWidgetItem(tocTW));
258                         else
259                                 item = (last ? new QTreeWidgetItem(parent,last) : new QTreeWidgetItem(parent));
260                 }
261                 item->setText(0, toqstr(iter->str));
262                 item->setOpen(iter->depth < depth_);
263                 curdepth = iter->depth;
264                 last = item;
265         }
266 */