]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QRef.C
reverse last change
[lyx.git] / src / frontends / qt2 / QRef.C
1 /**
2  * \file QRef.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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "ControlRef.h"
18 #include "QRef.h"
19 #include "QRefDialog.h"
20 #include "Qt2BC.h"
21
22 #include <qlineedit.h>
23 #include <qcheckbox.h>
24 #include <qlistbox.h>
25 #include <qcombobox.h>
26 #include <qpushbutton.h>
27 #include <qtooltip.h>
28
29 #include "helper_funcs.h" // getStringFromVector
30 #include "support/lstrings.h" // frontStrip, strip
31 #include "qt_helpers.h"
32 #include "insets/insetref.h"
33
34 using std::find;
35 using std::max;
36 using std::sort;
37 using std::vector;
38 using std::endl;
39
40
41 typedef Qt2CB<ControlRef, Qt2DB<QRefDialog> > base_class;
42
43
44 QRef::QRef()
45         : base_class(qt_("Cross Reference")),
46         sort_(false), at_ref_(false)
47 {
48 }
49
50
51 void QRef::build_dialog()
52 {
53         dialog_.reset(new QRefDialog(this));
54
55         bc().setOK(dialog_->okPB);
56         bc().setApply(dialog_->applyPB);
57         bc().setCancel(dialog_->closePB);
58         bc().addReadOnly(dialog_->refsLB);
59         bc().addReadOnly(dialog_->sortCB);
60         bc().addReadOnly(dialog_->nameED);
61         bc().addReadOnly(dialog_->referenceED);
62         bc().addReadOnly(dialog_->typeCO);
63         bc().addReadOnly(dialog_->bufferCO);
64 }
65
66
67 void QRef::update_contents()
68 {
69         InsetCommandParams const & params = controller().params();
70
71         dialog_->referenceED->setText(toqstr(params.getContents()));
72
73         dialog_->nameED->setText(toqstr(params.getOptions()));
74         dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
75
76         dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
77         dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
78         if (!typeAllowed())
79                 dialog_->typeCO->setCurrentItem(0);
80
81         dialog_->sortCB->setChecked(sort_);
82
83         // insert buffer list
84         dialog_->bufferCO->clear();
85         vector<string> const buffers = controller().getBufferList();
86         for (vector<string>::const_iterator it = buffers.begin();
87                 it != buffers.end(); ++it) {
88                 dialog_->bufferCO->insertItem(toqstr(*it));
89         }
90         dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
91
92         updateRefs();
93 }
94
95
96 void QRef::apply()
97 {
98         InsetCommandParams & params = controller().params();
99
100         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
101         params.setContents(fromqstr(dialog_->referenceED->text()));
102         params.setOptions(fromqstr(dialog_->nameED->text()));
103 }
104
105
106 bool QRef::nameAllowed()
107 {
108         return controller().docType() != ControlRef::LATEX &&
109                 controller().docType() != ControlRef::LITERATE;
110 }
111
112
113 bool QRef::typeAllowed()
114 {
115         return controller().docType() != ControlRef::LINUXDOC &&
116             controller().docType() != ControlRef::DOCBOOK;
117 }
118
119
120 void QRef::setGoBack()
121 {
122         dialog_->gotoPB->setText(qt_("&Go back"));
123         QToolTip::remove(dialog_->gotoPB);
124         QToolTip::add(dialog_->gotoPB, qt_("Go back"));
125 }
126
127
128 void QRef::setGotoRef()
129 {
130         dialog_->gotoPB->setText(qt_("&Goto"));
131         QToolTip::remove(dialog_->gotoPB);
132         QToolTip::add(dialog_->gotoPB, qt_("Go to reference"));
133 }
134
135
136 void QRef::gotoRef()
137 {
138         string ref(fromqstr(dialog_->referenceED->text()));
139
140         if (at_ref_) {
141                 // go back
142                 setGotoRef();
143                 controller().gotoBookmark();
144         } else {
145                 // go to the ref
146                 setGoBack();
147                 controller().gotoRef(ref);
148         }
149         at_ref_ = !at_ref_;
150 }
151
152
153 void QRef::redoRefs()
154 {
155         dialog_->refsLB->setAutoUpdate(false);
156
157         // need this because Qt will send a highlight() here for
158         // the first item inserted
159         QString const tmp(dialog_->referenceED->text());
160
161         for (std::vector<string>::const_iterator iter = refs_.begin();
162                 iter != refs_.end(); ++iter) {
163                 if (sort_)
164                         dialog_->refsLB->inSort(toqstr(*iter));
165                 else
166                         dialog_->refsLB->insertItem(toqstr(*iter));
167         }
168
169         dialog_->referenceED->setText(tmp);
170
171         for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
172                 if (tmp == dialog_->refsLB->text(i))
173                         dialog_->refsLB->setCurrentItem(i);
174         }
175
176         dialog_->refsLB->setAutoUpdate(true);
177         dialog_->refsLB->update();
178 }
179
180
181 void QRef::updateRefs()
182 {
183         refs_.clear();
184         if (at_ref_)
185                 gotoRef();
186         dialog_->refsLB->clear();
187         string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
188         refs_ = controller().getLabelList(name);
189         dialog_->sortCB->setEnabled(!refs_.empty());
190         dialog_->refsLB->setEnabled(!refs_.empty());
191         redoRefs();
192 }