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 "GuiApplication.h"
19 #include "BufferParams.h"
20 #include "BufferList.h"
21 #include "FuncRequest.h"
23 #include "qt_helpers.h"
25 #include "insets/InsetRef.h"
27 #include "support/FileName.h"
28 #include "support/FileNameList.h"
29 #include "support/filetools.h" // makeAbsPath, makeDisplayPath
33 #include <QTreeWidget>
34 #include <QTreeWidgetItem>
35 #include <QPushButton>
37 #include <QCloseEvent>
38 #include <QHeaderView>
41 using namespace lyx::support;
46 GuiRef::GuiRef(GuiView & lv)
47 : GuiDialog(lv, "ref", qt_("Cross-reference")),
48 params_(insetCode("ref"))
55 filter_ = new FancyLineEdit(this);
56 filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
57 filter_->setButtonVisible(FancyLineEdit::Right, true);
58 filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
59 filter_->setAutoHideButton(FancyLineEdit::Right, true);
60 filter_->setPlaceholderText(qt_("All available labels"));
61 filter_->setToolTip(qt_("Enter string to filter the list of available labels"));
62 #if (QT_VERSION < 0x050000)
63 connect(filter_, SIGNAL(downPressed()),
64 refsTW, SLOT(setFocus()));
66 connect(filter_, &FancyLineEdit::downPressed,
67 refsTW, [=](){ focusAndHighlight(refsTW); });
70 filterBarL->addWidget(filter_, 0);
71 findKeysLA->setBuddy(filter_);
73 sortingCO->addItem(qt_("By Occurrence"), "unsorted");
74 sortingCO->addItem(qt_("Alphabetically (Case-Insensitive)"), "nocase");
75 sortingCO->addItem(qt_("Alphabetically (Case-Sensitive)"), "case");
77 refsTW->setColumnCount(1);
78 refsTW->header()->setVisible(false);
80 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
81 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
82 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
83 connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog()));
84 connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
86 connect(typeCO, SIGNAL(activated(int)),
87 this, SLOT(changed_adaptor()));
88 connect(referenceED, SIGNAL(textChanged(QString)),
89 this, SLOT(refTextChanged(QString)));
90 connect(referenceED, SIGNAL(textChanged(QString)),
91 this, SLOT(changed_adaptor()));
92 connect(filter_, SIGNAL(textEdited(QString)),
93 this, SLOT(filterLabels()));
94 connect(filter_, SIGNAL(rightButtonClicked()),
95 this, SLOT(resetFilter()));
96 connect(csFindCB, SIGNAL(clicked()),
97 this, SLOT(filterLabels()));
98 connect(nameED, SIGNAL(textChanged(QString)),
99 this, SLOT(changed_adaptor()));
100 connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
101 this, SLOT(refHighlighted(QTreeWidgetItem *)));
102 connect(refsTW, SIGNAL(itemSelectionChanged()),
103 this, SLOT(selectionChanged()));
104 connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
105 this, SLOT(refSelected(QTreeWidgetItem *)));
106 connect(sortingCO, SIGNAL(activated(int)),
107 this, SLOT(sortToggled()));
108 connect(groupCB, SIGNAL(clicked()),
109 this, SLOT(groupToggled()));
110 connect(gotoPB, SIGNAL(clicked()),
111 this, SLOT(gotoClicked()));
112 connect(updatePB, SIGNAL(clicked()),
113 this, SLOT(updateClicked()));
114 connect(bufferCO, SIGNAL(activated(int)),
115 this, SLOT(updateClicked()));
116 connect(pluralCB, SIGNAL(clicked()),
117 this, SLOT(changed_adaptor()));
118 connect(capsCB, SIGNAL(clicked()),
119 this, SLOT(changed_adaptor()));
120 connect(noprefixCB, SIGNAL(clicked()),
121 this, SLOT(changed_adaptor()));
125 bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
127 bc().setApply(applyPB);
128 bc().setCancel(closePB);
129 bc().addReadOnly(typeCO);
131 restored_buffer_ = -1;
134 setFocusProxy(filter_);
138 void GuiRef::enableView(bool enable)
141 // In the opposite case, updateContents() will be called anyway.
143 GuiDialog::enableView(enable);
147 void GuiRef::enableBoxes()
149 bool const isFormatted =
150 (InsetRef::getName(typeCO->currentIndex()) == "formatted");
151 bool const isLabelOnly =
152 (InsetRef::getName(typeCO->currentIndex()) == "labelonly");
153 bool const usingRefStyle = buffer().params().use_refstyle;
154 pluralCB->setEnabled(isFormatted && usingRefStyle);
155 capsCB->setEnabled(isFormatted && usingRefStyle);
156 noprefixCB->setEnabled(isLabelOnly);
160 void GuiRef::changed_adaptor()
167 void GuiRef::gotoClicked()
169 // By setting last_reference_, we ensure that the reference
170 // to which we are going (or from which we are returning) is
171 // restored in the dialog. It's a bit of a hack, but it works,
172 // and no-one seems to have any better idea.
174 last_reference_.isEmpty() || last_reference_.isNull();
176 last_reference_ = referenceED->text();
179 last_reference_.clear();
183 void GuiRef::selectionChanged()
185 if (isBufferReadonly())
188 QList<QTreeWidgetItem *> selections = refsTW->selectedItems();
189 if (selections.isEmpty())
191 QTreeWidgetItem * sel = selections.first();
197 void GuiRef::refHighlighted(QTreeWidgetItem * sel)
199 if (sel->childCount() > 0) {
200 sel->setExpanded(true);
204 /* int const cur_item = refsTW->currentRow();
205 bool const cur_item_selected = cur_item >= 0 ?
206 refsLB->isSelected(cur_item) : false;*/
207 bool const cur_item_selected = refsTW->isItemSelected(sel);
209 if (cur_item_selected)
210 referenceED->setText(sel->text(0));
214 gotoPB->setEnabled(true);
215 if (typeAllowed() && !isBufferReadonly())
216 typeCO->setEnabled(true);
217 nameED->setHidden(!nameAllowed());
218 nameL->setHidden(!nameAllowed());
222 void GuiRef::refTextChanged(QString const & str)
224 gotoPB->setEnabled(!str.isEmpty());
225 typeCO->setEnabled(!str.isEmpty());
226 typeLA->setEnabled(!str.isEmpty());
230 void GuiRef::refSelected(QTreeWidgetItem * sel)
232 if (isBufferReadonly())
235 if (sel->childCount()) {
236 sel->setExpanded(false);
240 /* int const cur_item = refsTW->currentRow();
241 bool const cur_item_selected = cur_item >= 0 ?
242 refsLB->isSelected(cur_item) : false;*/
243 bool const cur_item_selected = refsTW->isItemSelected(sel);
245 if (cur_item_selected)
246 referenceED->setText(sel->text(0));
247 // <enter> or double click, inserts ref and closes dialog
252 void GuiRef::sortToggled()
258 void GuiRef::groupToggled()
264 void GuiRef::updateClicked()
270 void GuiRef::dialogRejected()
273 // We have to do this manually, instead of calling slotClose(), because
274 // the dialog has already been made invisible before rejected() triggers.
275 Dialog::disconnect();
279 void GuiRef::resetDialog()
286 void GuiRef::closeEvent(QCloseEvent * e)
294 void GuiRef::updateContents()
296 int orig_type = typeCO->currentIndex();
298 referenceED->clear();
301 referenceED->setText(toqstr(params_["reference"]));
302 nameED->setText(toqstr(params_["name"]));
303 nameED->setHidden(!nameAllowed());
304 nameL->setHidden(!nameAllowed());
306 // restore type settings for new insets
307 bool const new_inset = params_["reference"].empty();
309 typeCO->setCurrentIndex(orig_type);
311 typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
312 typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
314 typeCO->setCurrentIndex(0);
316 pluralCB->setChecked(params_["plural"] == "true");
317 capsCB->setChecked(params_["caps"] == "true");
318 noprefixCB->setChecked(params_["noprefix"] == "true");
320 // insert buffer list
322 FileNameList const buffers(theBufferList().fileNames());
323 for (FileNameList::const_iterator it = buffers.begin();
324 it != buffers.end(); ++it) {
325 bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
328 int const thebuffer = theBufferList().bufferNum(buffer().fileName());
329 // restore the buffer combo setting for new insets
330 if (new_inset && restored_buffer_ != -1
331 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
332 bufferCO->setCurrentIndex(restored_buffer_);
334 int const num = theBufferList().bufferNum(buffer().fileName());
335 bufferCO->setCurrentIndex(num);
336 if (thebuffer != active_buffer_)
337 restored_buffer_ = num;
339 active_buffer_ = thebuffer;
343 // Activate OK/Apply buttons if the users inserts a new ref
344 // and we have a valid pre-setting.
345 bc().setValid(isValid() && new_inset);
349 void GuiRef::applyView()
351 last_reference_ = referenceED->text();
353 params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
354 params_["reference"] = qstring_to_ucs4(last_reference_);
355 params_["name"] = qstring_to_ucs4(nameED->text());
356 params_["plural"] = pluralCB->isChecked() ?
357 from_ascii("true") : from_ascii("false");
358 params_["caps"] = capsCB->isChecked() ?
359 from_ascii("true") : from_ascii("false");
360 params_["noprefix"] = noprefixCB->isChecked() ?
361 from_ascii("true") : from_ascii("false");
362 restored_buffer_ = bufferCO->currentIndex();
366 bool GuiRef::nameAllowed()
368 KernelDocType const doc_type = docType();
369 return doc_type != LATEX && doc_type != LITERATE;
373 bool GuiRef::typeAllowed()
375 return docType() != DOCBOOK;
379 void GuiRef::setGoBack()
381 gotoPB->setText(qt_("&Go Back"));
382 gotoPB->setToolTip(qt_("Jump back to the original cursor location"));
386 void GuiRef::setGotoRef()
388 gotoPB->setText(qt_("&Go to Label"));
389 gotoPB->setToolTip(qt_("Jump to the selected label"));
393 void GuiRef::gotoRef()
395 string ref = fromqstr(referenceED->text());
409 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
411 return s1.toLower() < s2.toLower();
415 void GuiRef::redoRefs()
417 // Prevent these widgets from emitting any signals whilst
418 // we modify their state.
419 refsTW->blockSignals(true);
420 referenceED->blockSignals(true);
421 refsTW->setUpdatesEnabled(false);
425 // need this because Qt will send a highlight() here for
426 // the first item inserted
427 QString const oldSelection(referenceED->text());
429 QStringList refsStrings;
430 QStringList refsCategories;
431 vector<docstring>::const_iterator iter;
432 bool noprefix = false;
433 for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
434 QString const lab = toqstr(*iter);
435 refsStrings.append(lab);
436 if (groupCB->isChecked()) {
437 if (lab.contains(":")) {
438 QString const pref = lab.split(':')[0];
439 if (!refsCategories.contains(pref)) {
441 refsCategories.append(pref);
450 // sort categories case-intensively
451 qSort(refsCategories.begin(), refsCategories.end(),
452 caseInsensitiveLessThan /*defined above*/);
454 refsCategories.insert(0, qt_("<No prefix>"));
456 QString const sort = sortingCO->isEnabled() ?
457 sortingCO->itemData(sortingCO->currentIndex()).toString()
459 if (sort == "nocase")
460 qSort(refsStrings.begin(), refsStrings.end(),
461 caseInsensitiveLessThan /*defined above*/);
462 else if (sort == "case")
463 qSort(refsStrings.begin(), refsStrings.end());
465 if (groupCB->isChecked()) {
466 QList<QTreeWidgetItem *> refsCats;
467 for (int i = 0; i < refsCategories.size(); ++i) {
468 QString const cat = refsCategories.at(i);
469 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
470 item->setText(0, cat);
471 for (int i = 0; i < refsStrings.size(); ++i) {
472 QString const ref = refsStrings.at(i);
473 if ((ref.startsWith(cat + QString(":")))
474 || (cat == qt_("<No prefix>")
475 && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
476 QTreeWidgetItem * child =
477 new QTreeWidgetItem(item);
478 child->setText(0, ref);
479 item->addChild(child);
482 refsCats.append(item);
484 refsTW->addTopLevelItems(refsCats);
486 QList<QTreeWidgetItem *> refsItems;
487 for (int i = 0; i < refsStrings.size(); ++i) {
488 QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
489 item->setText(0, refsStrings.at(i));
490 refsItems.append(item);
492 refsTW->addTopLevelItems(refsItems);
495 // restore the last selection or, for new insets, highlight
496 // the previous selection
497 if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
498 bool const newInset = oldSelection.isEmpty();
499 QString textToFind = newInset ? last_reference_ : oldSelection;
500 referenceED->setText(textToFind);
501 last_reference_.clear();
502 QTreeWidgetItemIterator it(refsTW);
504 if ((*it)->text(0) == textToFind) {
505 refsTW->setCurrentItem(*it);
506 refsTW->setItemSelected(*it, true);
507 //Make sure selected item is visible
508 refsTW->scrollToItem(*it);
509 last_reference_ = textToFind;
515 refsTW->setUpdatesEnabled(true);
521 // Re-activate the emission of signals by these widgets.
522 refsTW->blockSignals(false);
523 referenceED->blockSignals(false);
525 gotoPB->setEnabled(!referenceED->text().isEmpty());
526 typeCO->setEnabled(!referenceED->text().isEmpty());
527 typeLA->setEnabled(!referenceED->text().isEmpty());
531 void GuiRef::updateRefs()
534 int const the_buffer = bufferCO->currentIndex();
535 if (the_buffer != -1) {
536 FileNameList const names(theBufferList().fileNames());
537 FileName const & name = names[the_buffer];
538 Buffer const * buf = theBufferList().getBuffer(name);
539 buf->getLabelList(refs_);
541 sortingCO->setEnabled(!refs_.empty());
542 refsTW->setEnabled(!refs_.empty());
543 groupCB->setEnabled(!refs_.empty());
548 bool GuiRef::isValid()
550 return !referenceED->text().isEmpty();
554 void GuiRef::gotoRef(string const & ref)
556 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
557 dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
561 void GuiRef::gotoBookmark()
563 dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
567 void GuiRef::filterLabels()
569 Qt::CaseSensitivity cs = csFindCB->isChecked() ?
570 Qt::CaseSensitive : Qt::CaseInsensitive;
571 QTreeWidgetItemIterator it(refsTW);
574 (*it)->childCount() == 0
575 && !(*it)->text(0).contains(filter_->text(), cs)
582 void GuiRef::resetFilter()
584 filter_->setText(QString());
589 bool GuiRef::initialiseParams(std::string const & data)
591 InsetCommand::string2params(data, params_);
596 void GuiRef::dispatchParams()
598 std::string const lfun = InsetCommand::params2string(params_);
599 dispatch(FuncRequest(getLfun(), lfun));
604 Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
607 } // namespace frontend
610 #include "moc_GuiRef.cpp"