1
0
mirror of https://github.com/danog/patches.git synced 2024-11-26 11:54:51 +01:00

Move preview support to QGtk3FileDialogHelper class

To avoid https://github.com/FedoraQt/QGnomePlatform/issues/63
This commit is contained in:
Ilya Fedin 2020-05-26 01:27:49 +04:00 committed by John Preston
parent 3e9be0f434
commit 1064a30709
2 changed files with 135 additions and 63 deletions

View File

@ -993,7 +993,7 @@ index ce67e46df3..a60edc151f 100644
inline bool isDropSiteEnabled() const { return m_dropTarget != 0; }
void setDropSiteEnabled(bool enabled);
diff --git a/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp
index c64a02fa0c..8207762022 100644
index c64a02fa0c..38198e61ba 100644
--- a/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp
+++ b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp
@@ -45,6 +45,7 @@
@ -1021,39 +1021,37 @@ index c64a02fa0c..8207762022 100644
class QGtk3Dialog : public QWindow
{
Q_OBJECT
@@ -77,18 +88,24 @@ Q_SIGNALS:
protected:
static void onResponse(QGtk3Dialog *dialog, int response);
+ static void onUpdatePreview(QGtk3Dialog *dialog);
private slots:
void onParentWindowDestroyed();
private:
GtkWidget *gtkWidget;
+ GtkWidget *previewWidget;
};
QGtk3Dialog::QGtk3Dialog(GtkWidget *gtkWidget) : gtkWidget(gtkWidget)
{
g_signal_connect_swapped(G_OBJECT(gtkWidget), "response", G_CALLBACK(onResponse), this);
g_signal_connect(G_OBJECT(gtkWidget), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
@@ -250,12 +261,21 @@ QGtk3FileDialogHelper::QGtk3FileDialogHelper()
g_signal_connect(GTK_FILE_CHOOSER(d->gtkDialog()), "selection-changed", G_CALLBACK(onSelectionChanged), this);
g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "current-folder-changed", G_CALLBACK(onCurrentFolderChanged), this);
g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "notify::filter", G_CALLBACK(onFilterChanged), this);
+
+ previewWidget = gtk_image_new();
+ g_signal_connect_swapped(G_OBJECT(gtkWidget), "update-preview", G_CALLBACK(onUpdatePreview), this);
+ gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(gtkWidget), previewWidget);
+ g_signal_connect(G_OBJECT(d->gtkDialog()), "update-preview", G_CALLBACK(onUpdatePreview), this);
+ gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(d->gtkDialog()), previewWidget);
}
QGtk3Dialog::~QGtk3Dialog()
@@ -162,6 +179,32 @@ void QGtk3Dialog::onResponse(QGtk3Dialog *dialog, int response)
emit dialog->reject();
QGtk3FileDialogHelper::~QGtk3FileDialogHelper()
{
}
+void QGtk3Dialog::onUpdatePreview(QGtk3Dialog *dialog) {
+ gchar *filename = gtk_file_chooser_get_preview_filename(GTK_FILE_CHOOSER(dialog->gtkWidget));
+GtkImage *QGtk3FileDialogHelper::previewImage() const
+{
+ return GTK_IMAGE(previewWidget);
+}
+
bool QGtk3FileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
{
_dir.clear();
@@ -390,6 +410,33 @@ void QGtk3FileDialogHelper::onFilterChanged(QGtk3FileDialogHelper *dialog)
emit dialog->filterSelected(dialog->selectedNameFilter());
}
+void QGtk3FileDialogHelper::onUpdatePreview(GtkDialog *gtkDialog, QGtk3FileDialogHelper *helper)
+{
+ gchar *filename = gtk_file_chooser_get_preview_filename(GTK_FILE_CHOOSER(gtkDialog));
+ if (!filename) {
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), false);
+ return;
+ }
+
@ -1062,7 +1060,7 @@ index c64a02fa0c..8207762022 100644
+ QFileInfo fileinfo(filename);
+ if (!fileinfo.exists() || !fileinfo.isFile()) {
+ g_free(filename);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), false);
+ return;
+ }
+
@ -1070,15 +1068,53 @@ index c64a02fa0c..8207762022 100644
+ GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, PREVIEW_WIDTH, PREVIEW_HEIGHT, 0);
+ g_free(filename);
+ if (pixbuf) {
+ gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->previewWidget), pixbuf);
+ gtk_image_set_from_pixbuf(helper->previewImage(), pixbuf);
+ g_object_unref(pixbuf);
+ }
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), pixbuf ? true : false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), pixbuf ? true : false);
+}
+
void QGtk3Dialog::onParentWindowDestroyed()
static GtkFileChooserAction gtkFileChooserAction(const QSharedPointer<QFileDialogOptions> &options)
{
// The QGtk3*DialogHelper classes own this object. Make sure the parent doesn't delete it.
switch (options->fileMode()) {
diff --git a/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.h b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.h
index e78a7fc6d1..8963dd7086 100644
--- a/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.h
+++ b/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.h
@@ -47,6 +47,8 @@
#include <QtCore/qstring.h>
#include <qpa/qplatformdialoghelper.h>
+typedef struct _GtkWidget GtkWidget;
+typedef struct _GtkImage GtkImage;
typedef struct _GtkDialog GtkDialog;
typedef struct _GtkFileFilter GtkFileFilter;
@@ -88,6 +90,8 @@ public:
QGtk3FileDialogHelper();
~QGtk3FileDialogHelper();
+ GtkImage *previewImage() const;
+
bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) override;
void exec() override;
void hide() override;
@@ -108,6 +112,7 @@ private:
static void onSelectionChanged(GtkDialog *dialog, QGtk3FileDialogHelper *helper);
static void onCurrentFolderChanged(QGtk3FileDialogHelper *helper);
static void onFilterChanged(QGtk3FileDialogHelper *helper);
+ static void onUpdatePreview(GtkDialog *dialog, QGtk3FileDialogHelper *helper);
void applyOptions();
void setNameFilters(const QStringList &filters);
void selectFileInternal(const QUrl &filename);
@@ -118,6 +123,7 @@ private:
QHash<QString, GtkFileFilter*> _filters;
QHash<GtkFileFilter*, QString> _filterNames;
QScopedPointer<QGtk3Dialog> d;
+ GtkWidget *previewWidget;
};
class QGtk3FontDialogHelper : public QPlatformFontDialogHelper
diff --git a/src/plugins/platformthemes/gtk3/qgtk3theme.cpp b/src/plugins/platformthemes/gtk3/qgtk3theme.cpp
index 077955eb4e..5c8a3dddf7 100644
--- a/src/plugins/platformthemes/gtk3/qgtk3theme.cpp

View File

@ -1,5 +1,5 @@
diff --git a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp
index 62e5dd2..3e609f7 100644
index 62e5dd2..ecabee7 100644
--- a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp
+++ b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.cpp
@@ -38,6 +38,7 @@
@ -27,39 +27,37 @@ index 62e5dd2..3e609f7 100644
class QGtk2Dialog : public QWindow
{
Q_OBJECT
@@ -70,18 +81,24 @@ Q_SIGNALS:
@@ -254,12 +265,21 @@ QGtk2FileDialogHelper::QGtk2FileDialogHelper()
protected:
static void onResponse(QGtk2Dialog *dialog, int response);
+ static void onUpdatePreview(QGtk2Dialog *dialog);
private slots:
void onParentWindowDestroyed();
private:
GtkWidget *gtkWidget;
+ GtkWidget *previewWidget;
};
QGtk2Dialog::QGtk2Dialog(GtkWidget *gtkWidget) : gtkWidget(gtkWidget)
{
g_signal_connect_swapped(G_OBJECT(gtkWidget), "response", G_CALLBACK(onResponse), this);
g_signal_connect(G_OBJECT(gtkWidget), "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
g_signal_connect(GTK_FILE_CHOOSER(d->gtkDialog()), "selection-changed", G_CALLBACK(onSelectionChanged), this);
g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "current-folder-changed", G_CALLBACK(onCurrentFolderChanged), this);
+
+ previewWidget = gtk_image_new();
+ g_signal_connect_swapped(G_OBJECT(gtkWidget), "update-preview", G_CALLBACK(onUpdatePreview), this);
+ gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(gtkWidget), previewWidget);
+ g_signal_connect(G_OBJECT(d->gtkDialog()), "update-preview", G_CALLBACK(onUpdatePreview), this);
+ gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(d->gtkDialog()), previewWidget);
}
QGtk2Dialog::~QGtk2Dialog()
@@ -151,6 +168,32 @@ void QGtk2Dialog::onResponse(QGtk2Dialog *dialog, int response)
emit dialog->reject();
QGtk2FileDialogHelper::~QGtk2FileDialogHelper()
{
}
+void QGtk2Dialog::onUpdatePreview(QGtk2Dialog *dialog) {
+ gchar *filename = gtk_file_chooser_get_preview_filename(GTK_FILE_CHOOSER(dialog->gtkWidget));
+GtkImage *QGtk2FileDialogHelper::previewImage() const
+{
+ return GTK_IMAGE(previewWidget);
+}
+
bool QGtk2FileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent)
{
_dir.clear();
@@ -392,6 +412,33 @@ void QGtk2FileDialogHelper::onCurrentFolderChanged(QGtk2FileDialogHelper *dialog
emit dialog->directoryEntered(dialog->directory());
}
+void QGtk2FileDialogHelper::onUpdatePreview(GtkDialog *gtkDialog, QGtk2FileDialogHelper *helper)
+{
+ gchar *filename = gtk_file_chooser_get_preview_filename(GTK_FILE_CHOOSER(gtkDialog));
+ if (!filename) {
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), false);
+ return;
+ }
+
@ -68,7 +66,7 @@ index 62e5dd2..3e609f7 100644
+ QFileInfo fileinfo(filename);
+ if (!fileinfo.exists() || !fileinfo.isFile()) {
+ g_free(filename);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), false);
+ return;
+ }
+
@ -76,12 +74,50 @@ index 62e5dd2..3e609f7 100644
+ GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, PREVIEW_WIDTH, PREVIEW_HEIGHT, 0);
+ g_free(filename);
+ if (pixbuf) {
+ gtk_image_set_from_pixbuf(GTK_IMAGE(dialog->previewWidget), pixbuf);
+ gtk_image_set_from_pixbuf(helper->previewImage(), pixbuf);
+ g_object_unref(pixbuf);
+ }
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog->gtkWidget), pixbuf ? true : false);
+ gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), pixbuf ? true : false);
+}
+
void QGtk2Dialog::onParentWindowDestroyed()
static GtkFileChooserAction gtkFileChooserAction(const QSharedPointer<QFileDialogOptions> &options)
{
// The QGtk2*DialogHelper classes own this object. Make sure the parent doesn't delete it.
switch (options->fileMode()) {
diff --git a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.h b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.h
index 1410566..ef05e38 100644
--- a/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.h
+++ b/src/plugins/platformthemes/gtk2/qgtk2dialoghelpers.h
@@ -41,6 +41,8 @@
#include <QtCore/qstring.h>
#include <qpa/qplatformdialoghelper.h>
+typedef struct _GtkWidget GtkWidget;
+typedef struct _GtkImage GtkImage;
typedef struct _GtkDialog GtkDialog;
typedef struct _GtkFileFilter GtkFileFilter;
@@ -82,6 +84,8 @@ public:
QGtk2FileDialogHelper();
~QGtk2FileDialogHelper();
+ GtkImage *previewImage() const;
+
bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) Q_DECL_OVERRIDE;
void exec() Q_DECL_OVERRIDE;
void hide() Q_DECL_OVERRIDE;
@@ -101,6 +105,7 @@ private Q_SLOTS:
private:
static void onSelectionChanged(GtkDialog *dialog, QGtk2FileDialogHelper *helper);
static void onCurrentFolderChanged(QGtk2FileDialogHelper *helper);
+ static void onUpdatePreview(GtkDialog *dialog, QGtk2FileDialogHelper *helper);
void applyOptions();
void setNameFilters(const QStringList &filters);
@@ -109,6 +114,7 @@ private:
QHash<QString, GtkFileFilter*> _filters;
QHash<GtkFileFilter*, QString> _filterNames;
QScopedPointer<QGtk2Dialog> d;
+ GtkWidget *previewWidget;
};
class QGtk2FontDialogHelper : public QPlatformFontDialogHelper