[LyX/master] Display equation/theorem numbers in insert cross reference dialog.

Richard Kimberly Heck rikiheck at lyx.org
Wed Jul 26 16:22:16 UTC 2023


commit c609e9cbcf5f013e9bcc8f13a9a3de1f45d8d212
Author: Alexander Dunlap <alexander.dunlap at gmail.com>
Date:   Wed Jul 26 13:35:42 2023 -0400

    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 e837a9b..1f90df3 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -2422,7 +2422,7 @@ void Buffer::validate(LaTeXFeatures & features) const
 }
 
 
-void Buffer::getLabelList(vector<std::pair<docstring, docstring>> & list) const
+void Buffer::getLabelList(vector<std::tuple<docstring, docstring, docstring>> & list) const
 {
 	// If this is a child document, use the master's list instead.
 	if (parent()) {
@@ -2433,8 +2433,9 @@ void Buffer::getLabelList(vector<std::pair<docstring, docstring>> & list) const
 	list.clear();
 	shared_ptr<Toc> 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 4905efb..de27084 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<std::pair<docstring, docstring>> &) const;
+	/// three strings: plain label name, label as gui string, and
+	/// dereferenced label name
+	void getLabelList(std::vector<std::tuple<docstring, docstring, docstring>> &) 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 d7f8630..8f2c60e 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 57fff35..41ee930 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 d99004f..b6a7887 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 c3a178d..7ad36fe 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 9b91443..5fb317d 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<QString, QString> const & s1, QPair<QString, QString> const & s2)
+inline bool caseInsensitiveLessThanVec(std::tuple<QString, QString, QString> const & s1, std::tuple<QString, QString, QString> 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<QPair<QString, QString>> refsNames;
+	// 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;
 	// Do we have a prefix-less label at all?
 	bool noprefix = false;
-	vector<std::pair<docstring, docstring>>::const_iterator iter;
+	vector<std::tuple<docstring, docstring,docstring>>::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_("<No prefix>")
 				       && (!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<QTreeWidgetItem *> 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 4bd50e5..7f8860b 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<std::pair<docstring, docstring>> refs_;
+	std::vector<std::tuple<docstring, docstring, docstring>> refs_;
 };
 
 } // namespace frontend
diff --git a/src/frontends/qt/ui/RefUi.ui b/src/frontends/qt/ui/RefUi.ui
index a8391e0..dac7d7f 100644
--- a/src/frontends/qt/ui/RefUi.ui
+++ b/src/frontends/qt/ui/RefUi.ui
@@ -19,9 +19,23 @@
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="2" column="0">
     <widget class="QTreeWidget" name="refsTW">
+     <property name="columnCount">
+      <number>2</number>
+     </property>
+     <attribute name="headerDefaultSectionSize">
+      <number>200</number>
+     </attribute>
+     <attribute name="headerStretchLastSection">
+      <bool>true</bool>
+     </attribute>
+     <column>
+      <property name="text">
+       <string>Label</string>
+      </property>
+     </column>
      <column>
       <property name="text">
-       <string>1</string>
+       <string>Reference counter value</string>
       </property>
      </column>
     </widget>
diff --git a/src/insets/InsetFlex.cpp b/src/insets/InsetFlex.cpp
index 30ed7bf..91c0b8c 100644
--- a/src/insets/InsetFlex.cpp
+++ b/src/insets/InsetFlex.cpp
@@ -166,6 +166,7 @@ void InsetFlex::updateBuffer(ParIterator const & it, UpdateType utype, bool cons
 		cnts.newCounter(equation, parentequation,
 		                eqlabel + from_ascii("\\alph{equation}"),
 		                eqlabel + from_ascii("\\alph{equation}"),
+		                eqlabel + from_ascii("\\alph{equation}"),
 		                cnts.guiName(parentequation));
 		InsetCollapsible::updateBuffer(it, utype, deleted);
 		// reset equation counter as it was.
diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp
index e143f83..1f0a045 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> 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 8032ce7..5cd4a88 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 b33bb0d..954f565 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


More information about the lyx-cvs mailing list