[LyX/master] New attempt on #9906: allow following hyperlinks via context menu.

Pavel Sanda sanda at lyx.org
Fri Aug 14 17:23:09 UTC 2020


commit 48b1e8a0aca2f3f3faa8f1f800568e47792ba9a0
Author: Pavel Sanda <sanda at lyx.org>
Date:   Fri Aug 14 19:46:13 2020 +0200

    New attempt on #9906: allow following hyperlinks via context menu.
    
    Now safer version with the help of Qt.
---
 lib/ui/stdcontext.inc         |    2 ++
 src/insets/InsetHyperlink.cpp |   19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index 0a2cbda..c2beb31 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -673,6 +673,8 @@ Menuset
 #
 	Menu "context-hyperlink"
 		Item "Settings...|S" "inset-settings"
+		Separator
+		Item "Open Target...|O" "inset-edit"
 	End
 
 #
diff --git a/src/insets/InsetHyperlink.cpp b/src/insets/InsetHyperlink.cpp
index 26850cd..bfa478d 100644
--- a/src/insets/InsetHyperlink.cpp
+++ b/src/insets/InsetHyperlink.cpp
@@ -12,6 +12,9 @@
 #include <config.h>
 #include "InsetHyperlink.h"
 
+#include <QtGui/QDesktopServices>
+#include <QUrl>
+
 #include "Buffer.h"
 #include "DispatchResult.h"
 #include "Encoding.h"
@@ -30,6 +33,7 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
+#include "support/qstring_helpers.h"
 
 #include "frontends/alert.h"
 
@@ -106,9 +110,13 @@ bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
 		FuncStatus & flag) const
 {
 	switch (cmd.action()) {
-	case LFUN_INSET_EDIT:
-		flag.setEnabled(getParam("type").empty() || getParam("type") == "file:");
+	case LFUN_INSET_EDIT: {
+		QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
+		bool url_valid = getParam("type").empty() && url.isValid();
+
+		flag.setEnabled(url_valid || getParam("type") == "file:");
 		return true;
+		}
 
 	default:
 		return InsetCommand::getStatus(cur, cmd, flag);
@@ -118,7 +126,12 @@ bool InsetHyperlink::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetHyperlink::viewTarget() const
 {
-	if (getParam("type") == "file:") {
+	if (getParam("type").empty()) { //==Web
+		QUrl url(toqstr(getParam("target")),QUrl::StrictMode);
+		if (!QDesktopServices::openUrl(url))
+			LYXERR0("Unable to open URL!");
+
+	} else if (getParam("type") == "file:") {
 		FileName url = makeAbsPath(to_utf8(getParam("target")), buffer().filePath());
 		string const format = theFormats().getFormatFromFile(url);
 		theFormats().view(buffer(), url, format);


More information about the lyx-cvs mailing list