File indexing completed on 2024-05-05 04:46:58

0001 #pragma once
0002 
0003 #include <QObject>
0004 #include <QQmlEngine>
0005 
0006 #include <QFont>
0007 #include <QStringList>
0008 #include <QFontDatabase>
0009 
0010 /**
0011  * @brief A model of fonts and its properties.
0012  * @note This class is exposed as the type `FontPickerModel` to the QML engine. * 
0013  */
0014 class FontPickerModel : public QObject
0015 {
0016     Q_OBJECT
0017     QML_ELEMENT
0018     /**
0019      * The current picked font for exposing its properties, such as FontPickerModel::styles, FontPickerModel::sizes, etc.
0020      */
0021     Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
0022     
0023     /**
0024      * The available styles for the current picked font.
0025      */
0026     Q_PROPERTY(QStringList styles READ styles NOTIFY stylesChanged FINAL)
0027     
0028     /**
0029      * The available optimal font sizes for the picked font.
0030      */
0031     Q_PROPERTY(QStringList sizes READ sizes NOTIFY sizesChanged FINAL)
0032     
0033     /**
0034      * All of the fonts available in the system.
0035      */
0036     Q_PROPERTY(QStringList fontsModel READ fontsModel NOTIFY fontsModelChanged FINAL)
0037     
0038     /**
0039      * The desired writing system to filter out the fonts model.
0040      */
0041     Q_PROPERTY(QFontDatabase::WritingSystem writingSystem READ writingSystem WRITE setWritingSystem NOTIFY writingSystemChanged)
0042     
0043     /**
0044      * Whether to only list fonts in the fonts model that are mono mono-spaced.
0045      */
0046     Q_PROPERTY(bool onlyMonospaced READ onlyMonospaced WRITE setOnlyMonospaced NOTIFY onlyMonospacedChanged FINAL)
0047     
0048 public:
0049     explicit FontPickerModel(QObject * parent = nullptr);
0050     
0051     QFont font();
0052     void setFont(const QFont &font);
0053     
0054     QStringList styles();
0055     QStringList sizes();
0056     
0057     QStringList fontsModel();
0058     
0059     QFontDatabase::WritingSystem writingSystem();
0060     void setWritingSystem(QFontDatabase::WritingSystem value);
0061     
0062     bool onlyMonospaced();
0063     void setOnlyMonospaced(bool value);
0064     
0065 public Q_SLOTS:
0066     
0067     /**
0068      * @brief Forces the model to be updated.
0069      */
0070     void updateModel();
0071     
0072     /**
0073      * @brief Set the current picked font to extract its properties.
0074      * @param desc the description of the font, and its properties.
0075      * @note See the Qt QFont documentation to see how the font string decsription works.
0076      */
0077     void setFont(const QString &desc);
0078     
0079     /**
0080      * @brief Converts the current picked font to its string description.
0081      * @return the converted QFont font and its all properties to a string text.
0082      */
0083     QString fontToString();
0084     
0085 private: 
0086     QFontDatabase m_fontDatabase;
0087     QFont m_font;
0088     QFontDatabase::WritingSystem m_writingSystem;
0089     bool m_onlyMonospaced;
0090     
0091 Q_SIGNALS:
0092     void fontChanged();
0093     void stylesChanged();
0094     void sizesChanged();
0095     void fontsModelChanged();
0096     void writingSystemChanged();
0097     void onlyMonospacedChanged();
0098 };