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::showView()
103 void GuiRefDialog::changed_adaptor()
109 void GuiRefDialog::gotoClicked()
114 void GuiRefDialog::selectionChanged()
119 QList<QListWidgetItem *> selections = refsLW->selectedItems();
120 if (selections.isEmpty())
122 QListWidgetItem * sel = selections.first();
128 void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
133 /* int const cur_item = refsLW->currentRow();
134 bool const cur_item_selected = cur_item >= 0 ?
135 refsLB->isSelected(cur_item) : false;*/
136 bool const cur_item_selected = refsLW->isItemSelected(sel);
138 if (cur_item_selected)
139 referenceED->setText(sel->text());
143 gotoPB->setEnabled(true);
145 typeCO->setEnabled(true);
147 nameED->setEnabled(true);
151 void GuiRefDialog::refSelected(QListWidgetItem * sel)
156 /* int const cur_item = refsLW->currentRow();
157 bool const cur_item_selected = cur_item >= 0 ?
158 refsLB->isSelected(cur_item) : false;*/
159 bool const cur_item_selected = refsLW->isItemSelected(sel);
161 if (cur_item_selected)
162 referenceED->setText(sel->text());
163 // <enter> or double click, inserts ref and closes dialog
168 void GuiRefDialog::sortToggled(bool on)
175 void GuiRefDialog::updateClicked()
181 void GuiRefDialog::reset_dialog()
188 void GuiRefDialog::closeEvent(QCloseEvent * e)
196 void GuiRefDialog::update_contents()
198 InsetCommandParams const & params = controller().params();
200 int orig_type = typeCO->currentIndex();
202 referenceED->setText(toqstr(params["reference"]));
204 nameED->setText(toqstr(params["name"]));
205 nameED->setReadOnly(!nameAllowed() && !readOnly());
207 // restore type settings for new insets
208 if (params["reference"].empty())
209 typeCO->setCurrentIndex(orig_type);
211 typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
212 typeCO->setEnabled(typeAllowed() && !readOnly());
214 typeCO->setCurrentIndex(0);
216 sortCB->setChecked(sort_);
218 // insert buffer list
220 vector<string> const buffers = controller().getBufferList();
221 for (vector<string>::const_iterator it = buffers.begin();
222 it != buffers.end(); ++it) {
223 bufferCO->addItem(toqstr(*it));
225 // restore the buffer combo setting for new insets
226 if (params["reference"].empty() && restored_buffer_ != -1
227 && restored_buffer_ < bufferCO->count())
228 bufferCO->setCurrentIndex(restored_buffer_);
230 bufferCO->setCurrentIndex(controller().getBufferNum());
233 bc().setValid(false);
237 void GuiRefDialog::applyView()
239 InsetCommandParams & params = controller().params();
241 last_reference_ = referenceED->text();
243 params.setCmdName(InsetRef::getName(typeCO->currentIndex()));
244 params["reference"] = qstring_to_ucs4(last_reference_);
245 params["name"] = qstring_to_ucs4(nameED->text());
247 restored_buffer_ = bufferCO->currentIndex();
251 bool GuiRefDialog::nameAllowed()
253 Kernel::DocType const doc_type = kernel().docType();
254 return doc_type != Kernel::LATEX &&
255 doc_type != Kernel::LITERATE;
259 bool GuiRefDialog::typeAllowed()
261 Kernel::DocType const doc_type = kernel().docType();
262 return doc_type != Kernel::DOCBOOK;
266 void GuiRefDialog::setGoBack()
268 gotoPB->setText(qt_("&Go Back"));
269 gotoPB->setToolTip("");
270 gotoPB->setToolTip(qt_("Jump back"));
274 void GuiRefDialog::setGotoRef()
276 gotoPB->setText(qt_("&Go to Label"));
277 gotoPB->setToolTip("");
278 gotoPB->setToolTip(qt_("Jump to label"));
282 void GuiRefDialog::gotoRef()
284 string ref(fromqstr(referenceED->text()));
289 controller().gotoBookmark();
293 controller().gotoRef(ref);
299 void GuiRefDialog::redoRefs()
301 // Prevent these widgets from emitting any signals whilst
302 // we modify their state.
303 refsLW->blockSignals(true);
304 referenceED->blockSignals(true);
305 refsLW->setUpdatesEnabled(false);
309 // need this because Qt will send a highlight() here for
310 // the first item inserted
311 QString const oldSelection(referenceED->text());
313 for (std::vector<docstring>::const_iterator iter = refs_.begin();
314 iter != refs_.end(); ++iter) {
315 refsLW->addItem(toqstr(*iter));
321 referenceED->setText(oldSelection);
323 // restore the last selection or, for new insets, highlight
324 // the previous selection
325 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
326 bool const newInset = oldSelection.isEmpty();
327 QString textToFind = newInset ? last_reference_ : oldSelection;
328 bool foundItem = false;
329 for (int i = 0; !foundItem && i < refsLW->count(); ++i) {
330 QListWidgetItem * item = refsLW->item(i);
331 if (textToFind == item->text()) {
332 refsLW->setCurrentItem(item);
333 refsLW->setItemSelected(item, !newInset);
334 //Make sure selected item is visible
335 refsLW->scrollToItem(item);
340 last_reference_ = textToFind;
341 else last_reference_ = "";
343 refsLW->setUpdatesEnabled(true);
346 // Re-activate the emission of signals by these widgets.
347 refsLW->blockSignals(false);
348 referenceED->blockSignals(false);
352 void GuiRefDialog::updateRefs()
355 string const name = controller().getBufferName(bufferCO->currentIndex());
356 refs_ = controller().getLabelList(name);
357 sortCB->setEnabled(!refs_.empty());
358 refsLW->setEnabled(!refs_.empty());
359 gotoPB->setEnabled(!refs_.empty());
364 bool GuiRefDialog::isValid()
366 return !referenceED->text().isEmpty();
369 } // namespace frontend
372 #include "GuiRef_moc.cpp"