File indexing completed on 2025-02-02 14:20:02
0001 /* 0002 SPDX-FileCopyrightText: 1997 Bernd Johannes Wuebben <wuebben@kde.org> 0003 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org> 0004 SPDX-FileCopyrightText: 1999 Mario Weilguni <mweilguni@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 #ifndef K_FONT_CHOOSER_DIALOG_H 0009 #define K_FONT_CHOOSER_DIALOG_H 0010 0011 #include <QDialog> 0012 #include <QStringList> 0013 0014 #include <kfontchooser.h> 0015 0016 #include <memory> 0017 0018 class QFont; 0019 0020 class KFontChooserDialogPrivate; 0021 0022 /** 0023 * @class KFontChooserDialog kfontchooserdialog.h KFontChooserDialog 0024 * 0025 * @short A font selection dialog. 0026 * 0027 * The KFontChooserDialog provides a dialog for interactive font selection. 0028 * It is basically a thin wrapper around the KFontChooser widget (the latter 0029 * can also be used standalone). In most cases, the simplest use of this 0030 * class is the static method KFontChooserDialog::getFont(), which shows 0031 * the dialog, allows the user to select a font, and returns when the 0032 * dialog is closed. 0033 * 0034 * Features offered by KFontChooserDialog/KFontChooser: 0035 * - The ability to set decimal font sizes (e.g. "12.1") 0036 * - When selecting an initial font, if the styleName property of that font 0037 * isn't set, the dialog will try and select the correct font style from the 0038 * styles list 0039 * - The ability to set multiple fonts at once, for an example of this functionality 0040 * see "Adjust All Fonts" in the Fonts KCM in Systemsettings; and you can change 0041 * the font family, style or size separately 0042 * - Discarding the styleName property when closing the dialog for "Regular" font 0043 * styles, since it doesn't make sense to set that property for such fonts and 0044 * this allows setBold(true) to work correctly for more details see: 0045 * https://bugreports.qt.io/browse/QTBUG-63792 0046 * https://bugs.kde.org/show_bug.cgi?id=378523 0047 * 0048 * Example, using the static getFont() method: 0049 * 0050 * \code 0051 * QFont myFont; 0052 * int result = KFontChooserDialog::getFont(myFont); 0053 * if (result == QDialog::Accepted) { 0054 * ... 0055 * } 0056 * \endcode 0057 * 0058 * Another example, this time showing the dialog with show() (or open()): 0059 * \code 0060 * KFontChooserDialog *fontDlg = new KFontChooserDialog(KFontChooser::NoDisplayFlags, this); 0061 * // Delete the dialog when it's closed 0062 * fontDlg->setAttribute(Qt::WA_DeleteOnClose); 0063 * 0064 * connect(fontDlg, &QDialog::accepted, this, [fontDlg]() { 0065 * // Get the selected font via fontDlg->font() and apply it, save it to 0066 * // the settings... etc. 0067 * }); 0068 * 0069 * fontDlg->show(); 0070 * \endcode 0071 * 0072 * \image html kfontchooserdialog.png "KFontChooserDialog" 0073 * 0074 * @author Preston Brown <pbrown@kde.org>, Bernd Wuebben <wuebben@kde.org> 0075 * 0076 * @since 5.69 0077 */ 0078 class KWIDGETSADDONS_EXPORT KFontChooserDialog : public QDialog 0079 { 0080 Q_OBJECT 0081 0082 public: 0083 /** 0084 * Constructs a font selection dialog. 0085 * 0086 * @param flags flags to define how the font chooser is displayed 0087 * @param parent parent widget of the dialog, if any, the dialog will be centered relative to it 0088 */ 0089 explicit KFontChooserDialog(const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, QWidget *parent = nullptr); 0090 0091 ~KFontChooserDialog() override; 0092 0093 /** 0094 * Sets the currently selected font in the dialog. 0095 * 0096 * @param font the font to select 0097 * @param onlyFixed if @c true, the font list will show only fixed width (monospace) 0098 * fonts, otherwise all available fonts are shown 0099 */ 0100 void setFont(const QFont &font, bool onlyFixed = false); 0101 0102 /** 0103 * @return the currently selected font in the dialog 0104 */ 0105 QFont font() const; 0106 0107 /** 0108 * Creates a modal font dialog, lets the user choose a font, and returns when 0109 * the dialog is closed. 0110 * 0111 * @param theFont a reference to the font to write the chosen font into 0112 * @param flags flags to define how the font chooser is displayed 0113 * @param parent parent widget of the dialog, if any, the dialog will be centered relative to it 0114 * @return QDialog::result() 0115 */ 0116 static int getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, QWidget *parent = nullptr); 0117 0118 /** 0119 * Creates a modal font difference dialog, lets the user choose a selection 0120 * of changes that should be made to a set of fonts, and returns when the 0121 * dialog is closed. Useful for choosing slight adjustments to the font set 0122 * when the user would otherwise have to manually edit a number of fonts. 0123 * 0124 * @param theFont a reference to the font to write the chosen font into 0125 * @param flags flags to define how the font chooser is displayed 0126 * @param diffFlags a reference to the integer bitmask into which the chosen 0127 * difference selection bitmask should be written. 0128 * Check the bitmask afterwards like: 0129 * \code 0130 * if ( diffFlags & KFontChooser::FontDiffFamily ) { 0131 * [...] 0132 * } 0133 * if ( diffFlags & KFontChooser::FontDiffStyle ) { 0134 * [...] 0135 * } 0136 * if ( diffFlags & KFontChooser::FontDiffSize ) { 0137 * [...] 0138 * } 0139 * \endcode 0140 * @param parent parent widget of the dialog, if any, the dialog will be centered relative to it 0141 * 0142 * @returns QDialog::result() 0143 */ 0144 static int getFontDiff(QFont &theFont, 0145 KFontChooser::FontDiffFlags &diffFlags, 0146 const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, 0147 QWidget *parent = nullptr); 0148 0149 Q_SIGNALS: 0150 /** 0151 * Emitted whenever the currently selected font changes. 0152 * Connect to this to monitor the font as it is selected if you are 0153 * not running modal. 0154 */ 0155 void fontSelected(const QFont &font); 0156 0157 private: 0158 std::unique_ptr<KFontChooserDialogPrivate> const d; 0159 0160 Q_DISABLE_COPY(KFontChooserDialog) 0161 }; 0162 0163 #endif