[LyX/master] GuiBibTeX: introduce way to toggle between absolute and relative local paths (#13097)

Juergen Spitzmueller spitz at lyx.org
Sat Sep 21 05:32:53 UTC 2024


commit 124f441a00289788554958caee871641887db7be
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sat Sep 21 07:30:55 2024 +0200

    GuiBibTeX: introduce way to toggle between absolute and relative local paths (#13097)
---
 src/frontends/qt/GuiBibtex.cpp  | 74 +++++++++++++++++++++++++++++++++++++++++
 src/frontends/qt/GuiBibtex.h    | 11 ++++++
 src/frontends/qt/ui/BibtexUi.ui | 10 ++++++
 3 files changed, 95 insertions(+)

diff --git a/src/frontends/qt/GuiBibtex.cpp b/src/frontends/qt/GuiBibtex.cpp
index 757c2c95e4..47dda81032 100644
--- a/src/frontends/qt/GuiBibtex.cpp
+++ b/src/frontends/qt/GuiBibtex.cpp
@@ -91,6 +91,8 @@ GuiBibtex::GuiBibtex(GuiView & lv)
 		this, SLOT(browseBibPressed()));
 	connect(inheritPB, SIGNAL(clicked()),
 		this, SLOT(inheritPressed()));
+	connect(relAbsPB, SIGNAL(clicked()),
+		this, SLOT(relAbsPressed()));
 
 	selected_model_.insertColumns(0, 1);
 	selectionManager = new GuiSelectionManager(this, availableLV, selectedLV,
@@ -123,6 +125,7 @@ GuiBibtex::GuiBibtex(GuiView & lv)
 	bc().addReadOnly(styleCB);
 	bc().addReadOnly(bibtocCB);
 	bc().addReadOnly(bibEncodingCO);
+	bc().addReadOnly(relAbsPB);
 
 	selectedLV->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
 
@@ -180,6 +183,7 @@ void GuiBibtex::selUpdated()
 {
 	selectionManager->update();
 	editPB->setEnabled(deletePB->isEnabled());
+	updateReAbs();
 	changed();
 }
 
@@ -234,6 +238,23 @@ void GuiBibtex::browseBibPressed()
 	}
 }
 
+GuiBibtex::LocalPath GuiBibtex::localPathSelected()
+{
+	QModelIndexList selIdx =
+		selectedLV->selectionModel()->selectedIndexes();
+	if (selIdx.isEmpty())
+		return LP_None;
+	QModelIndex idx = selIdx.first();
+	QString sel = idx.data().toString();
+	string const texfile = support::changeExtension(fromqstr(sel), "bib");
+	if (FileName::isAbsolute(texfile))
+		return LP_Absolute;
+	FileName const file = support::makeAbsPath(texfile, buffer().filePath());
+	if (file.exists())
+		return LP_Relative;
+	return LP_None;
+}
+
 
 bool GuiBibtex::hasInherits()
 {
@@ -278,6 +299,38 @@ void GuiBibtex::inheritPressed()
 }
 
 
+void GuiBibtex::relAbsPressed()
+{
+	LocalPath const p = localPathSelected();
+
+	if (p == LP_None)
+		return;
+
+	QModelIndexList selIdx =
+		selectedLV->selectionModel()->selectedIndexes();
+	QModelIndex idx = selIdx.first();
+	QString const qf = idx.data().toString();
+	string f = fromqstr(qf);
+	FileName const file = (p == LP_Absolute) ?
+				FileName(f)
+			      : support::makeAbsPath(f, buffer().filePath());
+	QString const new_item = (p == LP_Absolute)
+			? toqstr(file.relPath(buffer().filePath()))
+			: toqstr(file.absoluteFilePath());
+	QStringList sb;
+	for (QString s : selected_bibs_) {
+		if (s == qf)
+			sb << new_item;
+		else
+			sb << s;
+	}
+	selected_bibs_ = sb;
+	setSelectedBibs(selected_bibs_);
+	selectedLV->selectRow(selected_bibs_.indexOf(new_item));
+	changed();
+}
+
+
 void GuiBibtex::on_editPB_clicked()
 {
 	QModelIndexList selIdx =
@@ -305,6 +358,25 @@ void GuiBibtex::clearSelection()
 }
 
 
+void GuiBibtex::updateReAbs()
+{
+	switch (localPathSelected()) {
+	case LP_Absolute:
+		relAbsPB->setText(qt_("&Make Relative"));
+		relAbsPB->setEnabled(true);
+		break;
+	case LP_Relative:
+		relAbsPB->setText(qt_("&Make Absolute"));
+		relAbsPB->setEnabled(true);
+		break;
+	case LP_None:
+	default:
+		relAbsPB->setEnabled(false);
+		break;
+	}
+}
+
+
 void GuiBibtex::setSelectedBibs(QStringList const & sl)
 {
 	selected_model_.clear();
@@ -349,6 +421,7 @@ void GuiBibtex::setSelectedBibs(QStringList const & sl)
 		selectedLV->setIndexWidget(selected_model_.index(i, 1), cb);
 	}
 	editPB->setEnabled(deletePB->isEnabled());
+	updateReAbs();
 }
 
 
@@ -446,6 +519,7 @@ void GuiBibtex::updateContents()
 
 	setFileEncodings(getVectorFromString(params_["file_encodings"], from_ascii("\t")));
 	editPB->setEnabled(deletePB->isEnabled());
+	updateReAbs();
 }
 
 
diff --git a/src/frontends/qt/GuiBibtex.h b/src/frontends/qt/GuiBibtex.h
index 57f8bf3b1e..fdbe7901d7 100644
--- a/src/frontends/qt/GuiBibtex.h
+++ b/src/frontends/qt/GuiBibtex.h
@@ -39,6 +39,7 @@ private Q_SLOTS:
 	void on_buttonBox_accepted();
 	void browseBstPressed();
 	void browseBibPressed();
+	void relAbsPressed();
 	void inheritPressed();
 	void on_editPB_clicked();
 	void databaseChanged();
@@ -101,6 +102,16 @@ private:
 	bool isBufferDependent() const override { return true; }
 	/// Is his a child which can inherit bibs from its master?
 	bool hasInherits();
+	/// Is an item with local path selected?
+	/// returns which (none, absolute, relative)
+	enum LocalPath {
+		LP_None,
+		LP_Absolute,
+		LP_Relative
+	};
+	LocalPath localPathSelected();
+	///
+	void updateReAbs();
 
 private:
 	///
diff --git a/src/frontends/qt/ui/BibtexUi.ui b/src/frontends/qt/ui/BibtexUi.ui
index ab2769d9ac..679d7e3b8f 100644
--- a/src/frontends/qt/ui/BibtexUi.ui
+++ b/src/frontends/qt/ui/BibtexUi.ui
@@ -85,6 +85,16 @@
           </property>
          </widget>
         </item>
+        <item>
+         <widget class="QPushButton" name="relAbsPB">
+          <property name="toolTip">
+           <string>Transfer an absolute local path to a relative one or vice versa</string>
+          </property>
+          <property name="text">
+           <string>&Make Relative</string>
+          </property>
+         </widget>
+        </item>
         <item>
          <widget class="QPushButton" name="inheritPB">
           <property name="toolTip">


More information about the lyx-cvs mailing list