From e8c44747ae7e3e47fde6d6cbef8698a55b67ffea Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sun, 4 Nov 2018 15:11:50 +0000 Subject: [PATCH] [UI] Use Qt Labs File Dialogs. --- CMakeLists.txt | 5 - KittehPlayer.pro | 4 +- src/fileopendialog.cpp | 226 ----------------------------------------- src/fileopendialog.h | 86 ---------------- src/filesavedialog.cpp | 208 ------------------------------------- src/filesavedialog.h | 74 -------------- src/main.cpp | 5 - src/qml/main.qml | 12 ++- 8 files changed, 9 insertions(+), 611 deletions(-) delete mode 100644 src/fileopendialog.cpp delete mode 100644 src/fileopendialog.h delete mode 100644 src/filesavedialog.cpp delete mode 100644 src/filesavedialog.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b9ac5a3..ffdeabb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,14 +18,9 @@ qtquick_compiler_add_resources(qml_QRC src/qml/qml.qrc) find_package(PkgConfig) pkg_check_modules(MPV REQUIRED mpv) - -include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS}) - set(SOURCES src/main.cpp src/MpvPlayerBackend.cpp - src/filesavedialog.cpp - src/fileopendialog.cpp ) if(DEVELOP) diff --git a/KittehPlayer.pro b/KittehPlayer.pro index 6b6810a..5f8d9d5 100644 --- a/KittehPlayer.pro +++ b/KittehPlayer.pro @@ -2,7 +2,7 @@ TARGET = KittehPlayer TEMPLATE = app QT += qml quickcontrols2 widgets core-private gui-private -SOURCES += src/main.cpp src/MpvPlayerBackend.cpp src/filesavedialog.cpp src/fileopendialog.cpp +SOURCES += src/main.cpp src/MpvPlayerBackend.cpp CONFIG += release #CONFIG+=qtquickcompiler @@ -29,7 +29,7 @@ unix { INSTALLS += target -HEADERS += src/MpvPlayerBackend.h src/filesavedialog.h src/fileopendialog.h +HEADERS += src/MpvPlayerBackend.h DISTFILES += KittehPlayer.desktop KittehPlayer.png README.md LICENSE.txt diff --git a/src/fileopendialog.cpp b/src/fileopendialog.cpp deleted file mode 100644 index d5a9b82..0000000 --- a/src/fileopendialog.cpp +++ /dev/null @@ -1,226 +0,0 @@ -/* Copyright 2013–2017 Kullo GmbH. All rights reserved. */ -#include "fileopendialog.h" - -#include -#include -#include - -FileOpenDialog::FileOpenDialog(QQuickItem *parent) - : QQuickItem(parent) - , m_dlgHelper(init_helper()) - , m_modality(Qt::WindowModal) - #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - , m_options(QFileDialogOptions::create()) - #else - , m_options(QSharedPointer(new QFileDialogOptions())) - #endif -{ - /* - * Qt Widgets support must be present, i.e. the main app is a QApplication. - * The following line break at compile time, if the main app is a QGuiApplication - */ - QApplication *appHasQtWidgetsSupport = qobject_cast(QCoreApplication::instance()); - Q_ASSERT(appHasQtWidgetsSupport); - Q_UNUSED(appHasQtWidgetsSupport); - - if (valid()) - { - connect(m_dlgHelper, &QPlatformFileDialogHelper::accept, - this, &FileOpenDialog::accept); - connect(m_dlgHelper, &QPlatformFileDialogHelper::reject, - this, &FileOpenDialog::reject); - } -} - -FileOpenDialog::~FileOpenDialog() -{ - if (m_dlgHelper) - m_dlgHelper->hide(); - delete m_dlgHelper; -} - -bool FileOpenDialog::valid() const -{ - if (m_dlgHelper) return true; - else return false; -} - -QUrl FileOpenDialog::fileUrl() const -{ - return fileUrl_; -} - -QList FileOpenDialog::fileUrls() const -{ - return fileUrls_; -} - -void FileOpenDialog::setFileUrl(QUrl fileUrl) -{ - if (fileUrl_ != fileUrl) - { - fileUrl_ = fileUrl; - emit fileUrlChanged(); - } -} - -void FileOpenDialog::setFileUrls(QList fileUrls) -{ - if (fileUrls_ != fileUrls) - { - fileUrls_ = fileUrls; - emit fileUrlsChanged(); - } -} - -QString FileOpenDialog::filename() const -{ - return filename_; -} - -void FileOpenDialog::setFilename(QString filename) -{ - if (filename_ != filename) - { - filename_ = filename; - emit filenameChanged(); - } -} - -QString FileOpenDialog::title() const -{ - return title_; -} - -void FileOpenDialog::setTitle(QString title) -{ - if (title_ != title) - { - title_ = title; - emit titleChanged(); - } -} - -QStringList FileOpenDialog::nameFilters() const -{ - return nameFilters_; -} - -void FileOpenDialog::setNameFilters(QStringList nameFilters) -{ - if (nameFilters_ != nameFilters) - { - nameFilters_ = nameFilters; - emit nameFiltersChanged(); - } -} - -bool FileOpenDialog::selectMultiple() const -{ - return selectMultiple_; -} - -void FileOpenDialog::setSelectMultiple(bool selectMultiple) -{ - if (selectMultiple_ != selectMultiple) - { - selectMultiple_ = selectMultiple; - emit selectMultipleChanged(); - } -} - -QPlatformFileDialogHelper* FileOpenDialog::init_helper() -{ - return static_cast( - QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(QPlatformTheme::FileDialog) - ); -} - -void FileOpenDialog::open() -{ - if (!valid()) return; - - QQuickItem *parent = this->parentItem(); - Q_ASSERT(parent); - - QQuickWindow *window = parent->window(); - Q_ASSERT(window); - - m_parentWindow = window; - - m_options->setFileMode(selectMultiple_ ? QFileDialogOptions::ExistingFiles : QFileDialogOptions::ExistingFile); - m_options->setAcceptMode(QFileDialogOptions::AcceptOpen); - m_options->setWindowTitle(title()); - m_options->setNameFilters(nameFilters()); - - /* - * Mac: - * Set filename incl. directory via setInitiallySelectedFiles() - * - * Windows: - * Set filename via setInitiallySelectedFiles() and let Windows choose the directory. - * Default directory: C:\\Users\XYZ\Downloads - * - * Gnome: - * Set directory via QPlatformFileDialogHelper::setDirectory() and leave - * filename empty, since QGtk2FileDialogHelper can not set non-existing filenames. - * - */ -#ifdef Q_OS_OSX - QString initialSelection = QFileInfo(QDir::homePath(), filename()).absoluteFilePath(); - //qDebug() << "Initial file:" << initialSelection; - m_options->setInitiallySelectedFiles(QList() << QUrl::fromLocalFile(initialSelection)); -#endif -#ifdef Q_OS_WIN - //qDebug() << "Initial filename:" << filename(); - m_options->setInitiallySelectedFiles(QList() << QUrl::fromLocalFile(filename())); -#endif -#ifdef Q_OS_LINUX - //qDebug() << "Initial directory:" << QDir::homePath(); - m_dlgHelper->setDirectory(QUrl::fromLocalFile(QDir::homePath())); -#endif - - m_dlgHelper->setOptions(m_options); - m_dlgHelper->setFilter(); // applyOptions(); - - Qt::WindowFlags flags = Qt::Dialog; - if (!title().isEmpty()) flags |= Qt::WindowTitleHint; - - m_visible = m_dlgHelper->show(flags, m_modality, m_parentWindow); -} - -void FileOpenDialog::close() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - m_visible = false; -} - -void FileOpenDialog::accept() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - - QList selectedUrls = m_dlgHelper->selectedFiles(); - if (!selectedUrls.empty()) - { - if (selectedUrls.size() == 1) - setFileUrl(selectedUrls.at(0)); - else - setFileUrl(); - - setFileUrls(selectedUrls); - } - - emit accepted(); -} - -void FileOpenDialog::reject() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - emit rejected(); -} diff --git a/src/fileopendialog.h b/src/fileopendialog.h deleted file mode 100644 index e6790a0..0000000 --- a/src/fileopendialog.h +++ /dev/null @@ -1,86 +0,0 @@ -/* Copyright 2013–2017 Kullo GmbH. All rights reserved. */ -#pragma once - -#include -#include -#include -#include -#include -#include - -class FileOpenDialog : public QQuickItem -{ - Q_OBJECT - -public: - explicit FileOpenDialog(QQuickItem *parent = 0); - ~FileOpenDialog(); - - Q_PROPERTY(bool valid READ valid NOTIFY validChanged) - bool valid() const; - - Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged) - QUrl fileUrl() const; - - Q_PROPERTY(QList fileUrls READ fileUrls NOTIFY fileUrlsChanged) - QList fileUrls() const; - - Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged) - QString filename() const; - void setFilename(QString filename); - - Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) - QString title() const; - void setTitle(QString title); - - Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters NOTIFY nameFiltersChanged) - QStringList nameFilters() const; - void setNameFilters(QStringList nameFilters); - - Q_PROPERTY(bool selectMultiple READ selectMultiple WRITE setSelectMultiple NOTIFY selectMultipleChanged) - bool selectMultiple() const; - void setSelectMultiple(bool selectMultiple); - - Q_INVOKABLE void open(); - Q_INVOKABLE void close(); - -signals: - void fileUrlChanged(); - void fileUrlsChanged(); - void filenameChanged(); - void titleChanged(); - void nameFiltersChanged(); - void accepted(); - void rejected(); - void selectMultipleChanged(); - - // unused - void validChanged(); - -protected: - QPlatformFileDialogHelper* init_helper(); - -protected: - QPlatformFileDialogHelper *m_dlgHelper; - QQuickWindow *m_parentWindow; - Qt::WindowModality m_modality; - bool m_visible; - QSharedPointer m_options; - -protected Q_SLOTS: - virtual void accept(); - virtual void reject(); - -private: - void setFileUrl(QUrl fileUrl = QUrl()); - void setFileUrls(QList fileUrls = QList()); - - QUrl fileUrl_; - QList fileUrls_; - QString filename_; - QString title_; - QStringList nameFilters_; - bool selectMultiple_ = false; - - Q_DISABLE_COPY(FileOpenDialog) -}; diff --git a/src/filesavedialog.cpp b/src/filesavedialog.cpp deleted file mode 100644 index a2bd692..0000000 --- a/src/filesavedialog.cpp +++ /dev/null @@ -1,208 +0,0 @@ -/* Copyright 2013–2017 Kullo GmbH. All rights reserved. */ -#include "filesavedialog.h" - -#include -#include -#include -#include - -FileSaveDialog::FileSaveDialog(QQuickItem *parent) - : QQuickItem(parent) - , m_dlgHelper(init_helper()) - , m_modality(Qt::WindowModal) - #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - , m_options(QFileDialogOptions::create()) - #else - , m_options(QSharedPointer(new QFileDialogOptions())) - #endif -{ - /* - * Qt Widgets support must be present, i.e. the main app is a QApplication. - * The following line break at compile time, if the main app is a QGuiApplication - */ - QApplication *appHasQtWidgetsSupport = qobject_cast(QCoreApplication::instance()); - Q_ASSERT(appHasQtWidgetsSupport); - Q_UNUSED(appHasQtWidgetsSupport); - - if (valid()) - { - connect(m_dlgHelper, &QPlatformFileDialogHelper::accept, - this, &FileSaveDialog::accept); - connect(m_dlgHelper, &QPlatformFileDialogHelper::reject, - this, &FileSaveDialog::reject); - }else{ - qDebug() << "ERROR: You need the gtk3 platform plugin to use the file save dialog."; - qDebug() << "To automatically set gtk3 to QT_QPA_PLATFORMTHEME run the following at the command line"; - qDebug() << "echo \"export QT_QPA_PLATFORMTHEME=gtk3\">> ~/.profile && source ~/.profile"; - } -} - -FileSaveDialog::~FileSaveDialog() -{ - if (m_dlgHelper) - m_dlgHelper->hide(); - delete m_dlgHelper; -} - -bool FileSaveDialog::valid() const -{ - if (m_dlgHelper) return true; - else return false; -} - -QUrl FileSaveDialog::fileUrl() const -{ - return fileUrl_; -} - -void FileSaveDialog::setFileUrl(QUrl fileUrl) -{ - if (fileUrl_ != fileUrl) - { - fileUrl_ = fileUrl; - emit fileUrlChanged(); - } -} - -QString FileSaveDialog::filename() const -{ - return filename_; -} - -void FileSaveDialog::setFilename(QString filename) -{ - if (filename_ != filename) - { - filename_ = filename; - emit filenameChanged(); - } -} - -QString FileSaveDialog::title() const -{ - return title_; -} - -void FileSaveDialog::setTitle(QString title) -{ - if (title_ != title) - { - title_ = title; - emit titleChanged(); - } -} - -QStringList FileSaveDialog::nameFilters() const -{ - return nameFilters_; -} - -void FileSaveDialog::setNameFilters(QStringList nameFilters) -{ - if (nameFilters_ != nameFilters) - { - nameFilters_ = nameFilters; - emit nameFiltersChanged(); - } -} - -QPlatformFileDialogHelper* FileSaveDialog::init_helper() -{ - return static_cast( - QGuiApplicationPrivate::platformTheme()->createPlatformDialogHelper(QPlatformTheme::FileDialog) - ); -} - -void FileSaveDialog::open() -{ - if (!valid()) return; - - QQuickItem *parent = this->parentItem(); - Q_ASSERT(parent); - - QQuickWindow *window = parent->window(); - Q_ASSERT(window); - - m_parentWindow = window; - - m_options->setFileMode(QFileDialogOptions::AnyFile); - m_options->setAcceptMode(QFileDialogOptions::AcceptSave); - m_options->setWindowTitle(title()); - m_options->setNameFilters(nameFilters()); - - /* - * Mac: - * Set filename incl. directory via setInitiallySelectedFiles() - * - * Windows: - * Set filename via setInitiallySelectedFiles() and let Windows choose the directory. - * Default directory: C:\\Users\XYZ\Downloads - * - * Gnome: - * Set directory via QPlatformFileDialogHelper::setDirectory() and leave - * filename empty, since QGtk2FileDialogHelper can not set non-existing filenames. - * - */ - const QString folder = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); - const QString name = filename(); - -#ifdef Q_OS_OSX - QUrl initialSelection = QUrl::fromLocalFile(QFileInfo(folder, name).absoluteFilePath()); - //qDebug() << "Initial file:" << initialSelection; - m_options->setInitiallySelectedFiles(QList() << initialSelection); -#endif -#ifdef Q_OS_WIN - //qDebug() << "Initial filename:" << name; - m_options->setInitiallySelectedFiles(QList() << QUrl::fromLocalFile(name)); -#endif -#ifdef Q_OS_LINUX - #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 2)) - // Wohoo, big fix! https://codereview.qt-project.org/91501 - QUrl initialSelection = QUrl::fromLocalFile(QFileInfo(folder, name).absoluteFilePath()); - //qDebug() << "Initial file:" << initialSelection; - m_options->setInitiallySelectedFiles(QList() << initialSelection); - #else - //qDebug() << "Initial directory:" << folder; - m_dlgHelper->setDirectory(QUrl::fromLocalFile(folder)); - #endif -#endif - - m_dlgHelper->setOptions(m_options); - m_dlgHelper->setFilter(); // applyOptions(); - - Qt::WindowFlags flags = Qt::Dialog; - if (!title().isEmpty()) flags |= Qt::WindowTitleHint; - - m_visible = m_dlgHelper->show(flags, m_modality, m_parentWindow); -} - -void FileSaveDialog::close() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - m_visible = false; -} - -void FileSaveDialog::accept() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - - QList selectedUrls = m_dlgHelper->selectedFiles(); - if (!selectedUrls.empty()) - { - setFileUrl(selectedUrls.at(0)); - } - - emit accepted(); -} - -void FileSaveDialog::reject() -{ - if (!valid()) return; - - m_dlgHelper->hide(); - emit rejected(); -} diff --git a/src/filesavedialog.h b/src/filesavedialog.h deleted file mode 100644 index 7577d21..0000000 --- a/src/filesavedialog.h +++ /dev/null @@ -1,74 +0,0 @@ -/* Copyright 2013–2017 Kullo GmbH. All rights reserved. */ -#pragma once - -#include -#include -#include -#include -#include -#include - -class FileSaveDialog : public QQuickItem -{ - Q_OBJECT - -public: - explicit FileSaveDialog(QQuickItem *parent = 0); - ~FileSaveDialog(); - - Q_PROPERTY(bool valid READ valid NOTIFY validChanged) - bool valid() const; - - Q_PROPERTY(QUrl fileUrl READ fileUrl NOTIFY fileUrlChanged) - QUrl fileUrl() const; - - Q_PROPERTY(QString filename READ filename WRITE setFilename NOTIFY filenameChanged) - QString filename() const; - void setFilename(QString filename); - - Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) - QString title() const; - void setTitle(QString title); - - Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters NOTIFY nameFiltersChanged) - QStringList nameFilters() const; - void setNameFilters(QStringList nameFilters); - - Q_INVOKABLE void open(); - Q_INVOKABLE void close(); - -signals: - void fileUrlChanged(); - void filenameChanged(); - void titleChanged(); - void nameFiltersChanged(); - void accepted(); - void rejected(); - - // unused - void validChanged(); - -protected: - QPlatformFileDialogHelper* init_helper(); - -protected: - QPlatformFileDialogHelper *m_dlgHelper; - QQuickWindow *m_parentWindow; - Qt::WindowModality m_modality; - bool m_visible; - QSharedPointer m_options; - -protected Q_SLOTS: - virtual void accept(); - virtual void reject(); - -private: - void setFileUrl(QUrl fileUrl); - - QUrl fileUrl_; - QString filename_; - QString title_; - QStringList nameFilters_; - - Q_DISABLE_COPY(FileSaveDialog) -}; diff --git a/src/main.cpp b/src/main.cpp index 9fc34ca..bd3d2b1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,8 +8,6 @@ #include #include #include -#include "fileopendialog.h" -#include "filesavedialog.h" #include "MpvPlayerBackend.h" #ifdef WIN32 @@ -49,9 +47,6 @@ setenv("QT_QPA_PLATFORMTHEME", "gtk3", 0); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); qmlRegisterType("player", 1, 0, "PlayerBackend"); - qmlRegisterType("player", 1, 0, "FileOpenDialog"); - qmlRegisterType("player", 1, 0, "FileSaveDialog"); - std::setlocale(LC_NUMERIC, "C"); QQmlApplicationEngine engine; diff --git a/src/qml/main.qml b/src/qml/main.qml index 3ebf9ef..d774e02 100644 --- a/src/qml/main.qml +++ b/src/qml/main.qml @@ -4,6 +4,7 @@ import QtQuick.Dialogs 1.3 import QtQuick.Layouts 1.11 import QtQuick.Window 2.11 import Qt.labs.settings 1.0 +import Qt.labs.platform 1.0 as LabsPlatform import player 1.0 import "codes.js" as LanguageCodes @@ -267,14 +268,15 @@ ApplicationWindow { } } - FileSaveDialog { + LabsPlatform.FileDialog { id: screenshotSaveDialog title: translate.getTranslation("SAVE_SCREENSHOT", i18n.language) - filename: "screenshot.png" + fileMode: LabsPlatform.FileDialog.SaveFile + defaultSuffix: "png" nameFilters: ["Images (*.png)", "All files (*)"] onAccepted: { player.grabToImage(function (result) { - var filepath = String(screenshotSaveDialog.fileUrl).replace( + var filepath = String(screenshotSaveDialog.file).replace( "file://", '') result.saveToFile(filepath) subtitlesBar.visible = appearance.useMpvSubs ? false : true @@ -282,12 +284,12 @@ ApplicationWindow { } } - FileOpenDialog { + LabsPlatform.FileDialog { id: fileDialog title: translate.getTranslation("OPEN_FILE", i18n.language) nameFilters: ["All files (*)"] onAccepted: { - player.command(String(fileDialog.fileUrl)) + player.loadFile(String(fileDialog.file)) fileDialog.close() } onRejected: {