File indexing completed on 2024-04-28 03:59:05
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_H 0009 #define K_FONT_CHOOSER_H 0010 0011 #include <QStringList> 0012 #include <QWidget> 0013 #include <kwidgetsaddons_export.h> 0014 #include <memory> 0015 0016 class QFont; 0017 0018 /** 0019 * @class KFontChooser kfontchooser.h KFontChooser 0020 * 0021 * @short A font selection widget. 0022 * 0023 * While KFontChooser as an ordinary widget can be embedded in 0024 * custom dialogs and therefore is very flexible, in most cases 0025 * it is preferable to use the convenience functions in 0026 * QFontDialog. 0027 * 0028 * \image html kfontchooser.png "KFontChooser Widget" 0029 * 0030 * @see KFontRequester 0031 * 0032 * @author Preston Brown <pbrown@kde.org>, Bernd Wuebben <wuebben@kde.org> 0033 */ 0034 class KWIDGETSADDONS_EXPORT KFontChooser : public QWidget 0035 { 0036 Q_OBJECT 0037 Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontSelected USER true) 0038 Q_PROPERTY(QColor color READ color WRITE setColor) 0039 Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) 0040 Q_PROPERTY(QString sampleText READ sampleText WRITE setSampleText) 0041 0042 public: 0043 /** 0044 * Displayed columns. 0045 */ 0046 enum FontColumn { 0047 FamilyList = 0x01, ///< Identifies the family (leftmost) list. 0048 StyleList = 0x02, ///< Identifies the style (center) list. 0049 SizeList = 0x04, ///< Identifies the size (rightmost) list. 0050 }; 0051 0052 /** 0053 * Flags for selecting which font attributes to change 0054 * @see FontDiffFlags 0055 */ 0056 enum FontDiff { 0057 NoFontDiffFlags = 0, ///< No flags set 0058 FontDiffFamily = 1, ///< Identifies a requested change in the font family. 0059 FontDiffStyle = 2, ///< Identifies a requested change in the font style. 0060 FontDiffSize = 4, ///< Identifies a requested change in the font size. 0061 AllFontDiffs = FontDiffFamily | FontDiffStyle | FontDiffSize, 0062 }; 0063 /** 0064 * Stores an combination of #FontDiff values. 0065 */ 0066 Q_DECLARE_FLAGS(FontDiffFlags, FontDiff) 0067 0068 /** 0069 * Flags for selecting what is displayed in the widget. 0070 * @see DisplayFlags 0071 */ 0072 enum DisplayFlag { 0073 NoDisplayFlags = 0, ///< No flags set 0074 FixedFontsOnly = 1, ///< Only show monospaced/fixed-width fonts, excluding proportional fonts, (the 0075 ///< checkbox to toggle showing only monospaced fonts is not shown in this case) 0076 DisplayFrame = 2, ///< Show a visual frame around the chooser 0077 ShowDifferences = 4, ///< Display the font differences interfaces 0078 }; 0079 /** 0080 * Stores a combination of #DisplayFlag values. 0081 */ 0082 Q_DECLARE_FLAGS(DisplayFlags, DisplayFlag) 0083 0084 /** 0085 * Constructs a font picker widget. 0086 * 0087 * @param parent the parent widget 0088 * 0089 * @since 5.86 0090 */ 0091 explicit KFontChooser(QWidget *parent = nullptr); 0092 0093 /** 0094 * Create a font picker widget. 0095 * 0096 * @param flags a combination of OR-ed values from the @c KFontChooser::DisplayFlags enum, 0097 * the default is @c DisplayFonts::NoDisplayFlags 0098 * @param parent the parent widget, if not nullptr the windowing system will use it to position 0099 * the chooser widget relative to it 0100 * 0101 * @since 5.86 0102 */ 0103 explicit KFontChooser(DisplayFlags flags, QWidget *parent = nullptr); 0104 0105 /** 0106 * Destructor. 0107 */ 0108 ~KFontChooser() override; 0109 0110 /** 0111 * Enables or disables a column (family, style, size) in the widget. 0112 * 0113 * Use this function if your application does not need or support all font properties. 0114 * 0115 * @param column specify the column(s) to enable/disable, an OR-ed combination of 0116 * @c KFontChooser::FontColumn enum values 0117 * @param state if @p false the columns are disabled, and vice-versa 0118 */ 0119 void enableColumn(int column, bool state); 0120 0121 /** 0122 * Sets the currently selected font in the widget. 0123 * 0124 * @param font the font to select 0125 * @param onlyFixed if @c true, the font list will only display fixed-width fonts, 0126 * otherwise all fonts are displayed. The default is @c false. 0127 */ 0128 void setFont(const QFont &font, bool onlyFixed = false); 0129 0130 /** 0131 * Returns the bitmask corresponding to the attributes the user wishes to change. 0132 */ 0133 FontDiffFlags fontDiffFlags() const; 0134 0135 /** 0136 * Returns the currently selected font in the chooser. 0137 */ 0138 QFont font() const; 0139 0140 /** 0141 * Sets the color to use for the font in the preview area. 0142 */ 0143 void setColor(const QColor &col); 0144 0145 /** 0146 * Returns the color currently used for the font in the preview 0147 * area (default: the text color of the active color group). 0148 */ 0149 QColor color() const; 0150 0151 /** 0152 * Sets the background color to use in the preview area. 0153 */ 0154 void setBackgroundColor(const QColor &col); 0155 0156 /** 0157 * Returns the background color currently used in the preview area 0158 * (default: the base color of the active colorgroup) 0159 */ 0160 QColor backgroundColor() const; 0161 0162 /** 0163 * @return The current text in the sample text input area. 0164 */ 0165 QString sampleText() const; 0166 0167 /** 0168 * Sets the sample text in the preview area; this is useful if you 0169 * want to use text in your native language. 0170 * 0171 * @param text the new sample text (it will replace the current text) 0172 */ 0173 void setSampleText(const QString &text); 0174 0175 /** 0176 * If @p visible is @c true the preview area will be shown, and vice-versa 0177 * is it's @c false. 0178 */ 0179 void setSampleBoxVisible(bool visible); 0180 0181 /** 0182 * The selection criteria for the font families shown in the dialog. 0183 */ 0184 enum FontListCriteria { 0185 /** 0186 * If set, only show fixed fixed-width (monospace) fonts. 0187 */ 0188 FixedWidthFonts = 0x01, 0189 /** 0190 * If set, only show scalable fonts. 0191 * Certain configurations allow bitmap fonts to remain unscaled 0192 * and thus these fonts have limited number of sizes. 0193 */ 0194 ScalableFonts = 0x02, 0195 /** 0196 * If set, only show smooth scalable fonts. 0197 * This will return only non-bitmap fonts which are scalable to any size requested. 0198 * Setting this option means the @c ScalableFonts flag is ignored. 0199 */ 0200 SmoothScalableFonts = 0x04 0201 }; 0202 0203 /** 0204 * Returns a list of font faimly name strings filtered based on @p fontListCriteria. 0205 * 0206 * @param fontListCriteria specifies the criteria used to select fonts to add to 0207 * the list, a combination of OR-ed values from @ref KFontChooser::FontListCriteria 0208 * 0209 * @since 5.86 0210 */ 0211 static QStringList createFontList(uint fontListCriteria); 0212 0213 /** 0214 * Uses @p fontList to fill the font family list in the widget. 0215 * 0216 * You can create a custom list of fonts using the static @c createFontList(uint 0217 * criteria) to only include fonts that meet certain criteria (e.g. only 0218 * smooth-scalable fonts). 0219 * 0220 * @see KFontChooser::createFontList(uint), KFontChooser::FontListCriteria 0221 * 0222 * Note that if @p fontList is empty, the font list in the chooser will show 0223 * all the available fonts on the system. 0224 * @since 5.86 0225 */ 0226 void setFontListItems(const QStringList &fontList); 0227 0228 /** 0229 * Sets the minimum number of items that should be visible in the 0230 * child list widgets; this number will be used to compute and set 0231 * the minimum heights for those widgets. 0232 * 0233 * @since 5.86 0234 */ 0235 void setMinVisibleItems(int visibleItems); 0236 0237 /** 0238 * Reimplemented for internal reasons. 0239 */ 0240 QSize sizeHint(void) const override; 0241 0242 Q_SIGNALS: 0243 /** 0244 * Emitted when the selected font changes. 0245 */ 0246 void fontSelected(const QFont &font); 0247 0248 private: 0249 std::unique_ptr<class KFontChooserPrivate> const d; 0250 0251 Q_DISABLE_COPY(KFontChooser) 0252 }; 0253 0254 Q_DECLARE_OPERATORS_FOR_FLAGS(KFontChooser::DisplayFlags) 0255 0256 #endif