1
0
Fork 0

[UI] Use Qt Labs File Dialogs.

This commit is contained in:
Kitteh 2018-11-04 15:11:50 +00:00
parent e4f64c1c7f
commit e8c44747ae
8 changed files with 9 additions and 611 deletions

View file

@ -18,14 +18,9 @@ qtquick_compiler_add_resources(qml_QRC src/qml/qml.qrc)
find_package(PkgConfig) find_package(PkgConfig)
pkg_check_modules(MPV REQUIRED mpv) pkg_check_modules(MPV REQUIRED mpv)
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
src/MpvPlayerBackend.cpp src/MpvPlayerBackend.cpp
src/filesavedialog.cpp
src/fileopendialog.cpp
) )
if(DEVELOP) if(DEVELOP)

View file

@ -2,7 +2,7 @@ TARGET = KittehPlayer
TEMPLATE = app TEMPLATE = app
QT += qml quickcontrols2 widgets core-private gui-private 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 += release
#CONFIG+=qtquickcompiler #CONFIG+=qtquickcompiler
@ -29,7 +29,7 @@ unix {
INSTALLS += target 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 DISTFILES += KittehPlayer.desktop KittehPlayer.png README.md LICENSE.txt

View file

@ -1,226 +0,0 @@
/* Copyright 20132017 Kullo GmbH. All rights reserved. */
#include "fileopendialog.h"
#include <QApplication>
#include <QDebug>
#include <QQuickWindow>
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<QFileDialogOptions>(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<QApplication *>(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<QUrl> FileOpenDialog::fileUrls() const
{
return fileUrls_;
}
void FileOpenDialog::setFileUrl(QUrl fileUrl)
{
if (fileUrl_ != fileUrl)
{
fileUrl_ = fileUrl;
emit fileUrlChanged();
}
}
void FileOpenDialog::setFileUrls(QList<QUrl> 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<QPlatformFileDialogHelper*>(
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>() << QUrl::fromLocalFile(initialSelection));
#endif
#ifdef Q_OS_WIN
//qDebug() << "Initial filename:" << filename();
m_options->setInitiallySelectedFiles(QList<QUrl>() << 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<QUrl> 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();
}

View file

@ -1,86 +0,0 @@
/* Copyright 20132017 Kullo GmbH. All rights reserved. */
#pragma once
#include <QQuickItem>
#include <QFileDialog>
#include <QUrl>
#include <private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformdialoghelper.h>
#include <QtGui/qpa/qplatformtheme.h>
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<QUrl> fileUrls READ fileUrls NOTIFY fileUrlsChanged)
QList<QUrl> 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<QFileDialogOptions> m_options;
protected Q_SLOTS:
virtual void accept();
virtual void reject();
private:
void setFileUrl(QUrl fileUrl = QUrl());
void setFileUrls(QList<QUrl> fileUrls = QList<QUrl>());
QUrl fileUrl_;
QList<QUrl> fileUrls_;
QString filename_;
QString title_;
QStringList nameFilters_;
bool selectMultiple_ = false;
Q_DISABLE_COPY(FileOpenDialog)
};

View file

@ -1,208 +0,0 @@
/* Copyright 20132017 Kullo GmbH. All rights reserved. */
#include "filesavedialog.h"
#include <QApplication>
#include <QDebug>
#include <QQuickWindow>
#include <QStandardPaths>
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<QFileDialogOptions>(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<QApplication *>(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<QPlatformFileDialogHelper*>(
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<QUrl>() << initialSelection);
#endif
#ifdef Q_OS_WIN
//qDebug() << "Initial filename:" << name;
m_options->setInitiallySelectedFiles(QList<QUrl>() << 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<QUrl>() << 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<QUrl> selectedUrls = m_dlgHelper->selectedFiles();
if (!selectedUrls.empty())
{
setFileUrl(selectedUrls.at(0));
}
emit accepted();
}
void FileSaveDialog::reject()
{
if (!valid()) return;
m_dlgHelper->hide();
emit rejected();
}

View file

@ -1,74 +0,0 @@
/* Copyright 20132017 Kullo GmbH. All rights reserved. */
#pragma once
#include <QQuickItem>
#include <QFileDialog>
#include <QUrl>
#include <private/qguiapplication_p.h>
#include <QtGui/qpa/qplatformdialoghelper.h>
#include <QtGui/qpa/qplatformtheme.h>
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<QFileDialogOptions> 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)
};

View file

@ -8,8 +8,6 @@
#include <QApplication> #include <QApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include "fileopendialog.h"
#include "filesavedialog.h"
#include "MpvPlayerBackend.h" #include "MpvPlayerBackend.h"
#ifdef WIN32 #ifdef WIN32
@ -49,9 +47,6 @@ setenv("QT_QPA_PLATFORMTHEME", "gtk3", 0);
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
qmlRegisterType<MpvPlayerBackend>("player", 1, 0, "PlayerBackend"); qmlRegisterType<MpvPlayerBackend>("player", 1, 0, "PlayerBackend");
qmlRegisterType<FileOpenDialog>("player", 1, 0, "FileOpenDialog");
qmlRegisterType<FileSaveDialog>("player", 1, 0, "FileSaveDialog");
std::setlocale(LC_NUMERIC, "C"); std::setlocale(LC_NUMERIC, "C");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;

View file

@ -4,6 +4,7 @@ import QtQuick.Dialogs 1.3
import QtQuick.Layouts 1.11 import QtQuick.Layouts 1.11
import QtQuick.Window 2.11 import QtQuick.Window 2.11
import Qt.labs.settings 1.0 import Qt.labs.settings 1.0
import Qt.labs.platform 1.0 as LabsPlatform
import player 1.0 import player 1.0
import "codes.js" as LanguageCodes import "codes.js" as LanguageCodes
@ -267,14 +268,15 @@ ApplicationWindow {
} }
} }
FileSaveDialog { LabsPlatform.FileDialog {
id: screenshotSaveDialog id: screenshotSaveDialog
title: translate.getTranslation("SAVE_SCREENSHOT", i18n.language) title: translate.getTranslation("SAVE_SCREENSHOT", i18n.language)
filename: "screenshot.png" fileMode: LabsPlatform.FileDialog.SaveFile
defaultSuffix: "png"
nameFilters: ["Images (*.png)", "All files (*)"] nameFilters: ["Images (*.png)", "All files (*)"]
onAccepted: { onAccepted: {
player.grabToImage(function (result) { player.grabToImage(function (result) {
var filepath = String(screenshotSaveDialog.fileUrl).replace( var filepath = String(screenshotSaveDialog.file).replace(
"file://", '') "file://", '')
result.saveToFile(filepath) result.saveToFile(filepath)
subtitlesBar.visible = appearance.useMpvSubs ? false : true subtitlesBar.visible = appearance.useMpvSubs ? false : true
@ -282,12 +284,12 @@ ApplicationWindow {
} }
} }
FileOpenDialog { LabsPlatform.FileDialog {
id: fileDialog id: fileDialog
title: translate.getTranslation("OPEN_FILE", i18n.language) title: translate.getTranslation("OPEN_FILE", i18n.language)
nameFilters: ["All files (*)"] nameFilters: ["All files (*)"]
onAccepted: { onAccepted: {
player.command(String(fileDialog.fileUrl)) player.loadFile(String(fileDialog.file))
fileDialog.close() fileDialog.close()
} }
onRejected: { onRejected: {