From 5f830522a2224ed2e7dc6d75d2b10d071188b3ca Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 4 Mar 2019 15:42:02 -0800 Subject: [PATCH] UI: Add drag and drop for URLs Co-authored-by: Matt Gajownik --- UI/data/locale/en-US.ini | 5 +++ UI/window-basic-main-dropfiles.cpp | 70 +++++++++++++++++++++++++++++- UI/window-basic-main.hpp | 4 ++ 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini index b62fdf065..7db1f3f72 100644 --- a/UI/data/locale/en-US.ini +++ b/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." diff --git a/UI/window-basic-main-dropfiles.cpp b/UI/window-basic-main-dropfiles.cpp index abb29ffee..cfe91dd49 100644 --- a/UI/window-basic-main-dropfiles.cpp +++ b/UI/window-basic-main-dropfiles.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #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(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 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(); diff --git a/UI/window-basic-main.hpp b/UI/window-basic-main.hpp index a0fd0256e..6bcfa72cf 100644 --- a/UI/window-basic-main.hpp +++ b/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;