Ver Fonte

UI: Add drag and drop for URLs

Co-authored-by: Matt Gajownik <[email protected]>
jp9000 há 6 anos atrás
pai
commit
5f830522a2
3 ficheiros alterados com 77 adições e 2 exclusões
  1. 5 0
      UI/data/locale/en-US.ini
  2. 68 2
      UI/window-basic-main-dropfiles.cpp
  3. 4 0
      UI/window-basic-main.hpp

+ 5 - 0
UI/data/locale/en-US.ini

@@ -984,6 +984,11 @@ About.Authors="Authors"
 About.License="License"
 About.Contribute="Support the OBS Project"
 
+# Drag-drop URL
+AddUrl.Title="Add Source via URL"
+AddUrl.Text="You have dragged a URL into OBS. This will automatically add the link as a source. Continue?"
+AddUrl.Text.Url="URL: %1"
+
 # Dynamic output size
 ResizeOutputSizeOfSource="Resize output (source size)"
 ResizeOutputSizeOfSource.Text="The base and output resolutions will be resized to the size of the current source."

+ 68 - 2
UI/window-basic-main-dropfiles.cpp

@@ -4,6 +4,7 @@
 #include <QDropEvent>
 #include <QFileInfo>
 #include <QMimeData>
+#include <QUrlQuery>
 #include <string>
 
 #include "window-basic-main.hpp"
@@ -55,6 +56,35 @@ static string GenerateSourceName(const char *base)
 	}
 }
 
+void OBSBasic::AddDropURL(const char *url, QString &name, obs_data_t *settings,
+			  const obs_video_info &ovi)
+{
+	QUrl path = QString::fromUtf8(url);
+	QUrlQuery query = QUrlQuery(path.query(QUrl::FullyEncoded));
+
+	int cx = (int)ovi.base_width;
+	int cy = (int)ovi.base_height;
+
+	if (query.hasQueryItem("layer-width"))
+		cx = query.queryItemValue("layer-width").toInt();
+	if (query.hasQueryItem("layer-height"))
+		cy = query.queryItemValue("layer-height").toInt();
+
+	obs_data_set_int(settings, "width", cx);
+	obs_data_set_int(settings, "height", cy);
+
+	name = query.hasQueryItem("layer-name")
+		       ? query.queryItemValue("layer-name", QUrl::FullyDecoded)
+		       : path.host();
+
+	query.removeQueryItem("layer-width");
+	query.removeQueryItem("layer-height");
+	query.removeQueryItem("layer-name");
+	path.setQuery(query);
+
+	obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
+}
+
 void OBSBasic::AddDropSource(const char *data, DropType image)
 {
 	OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
@@ -105,6 +135,10 @@ void OBSBasic::AddDropSource(const char *data, DropType image)
 		name = QUrl::fromLocalFile(QString(data)).fileName();
 		type = "browser_source";
 		break;
+	case DropType_Url:
+		AddDropURL(data, name, settings, ovi);
+		type = "browser_source";
+		break;
 	}
 
 	if (!obs_source_get_display_name(type)) {
@@ -141,6 +175,35 @@ void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
 	event->acceptProposedAction();
 }
 
+void OBSBasic::ConfirmDropUrl(const QString &url)
+{
+	if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
+	    url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
+
+		activateWindow();
+
+		QString msg = QTStr("AddUrl.Text");
+		msg += "\n\n";
+		msg += QTStr("AddUrl.Text.Url").arg(url);
+
+		QMessageBox messageBox(this);
+		messageBox.setWindowTitle(QTStr("AddUrl.Title"));
+		messageBox.setText(msg);
+
+		QPushButton *yesButton = messageBox.addButton(
+			QTStr("Yes"), QMessageBox::YesRole);
+		QPushButton *noButton =
+			messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
+		messageBox.setDefaultButton(yesButton);
+		messageBox.setEscapeButton(noButton);
+		messageBox.setIcon(QMessageBox::Question);
+		messageBox.exec();
+
+		if (messageBox.clickedButton() == yesButton)
+			AddDropSource(QT_TO_UTF8(url), DropType_Url);
+	}
+}
+
 void OBSBasic::dropEvent(QDropEvent *event)
 {
 	const QMimeData *mimeData = event->mimeData();
@@ -149,11 +212,14 @@ void OBSBasic::dropEvent(QDropEvent *event)
 		QList<QUrl> urls = mimeData->urls();
 
 		for (int i = 0; i < urls.size(); i++) {
-			QString file = urls.at(i).toLocalFile();
+			QUrl url = urls[i];
+			QString file = url.toLocalFile();
 			QFileInfo fileInfo(file);
 
-			if (!fileInfo.exists())
+			if (!fileInfo.exists()) {
+				ConfirmDropUrl(url.url());
 				continue;
+			}
 
 			QString suffixQStr = fileInfo.suffix();
 			QByteArray suffixArray = suffixQStr.toUtf8();

+ 4 - 0
UI/window-basic-main.hpp

@@ -170,6 +170,7 @@ class OBSBasic : public OBSMainWindow {
 		DropType_Image,
 		DropType_Media,
 		DropType_Html,
+		DropType_Url,
 	};
 
 private:
@@ -435,6 +436,9 @@ private:
 	inline void OnDeactivate();
 
 	void AddDropSource(const char *file, DropType image);
+	void AddDropURL(const char *url, QString &name, obs_data_t *settings,
+			const obs_video_info &ovi);
+	void ConfirmDropUrl(const QString &url);
 	void dragEnterEvent(QDragEnterEvent *event) override;
 	void dragLeaveEvent(QDragLeaveEvent *event) override;
 	void dragMoveEvent(QDragMoveEvent *event) override;