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> > base_class;
44 QRef::QRef(Dialog & parent)
45 : 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 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
115 params["reference"] = qstring_to_ucs4(dialog_->referenceED->text());
116 params["name"] = qstring_to_ucs4(dialog_->nameED->text());
118 restored_buffer_ = dialog_->bufferCO->currentIndex();
122 bool QRef::nameAllowed()
124 Kernel::DocType const doc_type = kernel().docType();
125 return doc_type != Kernel::LATEX &&
126 doc_type != Kernel::LITERATE;
130 bool QRef::typeAllowed()
132 Kernel::DocType const doc_type = kernel().docType();
133 return doc_type != Kernel::DOCBOOK;
137 void QRef::setGoBack()
139 dialog_->gotoPB->setText(qt_("&Go Back"));
140 dialog_->gotoPB->setToolTip("");
141 dialog_->gotoPB->setToolTip(qt_("Jump back"));
145 void QRef::setGotoRef()
147 dialog_->gotoPB->setText(qt_("&Go to Label"));
148 dialog_->gotoPB->setToolTip("");
149 dialog_->gotoPB->setToolTip(qt_("Jump to label"));
155 string ref(fromqstr(dialog_->referenceED->text()));
160 controller().gotoBookmark();
164 controller().gotoRef(ref);
170 void QRef::redoRefs()
172 // Prevent these widgets from emitting any signals whilst
173 // we modify their state.
174 dialog_->refsLW->blockSignals(true);
175 dialog_->referenceED->blockSignals(true);
177 int lastref = dialog_->refsLW->currentRow();
179 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 tmp(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(tmp);
196 // restore the last selection for new insets
197 if (tmp.isEmpty() && lastref != -1
198 && lastref < int(dialog_->refsLW->count())) {
199 dialog_->refsLW->setCurrentRow(lastref);
200 dialog_->refsLW->clearSelection();
202 for (int i = 0; i < dialog_->refsLW->count(); ++i) {
203 QListWidgetItem * item = dialog_->refsLW->item(i);
204 if (tmp == item->text()) {
205 dialog_->refsLW->setItemSelected(item, true);
209 dialog_->refsLW->setUpdatesEnabled(true);
210 dialog_->refsLW->update();
212 // Re-activate the emission of signals by these widgets.
213 dialog_->refsLW->blockSignals(false);
214 dialog_->referenceED->blockSignals(false);
218 void QRef::updateRefs()
223 string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
224 refs_ = controller().getLabelList(name);
225 dialog_->sortCB->setEnabled(!refs_.empty());
226 dialog_->refsLW->setEnabled(!refs_.empty());
227 dialog_->gotoPB->setEnabled(!refs_.empty());
233 return !dialog_->referenceED->text().isEmpty();
236 } // namespace frontend