From c609e9cbcf5f013e9bcc8f13a9a3de1f45d8d212 Mon Sep 17 00:00:00 2001 From: Alexander Dunlap Date: Wed, 26 Jul 2023 13:35:42 -0400 Subject: [PATCH] Display equation/theorem numbers in insert cross reference dialog. Fixes bug #11466, --- src/Buffer.cpp | 7 ++++--- src/Buffer.h | 5 +++-- src/Counters.cpp | 24 +++++++++++++++--------- src/Counters.h | 4 +++- src/TextClass.cpp | 4 +++- src/TocBackend.h | 6 ++++++ src/frontends/qt/GuiRef.cpp | 36 ++++++++++++++++++++---------------- src/frontends/qt/GuiRef.h | 5 +++-- src/frontends/qt/ui/RefUi.ui | 16 +++++++++++++++- src/insets/InsetFlex.cpp | 1 + src/insets/InsetLabel.cpp | 27 +++++++++++++++++---------- src/insets/InsetLabel.h | 2 ++ src/mathed/InsetMathHull.cpp | 4 +++- 13 files changed, 95 insertions(+), 46 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index e837a9b552..1f90df3ed1 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -2422,7 +2422,7 @@ void Buffer::validate(LaTeXFeatures & features) const } -void Buffer::getLabelList(vector> & list) const +void Buffer::getLabelList(vector> & list) const { // If this is a child document, use the master's list instead. if (parent()) { @@ -2433,8 +2433,9 @@ void Buffer::getLabelList(vector> & list) const list.clear(); shared_ptr toc = d->toc_backend.toc("label"); for (auto const & tocit : *toc) { - if (tocit.depth() == 0) - list.push_back(make_pair(tocit.str(), tocit.asString())); + if (tocit.depth() == 0) { + list.push_back(make_tuple(tocit.str(), tocit.asString(),tocit.prettyStr())); + } } } diff --git a/src/Buffer.h b/src/Buffer.h index 4905efb45d..de27084af9 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -534,8 +534,9 @@ public: void invalidateCiteLabels() const; /// bool citeLabelsValid() const; - /// two strings: plain label name and label as gui string - void getLabelList(std::vector> &) const; + /// three strings: plain label name, label as gui string, and + /// dereferenced label name + void getLabelList(std::vector> &) const; /// This removes the .aux and .bbl files from the temp dir. void removeBiblioTempFiles() const; diff --git a/src/Counters.cpp b/src/Counters.cpp index d7f8630adf..8f2c60e461 100644 --- a/src/Counters.cpp +++ b/src/Counters.cpp @@ -42,9 +42,10 @@ Counter::Counter() Counter::Counter(docstring const & mc, docstring const & ls, - docstring const & lsa, docstring const & guiname) + docstring const & lsa, docstring const & prettyformat, + docstring const & guiname) : initial_value_(0), saved_value_(0), parent_(mc), labelstring_(ls), - labelstringappendix_(lsa), guiname_(guiname) + labelstringappendix_(lsa), prettyformat_(prettyformat), guiname_(guiname) { reset(); } @@ -130,6 +131,12 @@ bool Counter::read(Lexer & lex) getout = true; break; } + if (prettyformat_ == "") { // fall back on GuiName if PrettyFormat is empty + if (guiname_ == "") + prettyformat_ = from_ascii("##"); + else + prettyformat_ = "## (" + guiname_ + " counter)"; + } } // Here if have a full counter if getout == true @@ -220,6 +227,7 @@ void Counters::newCounter(docstring const & newc, docstring const & parentc, docstring const & ls, docstring const & lsa, + docstring const & prettyformat, docstring const & guiname) { if (!parentc.empty() && !hasCounter(parentc)) { @@ -228,7 +236,7 @@ void Counters::newCounter(docstring const & newc, << endl; return; } - counterList_[newc] = Counter(parentc, ls, lsa, guiname); + counterList_[newc] = Counter(parentc, ls, lsa, prettyformat, guiname); } @@ -344,7 +352,7 @@ void Counters::stepParent(docstring const & ctr, UpdateType utype) } -void Counters::step(docstring const & ctr, UpdateType utype) +void Counters::step(docstring const & ctr, UpdateType /* deleted */) { CounterList::iterator it = counterList_.find(ctr); if (it == counterList_.end()) { @@ -354,11 +362,9 @@ void Counters::step(docstring const & ctr, UpdateType utype) } it->second.step(); - if (utype == OutputUpdate) { - LBUFERR(!counter_stack_.empty()); - counter_stack_.pop_back(); - counter_stack_.push_back(ctr); - } + LBUFERR(!counter_stack_.empty()); + counter_stack_.pop_back(); + counter_stack_.push_back(ctr); resetChildren(ctr); } diff --git a/src/Counters.h b/src/Counters.h index 57fff35fae..41ee930583 100644 --- a/src/Counters.h +++ b/src/Counters.h @@ -35,7 +35,8 @@ public: Counter(); /// Counter(docstring const & mc, docstring const & ls, - docstring const & lsa, docstring const & guiname); + docstring const & lsa, docstring const & prettyformat, + docstring const & guiname); /// \return true on success bool read(Lexer & lex); /// @@ -129,6 +130,7 @@ public: docstring const & parentc, docstring const & ls, docstring const & lsa, + docstring const & prettyformat, docstring const & guiname); /// Checks whether the given counter exists. bool hasCounter(docstring const & c) const; diff --git a/src/TextClass.cpp b/src/TextClass.cpp index d99004f7aa..b6a7887ed8 100644 --- a/src/TextClass.cpp +++ b/src/TextClass.cpp @@ -1624,12 +1624,14 @@ bool TextClass::readFloat(Lexer & lexrc) // each float has its own counter counters_.newCounter(from_ascii(type), from_ascii(within), docstring(), docstring(), + bformat(_("%1$s ##"), _(name)), bformat(_("%1$s (Float)"), _(name))); // also define sub-float counters docstring const subtype = "sub-" + from_ascii(type); counters_.newCounter(subtype, from_ascii(type), "\\alph{" + subtype + "}", docstring(), - bformat(_("Sub-%1$s (Float)"), _(name))); + bformat(_("Sub-%1$s ##"), _(name)), + bformat(_("Sub-%1$s (Float)"), _(name))); } return getout; } diff --git a/src/TocBackend.h b/src/TocBackend.h index c3a178d12f..7ad36fec66 100644 --- a/src/TocBackend.h +++ b/src/TocBackend.h @@ -73,6 +73,10 @@ public: /// void str(docstring const & s) { str_ = s; } /// + docstring const & prettyStr() const { return pretty_str_; } + /// + void prettyStr (docstring const & s) { pretty_str_ = s; } + /// bool isOutput() const { return output_; } /// bool isMissing() const { return missing_; } @@ -93,6 +97,8 @@ private: int depth_; /// Full item string docstring str_; + /// Dereferenced name, for labels (e.g. Label 5.2 instead of lem:foobar) + docstring pretty_str_; /// Is this item in a note, inactive branch, etc? bool output_; /// Is this item missing, e.g. missing label? diff --git a/src/frontends/qt/GuiRef.cpp b/src/frontends/qt/GuiRef.cpp index 9b9144377a..5fb317d641 100644 --- a/src/frontends/qt/GuiRef.cpp +++ b/src/frontends/qt/GuiRef.cpp @@ -73,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)), @@ -423,9 +420,9 @@ void GuiRef::gotoRef() at_ref_ = !at_ref_; } -inline bool caseInsensitiveLessThanVec(QPair const & s1, QPair const & s2) +inline bool caseInsensitiveLessThanVec(std::tuple const & s1, std::tuple const & s2) { - return s1.first.toLower() < s2.first.toLower(); + return std::get<0>(s1).toLower() < std::get<0>(s2).toLower(); } inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2) @@ -448,17 +445,19 @@ void GuiRef::redoRefs() // the first item inserted QString const oldSelection(referenceED->text()); - // Plain label and GUI string. This might get resorted below - QVector> refsNames; + // Plain label, GUI string, and dereferenced string. + // This might get resorted below + QVector> refsNames; // List of categories (prefixes) QStringList refsCategories; // Do we have a prefix-less label at all? bool noprefix = false; - vector>::const_iterator iter; + vector>::const_iterator iter; for (iter = refs_.begin(); iter != refs_.end(); ++iter) { - // first: plain label name, second: gui name - QString const lab = toqstr((*iter).first); - refsNames.append({lab, toqstr((*iter).second)}); + // 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]; @@ -473,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) @@ -496,15 +495,17 @@ void GuiRef::redoRefs() QTreeWidgetItem * item = new QTreeWidgetItem(refsTW); item->setText(0, cat); for (int j = 0; j < refsNames.size(); ++j) { - QString const ref = refsNames.at(j).first; + QString const ref = std::get<0>(refsNames.at(j)); if ((ref.startsWith(cat + QString(":"))) || (cat == qt_("") && (!ref.mid(1).contains(":") || ref.left(1).contains(":")))) { QTreeWidgetItem * child = new QTreeWidgetItem(item); - QString const val = refsNames.at(j).second; + 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); } } @@ -515,10 +516,12 @@ void GuiRef::redoRefs() QList refsItems; for (int i = 0; i < refsNames.size(); ++i) { QTreeWidgetItem * item = new QTreeWidgetItem(refsTW); - QString const ref = refsNames.at(i).first; - QString const val = refsNames.at(i).second; + 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); @@ -605,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; } diff --git a/src/frontends/qt/GuiRef.h b/src/frontends/qt/GuiRef.h index 4bd50e57a8..7f8860bd53 100644 --- a/src/frontends/qt/GuiRef.h +++ b/src/frontends/qt/GuiRef.h @@ -106,9 +106,10 @@ private: int restored_buffer_; /// store the last active buffer int active_buffer_; - /// the references as two strings: plain label name and label as gui string + /// the references as three strings: plain label name, label as gui + /// string, and pretty dereferenced name ("Lemma 3") /// FIXME: might be a good idea to use a custom struct - std::vector> refs_; + std::vector> refs_; }; } // namespace frontend diff --git a/src/frontends/qt/ui/RefUi.ui b/src/frontends/qt/ui/RefUi.ui index a8391e0542..dac7d7f4b7 100644 --- a/src/frontends/qt/ui/RefUi.ui +++ b/src/frontends/qt/ui/RefUi.ui @@ -19,9 +19,23 @@ + + 2 + + + 200 + + + true + + + + Label + + - 1 + Reference counter value diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp index 30ed7bf045..91c0b8cea1 100644 --- a/src/insets/InsetFlex.cpp +++ b/src/insets/InsetFlex.cpp @@ -164,6 +164,7 @@ void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype, bool cons docstring const eqlabel = deleted ? from_ascii("#") : cnts.theCounter(equation, it->getParLanguage(bp)->code()); cnts.newCounter(equation, parentequation, + eqlabel + from_ascii("\\alph{equation}"), eqlabel + from_ascii("\\alph{equation}"), eqlabel + from_ascii("\\alph{equation}"), cnts.guiName(parentequation)); diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index e143f83e29..1f0a0459ac 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -156,7 +156,7 @@ docstring InsetLabel::screenLabel() const } -void InsetLabel::updateBuffer(ParIterator const & it, UpdateType utype, bool const /*deleted*/) +void InsetLabel::updateBuffer(ParIterator const & it, UpdateType, bool const /*deleted*/) { docstring const & label = getParam("name"); @@ -184,19 +184,24 @@ void InsetLabel::updateBuffer(ParIterator const & it, UpdateType utype, bool con buffer().setInsetLabel(label, this, active); screen_label_ = label; - if (utype == OutputUpdate) { - // save info on the active counter - Counters const & cnts = - buffer().masterBuffer()->params().documentClass().counters(); - active_counter_ = cnts.currentCounter(); - Language const * lang = it->getParLanguage(buffer().params()); - if (lang && !active_counter_.empty()) { + // save info on the active counter + Counters & cnts = + buffer().masterBuffer()->params().documentClass().counters(); + active_counter_ = cnts.currentCounter(); + Language const * lang = it->getParLanguage(buffer().params()); + if (lang && !active_counter_.empty()) { + if (active_counter_ != from_ascii("equation")) { counter_value_ = cnts.theCounter(active_counter_, lang->code()); pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code()); } else { + // For equations, the counter value and pretty counter + // value will be set by the parent InsetMathHull. counter_value_ = from_ascii("#"); - pretty_counter_ = from_ascii("#"); + pretty_counter_ = from_ascii(""); } + } else { + counter_value_ = from_ascii("#"); + pretty_counter_ = from_ascii("#"); } } @@ -212,7 +217,9 @@ void InsetLabel::addToToc(DocIterator const & cpit, bool output_active, // We put both active and inactive labels to the outliner shared_ptr toc = backend.toc("label"); - toc->push_back(TocItem(cpit, 0, screen_label_, output_active)); + TocItem toc_item = TocItem(cpit, 0, screen_label_, output_active); + toc_item.prettyStr(pretty_counter_); + toc->push_back(toc_item); // The refs get assigned only to the active label. If no active one exists, // assign the (BROKEN) refs to the first inactive one. if (buffer().insetLabel(label, true) == this || !buffer().activeLabel(label)) { diff --git a/src/insets/InsetLabel.h b/src/insets/InsetLabel.h index 8032ce7649..5cd4a88ddc 100644 --- a/src/insets/InsetLabel.h +++ b/src/insets/InsetLabel.h @@ -30,6 +30,8 @@ public: /// docstring const & prettyCounter() const { return pretty_counter_; } /// + void setPrettyCounter(docstring pc) { pretty_counter_ = pc; } + /// int rowFlags() const override { return CanBreakBefore | CanBreakAfter; } /// Updates only the label string, doesn't handle undo nor references. void updateLabel(docstring const & new_label, bool const active = true); diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp index b33bb0d711..954f5651aa 100644 --- a/src/mathed/InsetMathHull.cpp +++ b/src/mathed/InsetMathHull.cpp @@ -313,8 +313,10 @@ void InsetMathHull::addToToc(DocIterator const & pit, bool output_active, for (row_type row = 0; row != nrows(); ++row) { if (!numbered(row)) continue; - if (label_[row]) + if (label_[row]) { + label_[row]->setPrettyCounter(_("Equation ") + numbers_[row]); label_[row]->addToToc(pit, output_active, utype, backend); + } docstring label = nicelabel(row); if (first == last) // this is the only equation -- 2.39.2