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"
26 #include <QListWidget>
27 #include <QListWidgetItem>
28 #include <QPushButton>
40 // full qualification because qt4 has also a ControlRef type
41 typedef QController<lyx::frontend::ControlRef, QView<QRefDialog> > ref_base_class;
44 QRef::QRef(Dialog & parent)
45 : ref_base_class(parent, _("Cross-reference")),
46 sort_(false), at_ref_(false)
51 void QRef::build_dialog()
53 dialog_.reset(new QRefDialog(this));
55 bcview().setOK(dialog_->okPB);
56 bcview().setApply(dialog_->applyPB);
57 bcview().setCancel(dialog_->closePB);
58 bcview().addReadOnly(dialog_->refsLW);
59 bcview().addReadOnly(dialog_->sortCB);
60 bcview().addReadOnly(dialog_->nameED);
61 bcview().addReadOnly(dialog_->referenceED);
62 bcview().addReadOnly(dialog_->typeCO);
63 bcview().addReadOnly(dialog_->bufferCO);
65 restored_buffer_ = -1;
69 void QRef::update_contents()
71 InsetCommandParams const & params = controller().params();
73 int orig_type = dialog_->typeCO->currentIndex();
75 dialog_->referenceED->setText(toqstr(params["reference"]));
77 dialog_->nameED->setText(toqstr(params["name"]));
78 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
80 // restore type settings for new insets
81 if (params["reference"].empty())
82 dialog_->typeCO->setCurrentIndex(orig_type);
84 dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
85 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
87 dialog_->typeCO->setCurrentIndex(0);
89 dialog_->sortCB->setChecked(sort_);
92 dialog_->bufferCO->clear();
93 vector<string> const buffers = controller().getBufferList();
94 for (vector<string>::const_iterator it = buffers.begin();
95 it != buffers.end(); ++it) {
96 dialog_->bufferCO->addItem(toqstr(*it));
98 // restore the buffer combo setting for new insets
99 if (params["reference"].empty() && restored_buffer_ != -1
100 && restored_buffer_ < dialog_->bufferCO->count())
101 dialog_->bufferCO->setCurrentIndex(restored_buffer_);
103 dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
112 InsetCommandParams & params = controller().params();
114 last_reference_ = dialog_->referenceED->text();
116 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
117 params["reference"] = qstring_to_ucs4(last_reference_);
118 params["name"] = qstring_to_ucs4(dialog_->nameED->text());
120 restored_buffer_ = dialog_->bufferCO->currentIndex();
124 bool QRef::nameAllowed()
126 Kernel::DocType const doc_type = kernel().docType();
127 return doc_type != Kernel::LATEX &&
128 doc_type != Kernel::LITERATE;
132 bool QRef::typeAllowed()
134 Kernel::DocType const doc_type = kernel().docType();
135 return doc_type != Kernel::DOCBOOK;
139 void QRef::setGoBack()
141 dialog_->gotoPB->setText(qt_("&Go Back"));
142 dialog_->gotoPB->setToolTip("");
143 dialog_->gotoPB->setToolTip(qt_("Jump back"));
147 void QRef::setGotoRef()
149 dialog_->gotoPB->setText(qt_("&Go to Label"));
150 dialog_->gotoPB->setToolTip("");
151 dialog_->gotoPB->setToolTip(qt_("Jump to label"));
157 string ref(fromqstr(dialog_->referenceED->text()));
162 controller().gotoBookmark();
166 controller().gotoRef(ref);
172 void QRef::redoRefs()
174 // Prevent these widgets from emitting any signals whilst
175 // we modify their state.
176 dialog_->refsLW->blockSignals(true);
177 dialog_->referenceED->blockSignals(true);
178 dialog_->refsLW->setUpdatesEnabled(false);
180 dialog_->refsLW->clear();
182 // need this because Qt will send a highlight() here for
183 // the first item inserted
184 QString const oldSelection(dialog_->referenceED->text());
186 for (std::vector<docstring>::const_iterator iter = refs_.begin();
187 iter != refs_.end(); ++iter) {
188 dialog_->refsLW->addItem(toqstr(*iter));
192 dialog_->refsLW->sortItems();
194 dialog_->referenceED->setText(oldSelection);
196 // restore the last selection or, for new insets, highlight
197 // the previous selection
198 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
199 bool const newInset = oldSelection.isEmpty();
200 QString textToFind = newInset ? last_reference_ : oldSelection;
201 bool foundItem = false;
202 for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
203 QListWidgetItem * item = dialog_->refsLW->item(i);
204 if (textToFind == item->text()) {
205 dialog_->refsLW->setCurrentItem(item);
206 dialog_->refsLW->setItemSelected(item, !newInset);
207 //Make sure selected item is visible
208 dialog_->refsLW->scrollToItem(item);
213 last_reference_ = textToFind;
214 else last_reference_ = "";
216 dialog_->refsLW->setUpdatesEnabled(true);
217 dialog_->refsLW->update();
219 // Re-activate the emission of signals by these widgets.
220 dialog_->refsLW->blockSignals(false);
221 dialog_->referenceED->blockSignals(false);
225 void QRef::updateRefs()
228 string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
229 refs_ = controller().getLabelList(name);
230 dialog_->sortCB->setEnabled(!refs_.empty());
231 dialog_->refsLW->setEnabled(!refs_.empty());
232 dialog_->gotoPB->setEnabled(!refs_.empty());
238 return !dialog_->referenceED->text().isEmpty();
241 } // namespace frontend