3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
15 #include "QRefDialog.h"
17 #include "qt_helpers.h"
19 #include "controllers/ButtonController.h"
20 #include "controllers/ControlRef.h"
22 #include "insets/insetref.h"
24 #include <qlineedit.h>
25 #include <qcheckbox.h>
27 #include <qcombobox.h>
28 #include <qpushbutton.h>
40 typedef QController<ControlRef, QView<QRefDialog> > base_class;
43 QRef::QRef(Dialog & parent)
44 : base_class(parent, _("Cross-reference")),
45 sort_(false), at_ref_(false)
50 void QRef::build_dialog()
52 dialog_.reset(new QRefDialog(this));
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);
64 restored_buffer_ = -1;
68 void QRef::update_contents()
70 InsetCommandParams const & params = controller().params();
72 int orig_type = dialog_->typeCO->currentItem();
74 dialog_->referenceED->setText(toqstr(params["reference"]));
76 dialog_->nameED->setText(toqstr(params["name"]));
77 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
79 // restore type settings for new insets
80 if (params["reference"].empty())
81 dialog_->typeCO->setCurrentItem(orig_type);
83 dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
84 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
86 dialog_->typeCO->setCurrentItem(0);
88 dialog_->sortCB->setChecked(sort_);
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));
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_);
102 dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
105 bc().valid(isValid());
111 InsetCommandParams & params = controller().params();
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());
117 restored_buffer_ = dialog_->bufferCO->currentItem();
121 bool QRef::nameAllowed()
123 Kernel::DocType const doc_type = kernel().docType();
124 return doc_type != Kernel::LATEX &&
125 doc_type != Kernel::LITERATE;
129 bool QRef::typeAllowed()
131 Kernel::DocType const doc_type = kernel().docType();
132 return doc_type != Kernel::DOCBOOK;
136 void QRef::setGoBack()
138 dialog_->gotoPB->setText(qt_("&Go Back"));
139 QToolTip::remove(dialog_->gotoPB);
140 QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
144 void QRef::setGotoRef()
146 dialog_->gotoPB->setText(qt_("&Go to Label"));
147 QToolTip::remove(dialog_->gotoPB);
148 QToolTip::add(dialog_->gotoPB, qt_("Jump to label"));
154 string ref(fromqstr(dialog_->referenceED->text()));
159 controller().gotoBookmark();
163 controller().gotoRef(ref);
169 void QRef::redoRefs()
171 // Prevent these widgets from emitting any signals whilst
172 // we modify their state.
173 dialog_->refsLB->blockSignals(true);
174 dialog_->referenceED->blockSignals(true);
176 int lastref = dialog_->refsLB->currentItem();
178 dialog_->refsLB->setAutoUpdate(false);
179 dialog_->refsLB->clear();
181 // need this because Qt will send a highlight() here for
182 // the first item inserted
183 QString const tmp(dialog_->referenceED->text());
185 for (std::vector<docstring>::const_iterator iter = refs_.begin();
186 iter != refs_.end(); ++iter) {
187 dialog_->refsLB->insertItem(toqstr(*iter));
191 dialog_->refsLB->sort();
193 dialog_->referenceED->setText(tmp);
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();
202 for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
203 if (tmp == dialog_->refsLB->text(i))
204 dialog_->refsLB->setSelected(i, true);
207 dialog_->refsLB->setAutoUpdate(true);
208 dialog_->refsLB->update();
210 // Re-activate the emission of signals by these widgets.
211 dialog_->refsLB->blockSignals(false);
212 dialog_->referenceED->blockSignals(false);
216 void QRef::updateRefs()
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());
232 return !dialog_->referenceED->text().isEmpty();
235 } // namespace frontend