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>
26 #include <q3listbox.h>
27 #include <qpushbutton.h>
37 typedef QController<ControlRef, QView<QRefDialog> > base_class;
40 QRef::QRef(Dialog & parent)
41 : base_class(parent, _("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());
108 InsetCommandParams & params = controller().params();
110 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
111 params.setContents(fromqstr(dialog_->referenceED->text()));
112 params.setOptions(fromqstr(dialog_->nameED->text()));
114 restored_buffer_ = dialog_->bufferCO->currentItem();
118 bool QRef::nameAllowed()
120 Kernel::DocType const doc_type = kernel().docType();
121 return doc_type != Kernel::LATEX &&
122 doc_type != Kernel::LITERATE;
126 bool QRef::typeAllowed()
128 Kernel::DocType const doc_type = kernel().docType();
129 return doc_type != Kernel::LINUXDOC &&
130 doc_type != Kernel::DOCBOOK;
134 void QRef::setGoBack()
136 dialog_->gotoPB->setText(qt_("&Go Back"));
137 QToolTip::remove(dialog_->gotoPB);
138 QToolTip::add(dialog_->gotoPB, qt_("Jump back"));
142 void QRef::setGotoRef()
144 dialog_->gotoPB->setText(qt_("&Go to Label"));
145 QToolTip::remove(dialog_->gotoPB);
146 QToolTip::add(dialog_->gotoPB, qt_("Jump to label"));
152 string ref(fromqstr(dialog_->referenceED->text()));
157 controller().gotoBookmark();
161 controller().gotoRef(ref);
167 void QRef::redoRefs()
169 // Prevent these widgets from emitting any signals whilst
170 // we modify their state.
171 dialog_->refsLB->blockSignals(true);
172 dialog_->referenceED->blockSignals(true);
174 int lastref = dialog_->refsLB->currentItem();
176 dialog_->refsLB->setAutoUpdate(false);
177 dialog_->refsLB->clear();
179 // need this because Qt will send a highlight() here for
180 // the first item inserted
181 QString const tmp(dialog_->referenceED->text());
183 for (std::vector<string>::const_iterator iter = refs_.begin();
184 iter != refs_.end(); ++iter) {
185 dialog_->refsLB->insertItem(toqstr(*iter));
189 dialog_->refsLB->sort();
191 dialog_->referenceED->setText(tmp);
193 // restore the last selection for new insets
194 if (tmp.isEmpty() && lastref != -1
195 && lastref < int(dialog_->refsLB->count())) {
196 dialog_->refsLB->setCurrentItem(lastref);
197 dialog_->refsLB->clearSelection();
199 for (unsigned int i = 0; i < dialog_->refsLB->count(); ++i) {
200 if (tmp == dialog_->refsLB->text(i))
201 dialog_->refsLB->setSelected(i, true);
204 dialog_->refsLB->setAutoUpdate(true);
205 dialog_->refsLB->update();
207 // Re-activate the emission of signals by these widgets.
208 dialog_->refsLB->blockSignals(false);
209 dialog_->referenceED->blockSignals(false);
213 void QRef::updateRefs()
218 string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
219 refs_ = controller().getLabelList(name);
220 dialog_->sortCB->setEnabled(!refs_.empty());
221 dialog_->refsLB->setEnabled(!refs_.empty());
222 dialog_->gotoPB->setEnabled(!refs_.empty());
228 return !dialog_->referenceED->text().isEmpty();
231 } // namespace frontend