File indexing completed on 2024-04-28 15:32:03

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  * It normally comes up with all font families present on the system; the
0029  * getFont method below does allow some more fine-tuning of the selection of fonts
0030  * that will be displayed in the dialog.
0031  * <p>Consider the following code snippet;
0032  * \code
0033  *    QStringList list;
0034  *    KFontChooser::getFontList(list, KFontChooser::SmoothScalableFonts);
0035  *    KFontChooser *chooseFont = new KFontChooser(nullptr, KFontChooser::NoDisplayFlags, list);
0036  * \endcode
0037  * <p>
0038  * The above creates a font chooser dialog with only SmoothScaleble fonts.
0039  * \image html kfontchooser.png "KFontChooser Widget"
0040  *
0041  * @see KFontRequester
0042  *
0043  * @author Preston Brown <pbrown@kde.org>, Bernd Wuebben <wuebben@kde.org>
0044  */
0045 class KWIDGETSADDONS_EXPORT KFontChooser : public QWidget
0046 {
0047     Q_OBJECT
0048     Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontSelected USER true)
0049     Q_PROPERTY(QColor color READ color WRITE setColor)
0050     Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
0051     Q_PROPERTY(QString sampleText READ sampleText WRITE setSampleText)
0052 
0053 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 86)
0054     Q_PROPERTY(Qt::CheckState sizeIsRelative READ sizeIsRelative WRITE setSizeIsRelative)
0055 #endif
0056 
0057 public:
0058     /**
0059      * Displayed columns.
0060      */
0061     enum FontColumn {
0062         FamilyList = 0x01, ///< Identifies the family (leftmost) list.
0063         StyleList = 0x02, ///< Identifies the style (center) list.
0064         SizeList = 0x04, ///< Identifies the size (rightmost) list.
0065     };
0066 
0067     /**
0068      * Flags for selecting which font attributes to change
0069      * @see FontDiffFlags
0070      */
0071     enum FontDiff {
0072         NoFontDiffFlags = 0, ///< No flags set
0073         FontDiffFamily = 1, ///< Identifies a requested change in the font family.
0074         FontDiffStyle = 2, ///< Identifies a requested change in the font style.
0075         FontDiffSize = 4, ///< Identifies a requested change in the font size.
0076         AllFontDiffs = FontDiffFamily | FontDiffStyle | FontDiffSize,
0077     };
0078     /**
0079      * Stores an combination of #FontDiff values.
0080      */
0081     Q_DECLARE_FLAGS(FontDiffFlags, FontDiff)
0082 
0083     /**
0084      * Flags for selecting what is displayed in the widget.
0085      * @see DisplayFlags
0086      */
0087     enum DisplayFlag {
0088         NoDisplayFlags = 0, ///< No flags set
0089         FixedFontsOnly = 1, ///< Only show monospaced/fixed-width fonts, excluding proportional fonts, (the
0090                             ///< checkbox to toggle showing only monospaced fonts is not shown in this case)
0091         DisplayFrame = 2, ///< Show a visual frame around the chooser
0092         ShowDifferences = 4, ///< Display the font differences interfaces
0093     };
0094     /**
0095      * Stores a combination of #DisplayFlag values.
0096      */
0097     Q_DECLARE_FLAGS(DisplayFlags, DisplayFlag)
0098 
0099 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 86)
0100     /**
0101      * Constructs a font picker widget.
0102      *
0103      * @param parent The parent widget.
0104      * @param flags Defines how the font chooser is displayed.
0105      * @param fontList A list of fonts to display, in XLFD format.  If
0106      *        no list is formatted, the internal KDE font list is used.
0107      *        If that has not been created, X is queried, and all fonts
0108      *        available on the system are displayed.
0109      * @param visibleListSize The minimum number of visible entries in the
0110      *        fontlists.
0111      * @param sizeIsRelativeState If not zero the widget will show a
0112      *        checkbox where the user may choose whether the font size
0113      *        is to be interpreted as relative size.
0114      *        Initial state of this checkbox will be set according to
0115      *        *sizeIsRelativeState, user choice may be retrieved by
0116      *        calling sizeIsRelative().
0117      *
0118      * @deprecated since 5.86, use the KFontChooser(KFontChooser::DisplayFlags, QWidget*) constructor
0119      * and the setFontListItems() and setMinVisibleItems() methods.
0120      */
0121     KWIDGETSADDONS_DEPRECATED_VERSION(
0122         5,
0123         86,
0124         "Use the KFontChooser(KFontChooser::DisplayFlags, QWidget*) constructor and the setFontListItems() and setMinVisibleItems() methods.")
0125     explicit KFontChooser(QWidget *parent,
0126                           const DisplayFlags &flags,
0127                           const QStringList &fontList = QStringList(),
0128                           int visibleListSize = 8,
0129                           Qt::CheckState *sizeIsRelativeState = nullptr);
0130 #endif
0131 
0132     /**
0133      * Constructs a font picker widget.
0134      *
0135      * @param parent the parent widget
0136      *
0137      * @since 5.86
0138      */
0139     explicit KFontChooser(QWidget *parent = nullptr);
0140 
0141     /**
0142      * Create a font picker widget.
0143      *
0144      * @param flags a combination of OR-ed values from the @c KFontChooser::DisplayFlags enum,
0145      * the default is @c DisplayFonts::NoDisplayFlags
0146      * @param parent the parent widget, if not nullptr the windowing system will use it to position
0147      * the chooser widget relative to it
0148      *
0149      * @since 5.86
0150      */
0151     explicit KFontChooser(DisplayFlags flags, QWidget *parent = nullptr);
0152 
0153     /**
0154      * Destructor.
0155      */
0156     ~KFontChooser() override;
0157 
0158     /**
0159      * Enables or disables a column (family, style, size) in the widget.
0160      *
0161      * Use this function if your application does not need or support all font properties.
0162      *
0163      * @param column specify the column(s) to enable/disable, an OR-ed combination of
0164      * @c KFontChooser::FontColumn enum values
0165      * @param state if @p false the columns are disabled, and vice-versa
0166      */
0167     void enableColumn(int column, bool state);
0168 
0169     /**
0170      * Sets the currently selected font in the widget.
0171      *
0172      * @param font the font to select
0173      * @param onlyFixed if @c true, the font list will only display fixed-width fonts,
0174      * otherwise all fonts are displayed. The default is @c false.
0175      */
0176     void setFont(const QFont &font, bool onlyFixed = false);
0177 
0178     /**
0179      * Returns the bitmask corresponding to the attributes the user wishes to change.
0180      */
0181     FontDiffFlags fontDiffFlags() const;
0182 
0183     /**
0184      * Returns the currently selected font in the chooser.
0185      */
0186     QFont font() const;
0187 
0188     /**
0189      * Sets the color to use for the font in the preview area.
0190      */
0191     void setColor(const QColor &col);
0192 
0193     /**
0194      * Returns the color currently used for the font in the preview
0195      * area (default: the text color of the active color group).
0196      */
0197     QColor color() const;
0198 
0199     /**
0200      * Sets the background color to use in the preview area.
0201      */
0202     void setBackgroundColor(const QColor &col);
0203 
0204     /**
0205      * Returns the background color currently used in the preview area
0206      * (default: the base color of the active colorgroup)
0207      */
0208     QColor backgroundColor() const;
0209 
0210 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 86)
0211     /**
0212      * Sets the state of the checkbox indicating whether the font size
0213      * is to be interpreted as relative size.
0214      * @note If parameter @p sizeIsRelative was not set in the constructor
0215      *       of the widget this setting will be ignored.
0216      *
0217      * @deprecated Since 5.86, for lack of usage.
0218      */
0219     KWIDGETSADDONS_DEPRECATED_VERSION(5, 86, "For lack of usage.")
0220     void setSizeIsRelative(Qt::CheckState relative);
0221 #endif
0222 
0223 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 86)
0224     /**
0225      * @return whether the font size is to be interpreted as relative size
0226      *
0227      * @deprecated Since 5.86, for lack of usage.
0228      */
0229     KWIDGETSADDONS_DEPRECATED_VERSION(5, 86, "For lack of usage.")
0230     Qt::CheckState sizeIsRelative() const;
0231 #endif
0232 
0233     /**
0234      * @return The current text in the sample text input area.
0235      */
0236     QString sampleText() const;
0237 
0238     /**
0239      * Sets the sample text in the preview area; this is useful if you
0240      * want to use text in your native language.
0241      *
0242      * @param text the new sample text (it will replace the current text)
0243      */
0244     void setSampleText(const QString &text);
0245 
0246     /**
0247      * If @p visible is @c true the preview area will be shown, and vice-versa
0248      * is it's @c false.
0249      */
0250     void setSampleBoxVisible(bool visible);
0251 
0252     /**
0253      * The selection criteria for the font families shown in the dialog.
0254      */
0255     enum FontListCriteria {
0256         /**
0257          * If set, only show fixed fixed-width (monospace) fonts.
0258          */
0259         FixedWidthFonts = 0x01,
0260         /**
0261          * If set, only show scalable fonts.
0262          * Certain configurations allow bitmap fonts to remain unscaled
0263          * and thus these fonts have limited number of sizes.
0264          */
0265         ScalableFonts = 0x02,
0266         /**
0267          * If set, only show smooth scalable fonts.
0268          * This will return only non-bitmap fonts which are scalable to any size requested.
0269          * Setting this option means the @c ScalableFonts flag is ignored.
0270          */
0271         SmoothScalableFonts = 0x04
0272     };
0273 
0274 #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(5, 86)
0275     /**
0276      * Creates a list of font strings.
0277      *
0278      * @param list The list is returned here.
0279      * @param fontListCriteria should contain all the restrictions for font selection as OR-ed values
0280      *        from KFontChooser::FontListCriteria
0281      *
0282      * @deprecated since 5.86, use createFontList(uint) instead.
0283      */
0284     KWIDGETSADDONS_DEPRECATED_VERSION(5, 86, "Use KFontChooser::createFontList(uint) instead.")
0285     static void getFontList(QStringList &list, uint fontListCriteria);
0286 #endif
0287 
0288     /**
0289      * Returns a list of font faimly name strings filtered based on @p fontListCriteria.
0290      *
0291      * @param fontListCriteria specifies the criteria used to select fonts to add to
0292      * the list, a combination of OR-ed values from @ref KFontChooser::FontListCriteria
0293      *
0294      * @since 5.86
0295      */
0296     static QStringList createFontList(uint fontListCriteria);
0297 
0298     /**
0299      * Uses @p fontList to fill the font family list in the widget.
0300      *
0301      * You can create a custom list of fonts using the static @c createFontList(uint
0302      * criteria) to only include fonts that meet certain criteria (e.g. only
0303      * smooth-scalable fonts).
0304      *
0305      * @see KFontChooser::createFontList(uint), KFontChooser::FontListCriteria
0306      *
0307      * Note that if @p fontList is empty, the font list in the chooser will show
0308      * all the available fonts on the system.
0309      * @since 5.86
0310      */
0311     void setFontListItems(const QStringList &fontList);
0312 
0313     /**
0314      * Sets the minimum number of items that should be visible in the
0315      * child list widgets; this number will be used to compute and set
0316      * the minimum heights for those widgets.
0317      *
0318      * @since 5.86
0319      */
0320     void setMinVisibleItems(int visibleItems);
0321 
0322     /**
0323      * Reimplemented for internal reasons.
0324      */
0325     QSize sizeHint(void) const override;
0326 
0327 Q_SIGNALS:
0328     /**
0329      * Emitted when the selected font changes.
0330      */
0331     void fontSelected(const QFont &font);
0332 
0333 private:
0334     std::unique_ptr<class KFontChooserPrivate> const d;
0335 
0336     Q_DISABLE_COPY(KFontChooser)
0337 };
0338 
0339 Q_DECLARE_OPERATORS_FOR_FLAGS(KFontChooser::DisplayFlags)
0340 
0341 #endif