]> git.lyx.org Git - lyx.git/commitdiff
GuiBibTeX: introduce way to toggle between absolute and relative local paths (#13097)
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 21 Sep 2024 05:30:55 +0000 (07:30 +0200)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 21 Sep 2024 05:30:55 +0000 (07:30 +0200)
src/frontends/qt/GuiBibtex.cpp
src/frontends/qt/GuiBibtex.h
src/frontends/qt/ui/BibtexUi.ui

index 757c2c95e4094a0863a11bf6f8a6cd5dc0225842..47dda81032fd726bd0da2dd7c13be5414c4f12dc 100644 (file)
@@ -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();
 }
 
 
index 57f8bf3b1e895ff815ff9b9d036db95e71019412..fdbe7901d7a42773d717ae6969f5270425763d7e 100644 (file)
@@ -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:
        ///
index ab2769d9ac2d3c244e5d7c1c7a313cbe57d4c64a..679d7e3b8f5d0a7745090ec0e6073e0b46dc5bd0 100644 (file)
           </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>&amp;Make Relative</string>
+          </property>
+         </widget>
+        </item>
         <item>
          <widget class="QPushButton" name="inheritPB">
           <property name="toolTip">