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/ControlRef.h"
21 #include "insets/insetref.h"
23 #include <qlineedit.h>
24 #include <qcheckbox.h>
26 #include <qcombobox.h>
27 #include <qpushbutton.h>
37 typedef QController<ControlRef, QView<QRefDialog> > base_class;
40 QRef::QRef(Dialog & parent)
41 : base_class(parent, _("LyX: Cross-reference")),
42 sort_(false), at_ref_(false)
47 void QRef::build_dialog()
49 dialog_.reset(new QRefDialog(this));
51 bcview().setOK(dialog_->okPB);
52 bcview().setApply(dialog_->applyPB);
53 bcview().setCancel(dialog_->closePB);
54 bcview().addReadOnly(dialog_->refsLB);
55 bcview().addReadOnly(dialog_->sortCB);
56 bcview().addReadOnly(dialog_->nameED);
57 bcview().addReadOnly(dialog_->referenceED);
58 bcview().addReadOnly(dialog_->typeCO);
59 bcview().addReadOnly(dialog_->bufferCO);
61 restored_buffer_ = -1;
65 void QRef::update_contents()
67 InsetCommandParams const & params = controller().params();
69 int orig_type = dialog_->typeCO->currentItem();
71 dialog_->referenceED->setText(toqstr(params.getContents()));
73 dialog_->nameED->setText(toqstr(params.getOptions()));
74 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
76 // restore type settings for new insets
77 if (params.getContents().empty())
78 dialog_->typeCO->setCurrentItem(orig_type);
80 dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
81 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
83 dialog_->typeCO->setCurrentItem(0);
85 dialog_->sortCB->setChecked(sort_);
88 dialog_->bufferCO->clear();
89 vector<string> const buffers = controller().getBufferList();
90 for (vector<string>::const_iterator it = buffers.begin();
91 it != buffers.end(); ++it) {
92 dialog_->bufferCO->insertItem(toqstr(*it));
94 // restore the buffer combo setting for new insets
95 if (params.getContents().empty() && restored_buffer_ != -1
96 && restored_buffer_ < dialog_->bufferCO->count())
97 dialog_->bufferCO->setCurrentItem(restored_buffer_);
99 dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
107 InsetCommandParams & params = controller().params();
109 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
110 params.setContents(fromqstr(dialog_->referenceED->text()));
111 params.setOptions(fromqstr(dialog_->nameED->text()));
113 restored_buffer_ = dialog_->bufferCO->currentItem();
117 bool QRef::nameAllowed()
119 Kernel::DocTypes doc_type = kernel().docType();
120 return doc_type != Kernel::LATEX &&
121 doc_type != Kernel::LITERATE;
125 bool QRef::typeAllowed()
127 Kernel::DocTypes doc_type = kernel().docType();
128 return doc_type != Kernel::LINUXDOC &&
129 doc_type != Kernel::DOCBOOK;
133 void QRef::setGoBack()
135 dialog_->gotoPB->setText(qt_("&Go Back"));
136 QToolTip::remove(dialog_->gotoPB);
137 QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
141 void QRef::setGotoRef()
143 dialog_->gotoPB->setText(qt_("&Go to Reference"));
144 QToolTip::remove(dialog_->gotoPB);
145 QToolTip::add(dialog_->gotoPB, qt_("Jump to reference"));
151 string ref(fromqstr(dialog_->referenceED->text()));
156 controller().gotoBookmark();
160 controller().gotoRef(ref);
166 void QRef::redoRefs()
168 // Prevent these widgets from emitting any signals whilst
169 // we modify their state.
170 dialog_->refsLB->blockSignals(true);
171 dialog_->referenceED->blockSignals(true);
173 int lastref = dialog_->refsLB->currentItem();
175 dialog_->refsLB->setAutoUpdate(false);
176 dialog_->refsLB->clear();
178 // need this because Qt will send a highlight() here for
179 // the first item inserted
180 QString const tmp(dialog_->referenceED->text());
182 for (std::vector<string>::const_iterator iter = refs_.begin();
183 iter != refs_.end(); ++iter) {
184 dialog_->refsLB->insertItem(toqstr(*iter));
188 dialog_->refsLB->sort();
190 dialog_->referenceED->setText(tmp);
192 // restore the last selection for new insets
193 if (tmp.isEmpty() && lastref != -1
194 && lastref < int(dialog_->refsLB->count()))
195 dialog_->refsLB->setCurrentItem(lastref);
197 for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
198 if (tmp == dialog_->refsLB->text(i))
199 dialog_->refsLB->setCurrentItem(i);
202 dialog_->refsLB->setAutoUpdate(true);
203 dialog_->refsLB->update();
205 // Re-activate the emission of signals by these widgets.
206 dialog_->refsLB->blockSignals(false);
207 dialog_->referenceED->blockSignals(false);
211 void QRef::updateRefs()
216 string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
217 refs_ = controller().getLabelList(name);
218 dialog_->sortCB->setEnabled(!refs_.empty());
219 dialog_->refsLB->setEnabled(!refs_.empty());
220 dialog_->gotoPB->setEnabled(!refs_.empty());
224 } // namespace frontend