File indexing completed on 2024-05-12 16:59:35

0001 /*
0002     This file is part of the Qt Quick Controls module of the Qt Toolkit.
0003     SPDX-FileCopyrightText: 2013 Digia Plc and /or its subsidiary(-ies) <http://www.qt-project.org/legal>
0004 
0005     SPDX-License-Identifier: BSD-3-Clause
0006  */
0007 
0008 #ifndef DOCUMENTHANDLER_H
0009 #define DOCUMENTHANDLER_H
0010 
0011 #include <QQuickTextDocument>
0012 #include <QTextCharFormat>
0013 
0014 QT_BEGIN_NAMESPACE
0015 class QTextDocument;
0016 QT_END_NAMESPACE
0017 
0018 class DocumentHandler : public QObject
0019 {
0020     Q_OBJECT
0021 
0022     Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
0023     Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
0024     Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
0025     Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
0026 
0027     Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
0028     Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
0029     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
0030 
0031     Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
0032     Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
0033     Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
0034     Q_PROPERTY(bool strikeOut READ strikeOut WRITE setStrikeOut NOTIFY strikeOutChanged)
0035 
0036     Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
0037     Q_PROPERTY(int defaultFontSize READ defaultFontSize WRITE setDefaultFontSize NOTIFY defaultFontSizeChanged)
0038 
0039     Q_PROPERTY(QStringList defaultFontSizes READ defaultFontSizes NOTIFY defaultFontSizesChanged)
0040 
0041     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
0042     Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle NOTIFY documentTitleChanged)
0043 
0044 public:
0045     DocumentHandler();
0046 
0047     QQuickItem *target()
0048     {
0049         return m_target;
0050     }
0051 
0052     void setTarget(QQuickItem *target);
0053 
0054     void setCursorPosition(int position);
0055     void setSelectionStart(int position);
0056     void setSelectionEnd(int position);
0057 
0058     int cursorPosition() const
0059     {
0060         return m_cursorPosition;
0061     }
0062 
0063     int selectionStart() const
0064     {
0065         return m_selectionStart;
0066     }
0067 
0068     int selectionEnd() const
0069     {
0070         return m_selectionEnd;
0071     }
0072 
0073     QString fontFamily() const;
0074 
0075     QColor textColor() const;
0076 
0077     Qt::Alignment alignment() const;
0078     void setAlignment(Qt::Alignment a);
0079 
0080     bool bold() const;
0081     bool italic() const;
0082     bool underline() const;
0083     bool strikeOut() const;
0084     int fontSize() const;
0085     int defaultFontSize() const;
0086 
0087     QStringList defaultFontSizes() const;
0088     QString text() const;
0089 
0090     QString documentTitle() const;
0091 
0092     Q_INVOKABLE QString strip(const QString text);
0093     Q_INVOKABLE QString stripAndSimplify(const QString text);
0094     Q_INVOKABLE QString strippedClipboardText();
0095 
0096 public Q_SLOTS:
0097     void setBold(bool arg);
0098     void setItalic(bool arg);
0099     void setUnderline(bool arg);
0100     void setStrikeOut(bool arg);
0101     void setFontSize(int arg);
0102     void setDefaultFontSize(int arg);
0103     void setTextColor(const QColor &arg);
0104     void setFontFamily(const QString &arg);
0105 
0106     void setText(const QString &arg);
0107 
0108     void setDocumentTitle(QString arg);
0109 
0110     void pasteWithoutFormatting();
0111     void reset();
0112 
0113 Q_SIGNALS:
0114     void targetChanged();
0115     void cursorPositionChanged();
0116     void selectionStartChanged();
0117     void selectionEndChanged();
0118 
0119     void fontFamilyChanged();
0120     void textColorChanged();
0121     void alignmentChanged();
0122 
0123     void boldChanged();
0124     void italicChanged();
0125     void underlineChanged();
0126     void strikeOutChanged();
0127 
0128     void fontSizeChanged();
0129     void defaultFontSizeChanged();
0130     void defaultFontSizesChanged();
0131 
0132     void fileUrlChanged();
0133 
0134     void textChanged();
0135     void documentTitleChanged();
0136 
0137 private:
0138     QTextCursor textCursor() const;
0139     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
0140 
0141     QQuickItem *m_target;
0142     QTextDocument *m_doc;
0143 
0144     int m_cursorPosition;
0145     int m_selectionStart;
0146     int m_selectionEnd;
0147 
0148     QFont m_font;
0149     int m_fontSize;
0150     QString m_text;
0151     QString m_documentTitle;
0152 };
0153 
0154 #endif