[KFontRequester] Port from QFontDialog to KFontChooserDialog
ClosedPublic

Authored by ahmadsamir on Apr 12 2020, 2:50 PM.

Details

Test Plan

The kfontrequestertest app still works

Diff Detail

Repository
R236 KWidgetsAddons
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
ahmadsamir created this revision.Apr 12 2020, 2:50 PM
Restricted Application added a project: Frameworks. · View Herald TranscriptApr 12 2020, 2:50 PM
ahmadsamir requested review of this revision.Apr 12 2020, 2:50 PM
dfaure accepted this revision.Apr 13 2020, 10:16 AM
dfaure added inline comments.
src/kfontrequester.cpp
188

It's really not obvious from the above that this modifies m_selFont.

But I see QFont has no isValid() so we can't adopt an API like QColorDialog::getColor (plus KFontChooserDialog has been released now).

Oh well.

This revision is now accepted and ready to land.Apr 13 2020, 10:16 AM
This revision was automatically updated to reflect the committed changes.
ahmadsamir added inline comments.Apr 13 2020, 1:03 PM
src/kfontrequester.cpp
188

Plus we can't end up with a non-valid QFont, the dialog is filled from QFontDataBase; so unless the user went and uninstalled the font while the dialog was open, it should be OK. Also, font substitution will take care of a font-gone-MIA (I am sure of that on Linux, and most likely other OS's have similar solutions).

I was curious so I looked at QColorDialog::getColor() and it looks like they changed it (or you meant getRgba?).

ahmadsamir added inline comments.Apr 13 2020, 1:04 PM
src/kfontrequester.cpp
188

And yes, it's not obvious that m_selFont is modified, I had to go read the API docs KFontChooser multiple times... but I got used to it.

I mean, the user can press cancel, that's when QColorDialog returns an invalid color.
If there was a concept like an invalid font we could do the same here, but there's no such concept in QFont.
So yep, no better way.

I mean, the user can press cancel, that's when QColorDialog returns an invalid color.
If there was a concept like an invalid font we could do the same here, but there's no such concept in QFont.
So yep, no better way.

Looking at the code, if the user presses cancel, "theFont" is returned unchanged; seems pretty good to me:

int KFontChooserDialog::getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags, QWidget *parent)
{
    KFontChooserDialog dlg(flags, parent);
    dlg.setObjectName(QStringLiteral("Font Selector"));
    dlg.setFont(theFont, flags & KFontChooser::FixedFontsOnly);

    const int result = dlg.exec();
    if (result == Accepted) {
        theFont = dlg.d->chooser->font();
        stripRegularStyleName(theFont);
    }
    return result;
}

QFontDialog::getFont() returns a QFont, and one may pass a bool * to check the dialog result code:
QFont QFontDialog::getFont(bool *ok, const QFont &initial, QWidget *parent, const QString &title, FontDialogOptions options)

which is standard Qt behaviour from other dialogs/widgets that handle user yes/no use cases, IIRC.

The KFontChooer* approach is more "economical", the "initial" font and the font you get from the dialog if you press OK, are the same object, and the function returns the dialog result code. As some wise old builder once said "there's no right or wrong way to lay bricks, as long as it works".

volkov added a subscriber: volkov.Jul 22 2020, 3:54 PM

Why not add support for QPlatformTheme::FontDialog in KdePlatformTheme::createPlatformDialogHelper()?

Why not add support for QPlatformTheme::FontDialog in KdePlatformTheme::createPlatformDialogHelper()?

Sorry about the late reply; I had looked into the platformtheme integration stuff, but at the time I didn't think it was a viable solution (nor was it easy for me to port); see https://phabricator.kde.org/D27808 for more details.