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.
16 #include "ControlRef.h"
17 #include "qt_helpers.h"
19 #include "insets/InsetRef.h"
23 #include <QListWidget>
24 #include <QListWidgetItem>
25 #include <QPushButton>
27 #include <QCloseEvent>
37 GuiRefDialog::GuiRefDialog(LyXView & lv)
38 : GuiDialog(lv, "ref")
41 setController(new ControlRef(*this));
42 setViewTitle(_("Cross-reference"));
47 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
48 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
49 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
50 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
51 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
53 connect(typeCO, SIGNAL(activated(int)),
54 this, SLOT(changed_adaptor()));
55 connect(referenceED, SIGNAL(textChanged(const QString &)),
56 this, SLOT(changed_adaptor()));
57 connect(nameED, SIGNAL(textChanged(const QString &)),
58 this, SLOT(changed_adaptor()));
59 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
60 this, SLOT(refHighlighted(QListWidgetItem *)));
61 connect(refsLW, SIGNAL(itemSelectionChanged()),
62 this, SLOT(selectionChanged()));
63 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
64 this, SLOT(refSelected(QListWidgetItem *)));
65 connect(sortCB, SIGNAL(clicked(bool)),
66 this, SLOT(sortToggled(bool)));
67 connect(gotoPB, SIGNAL(clicked()),
68 this, SLOT(gotoClicked()));
69 connect(updatePB, SIGNAL(clicked()),
70 this, SLOT(updateClicked()));
71 connect(bufferCO, SIGNAL(activated(int)),
72 this, SLOT(updateClicked()));
74 setFocusProxy(refsLW);
76 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
78 bc().setApply(applyPB);
79 bc().setCancel(closePB);
80 bc().addReadOnly(refsLW);
81 bc().addReadOnly(sortCB);
82 bc().addReadOnly(nameED);
83 bc().addReadOnly(referenceED);
84 bc().addReadOnly(typeCO);
85 bc().addReadOnly(bufferCO);
87 restored_buffer_ = -1;
91 ControlRef & GuiRefDialog::controller() const
93 return static_cast<ControlRef &>(Dialog::controller());
97 void GuiRefDialog::changed_adaptor()
103 void GuiRefDialog::gotoClicked()
108 void GuiRefDialog::selectionChanged()
113 QList<QListWidgetItem *> selections = refsLW->selectedItems();
114 if (selections.isEmpty())
116 QListWidgetItem * sel = selections.first();
122 void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
127 /* int const cur_item = refsLW->currentRow();
128 bool const cur_item_selected = cur_item >= 0 ?
129 refsLB->isSelected(cur_item) : false;*/
130 bool const cur_item_selected = refsLW->isItemSelected(sel);
132 if (cur_item_selected)
133 referenceED->setText(sel->text());
137 gotoPB->setEnabled(true);
139 typeCO->setEnabled(true);
141 nameED->setEnabled(true);
145 void GuiRefDialog::refSelected(QListWidgetItem * sel)
150 /* int const cur_item = refsLW->currentRow();
151 bool const cur_item_selected = cur_item >= 0 ?
152 refsLB->isSelected(cur_item) : false;*/
153 bool const cur_item_selected = refsLW->isItemSelected(sel);
155 if (cur_item_selected)
156 referenceED->setText(sel->text());
157 // <enter> or double click, inserts ref and closes dialog
162 void GuiRefDialog::sortToggled(bool on)
169 void GuiRefDialog::updateClicked()
175 void GuiRefDialog::reset_dialog()
182 void GuiRefDialog::closeEvent(QCloseEvent * e)
190 void GuiRefDialog::update_contents()
192 InsetCommandParams const & params = controller().params();
194 int orig_type = typeCO->currentIndex();
196 referenceED->setText(toqstr(params["reference"]));
198 nameED->setText(toqstr(params["name"]));
199 nameED->setReadOnly(!nameAllowed() && !readOnly());
201 // restore type settings for new insets
202 if (params["reference"].empty())
203 typeCO->setCurrentIndex(orig_type);
205 typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
206 typeCO->setEnabled(typeAllowed() && !readOnly());
208 typeCO->setCurrentIndex(0);
210 sortCB->setChecked(sort_);
212 // insert buffer list
214 vector<string> const buffers = controller().getBufferList();
215 for (vector<string>::const_iterator it = buffers.begin();
216 it != buffers.end(); ++it) {
217 bufferCO->addItem(toqstr(*it));
219 // restore the buffer combo setting for new insets
220 if (params["reference"].empty() && restored_buffer_ != -1
221 && restored_buffer_ < bufferCO->count())
222 bufferCO->setCurrentIndex(restored_buffer_);
224 bufferCO->setCurrentIndex(controller().getBufferNum());
227 bc().setValid(false);
231 void GuiRefDialog::applyView()
233 InsetCommandParams & params = controller().params();
235 last_reference_ = referenceED->text();
237 params.setCmdName(InsetRef::getName(typeCO->currentIndex()));
238 params["reference"] = qstring_to_ucs4(last_reference_);
239 params["name"] = qstring_to_ucs4(nameED->text());
241 restored_buffer_ = bufferCO->currentIndex();
245 bool GuiRefDialog::nameAllowed()
247 Kernel::DocType const doc_type = kernel().docType();
248 return doc_type != Kernel::LATEX &&
249 doc_type != Kernel::LITERATE;
253 bool GuiRefDialog::typeAllowed()
255 Kernel::DocType const doc_type = kernel().docType();
256 return doc_type != Kernel::DOCBOOK;
260 void GuiRefDialog::setGoBack()
262 gotoPB->setText(qt_("&Go Back"));
263 gotoPB->setToolTip("");
264 gotoPB->setToolTip(qt_("Jump back"));
268 void GuiRefDialog::setGotoRef()
270 gotoPB->setText(qt_("&Go to Label"));
271 gotoPB->setToolTip("");
272 gotoPB->setToolTip(qt_("Jump to label"));
276 void GuiRefDialog::gotoRef()
278 string ref(fromqstr(referenceED->text()));
283 controller().gotoBookmark();
287 controller().gotoRef(ref);
293 void GuiRefDialog::redoRefs()
295 // Prevent these widgets from emitting any signals whilst
296 // we modify their state.
297 refsLW->blockSignals(true);
298 referenceED->blockSignals(true);
299 refsLW->setUpdatesEnabled(false);
303 // need this because Qt will send a highlight() here for
304 // the first item inserted
305 QString const oldSelection(referenceED->text());
307 for (std::vector<docstring>::const_iterator iter = refs_.begin();
308 iter != refs_.end(); ++iter) {
309 refsLW->addItem(toqstr(*iter));
315 referenceED->setText(oldSelection);
317 // restore the last selection or, for new insets, highlight
318 // the previous selection
319 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
320 bool const newInset = oldSelection.isEmpty();
321 QString textToFind = newInset ? last_reference_ : oldSelection;
322 bool foundItem = false;
323 for (int i = 0; !foundItem && i < refsLW->count(); ++i) {
324 QListWidgetItem * item = refsLW->item(i);
325 if (textToFind == item->text()) {
326 refsLW->setCurrentItem(item);
327 refsLW->setItemSelected(item, !newInset);
328 //Make sure selected item is visible
329 refsLW->scrollToItem(item);
334 last_reference_ = textToFind;
335 else last_reference_ = "";
337 refsLW->setUpdatesEnabled(true);
340 // Re-activate the emission of signals by these widgets.
341 refsLW->blockSignals(false);
342 referenceED->blockSignals(false);
346 void GuiRefDialog::updateRefs()
349 string const name = controller().getBufferName(bufferCO->currentIndex());
350 refs_ = controller().getLabelList(name);
351 sortCB->setEnabled(!refs_.empty());
352 refsLW->setEnabled(!refs_.empty());
353 gotoPB->setEnabled(!refs_.empty());
358 bool GuiRefDialog::isValid()
360 return !referenceED->text().isEmpty();
363 } // namespace frontend
366 #include "GuiRef_moc.cpp"