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()
93 return static_cast<ControlRef &>(GuiDialog::controller());
97 void GuiRefDialog::changed_adaptor()
103 void GuiRefDialog::gotoClicked()
108 void GuiRefDialog::selectionChanged()
110 if (controller().isBufferReadonly())
113 QList<QListWidgetItem *> selections = refsLW->selectedItems();
114 if (selections.isEmpty())
116 QListWidgetItem * sel = selections.first();
122 void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
124 if (controller().isBufferReadonly())
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)
147 if (controller().isBufferReadonly())
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)
186 GuiDialog::closeEvent(e);
190 void GuiRefDialog::updateContents()
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() && !controller().isBufferReadonly());
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() && !controller().isBufferReadonly());
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 KernelDocType const doc_type = controller().docType();
248 return doc_type != LATEX && doc_type != LITERATE;
252 bool GuiRefDialog::typeAllowed()
254 return controller().docType() != DOCBOOK;
258 void GuiRefDialog::setGoBack()
260 gotoPB->setText(qt_("&Go Back"));
261 gotoPB->setToolTip("");
262 gotoPB->setToolTip(qt_("Jump back"));
266 void GuiRefDialog::setGotoRef()
268 gotoPB->setText(qt_("&Go to Label"));
269 gotoPB->setToolTip("");
270 gotoPB->setToolTip(qt_("Jump to label"));
274 void GuiRefDialog::gotoRef()
276 string ref = fromqstr(referenceED->text());
281 controller().gotoBookmark();
285 controller().gotoRef(ref);
291 void GuiRefDialog::redoRefs()
293 // Prevent these widgets from emitting any signals whilst
294 // we modify their state.
295 refsLW->blockSignals(true);
296 referenceED->blockSignals(true);
297 refsLW->setUpdatesEnabled(false);
301 // need this because Qt will send a highlight() here for
302 // the first item inserted
303 QString const oldSelection(referenceED->text());
305 for (std::vector<docstring>::const_iterator iter = refs_.begin();
306 iter != refs_.end(); ++iter) {
307 refsLW->addItem(toqstr(*iter));
313 referenceED->setText(oldSelection);
315 // restore the last selection or, for new insets, highlight
316 // the previous selection
317 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
318 bool const newInset = oldSelection.isEmpty();
319 QString textToFind = newInset ? last_reference_ : oldSelection;
320 bool foundItem = false;
321 for (int i = 0; !foundItem && i < refsLW->count(); ++i) {
322 QListWidgetItem * item = refsLW->item(i);
323 if (textToFind == item->text()) {
324 refsLW->setCurrentItem(item);
325 refsLW->setItemSelected(item, !newInset);
326 //Make sure selected item is visible
327 refsLW->scrollToItem(item);
332 last_reference_ = textToFind;
333 else last_reference_ = "";
335 refsLW->setUpdatesEnabled(true);
338 // Re-activate the emission of signals by these widgets.
339 refsLW->blockSignals(false);
340 referenceED->blockSignals(false);
344 void GuiRefDialog::updateRefs()
347 string const name = controller().getBufferName(bufferCO->currentIndex());
348 refs_ = controller().getLabelList(name);
349 sortCB->setEnabled(!refs_.empty());
350 refsLW->setEnabled(!refs_.empty());
351 gotoPB->setEnabled(!refs_.empty());
356 bool GuiRefDialog::isValid()
358 return !referenceED->text().isEmpty();
361 } // namespace frontend
364 #include "GuiRef_moc.cpp"