]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiRef.cpp
Display equation/theorem numbers in insert cross reference dialog.
[lyx.git] / src / frontends / qt / GuiRef.cpp
index c1870bed98770e01980dc8003fcaf4339541bc6a..5fb317d64117f3301df5f91f3ead3fe82186ba97 100644 (file)
@@ -20,7 +20,9 @@
 #include "BufferList.h"
 #include "BufferView.h"
 #include "Cursor.h"
+#include "FancyLineEdit.h"
 #include "FuncRequest.h"
+#include "PDFOptions.h"
 
 #include "qt_helpers.h"
 
@@ -55,19 +57,11 @@ GuiRef::GuiRef(GuiView & lv)
 
        // 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_->setClearButton(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
+               refsTW, [this](){ focusAndHighlight(refsTW); });
 
        filterBarL->addWidget(filter_, 0);
        findKeysLA->setBuddy(filter_);
@@ -79,9 +73,6 @@ GuiRef::GuiRef(GuiView & lv)
        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(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
 
        connect(typeCO, SIGNAL(activated(int)),
@@ -96,8 +87,6 @@ GuiRef::GuiRef(GuiView & lv)
                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)),
                this, SLOT(refHighlighted(QTreeWidgetItem *)));
        connect(refsTW, SIGNAL(itemSelectionChanged()),
@@ -118,6 +107,8 @@ GuiRef::GuiRef(GuiView & lv)
                this, SLOT(changed_adaptor()));
        connect(noprefixCB, SIGNAL(clicked()),
                this, SLOT(changed_adaptor()));
+       connect(nolinkCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
 
        enableBoxes();
 
@@ -146,13 +137,17 @@ void GuiRef::enableView(bool enable)
 void GuiRef::enableBoxes()
 {
        QString const reftype =
-               typeCO->itemData(typeCO->currentIndex()).toString();
+           typeCO->itemData(typeCO->currentIndex()).toString();
        bool const isFormatted = (reftype == "formatted");
        bool const isLabelOnly = (reftype == "labelonly");
        bool const usingRefStyle = buffer().params().use_refstyle;
-       pluralCB->setEnabled(isFormatted && usingRefStyle);
-       capsCB->setEnabled(isFormatted && usingRefStyle);
-       noprefixCB->setEnabled(isLabelOnly);
+       bool const intext = bufferview()->cursor().inTexted();
+       bool const hyper_on = buffer().params().pdfoptions().use_hyperref;
+       pluralCB->setEnabled(intext && isFormatted && usingRefStyle);
+       capsCB->setEnabled(intext && isFormatted && usingRefStyle);
+       noprefixCB->setEnabled(intext && isLabelOnly);
+       // disabling of hyperlinks not supported by formatted references
+       nolinkCB->setEnabled(hyper_on && intext && !isFormatted && !isLabelOnly);
 }
 
 
@@ -189,7 +184,6 @@ void GuiRef::selectionChanged()
                return;
        QTreeWidgetItem * sel = selections.first();
        refHighlighted(sel);
-       return;
 }
 
 
@@ -206,15 +200,13 @@ void GuiRef::refHighlighted(QTreeWidgetItem * sel)
        bool const cur_item_selected = sel->isSelected();
 
        if (cur_item_selected)
-               referenceED->setText(sel->text(0));
+               referenceED->setText(sel->data(0, Qt::UserRole).toString());
 
        if (at_ref_)
                gotoRef();
        gotoPB->setEnabled(true);
-       if (typeAllowed() && !isBufferReadonly())
+       if (!isBufferReadonly())
                typeCO->setEnabled(true);
-       nameED->setHidden(!nameAllowed());
-       nameL->setHidden(!nameAllowed());
 }
 
 
@@ -242,7 +234,7 @@ void GuiRef::refSelected(QTreeWidgetItem * sel)
        bool const cur_item_selected = sel->isSelected();
 
        if (cur_item_selected)
-               referenceED->setText(sel->text(0));
+               referenceED->setText(sel->data(0, Qt::UserRole).toString());
        // <enter> or double click, inserts ref and closes dialog
        slotOK();
 }
@@ -318,27 +310,20 @@ void GuiRef::updateContents()
                typeCO->itemData(typeCO->currentIndex()).toString();
 
        referenceED->clear();
-       nameED->clear();
        typeCO->clear();
 
        // FIXME Bring InsetMathRef on par with InsetRef
-       // (see #9798)
+       // (see #11104)
        typeCO->addItem(qt_("<reference>"), "ref");
        typeCO->addItem(qt_("(<reference>)"), "eqref");
        typeCO->addItem(qt_("<page>"), "pageref");
        typeCO->addItem(qt_("on page <page>"), "vpageref");
        typeCO->addItem(qt_("<reference> on page <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");
+       typeCO->addItem(qt_("Formatted reference"), "formatted");
+       typeCO->addItem(qt_("Label only"), "labelonly");
 
        referenceED->setText(toqstr(params_["reference"]));
-       nameED->setText(toqstr(params_["name"]));
-       nameED->setHidden(!nameAllowed());
-       nameL->setHidden(!nameAllowed());
 
        // restore type settings for new insets
        bool const new_inset = params_["reference"].empty();
@@ -351,13 +336,12 @@ void GuiRef::updateContents()
        else
                typeCO->setCurrentIndex(
                        typeCO->findData(toqstr(params_.getCmdName())));
-       typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
-       if (!typeAllowed())
-               typeCO->setCurrentIndex(0);
+       typeCO->setEnabled(!isBufferReadonly());
 
        pluralCB->setChecked(params_["plural"] == "true");
        capsCB->setChecked(params_["caps"] == "true");
        noprefixCB->setChecked(params_["noprefix"] == "true");
+       nolinkCB->setChecked(params_["nolink"] == "true");
 
        // insert buffer list
        bufferCO->clear();
@@ -394,30 +378,18 @@ void GuiRef::applyView()
 
        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");
+       params_["nolink"] = nolinkCB->isChecked() ?
+             from_ascii("true") : from_ascii("false");
        restored_buffer_ = bufferCO->currentIndex();
 }
 
 
-bool GuiRef::nameAllowed()
-{
-       KernelDocType const doc_type = docType();
-       return doc_type != LATEX && doc_type != LITERATE;
-}
-
-
-bool GuiRef::typeAllowed()
-{
-       return docType() != DOCBOOK;
-}
-
-
 void GuiRef::setGoBack()
 {
        gotoPB->setText(qt_("&Go Back"));
@@ -448,6 +420,11 @@ void GuiRef::gotoRef()
        at_ref_ = !at_ref_;
 }
 
+inline bool caseInsensitiveLessThanVec(std::tuple<QString, QString, QString> const & s1, std::tuple<QString, QString, QString> const & s2)
+{
+       return std::get<0>(s1).toLower() < std::get<0>(s2).toLower();
+}
+
 inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
 {
        return s1.toLower() < s2.toLower();
@@ -468,13 +445,19 @@ void GuiRef::redoRefs()
        // the first item inserted
        QString const oldSelection(referenceED->text());
 
-       QStringList refsStrings;
+       // Plain label, GUI string, and dereferenced string.
+       // This might get resorted below
+       QVector<std::tuple<QString, QString,QString>> refsNames;
+       // List of categories (prefixes)
        QStringList refsCategories;
-       vector<docstring>::const_iterator iter;
+       // Do we have a prefix-less label at all?
        bool noprefix = false;
+       vector<std::tuple<docstring, docstring,docstring>>::const_iterator iter;
        for (iter = refs_.begin(); iter != refs_.end(); ++iter) {
-               QString const lab = toqstr(*iter);
-               refsStrings.append(lab);
+               // first: plain label name, second: gui name, third: pretty name
+               QString const lab = toqstr(std::get<0>(*iter));
+               refsNames.append({lab, toqstr(std::get<1>(*iter)),
+                                   toqstr(std::get<2>(*iter))});
                if (groupCB->isChecked()) {
                        if (lab.contains(":")) {
                                QString const pref = lab.split(':')[0];
@@ -489,7 +472,7 @@ void GuiRef::redoRefs()
                                noprefix = true;
                }
        }
-       // sort categories case-intensively
+       // sort categories case-insensitively
        sort(refsCategories.begin(), refsCategories.end(),
                  caseInsensitiveLessThan /*defined above*/);
        if (noprefix)
@@ -498,26 +481,31 @@ void GuiRef::redoRefs()
        QString const sort_method = sortingCO->isEnabled() ?
                                        sortingCO->itemData(sortingCO->currentIndex()).toString()
                                        : QString();
+       // Sort items if so requested.
        if (sort_method == "nocase")
-               sort(refsStrings.begin(), refsStrings.end(),
-                         caseInsensitiveLessThan /*defined above*/);
+               sort(refsNames.begin(), refsNames.end(),
+                         caseInsensitiveLessThanVec /*defined above*/);
        else if (sort_method == "case")
-               sort(refsStrings.begin(), refsStrings.end());
+               sort(refsNames.begin(), refsNames.end());
 
        if (groupCB->isChecked()) {
                QList<QTreeWidgetItem *> refsCats;
                for (int i = 0; i < refsCategories.size(); ++i) {
-                       QString const cat = refsCategories.at(i);
+                       QString const cat = refsCategories.at(i);
                        QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
                        item->setText(0, cat);
-                       for (int j = 0; j < refsStrings.size(); ++j) {
-                               QString const ref = refsStrings.at(j);
+                       for (int j = 0; j < refsNames.size(); ++j) {
+                               QString const ref = std::get<0>(refsNames.at(j));
                                if ((ref.startsWith(cat + QString(":")))
                                    || (cat == qt_("<No prefix>")
                                       && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) {
                                                QTreeWidgetItem * child =
                                                        new QTreeWidgetItem(item);
-                                               child->setText(0, ref);
+                                               QString const val = std::get<1>(refsNames.at(j));
+                                               QString const pretty = std::get<2>(refsNames.at(j));
+                                               child->setText(0, val);
+                                               child->setData(0, Qt::UserRole, ref);
+                                               child->setText(1, pretty);
                                                item->addChild(child);
                                }
                        }
@@ -526,9 +514,14 @@ void GuiRef::redoRefs()
                refsTW->addTopLevelItems(refsCats);
        } else {
                QList<QTreeWidgetItem *> refsItems;
-               for (int i = 0; i < refsStrings.size(); ++i) {
+               for (int i = 0; i < refsNames.size(); ++i) {
                        QTreeWidgetItem * item = new QTreeWidgetItem(refsTW);
-                       item->setText(0, refsStrings.at(i));
+                       QString const ref = std::get<0>(refsNames.at(i));
+                       QString const val = std::get<1>(refsNames.at(i));
+                       QString const pretty = std::get<2>(refsNames.at(i));
+                       item->setText(0, val);
+                       item->setData(0, Qt::UserRole, ref);
+                       item->setText(1, pretty);
                        refsItems.append(item);
                }
                refsTW->addTopLevelItems(refsItems);
@@ -615,6 +608,7 @@ void GuiRef::filterLabels()
                (*it)->setHidden(
                        (*it)->childCount() == 0
                        && !(*it)->text(0).contains(filter_->text(), cs)
+                       && !(*it)->text(1).contains(filter_->text(), cs)
                );
                ++it;
        }
@@ -639,13 +633,10 @@ void GuiRef::dispatchParams()
 {
        std::string const lfun = InsetCommand::params2string(params_);
        dispatch(FuncRequest(getLfun(), lfun));
+       connectToNewInset();
 }
 
 
-
-Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
-
-
 } // namespace frontend
 } // namespace lyx