1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
5 | |
6 | #include "qgtk3dialoghelpers.h" |
7 | #include "qgtk3theme.h" |
8 | |
9 | #include <qeventloop.h> |
10 | #include <qwindow.h> |
11 | #include <qcolor.h> |
12 | #include <qdebug.h> |
13 | #include <qfont.h> |
14 | #include <qfileinfo.h> |
15 | |
16 | #include <private/qguiapplication_p.h> |
17 | #include <private/qgenericunixservices_p.h> |
18 | #include <qpa/qplatformintegration.h> |
19 | #include <qpa/qplatformfontdatabase.h> |
20 | |
21 | #undef signals |
22 | #include <gtk/gtk.h> |
23 | #include <gdk/gdk.h> |
24 | #include <pango/pango.h> |
25 | |
26 | #if QT_CONFIG(xlib) && defined(GDK_WINDOWING_X11) |
27 | #include <gdk/gdkx.h> |
28 | #endif |
29 | |
30 | #ifdef GDK_WINDOWING_WAYLAND |
31 | #include <gdk/gdkwayland.h> |
32 | #endif |
33 | |
34 | // The size of the preview we display for selected image files. We set height |
35 | // larger than width because generally there is more free space vertically |
36 | // than horizontally (setting the preview image will always expand the width of |
37 | // the dialog, but usually not the height). The image's aspect ratio will always |
38 | // be preserved. |
39 | #define PREVIEW_WIDTH 256 |
40 | #define PREVIEW_HEIGHT 512 |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | using namespace Qt::StringLiterals; |
45 | |
46 | class QGtk3Dialog |
47 | { |
48 | public: |
49 | QGtk3Dialog(GtkWidget *gtkWidget, QPlatformDialogHelper *helper); |
50 | ~QGtk3Dialog(); |
51 | |
52 | GtkDialog *gtkDialog() const; |
53 | |
54 | void exec(); |
55 | bool show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent); |
56 | void hide(); |
57 | |
58 | protected: |
59 | static void onResponse(QPlatformDialogHelper *helper, int response); |
60 | |
61 | private: |
62 | GtkWidget *gtkWidget; |
63 | QPlatformDialogHelper *helper; |
64 | Qt::WindowModality modality; |
65 | }; |
66 | |
67 | QGtk3Dialog::QGtk3Dialog(GtkWidget *gtkWidget, QPlatformDialogHelper *helper) |
68 | : gtkWidget(gtkWidget) |
69 | , helper(helper) |
70 | { |
71 | g_signal_connect_swapped(G_OBJECT(gtkWidget), "response" , G_CALLBACK(onResponse), helper); |
72 | g_signal_connect(G_OBJECT(gtkWidget), "delete-event" , G_CALLBACK(gtk_widget_hide_on_delete), NULL); |
73 | } |
74 | |
75 | QGtk3Dialog::~QGtk3Dialog() |
76 | { |
77 | gtk_clipboard_store(clipboard: gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); |
78 | gtk_widget_destroy(widget: gtkWidget); |
79 | } |
80 | |
81 | GtkDialog *QGtk3Dialog::gtkDialog() const |
82 | { |
83 | return GTK_DIALOG(gtkWidget); |
84 | } |
85 | |
86 | void QGtk3Dialog::exec() |
87 | { |
88 | if (modality == Qt::ApplicationModal) { |
89 | // block input to the whole app, including other GTK dialogs |
90 | gtk_dialog_run(dialog: gtkDialog()); |
91 | } else { |
92 | // block input to the window, allow input to other GTK dialogs |
93 | QEventLoop loop; |
94 | loop.connect(asender: helper, SIGNAL(accept()), SLOT(quit())); |
95 | loop.connect(asender: helper, SIGNAL(reject()), SLOT(quit())); |
96 | loop.exec(); |
97 | } |
98 | } |
99 | |
100 | bool QGtk3Dialog::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) |
101 | { |
102 | Q_UNUSED(flags); |
103 | this->modality = modality; |
104 | |
105 | gtk_widget_realize(widget: gtkWidget); // creates X window |
106 | |
107 | GdkWindow *gdkWindow = gtk_widget_get_window(widget: gtkWidget); |
108 | if (parent) { |
109 | if (false) { |
110 | #if defined(GDK_WINDOWING_WAYLAND) && GTK_CHECK_VERSION(3, 22, 0) |
111 | } else if (GDK_IS_WAYLAND_WINDOW(gdkWindow)) { |
112 | const auto unixServices = dynamic_cast<QGenericUnixServices *>( |
113 | QGuiApplicationPrivate::platformIntegration()->services()); |
114 | if (unixServices) { |
115 | const auto handle = unixServices->portalWindowIdentifier(window: parent); |
116 | if (handle.startsWith(s: "wayland:"_L1 )) { |
117 | auto handleBa = handle.sliced(pos: 8).toUtf8(); |
118 | gdk_wayland_window_set_transient_for_exported(window: gdkWindow, parent_handle_str: handleBa.data()); |
119 | } |
120 | } |
121 | #endif |
122 | #if QT_CONFIG(xlib) && defined(GDK_WINDOWING_X11) |
123 | } else if (GDK_IS_X11_WINDOW(gdkWindow)) { |
124 | GdkDisplay *gdkDisplay = gdk_window_get_display(window: gdkWindow); |
125 | XSetTransientForHint(gdk_x11_display_get_xdisplay(display: gdkDisplay), |
126 | gdk_x11_window_get_xid(window: gdkWindow), |
127 | parent->winId()); |
128 | #endif |
129 | } |
130 | } |
131 | |
132 | if (modality != Qt::NonModal) { |
133 | gdk_window_set_modal_hint(window: gdkWindow, modal: true); |
134 | } |
135 | |
136 | gtk_widget_show(widget: gtkWidget); |
137 | gdk_window_focus(window: gdkWindow, GDK_CURRENT_TIME); |
138 | return true; |
139 | } |
140 | |
141 | void QGtk3Dialog::hide() |
142 | { |
143 | gtk_widget_hide(widget: gtkWidget); |
144 | } |
145 | |
146 | void QGtk3Dialog::onResponse(QPlatformDialogHelper *helper, int response) |
147 | { |
148 | if (response == GTK_RESPONSE_OK) |
149 | emit helper->accept(); |
150 | else |
151 | emit helper->reject(); |
152 | } |
153 | |
154 | QGtk3ColorDialogHelper::QGtk3ColorDialogHelper() |
155 | { |
156 | d.reset(other: new QGtk3Dialog(gtk_color_chooser_dialog_new(title: "" , parent: nullptr), this)); |
157 | g_signal_connect_swapped(d->gtkDialog(), "notify::rgba" , G_CALLBACK(onColorChanged), this); |
158 | } |
159 | |
160 | QGtk3ColorDialogHelper::~QGtk3ColorDialogHelper() |
161 | { |
162 | } |
163 | |
164 | bool QGtk3ColorDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) |
165 | { |
166 | applyOptions(); |
167 | return d->show(flags, modality, parent); |
168 | } |
169 | |
170 | void QGtk3ColorDialogHelper::exec() |
171 | { |
172 | d->exec(); |
173 | } |
174 | |
175 | void QGtk3ColorDialogHelper::hide() |
176 | { |
177 | d->hide(); |
178 | } |
179 | |
180 | void QGtk3ColorDialogHelper::setCurrentColor(const QColor &color) |
181 | { |
182 | GtkDialog *gtkDialog = d->gtkDialog(); |
183 | if (color.alpha() < 255) |
184 | gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(gtkDialog), use_alpha: true); |
185 | GdkRGBA gdkColor; |
186 | gdkColor.red = color.redF(); |
187 | gdkColor.green = color.greenF(); |
188 | gdkColor.blue = color.blueF(); |
189 | gdkColor.alpha = color.alphaF(); |
190 | gtk_color_chooser_set_rgba(GTK_COLOR_CHOOSER(gtkDialog), color: &gdkColor); |
191 | } |
192 | |
193 | QColor QGtk3ColorDialogHelper::currentColor() const |
194 | { |
195 | GtkDialog *gtkDialog = d->gtkDialog(); |
196 | GdkRGBA gdkColor; |
197 | gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(gtkDialog), color: &gdkColor); |
198 | return QColor::fromRgbF(r: gdkColor.red, g: gdkColor.green, b: gdkColor.blue, a: gdkColor.alpha); |
199 | } |
200 | |
201 | void QGtk3ColorDialogHelper::onColorChanged(QGtk3ColorDialogHelper *dialog) |
202 | { |
203 | emit dialog->currentColorChanged(color: dialog->currentColor()); |
204 | } |
205 | |
206 | void QGtk3ColorDialogHelper::applyOptions() |
207 | { |
208 | GtkDialog *gtkDialog = d->gtkDialog(); |
209 | gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(options()->windowTitle())); |
210 | |
211 | gtk_color_chooser_set_use_alpha(GTK_COLOR_CHOOSER(gtkDialog), use_alpha: options()->testOption(option: QColorDialogOptions::ShowAlphaChannel)); |
212 | } |
213 | |
214 | QGtk3FileDialogHelper::QGtk3FileDialogHelper() |
215 | { |
216 | d.reset(other: new QGtk3Dialog(gtk_file_chooser_dialog_new(title: "" , parent: nullptr, |
217 | action: GTK_FILE_CHOOSER_ACTION_OPEN, |
218 | qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Cancel)), GTK_RESPONSE_CANCEL, |
219 | qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Ok)), GTK_RESPONSE_OK, |
220 | NULL), this)); |
221 | |
222 | g_signal_connect(GTK_FILE_CHOOSER(d->gtkDialog()), "selection-changed" , G_CALLBACK(onSelectionChanged), this); |
223 | g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "current-folder-changed" , G_CALLBACK(onCurrentFolderChanged), this); |
224 | g_signal_connect_swapped(GTK_FILE_CHOOSER(d->gtkDialog()), "notify::filter" , G_CALLBACK(onFilterChanged), this); |
225 | |
226 | previewWidget = gtk_image_new(); |
227 | g_signal_connect(G_OBJECT(d->gtkDialog()), "update-preview" , G_CALLBACK(onUpdatePreview), this); |
228 | gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(d->gtkDialog()), preview_widget: previewWidget); |
229 | } |
230 | |
231 | QGtk3FileDialogHelper::~QGtk3FileDialogHelper() |
232 | { |
233 | } |
234 | |
235 | bool QGtk3FileDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) |
236 | { |
237 | _dir.clear(); |
238 | _selection.clear(); |
239 | |
240 | applyOptions(); |
241 | return d->show(flags, modality, parent); |
242 | } |
243 | |
244 | void QGtk3FileDialogHelper::exec() |
245 | { |
246 | d->exec(); |
247 | } |
248 | |
249 | void QGtk3FileDialogHelper::hide() |
250 | { |
251 | // After GtkFileChooserDialog has been hidden, gtk_file_chooser_get_current_folder() |
252 | // & gtk_file_chooser_get_filenames() will return bogus values -> cache the actual |
253 | // values before hiding the dialog |
254 | _dir = directory(); |
255 | _selection = selectedFiles(); |
256 | |
257 | d->hide(); |
258 | } |
259 | |
260 | bool QGtk3FileDialogHelper::defaultNameFilterDisables() const |
261 | { |
262 | return false; |
263 | } |
264 | |
265 | void QGtk3FileDialogHelper::setDirectory(const QUrl &directory) |
266 | { |
267 | GtkDialog *gtkDialog = d->gtkDialog(); |
268 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(directory.toLocalFile())); |
269 | } |
270 | |
271 | QUrl QGtk3FileDialogHelper::directory() const |
272 | { |
273 | // While GtkFileChooserDialog is hidden, gtk_file_chooser_get_current_folder() |
274 | // returns a bogus value -> return the cached value before hiding |
275 | if (!_dir.isEmpty()) |
276 | return _dir; |
277 | |
278 | QString ret; |
279 | GtkDialog *gtkDialog = d->gtkDialog(); |
280 | gchar *folder = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(gtkDialog)); |
281 | if (folder) { |
282 | ret = QString::fromUtf8(utf8: folder); |
283 | g_free(mem: folder); |
284 | } |
285 | return QUrl::fromLocalFile(localfile: ret); |
286 | } |
287 | |
288 | void QGtk3FileDialogHelper::selectFile(const QUrl &filename) |
289 | { |
290 | setFileChooserAction(); |
291 | selectFileInternal(filename); |
292 | } |
293 | |
294 | void QGtk3FileDialogHelper::selectFileInternal(const QUrl &filename) |
295 | { |
296 | GtkDialog *gtkDialog = d->gtkDialog(); |
297 | if (options()->acceptMode() == QFileDialogOptions::AcceptSave) { |
298 | QFileInfo fi(filename.toLocalFile()); |
299 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(fi.path())); |
300 | gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(fi.fileName())); |
301 | } else { |
302 | gtk_file_chooser_select_filename(GTK_FILE_CHOOSER(gtkDialog), qUtf8Printable(filename.toLocalFile())); |
303 | } |
304 | } |
305 | |
306 | QList<QUrl> QGtk3FileDialogHelper::selectedFiles() const |
307 | { |
308 | // While GtkFileChooserDialog is hidden, gtk_file_chooser_get_filenames() |
309 | // returns a bogus value -> return the cached value before hiding |
310 | if (!_selection.isEmpty()) |
311 | return _selection; |
312 | |
313 | QList<QUrl> selection; |
314 | GtkDialog *gtkDialog = d->gtkDialog(); |
315 | GSList *filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(gtkDialog)); |
316 | for (GSList *it = filenames; it; it = it->next) |
317 | selection += QUrl::fromLocalFile(localfile: QString::fromUtf8(utf8: (const char*)it->data)); |
318 | g_slist_free(list: filenames); |
319 | return selection; |
320 | } |
321 | |
322 | void QGtk3FileDialogHelper::setFilter() |
323 | { |
324 | applyOptions(); |
325 | } |
326 | |
327 | void QGtk3FileDialogHelper::selectNameFilter(const QString &filter) |
328 | { |
329 | GtkFileFilter *gtkFilter = _filters.value(key: filter); |
330 | if (gtkFilter) { |
331 | GtkDialog *gtkDialog = d->gtkDialog(); |
332 | gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(gtkDialog), filter: gtkFilter); |
333 | } |
334 | } |
335 | |
336 | QString QGtk3FileDialogHelper::selectedNameFilter() const |
337 | { |
338 | GtkDialog *gtkDialog = d->gtkDialog(); |
339 | GtkFileFilter *gtkFilter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(gtkDialog)); |
340 | return _filterNames.value(key: gtkFilter); |
341 | } |
342 | |
343 | void QGtk3FileDialogHelper::onSelectionChanged(GtkDialog *gtkDialog, QGtk3FileDialogHelper *helper) |
344 | { |
345 | QString selection; |
346 | gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(gtkDialog)); |
347 | if (filename) { |
348 | selection = QString::fromUtf8(utf8: filename); |
349 | g_free(mem: filename); |
350 | } |
351 | emit helper->currentChanged(path: QUrl::fromLocalFile(localfile: selection)); |
352 | } |
353 | |
354 | void QGtk3FileDialogHelper::onCurrentFolderChanged(QGtk3FileDialogHelper *dialog) |
355 | { |
356 | emit dialog->directoryEntered(directory: dialog->directory()); |
357 | } |
358 | |
359 | void QGtk3FileDialogHelper::onFilterChanged(QGtk3FileDialogHelper *dialog) |
360 | { |
361 | emit dialog->filterSelected(filter: dialog->selectedNameFilter()); |
362 | } |
363 | |
364 | void QGtk3FileDialogHelper::onUpdatePreview(GtkDialog *gtkDialog, QGtk3FileDialogHelper *helper) |
365 | { |
366 | gchar *filename = gtk_file_chooser_get_preview_filename(GTK_FILE_CHOOSER(gtkDialog)); |
367 | if (!filename) { |
368 | gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), active: false); |
369 | return; |
370 | } |
371 | |
372 | // Don't attempt to open anything which isn't a regular file. If a named pipe, |
373 | // this may hang. |
374 | QFileInfo fileinfo(filename); |
375 | if (!fileinfo.exists() || !fileinfo.isFile()) { |
376 | g_free(mem: filename); |
377 | gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), active: false); |
378 | return; |
379 | } |
380 | |
381 | // This will preserve the image's aspect ratio. |
382 | GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, PREVIEW_WIDTH, PREVIEW_HEIGHT, error: 0); |
383 | g_free(mem: filename); |
384 | if (pixbuf) { |
385 | gtk_image_set_from_pixbuf(GTK_IMAGE(helper->previewWidget), pixbuf); |
386 | g_object_unref(object: pixbuf); |
387 | } |
388 | gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(gtkDialog), active: pixbuf ? true : false); |
389 | } |
390 | |
391 | static GtkFileChooserAction gtkFileChooserAction(const QSharedPointer<QFileDialogOptions> &options) |
392 | { |
393 | switch (options->fileMode()) { |
394 | case QFileDialogOptions::AnyFile: |
395 | case QFileDialogOptions::ExistingFile: |
396 | case QFileDialogOptions::ExistingFiles: |
397 | if (options->acceptMode() == QFileDialogOptions::AcceptOpen) |
398 | return GTK_FILE_CHOOSER_ACTION_OPEN; |
399 | else |
400 | return GTK_FILE_CHOOSER_ACTION_SAVE; |
401 | case QFileDialogOptions::Directory: |
402 | case QFileDialogOptions::DirectoryOnly: |
403 | default: |
404 | if (options->acceptMode() == QFileDialogOptions::AcceptOpen) |
405 | return GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER; |
406 | else |
407 | return GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER; |
408 | } |
409 | } |
410 | |
411 | void QGtk3FileDialogHelper::setFileChooserAction() |
412 | { |
413 | GtkDialog *gtkDialog = d->gtkDialog(); |
414 | |
415 | const GtkFileChooserAction action = gtkFileChooserAction(options: options()); |
416 | gtk_file_chooser_set_action(GTK_FILE_CHOOSER(gtkDialog), action); |
417 | } |
418 | |
419 | void QGtk3FileDialogHelper::applyOptions() |
420 | { |
421 | GtkDialog *gtkDialog = d->gtkDialog(); |
422 | const QSharedPointer<QFileDialogOptions> &opts = options(); |
423 | |
424 | gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(opts->windowTitle())); |
425 | gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(gtkDialog), local_only: true); |
426 | |
427 | setFileChooserAction(); |
428 | |
429 | const bool selectMultiple = opts->fileMode() == QFileDialogOptions::ExistingFiles; |
430 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(gtkDialog), select_multiple: selectMultiple); |
431 | |
432 | const bool confirmOverwrite = !opts->testOption(option: QFileDialogOptions::DontConfirmOverwrite); |
433 | gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(gtkDialog), do_overwrite_confirmation: confirmOverwrite); |
434 | |
435 | const bool readOnly = opts->testOption(option: QFileDialogOptions::ReadOnly); |
436 | gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(gtkDialog), create_folders: !readOnly); |
437 | |
438 | const QStringList nameFilters = opts->nameFilters(); |
439 | if (!nameFilters.isEmpty()) |
440 | setNameFilters(nameFilters); |
441 | |
442 | if (opts->initialDirectory().isLocalFile()) |
443 | setDirectory(opts->initialDirectory()); |
444 | |
445 | foreach (const QUrl &filename, opts->initiallySelectedFiles()) |
446 | selectFileInternal(filename); |
447 | |
448 | const QString initialNameFilter = opts->initiallySelectedNameFilter(); |
449 | if (!initialNameFilter.isEmpty()) |
450 | selectNameFilter(filter: initialNameFilter); |
451 | |
452 | GtkWidget *acceptButton = gtk_dialog_get_widget_for_response(dialog: gtkDialog, response_id: GTK_RESPONSE_OK); |
453 | if (acceptButton) { |
454 | if (opts->isLabelExplicitlySet(label: QFileDialogOptions::Accept)) |
455 | gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(opts->labelText(QFileDialogOptions::Accept))); |
456 | else if (opts->acceptMode() == QFileDialogOptions::AcceptOpen) |
457 | gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Open))); |
458 | else |
459 | gtk_button_set_label(GTK_BUTTON(acceptButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Save))); |
460 | } |
461 | |
462 | GtkWidget *rejectButton = gtk_dialog_get_widget_for_response(dialog: gtkDialog, response_id: GTK_RESPONSE_CANCEL); |
463 | if (rejectButton) { |
464 | if (opts->isLabelExplicitlySet(label: QFileDialogOptions::Reject)) |
465 | gtk_button_set_label(GTK_BUTTON(rejectButton), qUtf8Printable(opts->labelText(QFileDialogOptions::Reject))); |
466 | else |
467 | gtk_button_set_label(GTK_BUTTON(rejectButton), qUtf8Printable(QGtk3Theme::defaultStandardButtonText(QPlatformDialogHelper::Cancel))); |
468 | } |
469 | } |
470 | |
471 | void QGtk3FileDialogHelper::setNameFilters(const QStringList &filters) |
472 | { |
473 | GtkDialog *gtkDialog = d->gtkDialog(); |
474 | foreach (GtkFileFilter *filter, _filters) |
475 | gtk_file_chooser_remove_filter(GTK_FILE_CHOOSER(gtkDialog), filter); |
476 | |
477 | _filters.clear(); |
478 | _filterNames.clear(); |
479 | |
480 | foreach (const QString &filter, filters) { |
481 | GtkFileFilter *gtkFilter = gtk_file_filter_new(); |
482 | const QString name = filter.left(n: filter.indexOf(ch: u'(')); |
483 | const QStringList extensions = cleanFilterList(filter); |
484 | |
485 | gtk_file_filter_set_name(filter: gtkFilter, qUtf8Printable(name.isEmpty() ? extensions.join(", "_L1 ) : name)); |
486 | foreach (const QString &ext, extensions) |
487 | gtk_file_filter_add_pattern(filter: gtkFilter, qUtf8Printable(ext)); |
488 | |
489 | gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDialog), filter: gtkFilter); |
490 | |
491 | _filters.insert(key: filter, value: gtkFilter); |
492 | _filterNames.insert(key: gtkFilter, value: filter); |
493 | } |
494 | } |
495 | |
496 | QGtk3FontDialogHelper::QGtk3FontDialogHelper() |
497 | { |
498 | d.reset(other: new QGtk3Dialog(gtk_font_chooser_dialog_new(title: "" , parent: nullptr), this)); |
499 | g_signal_connect_swapped(d->gtkDialog(), "notify::font" , G_CALLBACK(onFontChanged), this); |
500 | } |
501 | |
502 | QGtk3FontDialogHelper::~QGtk3FontDialogHelper() |
503 | { |
504 | } |
505 | |
506 | bool QGtk3FontDialogHelper::show(Qt::WindowFlags flags, Qt::WindowModality modality, QWindow *parent) |
507 | { |
508 | applyOptions(); |
509 | return d->show(flags, modality, parent); |
510 | } |
511 | |
512 | void QGtk3FontDialogHelper::exec() |
513 | { |
514 | d->exec(); |
515 | } |
516 | |
517 | void QGtk3FontDialogHelper::hide() |
518 | { |
519 | d->hide(); |
520 | } |
521 | |
522 | static QString qt_fontToString(const QFont &font) |
523 | { |
524 | PangoFontDescription *desc = pango_font_description_new(); |
525 | pango_font_description_set_size(desc, size: (font.pointSizeF() > 0.0 ? font.pointSizeF() : QFontInfo(font).pointSizeF()) * PANGO_SCALE); |
526 | pango_font_description_set_family(desc, qUtf8Printable(QFontInfo(font).family())); |
527 | |
528 | int weight = font.weight(); |
529 | if (weight >= QFont::Black) |
530 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_HEAVY); |
531 | else if (weight >= QFont::ExtraBold) |
532 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_ULTRABOLD); |
533 | else if (weight >= QFont::Bold) |
534 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_BOLD); |
535 | else if (weight >= QFont::DemiBold) |
536 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_SEMIBOLD); |
537 | else if (weight >= QFont::Medium) |
538 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_MEDIUM); |
539 | else if (weight >= QFont::Normal) |
540 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_NORMAL); |
541 | else if (weight >= QFont::Light) |
542 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_LIGHT); |
543 | else if (weight >= QFont::ExtraLight) |
544 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_ULTRALIGHT); |
545 | else |
546 | pango_font_description_set_weight(desc, weight: PANGO_WEIGHT_THIN); |
547 | |
548 | int style = font.style(); |
549 | if (style == QFont::StyleItalic) |
550 | pango_font_description_set_style(desc, style: PANGO_STYLE_ITALIC); |
551 | else if (style == QFont::StyleOblique) |
552 | pango_font_description_set_style(desc, style: PANGO_STYLE_OBLIQUE); |
553 | else |
554 | pango_font_description_set_style(desc, style: PANGO_STYLE_NORMAL); |
555 | |
556 | char *str = pango_font_description_to_string(desc); |
557 | QString name = QString::fromUtf8(utf8: str); |
558 | pango_font_description_free(desc); |
559 | g_free(mem: str); |
560 | return name; |
561 | } |
562 | |
563 | static QFont qt_fontFromString(const QString &name) |
564 | { |
565 | QFont font; |
566 | PangoFontDescription *desc = pango_font_description_from_string(qUtf8Printable(name)); |
567 | font.setPointSizeF(static_cast<float>(pango_font_description_get_size(desc)) / PANGO_SCALE); |
568 | |
569 | QString family = QString::fromUtf8(utf8: pango_font_description_get_family(desc)); |
570 | if (!family.isEmpty()) |
571 | font.setFamilies(QStringList{family}); |
572 | |
573 | font.setWeight(QFont::Weight(pango_font_description_get_weight(desc))); |
574 | |
575 | PangoStyle style = pango_font_description_get_style(desc); |
576 | if (style == PANGO_STYLE_ITALIC) |
577 | font.setStyle(QFont::StyleItalic); |
578 | else if (style == PANGO_STYLE_OBLIQUE) |
579 | font.setStyle(QFont::StyleOblique); |
580 | else |
581 | font.setStyle(QFont::StyleNormal); |
582 | |
583 | pango_font_description_free(desc); |
584 | return font; |
585 | } |
586 | |
587 | void QGtk3FontDialogHelper::setCurrentFont(const QFont &font) |
588 | { |
589 | GtkFontChooser *gtkDialog = GTK_FONT_CHOOSER(d->gtkDialog()); |
590 | gtk_font_chooser_set_font(fontchooser: gtkDialog, qUtf8Printable(qt_fontToString(font))); |
591 | } |
592 | |
593 | QFont QGtk3FontDialogHelper::currentFont() const |
594 | { |
595 | GtkFontChooser *gtkDialog = GTK_FONT_CHOOSER(d->gtkDialog()); |
596 | gchar *name = gtk_font_chooser_get_font(fontchooser: gtkDialog); |
597 | QFont font = qt_fontFromString(name: QString::fromUtf8(utf8: name)); |
598 | g_free(mem: name); |
599 | return font; |
600 | } |
601 | |
602 | void QGtk3FontDialogHelper::onFontChanged(QGtk3FontDialogHelper *dialog) |
603 | { |
604 | emit dialog->currentFontChanged(font: dialog->currentFont()); |
605 | } |
606 | |
607 | void QGtk3FontDialogHelper::applyOptions() |
608 | { |
609 | GtkDialog *gtkDialog = d->gtkDialog(); |
610 | const QSharedPointer<QFontDialogOptions> &opts = options(); |
611 | |
612 | gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(opts->windowTitle())); |
613 | } |
614 | |
615 | QT_END_NAMESPACE |
616 | |
617 | #include "moc_qgtk3dialoghelpers.cpp" |
618 | |