implement QFontDialog options similar to the QFontComboBox filters

so that QFontDialog can replace (functionality wise) KFontDialog and
KFontChooser

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-31 19:00:40 +03:00
parent fe072de82a
commit 97fea30784
3 changed files with 40 additions and 19 deletions

View file

@ -207,9 +207,6 @@ void QFontDialogPrivate::init()
sampleEdit->setText(QLatin1String("AaBbYyZz"));
hbox->addWidget(sampleEdit);
size = 0;
smoothScalable = false;
QObject::connect(familyList, SIGNAL(highlighted(int)), q, SLOT(_q_familyHighlighted(int)));
QObject::connect(styleList, SIGNAL(highlighted(int)), q, SLOT(_q_styleHighlighted(int)));
QObject::connect(sizeList, SIGNAL(highlighted(int)), q, SLOT(_q_sizeHighlighted(int)));
@ -419,6 +416,22 @@ void QFontDialogPrivate::updateFamilies()
QStringList familyNames = fdb.families();
const bool scalable = (opts & QFontDialog::ScalableFonts);
const bool mono = (opts & QFontDialog::MonospacedFonts);
if (!scalable || !mono) {
QMutableListIterator<QString> familyIter(familyNames);
while (familyIter.hasNext()) {
const QString family = familyIter.next();
if (scalable && !fdb.isScalable(family)) {
familyIter.remove();
continue;
}
if (mono && !fdb.isFixedPitch(family)) {
familyIter.remove();
}
}
}
familyList->model()->setStringList(familyNames);
QString foundryName1, familyName1, foundryName2, familyName2;
@ -464,8 +477,9 @@ void QFontDialogPrivate::updateFamilies()
familyList->setCurrentItem(0);
familyEdit->setText(familyList->currentText());
if (q->style()->styleHint(QStyle::SH_FontDialog_SelectAssociatedText, 0, q)
&& familyList->hasFocus())
&& familyList->hasFocus()) {
familyEdit->selectAll();
}
updateStyles();
}
@ -758,6 +772,9 @@ QFont QFontDialog::selectedFont() const
of a font dialog.
\value NoButtons Don't display \gui{OK} and \gui{Cancel} buttons. (Useful for "live dialogs".)
\value ScalableFonts Show scalable fonts
\value MonospacedFonts Show monospaced fonts
\value AllFonts Show scalable and monospaced fonts
\sa options, setOption(), testOption()
*/
@ -771,8 +788,9 @@ QFont QFontDialog::selectedFont() const
void QFontDialog::setOption(FontDialogOption option, bool on)
{
Q_D(QFontDialog);
if (!(d->opts & option) != !on)
if (!(d->opts & option) != !on) {
setOptions(d->opts ^ option);
}
}
/*!
@ -784,7 +802,7 @@ void QFontDialog::setOption(FontDialogOption option, bool on)
bool QFontDialog::testOption(FontDialogOption option) const
{
Q_D(const QFontDialog);
return (d->opts & option) != 0;
return (d->opts & option);
}
/*!
@ -803,13 +821,12 @@ bool QFontDialog::testOption(FontDialogOption option) const
void QFontDialog::setOptions(FontDialogOptions options)
{
Q_D(QFontDialog);
FontDialogOptions changed = (options ^ d->opts);
if (!changed)
if (d->opts == options) {
return;
}
d->opts = options;
d->buttonBox->setVisible(!(options & NoButtons));
d->updateFamilies();
}
QFontDialog::FontDialogOptions QFontDialog::options() const
@ -879,8 +896,9 @@ void QFontDialog::done(int result)
if (result == Accepted) {
// We check if this is the same font we had before, if so we emit currentFontChanged
QFont selectedFont = currentFont();
if(selectedFont != d->selectedFont)
if (selectedFont != d->selectedFont) {
emit(currentFontChanged(selectedFont));
}
d->selectedFont = selectedFont;
emit fontSelected(d->selectedFont);
} else

View file

@ -44,9 +44,11 @@ class Q_GUI_EXPORT QFontDialog : public QDialog
public:
enum FontDialogOption {
NoButtons = 0x00000001,
NoButtons = 0x01,
ScalableFonts = 0x2,
MonospacedFonts = 0x4,
AllFonts = (ScalableFonts | MonospacedFonts)
};
Q_DECLARE_FLAGS(FontDialogOptions, FontDialogOption)
explicit QFontDialog(QWidget *parent = nullptr);
@ -71,7 +73,7 @@ public:
void open(QObject *receiver, const char *member);
static QFont getFont(bool *ok, const QFont &initial, QWidget *parent = nullptr,
const QString &title = QString(), FontDialogOptions options = 0);
const QString &title = QString(), FontDialogOptions options = AllFonts);
static QFont getFont(bool *ok, QWidget *parent = nullptr);
@ -82,9 +84,6 @@ Q_SIGNALS:
protected:
void changeEvent(QEvent *event);
void done(int result);
private:
// ### Qt 5: make protected
bool eventFilter(QObject *object, QEvent *event);
Q_DISABLE_COPY(QFontDialog)
@ -95,7 +94,6 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_sizeHighlighted(int))
Q_PRIVATE_SLOT(d_func(), void _q_updateSample())
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QFontDialog::FontDialogOptions)
#endif // QT_NO_FONTDIALOG

View file

@ -56,7 +56,12 @@ class QFontDialogPrivate : public QDialogPrivate
Q_DECLARE_PUBLIC(QFontDialog)
public:
inline QFontDialogPrivate() { }
inline QFontDialogPrivate()
: size(0),
smoothScalable(false),
opts(QFontDialog::AllFonts)
{
}
void updateFamilies();
void updateStyles();