Qt: Move OpenURL to QtUtils namespace

This commit is contained in:
Connor McLaughlin 2020-05-20 23:25:16 +10:00
parent d41bf86eaa
commit e7778ea86e
3 changed files with 29 additions and 20 deletions

View file

@ -13,11 +13,11 @@
#include "scmversion/scmversion.h" #include "scmversion/scmversion.h"
#include "settingsdialog.h" #include "settingsdialog.h"
#include "settingwidgetbinder.h" #include "settingwidgetbinder.h"
#include "qtutils.h"
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <QtGui/QCursor> #include <QtGui/QCursor>
#include <QtGui/QDesktopServices>
#include <QtGui/QWindowStateChangeEvent> #include <QtGui/QWindowStateChangeEvent>
#include <QtWidgets/QFileDialog> #include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox> #include <QtWidgets/QMessageBox>
@ -277,33 +277,19 @@ void MainWindow::onRemoveDiscActionTriggered()
m_host_interface->changeDisc(QString()); m_host_interface->changeDisc(QString());
} }
static void OpenURL(QWidget* parent, const QUrl& qurl)
{
if (!QDesktopServices::openUrl(qurl))
{
QMessageBox::critical(parent, QObject::tr("Failed to open URL"),
QObject::tr("Failed to open URL.\n\nThe URL was: %1").arg(qurl.toString()));
}
}
static void OpenURL(QWidget* parent, const char* url)
{
return OpenURL(parent, QUrl::fromEncoded(QByteArray(url, static_cast<int>(std::strlen(url)))));
}
void MainWindow::onGitHubRepositoryActionTriggered() void MainWindow::onGitHubRepositoryActionTriggered()
{ {
OpenURL(this, "https://github.com/stenzek/duckstation/"); QtUtils::OpenURL(this, "https://github.com/stenzek/duckstation/");
} }
void MainWindow::onIssueTrackerActionTriggered() void MainWindow::onIssueTrackerActionTriggered()
{ {
OpenURL(this, "https://github.com/stenzek/duckstation/issues"); QtUtils::OpenURL(this, "https://github.com/stenzek/duckstation/issues");
} }
void MainWindow::onDiscordServerActionTriggered() void MainWindow::onDiscordServerActionTriggered()
{ {
OpenURL(this, "https://discord.gg/Buktv3t"); QtUtils::OpenURL(this, "https://discord.gg/Buktv3t");
} }
void MainWindow::onAboutActionTriggered() {} void MainWindow::onAboutActionTriggered() {}
@ -358,7 +344,7 @@ void MainWindow::onGameListContextMenuRequested(const QPoint& point, const GameL
connect(menu.addAction(tr("Open Containing Directory...")), &QAction::triggered, [this, entry]() { connect(menu.addAction(tr("Open Containing Directory...")), &QAction::triggered, [this, entry]() {
const QFileInfo fi(QString::fromStdString(entry->path)); const QFileInfo fi(QString::fromStdString(entry->path));
OpenURL(this, QUrl::fromLocalFile(fi.absolutePath())); QtUtils::OpenURL(this, QUrl::fromLocalFile(fi.absolutePath()));
}); });
menu.addSeparator(); menu.addSeparator();

View file

@ -1,9 +1,11 @@
#include "qtutils.h" #include "qtutils.h"
#include "common/byte_stream.h" #include "common/byte_stream.h"
#include <QtCore/QMetaObject> #include <QtCore/QMetaObject>
#include <QtGui/QDesktopServices>
#include <QtGui/QKeyEvent> #include <QtGui/QKeyEvent>
#include <QtWidgets/QDialog> #include <QtWidgets/QDialog>
#include <QtWidgets/QMainWindow> #include <QtWidgets/QMainWindow>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QScrollBar> #include <QtWidgets/QScrollBar>
#include <QtWidgets/QStyle> #include <QtWidgets/QStyle>
#include <QtWidgets/QTableView> #include <QtWidgets/QTableView>
@ -615,4 +617,18 @@ bool WriteQByteArrayToStream(QByteArray& arr, ByteStream* stream)
return arr.isEmpty() || stream->Write2(arr.data(), static_cast<u32>(arr.size())); return arr.isEmpty() || stream->Write2(arr.data(), static_cast<u32>(arr.size()));
} }
void OpenURL(QWidget* parent, const QUrl& qurl)
{
if (!QDesktopServices::openUrl(qurl))
{
QMessageBox::critical(parent, QObject::tr("Failed to open URL"),
QObject::tr("Failed to open URL.\n\nThe URL was: %1").arg(qurl.toString()));
}
}
void OpenURL(QWidget* parent, const char* url)
{
return OpenURL(parent, QUrl::fromEncoded(QByteArray(url, static_cast<int>(std::strlen(url)))));
}
} // namespace QtUtils } // namespace QtUtils

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <QtCore/QString>
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QString>
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
@ -10,6 +10,7 @@ class QFrame;
class QKeyEvent; class QKeyEvent;
class QTableView; class QTableView;
class QWidget; class QWidget;
class QUrl;
namespace QtUtils { namespace QtUtils {
@ -44,4 +45,10 @@ QByteArray ReadStreamToQByteArray(ByteStream* stream, bool rewind = false);
/// Creates a stream from a Qt byte array. /// Creates a stream from a Qt byte array.
bool WriteQByteArrayToStream(QByteArray& arr, ByteStream* stream); bool WriteQByteArrayToStream(QByteArray& arr, ByteStream* stream);
/// Opens a URL with the default handler.
void OpenURL(QWidget* parent, const QUrl& qurl);
/// Opens a URL string with the default handler.
void OpenURL(QWidget* parent, const char* url);
} // namespace QtUtils } // namespace QtUtils