]> git.lyx.org Git - features.git/blob - src/frontends/qt3/QRef.C
Use the new InsetCommandParams interface (frontend part), from Ugras and me
[features.git] / src / frontends / qt3 / 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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QRef.h"
15 #include "QRefDialog.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "controllers/ButtonController.h"
20 #include "controllers/ControlRef.h"
21
22 #include "insets/insetref.h"
23
24 #include <qlineedit.h>
25 #include <qcheckbox.h>
26 #include <qlistbox.h>
27 #include <qcombobox.h>
28 #include <qpushbutton.h>
29 #include <qtooltip.h>
30
31
32 using lyx::docstring;
33
34 using std::vector;
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40 typedef QController<ControlRef, QView<QRefDialog> > base_class;
41
42
43 QRef::QRef(Dialog & parent)
44         : base_class(parent, _("Cross-reference")),
45         sort_(false), at_ref_(false)
46 {
47 }
48
49
50 void QRef::build_dialog()
51 {
52         dialog_.reset(new QRefDialog(this));
53
54         bcview().setOK(dialog_->okPB);
55         bcview().setApply(dialog_->applyPB);
56         bcview().setCancel(dialog_->closePB);
57         bcview().addReadOnly(dialog_->refsLB);
58         bcview().addReadOnly(dialog_->sortCB);
59         bcview().addReadOnly(dialog_->nameED);
60         bcview().addReadOnly(dialog_->referenceED);
61         bcview().addReadOnly(dialog_->typeCO);
62         bcview().addReadOnly(dialog_->bufferCO);
63
64         restored_buffer_ = -1;
65 }
66
67
68 void QRef::update_contents()
69 {
70         InsetCommandParams const & params = controller().params();
71
72         int orig_type = dialog_->typeCO->currentItem();
73
74         dialog_->referenceED->setText(toqstr(params["reference"]));
75
76         dialog_->nameED->setText(toqstr(params["name"]));
77         dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
78
79         // restore type settings for new insets
80         if (params["reference"].empty())
81                 dialog_->typeCO->setCurrentItem(orig_type);
82         else
83                 dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
84         dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
85         if (!typeAllowed())
86                 dialog_->typeCO->setCurrentItem(0);
87
88         dialog_->sortCB->setChecked(sort_);
89
90         // insert buffer list
91         dialog_->bufferCO->clear();
92         vector<string> const buffers = controller().getBufferList();
93         for (vector<string>::const_iterator it = buffers.begin();
94                 it != buffers.end(); ++it) {
95                 dialog_->bufferCO->insertItem(toqstr(*it));
96         }
97         // restore the buffer combo setting for new insets
98         if (params["reference"].empty() && restored_buffer_ != -1
99         && restored_buffer_ < dialog_->bufferCO->count())
100                 dialog_->bufferCO->setCurrentItem(restored_buffer_);
101         else
102                 dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
103
104         updateRefs();
105         bc().valid(isValid());
106 }
107
108
109 void QRef::apply()
110 {
111         InsetCommandParams & params = controller().params();
112
113         params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
114         params["reference"] = qstring_to_ucs4(dialog_->referenceED->text());
115         params["name"] = qstring_to_ucs4(dialog_->nameED->text());
116
117         restored_buffer_ = dialog_->bufferCO->currentItem();
118 }
119
120
121 bool QRef::nameAllowed()
122 {
123         Kernel::DocType const doc_type = kernel().docType();
124         return doc_type != Kernel::LATEX &&
125                 doc_type != Kernel::LITERATE;
126 }
127
128
129 bool QRef::typeAllowed()
130 {
131         Kernel::DocType const doc_type = kernel().docType();
132         return doc_type != Kernel::DOCBOOK;
133 }
134
135
136 void QRef::setGoBack()
137 {
138         dialog_->gotoPB->setText(qt_("&Go Back"));
139         QToolTip::remove(dialog_->gotoPB);
140         QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
141 }
142
143
144 void QRef::setGotoRef()
145 {
146         dialog_->gotoPB->setText(qt_("&Go to Label"));
147         QToolTip::remove(dialog_->gotoPB);
148         QToolTip::add(dialog_->gotoPB, qt_("Jump to label"));
149 }
150
151
152 void QRef::gotoRef()
153 {
154         string ref(fromqstr(dialog_->referenceED->text()));
155
156         if (at_ref_) {
157                 // go back
158                 setGotoRef();
159                 controller().gotoBookmark();
160         } else {
161                 // go to the ref
162                 setGoBack();
163                 controller().gotoRef(ref);
164         }
165         at_ref_ = !at_ref_;
166 }
167
168
169 void QRef::redoRefs()
170 {
171         // Prevent these widgets from emitting any signals whilst
172         // we modify their state.
173         dialog_->refsLB->blockSignals(true);
174         dialog_->referenceED->blockSignals(true);
175
176         int lastref = dialog_->refsLB->currentItem();
177
178         dialog_->refsLB->setAutoUpdate(false);
179         dialog_->refsLB->clear();
180
181         // need this because Qt will send a highlight() here for
182         // the first item inserted
183         QString const tmp(dialog_->referenceED->text());
184
185         for (std::vector<docstring>::const_iterator iter = refs_.begin();
186                 iter != refs_.end(); ++iter) {
187                 dialog_->refsLB->insertItem(toqstr(*iter));
188         }
189
190         if (sort_)
191                 dialog_->refsLB->sort();
192
193         dialog_->referenceED->setText(tmp);
194
195         // restore the last selection for new insets
196         // but do not highlight it
197         if (tmp.isEmpty() && lastref != -1
198             && lastref < int(dialog_->refsLB->count())) {
199                 dialog_->refsLB->setCurrentItem(lastref);
200                 dialog_->refsLB->clearSelection();
201         } else
202                 for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
203                         if (tmp == dialog_->refsLB->text(i))
204                                 dialog_->refsLB->setSelected(i, true);
205                 }
206
207         dialog_->refsLB->setAutoUpdate(true);
208         dialog_->refsLB->update();
209
210         // Re-activate the emission of signals by these widgets.
211         dialog_->refsLB->blockSignals(false);
212         dialog_->referenceED->blockSignals(false);
213 }
214
215
216 void QRef::updateRefs()
217 {
218         refs_.clear();
219         if (at_ref_)
220                 gotoRef();
221         string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
222         refs_ = controller().getLabelList(name);
223         dialog_->sortCB->setEnabled(!refs_.empty());
224         dialog_->refsLB->setEnabled(!refs_.empty());
225         dialog_->gotoPB->setEnabled(!refs_.empty());
226         redoRefs();
227 }
228
229
230 bool QRef::isValid()
231 {
232         return !dialog_->referenceED->text().isEmpty();
233 }
234
235 } // namespace frontend
236 } // namespace lyx