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 "qt_helpers.h"
18 #include "ButtonController.h"
20 #include "insets/InsetRef.h"
24 #include <QListWidget>
25 #include <QListWidgetItem>
26 #include <QPushButton>
28 #include <QCloseEvent>
38 /////////////////////////////////////////////////////////////////////
42 /////////////////////////////////////////////////////////////////////
44 GuiRefDialog::GuiRefDialog(GuiRef * form)
49 connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
50 connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
51 connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
52 connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
53 connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
55 connect(typeCO, SIGNAL(activated(int)),
56 this, SLOT(changed_adaptor()));
57 connect(referenceED, SIGNAL(textChanged(const QString &)),
58 this, SLOT(changed_adaptor()));
59 connect(nameED, SIGNAL(textChanged(const QString &)),
60 this, SLOT(changed_adaptor()));
61 connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
62 this, SLOT(refHighlighted(QListWidgetItem *)));
63 connect(refsLW, SIGNAL(itemSelectionChanged()),
64 this, SLOT(selectionChanged()));
65 connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
66 this, SLOT(refSelected(QListWidgetItem *)));
67 connect(sortCB, SIGNAL(clicked(bool)),
68 this, SLOT(sortToggled(bool)));
69 connect(gotoPB, SIGNAL(clicked()),
70 this, SLOT(gotoClicked()));
71 connect(updatePB, SIGNAL(clicked()),
72 this, SLOT(updateClicked()));
73 connect(bufferCO, SIGNAL(activated(int)),
74 this, SLOT(updateClicked()));
76 setFocusProxy(refsLW);
79 void GuiRefDialog::show()
85 void GuiRefDialog::changed_adaptor()
91 void GuiRefDialog::gotoClicked()
96 void GuiRefDialog::selectionChanged()
98 if (form_->readOnly())
101 QList<QListWidgetItem *> selections = refsLW->selectedItems();
102 if (selections.isEmpty())
104 QListWidgetItem * sel = selections.first();
110 void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
112 if (form_->readOnly())
115 /* int const cur_item = refsLW->currentRow();
116 bool const cur_item_selected = cur_item >= 0 ?
117 refsLB->isSelected(cur_item) : false;*/
118 bool const cur_item_selected = refsLW->isItemSelected(sel);
120 if (cur_item_selected)
121 referenceED->setText(sel->text());
125 gotoPB->setEnabled(true);
126 if (form_->typeAllowed())
127 typeCO->setEnabled(true);
128 if (form_->nameAllowed())
129 nameED->setEnabled(true);
133 void GuiRefDialog::refSelected(QListWidgetItem * sel)
135 if (form_->readOnly())
138 /* int const cur_item = refsLW->currentRow();
139 bool const cur_item_selected = cur_item >= 0 ?
140 refsLB->isSelected(cur_item) : false;*/
141 bool const cur_item_selected = refsLW->isItemSelected(sel);
143 if (cur_item_selected)
144 referenceED->setText(sel->text());
145 // <enter> or double click, inserts ref and closes dialog
150 void GuiRefDialog::sortToggled(bool on)
157 void GuiRefDialog::updateClicked()
163 void GuiRefDialog::reset_dialog() {
164 form_->at_ref_ = false;
169 void GuiRefDialog::closeEvent(QCloseEvent * e)
177 /////////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////////
184 GuiRef::GuiRef(Dialog & parent)
185 : GuiView<GuiRefDialog>(parent, _("Cross-reference")),
186 sort_(false), at_ref_(false)
191 void GuiRef::build_dialog()
193 dialog_.reset(new GuiRefDialog(this));
195 bcview().setOK(dialog_->okPB);
196 bcview().setApply(dialog_->applyPB);
197 bcview().setCancel(dialog_->closePB);
198 bcview().addReadOnly(dialog_->refsLW);
199 bcview().addReadOnly(dialog_->sortCB);
200 bcview().addReadOnly(dialog_->nameED);
201 bcview().addReadOnly(dialog_->referenceED);
202 bcview().addReadOnly(dialog_->typeCO);
203 bcview().addReadOnly(dialog_->bufferCO);
205 restored_buffer_ = -1;
209 void GuiRef::update_contents()
211 InsetCommandParams const & params = controller().params();
213 int orig_type = dialog_->typeCO->currentIndex();
215 dialog_->referenceED->setText(toqstr(params["reference"]));
217 dialog_->nameED->setText(toqstr(params["name"]));
218 dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
220 // restore type settings for new insets
221 if (params["reference"].empty())
222 dialog_->typeCO->setCurrentIndex(orig_type);
224 dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
225 dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
227 dialog_->typeCO->setCurrentIndex(0);
229 dialog_->sortCB->setChecked(sort_);
231 // insert buffer list
232 dialog_->bufferCO->clear();
233 vector<string> const buffers = controller().getBufferList();
234 for (vector<string>::const_iterator it = buffers.begin();
235 it != buffers.end(); ++it) {
236 dialog_->bufferCO->addItem(toqstr(*it));
238 // restore the buffer combo setting for new insets
239 if (params["reference"].empty() && restored_buffer_ != -1
240 && restored_buffer_ < dialog_->bufferCO->count())
241 dialog_->bufferCO->setCurrentIndex(restored_buffer_);
243 dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
252 InsetCommandParams & params = controller().params();
254 last_reference_ = dialog_->referenceED->text();
256 params.setCmdName(InsetRef::getName(dialog_->typeCO->currentIndex()));
257 params["reference"] = qstring_to_ucs4(last_reference_);
258 params["name"] = qstring_to_ucs4(dialog_->nameED->text());
260 restored_buffer_ = dialog_->bufferCO->currentIndex();
264 bool GuiRef::nameAllowed()
266 Kernel::DocType const doc_type = kernel().docType();
267 return doc_type != Kernel::LATEX &&
268 doc_type != Kernel::LITERATE;
272 bool GuiRef::typeAllowed()
274 Kernel::DocType const doc_type = kernel().docType();
275 return doc_type != Kernel::DOCBOOK;
279 void GuiRef::setGoBack()
281 dialog_->gotoPB->setText(qt_("&Go Back"));
282 dialog_->gotoPB->setToolTip("");
283 dialog_->gotoPB->setToolTip(qt_("Jump back"));
287 void GuiRef::setGotoRef()
289 dialog_->gotoPB->setText(qt_("&Go to Label"));
290 dialog_->gotoPB->setToolTip("");
291 dialog_->gotoPB->setToolTip(qt_("Jump to label"));
295 void GuiRef::gotoRef()
297 string ref(fromqstr(dialog_->referenceED->text()));
302 controller().gotoBookmark();
306 controller().gotoRef(ref);
312 void GuiRef::redoRefs()
314 // Prevent these widgets from emitting any signals whilst
315 // we modify their state.
316 dialog_->refsLW->blockSignals(true);
317 dialog_->referenceED->blockSignals(true);
318 dialog_->refsLW->setUpdatesEnabled(false);
320 dialog_->refsLW->clear();
322 // need this because Qt will send a highlight() here for
323 // the first item inserted
324 QString const oldSelection(dialog_->referenceED->text());
326 for (std::vector<docstring>::const_iterator iter = refs_.begin();
327 iter != refs_.end(); ++iter) {
328 dialog_->refsLW->addItem(toqstr(*iter));
332 dialog_->refsLW->sortItems();
334 dialog_->referenceED->setText(oldSelection);
336 // restore the last selection or, for new insets, highlight
337 // the previous selection
338 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
339 bool const newInset = oldSelection.isEmpty();
340 QString textToFind = newInset ? last_reference_ : oldSelection;
341 bool foundItem = false;
342 for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
343 QListWidgetItem * item = dialog_->refsLW->item(i);
344 if (textToFind == item->text()) {
345 dialog_->refsLW->setCurrentItem(item);
346 dialog_->refsLW->setItemSelected(item, !newInset);
347 //Make sure selected item is visible
348 dialog_->refsLW->scrollToItem(item);
353 last_reference_ = textToFind;
354 else last_reference_ = "";
356 dialog_->refsLW->setUpdatesEnabled(true);
357 dialog_->refsLW->update();
359 // Re-activate the emission of signals by these widgets.
360 dialog_->refsLW->blockSignals(false);
361 dialog_->referenceED->blockSignals(false);
365 void GuiRef::updateRefs()
368 string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
369 refs_ = controller().getLabelList(name);
370 dialog_->sortCB->setEnabled(!refs_.empty());
371 dialog_->refsLW->setEnabled(!refs_.empty());
372 dialog_->gotoPB->setEnabled(!refs_.empty());
376 bool GuiRef::isValid()
378 return !dialog_->referenceED->text().isEmpty();
381 } // namespace frontend
384 #include "GuiRef_moc.cpp"