X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Ffrontends%2Fqt4%2FGuiRef.cpp;h=5d46a471c710bb980c0c062085c8f3a480d405b5;hb=b91e2b99986c0f957ba27eeba74b5ae0460d73bc;hp=93f8193fc2d8546e9607e1a7aad98ec33c26bb2c;hpb=e0d2a2adff18aa869dc5ded6129ddee5a872e27e;p=lyx.git diff --git a/src/frontends/qt4/GuiRef.cpp b/src/frontends/qt4/GuiRef.cpp index 93f8193fc2..5d46a471c7 100644 --- a/src/frontends/qt4/GuiRef.cpp +++ b/src/frontends/qt4/GuiRef.cpp @@ -13,8 +13,13 @@ #include "GuiRef.h" +#include "GuiApplication.h" + #include "Buffer.h" +#include "BufferParams.h" #include "BufferList.h" +#include "BufferView.h" +#include "Cursor.h" #include "FuncRequest.h" #include "qt_helpers.h" @@ -32,6 +37,7 @@ #include #include #include +#include using namespace std; using namespace lyx::support; @@ -47,31 +53,49 @@ GuiRef::GuiRef(GuiView & lv) at_ref_ = false; - // Enabling is set in updateRefs. Disable for now in case no - // call to updateContents follows (e.g. read-only documents). - sortCB->setEnabled(false); - caseSensitiveCB->setEnabled(false); - caseSensitiveCB->setChecked(false); - refsTW->setEnabled(false); - gotoPB->setEnabled(false); + // The filter bar + filter_ = new FancyLineEdit(this); + filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png")); + filter_->setButtonVisible(FancyLineEdit::Right, true); + filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text")); + filter_->setAutoHideButton(FancyLineEdit::Right, true); + filter_->setPlaceholderText(qt_("All available labels")); + filter_->setToolTip(qt_("Enter string to filter the list of available labels")); +#if (QT_VERSION < 0x050000) + connect(filter_, SIGNAL(downPressed()), + refsTW, SLOT(setFocus())); +#else + connect(filter_, &FancyLineEdit::downPressed, + refsTW, [=](){ focusAndHighlight(refsTW); }); +#endif + + filterBarL->addWidget(filter_, 0); + findKeysLA->setBuddy(filter_); + + sortingCO->addItem(qt_("By Occurrence"), "unsorted"); + sortingCO->addItem(qt_("Alphabetically (Case-Insensitive)"), "nocase"); + sortingCO->addItem(qt_("Alphabetically (Case-Sensitive)"), "case"); + + buttonBox->button(QDialogButtonBox::Reset)->setText(qt_("&Update")); + buttonBox->button(QDialogButtonBox::Reset)->setToolTip(qt_("Update the label list")); refsTW->setColumnCount(1); refsTW->header()->setVisible(false); - connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK())); - connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply())); - connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose())); - connect(closePB, SIGNAL(clicked()), this, SLOT(resetDialog())); connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected())); connect(typeCO, SIGNAL(activated(int)), this, SLOT(changed_adaptor())); + connect(referenceED, SIGNAL(textChanged(QString)), + this, SLOT(refTextChanged(QString))); connect(referenceED, SIGNAL(textChanged(QString)), this, SLOT(changed_adaptor())); - connect(findLE, SIGNAL(returnPressed()), - this, SLOT(on_searchPB_clicked())); - connect(csFindCB, SIGNAL(clicked()), - this, SLOT(on_searchPB_clicked())); + connect(filter_, SIGNAL(textEdited(QString)), + this, SLOT(filterLabels())); + connect(filter_, SIGNAL(rightButtonClicked()), + this, SLOT(resetFilter())); + connect(csFindCB, SIGNAL(clicked()), + this, SLOT(filterLabels())); connect(nameED, SIGNAL(textChanged(QString)), this, SLOT(changed_adaptor())); connect(refsTW, SIGNAL(itemClicked(QTreeWidgetItem *, int)), @@ -80,45 +104,78 @@ GuiRef::GuiRef(GuiView & lv) this, SLOT(selectionChanged())); connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(refSelected(QTreeWidgetItem *))); - connect(sortCB, SIGNAL(clicked()), + connect(sortingCO, SIGNAL(activated(int)), this, SLOT(sortToggled())); - connect(caseSensitiveCB, SIGNAL(clicked()), - this, SLOT(caseSensitiveToggled())); connect(groupCB, SIGNAL(clicked()), this, SLOT(groupToggled())); connect(gotoPB, SIGNAL(clicked()), this, SLOT(gotoClicked())); - connect(updatePB, SIGNAL(clicked()), - this, SLOT(updateClicked())); connect(bufferCO, SIGNAL(activated(int)), this, SLOT(updateClicked())); + connect(pluralCB, SIGNAL(clicked()), + this, SLOT(changed_adaptor())); + connect(capsCB, SIGNAL(clicked()), + this, SLOT(changed_adaptor())); + connect(noprefixCB, SIGNAL(clicked()), + this, SLOT(changed_adaptor())); + + enableBoxes(); bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy); - bc().setOK(okPB); - bc().setApply(applyPB); - bc().setCancel(closePB); - bc().addReadOnly(refsTW); - bc().addReadOnly(sortCB); - bc().addReadOnly(caseSensitiveCB); - bc().addReadOnly(nameED); - bc().addReadOnly(referenceED); + bc().setOK(buttonBox->button(QDialogButtonBox::Ok)); + bc().setApply(buttonBox->button(QDialogButtonBox::Apply)); + bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel)); bc().addReadOnly(typeCO); - bc().addReadOnly(bufferCO); restored_buffer_ = -1; active_buffer_ = -1; + + setFocusProxy(filter_); +} + + +void GuiRef::enableView(bool enable) +{ + if (!enable) + // In the opposite case, updateContents() will be called anyway. + updateContents(); + GuiDialog::enableView(enable); +} + + +void GuiRef::enableBoxes() +{ + bool const isFormatted = + (InsetRef::getName(typeCO->currentIndex()) == "formatted"); + bool const isLabelOnly = + (InsetRef::getName(typeCO->currentIndex()) == "labelonly"); + bool const usingRefStyle = buffer().params().use_refstyle; + pluralCB->setEnabled(isFormatted && usingRefStyle); + capsCB->setEnabled(isFormatted && usingRefStyle); + noprefixCB->setEnabled(isLabelOnly); } void GuiRef::changed_adaptor() { changed(); + enableBoxes(); } void GuiRef::gotoClicked() { + // By setting last_reference_, we ensure that the reference + // to which we are going (or from which we are returning) is + // restored in the dialog. It's a bit of a hack, but it works, + // and no-one seems to have any better idea. + bool const toggled = + last_reference_.isEmpty() || last_reference_.isNull(); + if (toggled) + last_reference_ = referenceED->text(); gotoRef(); + if (toggled) + last_reference_.clear(); } @@ -138,9 +195,6 @@ void GuiRef::selectionChanged() void GuiRef::refHighlighted(QTreeWidgetItem * sel) { - if (isBufferReadonly()) - return; - if (sel->childCount() > 0) { sel->setExpanded(true); return; @@ -157,18 +211,31 @@ void GuiRef::refHighlighted(QTreeWidgetItem * sel) if (at_ref_) gotoRef(); gotoPB->setEnabled(true); - if (typeAllowed()) + if (typeAllowed() && !isBufferReadonly()) typeCO->setEnabled(true); nameED->setHidden(!nameAllowed()); nameL->setHidden(!nameAllowed()); } +void GuiRef::refTextChanged(QString const & str) +{ + gotoPB->setEnabled(!str.isEmpty()); + typeCO->setEnabled(!str.isEmpty()); + typeLA->setEnabled(!str.isEmpty()); +} + + void GuiRef::refSelected(QTreeWidgetItem * sel) { if (isBufferReadonly()) return; + if (sel->childCount()) { + sel->setExpanded(false); + return; + } + /* int const cur_item = refsTW->currentRow(); bool const cur_item_selected = cur_item >= 0 ? refsLB->isSelected(cur_item) : false;*/ @@ -183,26 +250,40 @@ void GuiRef::refSelected(QTreeWidgetItem * sel) void GuiRef::sortToggled() { - caseSensitiveCB->setEnabled(sortCB->isChecked()); redoRefs(); } -void GuiRef::caseSensitiveToggled() +void GuiRef::groupToggled() { redoRefs(); } -void GuiRef::groupToggled() +void GuiRef::on_buttonBox_clicked(QAbstractButton * button) { - redoRefs(); + switch (buttonBox->standardButton(button)) { + case QDialogButtonBox::Ok: + slotOK(); + break; + case QDialogButtonBox::Apply: + slotApply(); + break; + case QDialogButtonBox::Cancel: + slotClose(); + resetDialog(); + break; + case QDialogButtonBox::Reset: + updateClicked(); + break; + default: + break; + } } void GuiRef::updateClicked() { - findLE->clear(); updateRefs(); } @@ -233,34 +314,62 @@ void GuiRef::closeEvent(QCloseEvent * e) void GuiRef::updateContents() { - int orig_type = typeCO->currentIndex(); + QString const orig_type = + typeCO->itemData(typeCO->currentIndex()).toString(); + + referenceED->clear(); + nameED->clear(); + typeCO->clear(); + + // FIXME Bring InsetMathRef on par with InsetRef + // (see #9798) + typeCO->addItem(qt_(""), "ref"); + typeCO->addItem(qt_("()"), "eqref"); + typeCO->addItem(qt_(""), "pageref"); + typeCO->addItem(qt_("on page "), "vpageref"); + typeCO->addItem(qt_(" on page "), "vref"); + typeCO->addItem(qt_("Textual reference"), "nameref"); + if (bufferview()->cursor().inTexted()) { + typeCO->addItem(qt_("Formatted reference"), "formatted"); + typeCO->addItem(qt_("Label only"), "labelonly"); + } else + typeCO->addItem(qt_("Formatted reference"), "prettyref"); referenceED->setText(toqstr(params_["reference"])); - nameED->setText(toqstr(params_["name"])); - nameED->setHidden(!nameAllowed() && !isBufferReadonly()); - nameL->setHidden(!nameAllowed() && !isBufferReadonly()); + nameED->setHidden(!nameAllowed()); + nameL->setHidden(!nameAllowed()); // restore type settings for new insets - if (params_["reference"].empty()) - typeCO->setCurrentIndex(orig_type); + bool const new_inset = params_["reference"].empty(); + if (new_inset) { + int index = typeCO->findData(orig_type); + if (index == -1) + index = 0; + typeCO->setCurrentIndex(index); + } else - typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName())); + typeCO->setCurrentIndex( + typeCO->findData(toqstr(params_.getCmdName()))); typeCO->setEnabled(typeAllowed() && !isBufferReadonly()); if (!typeAllowed()) typeCO->setCurrentIndex(0); + pluralCB->setChecked(params_["plural"] == "true"); + capsCB->setChecked(params_["caps"] == "true"); + noprefixCB->setChecked(params_["noprefix"] == "true"); + // insert buffer list bufferCO->clear(); - FileNameList const & buffers = theBufferList().fileNames(); + FileNameList const buffers(theBufferList().fileNames()); for (FileNameList::const_iterator it = buffers.begin(); it != buffers.end(); ++it) { - bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename()))); + bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName()))); } int const thebuffer = theBufferList().bufferNum(buffer().fileName()); // restore the buffer combo setting for new insets - if (params_["reference"].empty() && restored_buffer_ != -1 + if (new_inset && restored_buffer_ != -1 && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_) bufferCO->setCurrentIndex(restored_buffer_); else { @@ -272,7 +381,10 @@ void GuiRef::updateContents() active_buffer_ = thebuffer; updateRefs(); - bc().setValid(false); + enableBoxes(); + // Activate OK/Apply buttons if the users inserts a new ref + // and we have a valid pre-setting. + bc().setValid(isValid() && new_inset); } @@ -280,10 +392,15 @@ void GuiRef::applyView() { last_reference_ = referenceED->text(); - params_.setCmdName(InsetRef::getName(typeCO->currentIndex())); + params_.setCmdName(fromqstr(typeCO->itemData(typeCO->currentIndex()).toString())); params_["reference"] = qstring_to_ucs4(last_reference_); params_["name"] = qstring_to_ucs4(nameED->text()); - + params_["plural"] = pluralCB->isChecked() ? + from_ascii("true") : from_ascii("false"); + params_["caps"] = capsCB->isChecked() ? + from_ascii("true") : from_ascii("false"); + params_["noprefix"] = noprefixCB->isChecked() ? + from_ascii("true") : from_ascii("false"); restored_buffer_ = bufferCO->currentIndex(); } @@ -304,16 +421,14 @@ bool GuiRef::typeAllowed() void GuiRef::setGoBack() { gotoPB->setText(qt_("&Go Back")); - gotoPB->setToolTip(""); - gotoPB->setToolTip(qt_("Jump back")); + gotoPB->setToolTip(qt_("Jump back to the original cursor location")); } void GuiRef::setGotoRef() { gotoPB->setText(qt_("&Go to Label")); - gotoPB->setToolTip(""); - gotoPB->setToolTip(qt_("Jump to label")); + gotoPB->setToolTip(qt_("Jump to the selected label")); } @@ -355,41 +470,55 @@ void GuiRef::redoRefs() QStringList refsStrings; QStringList refsCategories; - refsCategories.append(qt_("No prefix")); vector::const_iterator iter; + bool noprefix = false; for (iter = refs_.begin(); iter != refs_.end(); ++iter) { QString const lab = toqstr(*iter); refsStrings.append(lab); - if (groupCB->isChecked() && lab.contains(":")) { - QString const pref = lab.split(':')[0]; - if (!pref.isEmpty() && !refsCategories.contains(pref)) - refsCategories.append(pref); + if (groupCB->isChecked()) { + if (lab.contains(":")) { + QString const pref = lab.split(':')[0]; + if (!refsCategories.contains(pref)) { + if (!pref.isEmpty()) + refsCategories.append(pref); + else + noprefix = true; + } + } + else + noprefix = true; } } + // sort categories case-intensively + qSort(refsCategories.begin(), refsCategories.end(), + caseInsensitiveLessThan /*defined above*/); + if (noprefix) + refsCategories.insert(0, qt_("")); + + QString const sort = sortingCO->isEnabled() ? + sortingCO->itemData(sortingCO->currentIndex()).toString() + : QString(); + if (sort == "nocase") + qSort(refsStrings.begin(), refsStrings.end(), + caseInsensitiveLessThan /*defined above*/); + else if (sort == "case") + qSort(refsStrings.begin(), refsStrings.end()); - if (sortCB->isEnabled() && sortCB->isChecked()) { - if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked()) - qSort(refsStrings.begin(), refsStrings.end()); - else - qSort(refsStrings.begin(), refsStrings.end(), - caseInsensitiveLessThan /*defined above*/); - } - if (groupCB->isChecked()) { QList refsCats; for (int i = 0; i < refsCategories.size(); ++i) { QString const cat = refsCategories.at(i); QTreeWidgetItem * item = new QTreeWidgetItem(refsTW); item->setText(0, cat); - for (int i = 0; i < refsStrings.size(); ++i) { - QString const ref = refsStrings.at(i); + for (int j = 0; j < refsStrings.size(); ++j) { + QString const ref = refsStrings.at(j); if ((ref.startsWith(cat + QString(":"))) - || (cat == qt_("No prefix") - && !ref.contains(":"))) { - QTreeWidgetItem * child = - new QTreeWidgetItem(item); - child->setText(0, ref); - item->addChild(child); + || (cat == qt_("") + && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) { + QTreeWidgetItem * child = + new QTreeWidgetItem(item); + child->setText(0, ref); + item->addChild(child); } } refsCats.append(item); @@ -405,19 +534,18 @@ void GuiRef::redoRefs() refsTW->addTopLevelItems(refsItems); } - referenceED->setText(oldSelection); - // restore the last selection or, for new insets, highlight // the previous selection if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) { bool const newInset = oldSelection.isEmpty(); QString textToFind = newInset ? last_reference_ : oldSelection; + referenceED->setText(textToFind); last_reference_.clear(); QTreeWidgetItemIterator it(refsTW); while (*it) { if ((*it)->text(0) == textToFind) { - refsTW->setCurrentItem(*it); - refsTW->setItemSelected(*it, !newInset); + refsTW->setCurrentItem(*it); + refsTW->setItemSelected(*it, true); //Make sure selected item is visible refsTW->scrollToItem(*it); last_reference_ = textToFind; @@ -429,9 +557,16 @@ void GuiRef::redoRefs() refsTW->setUpdatesEnabled(true); refsTW->update(); + // redo filter + filterLabels(); + // Re-activate the emission of signals by these widgets. refsTW->blockSignals(false); referenceED->blockSignals(false); + + gotoPB->setEnabled(!referenceED->text().isEmpty()); + typeCO->setEnabled(!referenceED->text().isEmpty()); + typeLA->setEnabled(!referenceED->text().isEmpty()); } @@ -440,16 +575,14 @@ void GuiRef::updateRefs() refs_.clear(); int const the_buffer = bufferCO->currentIndex(); if (the_buffer != -1) { - FileName const & name = theBufferList().fileNames()[the_buffer]; + FileNameList const names(theBufferList().fileNames()); + FileName const & name = names[the_buffer]; Buffer const * buf = theBufferList().getBuffer(name); buf->getLabelList(refs_); - } - sortCB->setEnabled(!refs_.empty()); - caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked()); + } + sortingCO->setEnabled(!refs_.empty()); refsTW->setEnabled(!refs_.empty()); - // refsTW should only be the focus proxy when it is enabled - setFocusProxy(refs_.empty() ? 0 : refsTW); - gotoPB->setEnabled(!refs_.empty()); + groupCB->setEnabled(!refs_.empty()); redoRefs(); } @@ -473,37 +606,38 @@ void GuiRef::gotoBookmark() } -void GuiRef::on_findLE_textChanged(const QString & text) -{ - searchPB->setDisabled(text.isEmpty()); -} - - -void GuiRef::on_searchPB_clicked() +void GuiRef::filterLabels() { - QTreeWidgetItemIterator it(refsTW); Qt::CaseSensitivity cs = csFindCB->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; + QTreeWidgetItemIterator it(refsTW); while (*it) { (*it)->setHidden( (*it)->childCount() == 0 - && !(*it)->text(0).contains(findLE->text(), cs) + && !(*it)->text(0).contains(filter_->text(), cs) ); ++it; } } -bool GuiRef::initialiseParams(std::string const & data) +void GuiRef::resetFilter() +{ + filter_->setText(QString()); + filterLabels(); +} + + +bool GuiRef::initialiseParams(std::string const & sdata) { - InsetCommand::string2params("ref", data, params_); + InsetCommand::string2params(sdata, params_); return true; } void GuiRef::dispatchParams() { - std::string const lfun = InsetCommand::params2string("ref", params_); + std::string const lfun = InsetCommand::params2string(params_); dispatch(FuncRequest(getLfun(), lfun)); }