The attached patches allow the creation of a child document from the
include dialog, as requested, and also fix a problem with the edit
button in that dialog. Comments?

>From ccf28ced3c3e69316715fc4d1556d3b19fe3dbba Mon Sep 17 00:00:00 2001
From: Richard Kimberly Heck <[email protected]>
Date: Sat, 22 Aug 2020 15:15:47 -0400
Subject: [PATCH 1/2] Fix bug 4475. Add ability to create child from the
 include dialog.

---
 src/frontends/qt/GuiInclude.cpp | 76 ++++++++++++++++++++++++---------
 1 file changed, 57 insertions(+), 19 deletions(-)

diff --git a/src/frontends/qt/GuiInclude.cpp b/src/frontends/qt/GuiInclude.cpp
index c55d71fa7f..5a386547dd 100644
--- a/src/frontends/qt/GuiInclude.cpp
+++ b/src/frontends/qt/GuiInclude.cpp
@@ -32,6 +32,7 @@
 #include "insets/InsetInclude.h"
 
 #include <QCheckBox>
+#include <QFile>
 #include <QLineEdit>
 #include <QPushButton>
 
@@ -265,6 +266,62 @@ void GuiInclude::applyView()
 	}
 	params_["literal"] = literalCB->isChecked()
 			? from_ascii("true") : from_ascii("false");
+
+	// Do we need to create a LyX file?
+	if (item == 0 || item == 1) {
+		QString fname = filenameED->text();
+		string const mypath = buffer().absFileName();
+		string const bpath = buffer().filePath();
+		QString absfname = makeAbsPath(fname, toqstr(bpath));
+		if (!QFile::exists(absfname)) {
+			dispatch(FuncRequest(LFUN_BUFFER_NEW, fromqstr(absfname)));
+			dispatch(FuncRequest(LFUN_BUFFER_WRITE));
+			dispatch(FuncRequest(LFUN_BUFFER_SWITCH, mypath));
+		}
+	}
+}
+
+
+void GuiInclude::edit()
+{
+	if (!isValid())
+		return;
+	if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
+		slotOK();
+		applyView();
+	} else
+		hideView();
+	dispatch(FuncRequest(LFUN_INSET_EDIT));
+}
+
+
+bool GuiInclude::isValid()
+{
+	QString fname = filenameED->text();
+	bool fempty = fname.isEmpty();
+	if (fempty || !validate_listings_params().empty()) {
+		editPB->setEnabled(false);
+		return false;
+	}
+
+	QPushButton * okbutton = buttonBox->button(QDialogButtonBox::Ok);
+	int const item = typeCO->currentIndex();
+	// Are we inputting or including a LyX file?
+	if (item != 0 && item != 1) {
+		okbutton->setText("OK");
+		return true;
+	}
+	// Do we have a LyX filename?
+	if (!support::isLyXFileName(fromqstr(fname))) {
+		okbutton->setText("OK");
+		return false;
+	}
+	string const bpath = buffer().filePath();
+	QString absfname = makeAbsPath(fname, toqstr(bpath));
+	bool const fexists = QFile::exists(absfname);
+	okbutton->setText(fexists ? "OK" : "Create");
+	editPB->setEnabled(fexists);
+	return true;
 }
 
 
@@ -288,25 +345,6 @@ void GuiInclude::browse()
 }
 
 
-void GuiInclude::edit()
-{
-	if (!isValid())
-		return;
-	if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
-		slotOK();
-		applyView();
-	} else
-		hideView();
-	dispatch(FuncRequest(LFUN_INSET_EDIT));
-}
-
-
-bool GuiInclude::isValid()
-{
-	return !filenameED->text().isEmpty() && validate_listings_params().empty();
-}
-
-
 QString GuiInclude::browse(QString const & in_name, Type in_type) const
 {
 	QString const title = qt_("Select document to include");
-- 
2.25.4

>From 9be665463f0b747305660ff020847dcf4e9992e8 Mon Sep 17 00:00:00 2001
From: Richard Kimberly Heck <[email protected]>
Date: Sat, 22 Aug 2020 15:21:53 -0400
Subject: [PATCH 2/2] Fix problem with edit button.

This relied upon the cursor being immediately in front of the inset.
A bad idea.
---
 src/frontends/qt/GuiInclude.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/frontends/qt/GuiInclude.cpp b/src/frontends/qt/GuiInclude.cpp
index 5a386547dd..0c09c6dd59 100644
--- a/src/frontends/qt/GuiInclude.cpp
+++ b/src/frontends/qt/GuiInclude.cpp
@@ -291,7 +291,10 @@ void GuiInclude::edit()
 		applyView();
 	} else
 		hideView();
-	dispatch(FuncRequest(LFUN_INSET_EDIT));
+	QString fname = filenameED->text();
+	string const bpath = buffer().filePath();
+	string absfname = support::makeAbsPath(fromqstr(fname), bpath).absFileName();
+	dispatch(FuncRequest(LFUN_BUFFER_SWITCH, absfname));
 }
 
 
-- 
2.25.4

-- 
lyx-devel mailing list
[email protected]
http://lists.lyx.org/mailman/listinfo/lyx-devel

Reply via email to