File indexing completed on 2024-05-12 16:02:27

0001 /* This file is part of the KDE project
0002  *
0003  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #ifndef KIS_FONT_FAMILY_COMBO_BOX_H
0008 #define KIS_FONT_FAMILY_COMBO_BOX_H
0009 
0010 #include <QObject>
0011 #include <KisSqueezedComboBox.h>
0012 #include <QList>
0013 #include <QFont>
0014 #include <QFontDatabase>
0015 #include <QStyledItemDelegate>
0016 
0017 #include "kritawidgetutils_export.h"
0018 
0019 /**
0020  * @brief The KisFontComboBoxes class
0021  * This is a little widget with two comboboxes.
0022  * One is for the font family, and the other for the style, using the power of QFontDataBase.
0023  * This allows us to limit the amount of fonts visible in the fonts drop down,
0024  * as that can be a quite intense number when you have several 'style complete' fonts.
0025  */
0026 class KRITAWIDGETUTILS_EXPORT KisFontComboBoxes : public QWidget
0027 {
0028     Q_OBJECT
0029 public:
0030     KisFontComboBoxes(QWidget *parent = 0);
0031 
0032     /**
0033      * @brief setCurrentFont
0034      * sets the style and font comboboxes appropriately.
0035      * @param font the QFont to set.
0036      */
0037     void setCurrentFont(QFont font);
0038 
0039     void setCurrentFamily(const QString family);
0040     void setCurrentStyle(QString style);
0041 
0042     // Current family name.
0043     QString currentFamily() const;
0044     // Current style
0045     QString currentStyle() const;
0046 
0047     /**
0048      * @brief currentFont the current QFont from both family and style combinations
0049      * @param pointSize as this widget has no idea about point size, input desired point size.
0050      * @return
0051      */
0052     QFont currentFont(int pointSize = 10) const;
0053 
0054     void refillComboBox(QVector<QFontDatabase::WritingSystem> writingSystems = QVector<QFontDatabase::WritingSystem>());
0055     void setInitialized();
0056 
0057 Q_SIGNALS:
0058     void fontChanged(QString);
0059 private Q_SLOTS:
0060     void fontFamilyChanged();
0061     void fontChange();
0062 private:
0063     QComboBox *m_family;
0064     QComboBox *m_styles;
0065 };
0066 
0067 class KRITAWIDGETUTILS_EXPORT PinnedFontsSeparator : public QStyledItemDelegate {
0068 public:
0069     PinnedFontsSeparator(QAbstractItemDelegate *_default, QWidget *parent = nullptr);
0070     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0071     void setSeparatorIndex(int index);
0072     void setSeparatorAdded();
0073     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0074 
0075 private:
0076     int m_separatorIndex;
0077     bool m_separatorAdded;
0078     QAbstractItemDelegate *m_defaultDelegate;
0079 };
0080 
0081 /**
0082  * @brief The KisFontFamilyComboBox class
0083  * A QCombobox that limits the amount of fonts it contains.
0084  * Amongst others it blacklists certain fonts, it also filters
0085  * out 'style' fonts, like those used for Bold and Italic,
0086  * and it allows you to limit the amount of fonts to certain writing systems
0087  */
0088 class KRITAWIDGETUTILS_EXPORT KisFontFamilyComboBox : public QComboBox
0089 {
0090     Q_OBJECT
0091 public:
0092     KisFontFamilyComboBox(QWidget *parent = 0);
0093 
0094     // List of writing systems to use. If empty will default to "all"
0095     void refillComboBox(QVector<QFontDatabase::WritingSystem> writingSystems = QVector<QFontDatabase::WritingSystem>());
0096     void setTopFont(const QString &family);
0097     void setInitialized();
0098 
0099 private:
0100     QStringList m_pinnedFonts;
0101     QStringList m_blacklistedFonts;
0102     bool m_initilized {false};
0103     bool m_initializeFromConfig;
0104     int m_separatorIndex;
0105     PinnedFontsSeparator *m_fontSeparator;
0106 };
0107 
0108 #endif // KIS_FONT_FAMILY_COMBO_BOX_H