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 "qt_helpers.h"
17 #include "insets/InsetRef.h"
21 #include <QListWidget>
22 #include <QListWidgetItem>
23 #include <QPushButton>
25 #include <QCloseEvent>
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
41 GuiRefDialog::GuiRefDialog(GuiRef * form)
46 connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
47 connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
48 connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
49 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
50 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
52 connect(typeCO, SIGNAL(activated(int)),
53 this, SLOT(changed_adaptor()));
54 connect(referenceED, SIGNAL(textChanged(const QString &)),
55 this, SLOT(changed_adaptor()));
56 connect(nameED, SIGNAL(textChanged(const QString &)),
57 this, SLOT(changed_adaptor()));
58 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
59 this, SLOT(refHighlighted(QListWidgetItem *)));
60 connect(refsLW, SIGNAL(itemSelectionChanged()),
61 this, SLOT(selectionChanged()));
62 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
63 this, SLOT(refSelected(QListWidgetItem *)));
64 connect(sortCB, SIGNAL(clicked(bool)),
65 this, SLOT(sortToggled(bool)));
66 connect(gotoPB, SIGNAL(clicked()),
67 this, SLOT(gotoClicked()));
68 connect(updatePB, SIGNAL(clicked()),
69 this, SLOT(updateClicked()));
70 connect(bufferCO, SIGNAL(activated(int)),
71 this, SLOT(updateClicked()));
73 setFocusProxy(refsLW);
76 void GuiRefDialog::show()
82 void GuiRefDialog::changed_adaptor()
88 void GuiRefDialog::gotoClicked()
93 void GuiRefDialog::selectionChanged()
95 if (form_->readOnly())
98 QList<QListWidgetItem *> selections = refsLW->selectedItems();
99 if (selections.isEmpty())
101 QListWidgetItem * sel = selections.first();
107 void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
109 if (form_->readOnly())
112 /* int const cur_item = refsLW->currentRow();
113 bool const cur_item_selected = cur_item >= 0 ?
114 refsLB->isSelected(cur_item) : false;*/
115 bool const cur_item_selected = refsLW->isItemSelected(sel);
117 if (cur_item_selected)
118 referenceED->setText(sel->text());
122 gotoPB->setEnabled(true);
123 if (form_->typeAllowed())
124 typeCO->setEnabled(true);
125 if (form_->nameAllowed())
126 nameED->setEnabled(true);
130 void GuiRefDialog::refSelected(QListWidgetItem * sel)
132 if (form_->readOnly())
135 /* int const cur_item = refsLW->currentRow();
136 bool const cur_item_selected = cur_item >= 0 ?
137 refsLB->isSelected(cur_item) : false;*/
138 bool const cur_item_selected = refsLW->isItemSelected(sel);
140 if (cur_item_selected)
141 referenceED->setText(sel->text());
142 // <enter> or double click, inserts ref and closes dialog
147 void GuiRefDialog::sortToggled(bool on)
154 void GuiRefDialog::updateClicked()
160 void GuiRefDialog::reset_dialog() {
161 form_->at_ref_ = false;
166 void GuiRefDialog::closeEvent(QCloseEvent * e)
174 /////////////////////////////////////////////////////////////////////
178 /////////////////////////////////////////////////////////////////////
181 GuiRef::GuiRef(GuiDialog & parent)
182 : GuiView<GuiRefDialog>(parent, _("Cross-reference")),
183 sort_(false), at_ref_(false)
188 void GuiRef::build_dialog()
190 dialog_.reset(new GuiRefDialog(this));
192 bc().setOK(dialog_->okPB);
193 bc().setApply(dialog_->applyPB);
194 bc().setCancel(dialog_->closePB);
195 bc().addReadOnly(dialog_->refsLW);
196 bc().addReadOnly(dialog_->sortCB);
197 bc().addReadOnly(dialog_->nameED);
198 bc().addReadOnly(dialog_->referenceED);
199 bc().addReadOnly(dialog_->typeCO);
200 bc().addReadOnly(dialog_->bufferCO);
202 restored_buffer_ = -1;
206 void GuiRef::update_contents()
208 InsetCommandParams const & params = controller().params();
210 int orig_type = dialog_->typeCO->currentIndex();
212 dialog_->referenceED->setText(toqstr(params["reference"]));
214 dialog_->nameED->setText(toqstr(params["name"]));
215 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
217 // restore type settings for new insets
218 if (params["reference"].empty())
219 dialog_->typeCO->setCurrentIndex(orig_type);
221 dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
222 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
224 dialog_->typeCO->setCurrentIndex(0);
226 dialog_->sortCB->setChecked(sort_);
228 // insert buffer list
229 dialog_->bufferCO->clear();
230 vector<string> const buffers = controller().getBufferList();
231 for (vector<string>::const_iterator it = buffers.begin();
232 it != buffers.end(); ++it) {
233 dialog_->bufferCO->addItem(toqstr(*it));
235 // restore the buffer combo setting for new insets
236 if (params["reference"].empty() && restored_buffer_ != -1
237 && restored_buffer_ < dialog_->bufferCO->count())
238 dialog_->bufferCO->setCurrentIndex(restored_buffer_);
240 dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
243 bc().setValid(false);
249 InsetCommandParams & params = controller().params();
251 last_reference_ = dialog_->referenceED->text();
253 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
254 params["reference"] = qstring_to_ucs4(last_reference_);
255 params["name"] = qstring_to_ucs4(dialog_->nameED->text());
257 restored_buffer_ = dialog_->bufferCO->currentIndex();
261 bool GuiRef::nameAllowed()
263 Kernel::DocType const doc_type = kernel().docType();
264 return doc_type != Kernel::LATEX &&
265 doc_type != Kernel::LITERATE;
269 bool GuiRef::typeAllowed()
271 Kernel::DocType const doc_type = kernel().docType();
272 return doc_type != Kernel::DOCBOOK;
276 void GuiRef::setGoBack()
278 dialog_->gotoPB->setText(qt_("&Go Back"));
279 dialog_->gotoPB->setToolTip("");
280 dialog_->gotoPB->setToolTip(qt_("Jump back"));
284 void GuiRef::setGotoRef()
286 dialog_->gotoPB->setText(qt_("&Go to Label"));
287 dialog_->gotoPB->setToolTip("");
288 dialog_->gotoPB->setToolTip(qt_("Jump to label"));
292 void GuiRef::gotoRef()
294 string ref(fromqstr(dialog_->referenceED->text()));
299 controller().gotoBookmark();
303 controller().gotoRef(ref);
309 void GuiRef::redoRefs()
311 // Prevent these widgets from emitting any signals whilst
312 // we modify their state.
313 dialog_->refsLW->blockSignals(true);
314 dialog_->referenceED->blockSignals(true);
315 dialog_->refsLW->setUpdatesEnabled(false);
317 dialog_->refsLW->clear();
319 // need this because Qt will send a highlight() here for
320 // the first item inserted
321 QString const oldSelection(dialog_->referenceED->text());
323 for (std::vector<docstring>::const_iterator iter = refs_.begin();
324 iter != refs_.end(); ++iter) {
325 dialog_->refsLW->addItem(toqstr(*iter));
329 dialog_->refsLW->sortItems();
331 dialog_->referenceED->setText(oldSelection);
333 // restore the last selection or, for new insets, highlight
334 // the previous selection
335 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
336 bool const newInset = oldSelection.isEmpty();
337 QString textToFind = newInset ? last_reference_ : oldSelection;
338 bool foundItem = false;
339 for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
340 QListWidgetItem * item = dialog_->refsLW->item(i);
341 if (textToFind == item->text()) {
342 dialog_->refsLW->setCurrentItem(item);
343 dialog_->refsLW->setItemSelected(item, !newInset);
344 //Make sure selected item is visible
345 dialog_->refsLW->scrollToItem(item);
350 last_reference_ = textToFind;
351 else last_reference_ = "";
353 dialog_->refsLW->setUpdatesEnabled(true);
354 dialog_->refsLW->update();
356 // Re-activate the emission of signals by these widgets.
357 dialog_->refsLW->blockSignals(false);
358 dialog_->referenceED->blockSignals(false);
362 void GuiRef::updateRefs()
365 string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
366 refs_ = controller().getLabelList(name);
367 dialog_->sortCB->setEnabled(!refs_.empty());
368 dialog_->refsLW->setEnabled(!refs_.empty());
369 dialog_->gotoPB->setEnabled(!refs_.empty());
373 bool GuiRef::isValid()
375 return !dialog_->referenceED->text().isEmpty();
378 } // namespace frontend
381 #include "GuiRef_moc.cpp"