]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QToc.C
first commit new graphics dialog
[lyx.git] / src / frontends / qt2 / QToc.C
1 /**
2  * \file QToc.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <stack>
16
17 #include <qslider.h>
18 #include <qlistview.h>
19 #include <qpushbutton.h>
20 #include <qcombobox.h>
21  
22 #include "QTocDialog.h"
23 #include "QToc.h"
24 #include "Qt2BC.h" 
25 #include "gettext.h"
26 #include "support/lstrings.h"
27 #include "debug.h"
28
29 #include "QtLyXView.h"
30  
31 using std::endl;
32 using std::pair;
33 using std::stack;
34 using std::vector;
35  
36 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
37  
38 QToc::QToc(ControlToc & c)
39         : base_class(c, _("Table of contents"))
40 {}
41  
42  
43 void QToc::build_dialog()
44 {
45         dialog_.reset(new QTocDialog(this));
46
47         // Manage the cancel/close button
48         bc().setCancel(dialog_->closePB);
49 }
50
51
52 void QToc::updateType()
53
54         dialog_->typeCO->clear();
55  
56         vector<string> const & choice = controller().getTypes();
57         string const & type = toc::getType(controller().params().getCmdName());
58
59         for (vector<string>::const_iterator it = choice.begin();
60                 it != choice.end(); ++it) {
61                 dialog_->typeCO->insertItem(it->c_str());
62                 if (*it == type) {
63                         dialog_->typeCO->setCurrentItem(it - choice.begin());
64                         dialog_->setCaption(type.c_str());
65                 }
66         }
67
68  
69  
70 void QToc::update_contents()
71 {
72         updateType();
73         updateToc(depth_);
74 }
75
76  
77 void QToc::updateToc(int newdepth)
78 {
79         string const type = dialog_->typeCO->currentText().latin1();
80  
81         Buffer::SingleList const & contents = controller().getContents(type);
82  
83         // Check if all elements are the same.
84         if (newdepth == depth_ && toclist == contents) {
85                 return;
86         }
87
88         dialog_->tocLV->clear();
89  
90         depth_ = newdepth;
91
92         toclist = contents;
93
94         if (toclist.empty())
95                 return;
96
97         dialog_->tocLV->setUpdatesEnabled(false);
98
99         int curdepth = 0;
100         stack< pair< QListViewItem *, QListViewItem *> > istack;
101         QListViewItem *last = 0;
102         QListViewItem *parent = 0;
103         QListViewItem *item;
104
105         // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
106         // rather than QListViewItem; and the TOC can move in and out an arbitrary number
107         // of levels
108
109         for (Buffer::SingleList::const_iterator iter = toclist.begin();
110                 iter != toclist.end(); ++iter) {
111                 if (iter->depth == curdepth) {
112                         // insert it after the last one we processed
113                         if (!parent)
114                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
115                         else
116                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
117                 } else if (iter->depth > curdepth) {
118                         int diff = iter->depth - curdepth;
119                         // first save old parent and last
120                         while (diff--)
121                                 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
122                         item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
123                         parent = last;
124                 } else {
125                         int diff = curdepth - iter->depth;
126                         pair< QListViewItem *, QListViewItem * > top;
127                         // restore context
128                         while (diff--) {
129                                 top = istack.top();
130                                 istack.pop();
131                         }
132                         parent = top.first;
133                         last = top.second;
134                         // insert it after the last one we processed
135                         if (!parent)
136                                 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
137                         else
138                                 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
139                 }
140  
141                 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
142                         << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
143                         << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
144                 item->setText(0,iter->str.c_str());
145                 item->setOpen(iter->depth < depth_);
146                 curdepth = iter->depth;
147                 last = item;
148         }
149
150         dialog_->tocLV->setUpdatesEnabled(true);
151         dialog_->tocLV->update();
152 }
153
154
155 void QToc::select(string const & text)
156 {
157         Buffer::SingleList::const_iterator iter = toclist.begin();
158  
159         for (; iter != toclist.end(); ++iter) {
160                 if (iter->str == text)
161                         break;
162         }
163         
164         if (iter == toclist.end()) {
165                 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " 
166                         << text << endl;
167                 return;
168         }
169
170         controller().Goto(iter->par->id());
171 }
172
173
174 void QToc::set_depth(int depth)
175 {
176         if (depth != depth_)
177                 updateToc(depth);
178 }