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>
38 // full qualification because qt4 has also a ControlRef type
39 typedef QController<lyx::frontend::ControlRef, QView<QRefDialog> > base_class;
42 QRef::QRef(Dialog & parent)
43 : base_class(parent, _("Cross-reference")),
44 sort_(false), at_ref_(false)
49 void QRef::build_dialog()
51 dialog_.reset(new QRefDialog(this));
53 bcview().setOK(dialog_->okPB);
54 bcview().setApply(dialog_->applyPB);
55 bcview().setCancel(dialog_->closePB);
56 bcview().addReadOnly(dialog_->refsLW);
57 bcview().addReadOnly(dialog_->sortCB);
58 bcview().addReadOnly(dialog_->nameED);
59 bcview().addReadOnly(dialog_->referenceED);
60 bcview().addReadOnly(dialog_->typeCO);
61 bcview().addReadOnly(dialog_->bufferCO);
63 restored_buffer_ = -1;
67 void QRef::update_contents()
69 InsetCommandParams const & params = controller().params();
71 int orig_type = dialog_->typeCO->currentItem();
73 dialog_->referenceED->setText(toqstr(params.getContents()));
75 dialog_->nameED->setText(toqstr(params.getOptions()));
76 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
78 // restore type settings for new insets
79 if (params.getContents().empty())
80 dialog_->typeCO->setCurrentItem(orig_type);
82 dialog_->typeCO->setCurrentItem(InsetRef::getType(params.getCmdName()));
83 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
85 dialog_->typeCO->setCurrentItem(0);
87 dialog_->sortCB->setChecked(sort_);
90 dialog_->bufferCO->clear();
91 vector<string> const buffers = controller().getBufferList();
92 for (vector<string>::const_iterator it = buffers.begin();
93 it != buffers.end(); ++it) {
94 dialog_->bufferCO->insertItem(toqstr(*it));
96 // restore the buffer combo setting for new insets
97 if (params.getContents().empty() && restored_buffer_ != -1
98 && restored_buffer_ < dialog_->bufferCO->count())
99 dialog_->bufferCO->setCurrentItem(restored_buffer_);
101 dialog_->bufferCO->setCurrentItem(controller().getBufferNum());
110 InsetCommandParams & params = controller().params();
112 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentItem()));
113 params.setContents(fromqstr(dialog_->referenceED->text()));
114 params.setOptions(fromqstr(dialog_->nameED->text()));
116 restored_buffer_ = dialog_->bufferCO->currentItem();
120 bool QRef::nameAllowed()
122 Kernel::DocType const doc_type = kernel().docType();
123 return doc_type != Kernel::LATEX &&
124 doc_type != Kernel::LITERATE;
128 bool QRef::typeAllowed()
130 Kernel::DocType const doc_type = kernel().docType();
131 return doc_type != Kernel::LINUXDOC &&
132 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_->refsLW->blockSignals(true);
174 dialog_->referenceED->blockSignals(true);
176 int lastref = dialog_->refsLW->currentRow();
178 dialog_->refsLW->setUpdatesEnabled(false);
179 dialog_->refsLW->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<string>::const_iterator iter = refs_.begin();
186 iter != refs_.end(); ++iter) {
187 dialog_->refsLW->addItem(toqstr(*iter));
191 dialog_->refsLW->sortItems();
193 dialog_->referenceED->setText(tmp);
195 // restore the last selection for new insets
196 if (tmp.isEmpty() && lastref != -1
197 && lastref < int(dialog_->refsLW->count())) {
198 dialog_->refsLW->setCurrentRow(lastref);
199 dialog_->refsLW->clearSelection();
201 for (int i = 0; i < dialog_->refsLW->count(); ++i) {
202 QListWidgetItem * item = dialog_->refsLW->item(i);
203 if (tmp == item->text()) {
204 dialog_->refsLW->setItemSelected(item, true);
208 dialog_->refsLW->setUpdatesEnabled(true);
209 dialog_->refsLW->update();
211 // Re-activate the emission of signals by these widgets.
212 dialog_->refsLW->blockSignals(false);
213 dialog_->referenceED->blockSignals(false);
217 void QRef::updateRefs()
222 string const name = controller().getBufferName(dialog_->bufferCO->currentItem());
223 refs_ = controller().getLabelList(name);
224 dialog_->sortCB->setEnabled(!refs_.empty());
225 dialog_->refsLW->setEnabled(!refs_.empty());
226 dialog_->gotoPB->setEnabled(!refs_.empty());
232 return !dialog_->referenceED->text().isEmpty();
235 } // namespace frontend