File indexing completed on 2025-01-05 04:55:01

0001 /*
0002     Copyright (c) 2017 Christian Mollekopf <mollekopf@kolabsystems.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 #pragma once
0020 #include "kube_export.h"
0021 #include <QObject>
0022 #include <QFont>
0023 #include <QTextCursor>
0024 
0025 class QTextDocument;
0026 class QQuickTextDocument;
0027 
0028 class KUBE_EXPORT TextDocumentHandler : public QObject
0029 {
0030     Q_OBJECT
0031 
0032     Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged)
0033     Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
0034     Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
0035     Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
0036 
0037     Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
0038     Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
0039     Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
0040 
0041     Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
0042     Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
0043     Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
0044     Q_PROPERTY(bool containsFormatting READ containsFormatting NOTIFY textChanged)
0045 
0046     Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
0047 
0048     Q_PROPERTY(QString text READ text NOTIFY textChanged)
0049     Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged)
0050     Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged)
0051 
0052 public:
0053     explicit TextDocumentHandler(QObject *parent = nullptr);
0054 
0055     QQuickTextDocument *document() const;
0056     void setDocument(QQuickTextDocument *document);
0057 
0058     QString text() const;
0059     QString plainText() const;
0060     QString htmlText() const;
0061 
0062     bool containsFormatting() const;
0063 
0064     int cursorPosition() const;
0065     void setCursorPosition(int position);
0066 
0067     int selectionStart() const;
0068     void setSelectionStart(int position);
0069 
0070     int selectionEnd() const;
0071     void setSelectionEnd(int position);
0072 
0073     QString fontFamily() const;
0074     void setFontFamily(const QString &family);
0075 
0076     QColor textColor() const;
0077     void setTextColor(const QColor &color);
0078 
0079     Qt::Alignment alignment() const;
0080     void setAlignment(Qt::Alignment alignment);
0081 
0082     bool bold() const;
0083     void setBold(bool bold);
0084 
0085     bool italic() const;
0086     void setItalic(bool italic);
0087 
0088     bool underline() const;
0089     void setUnderline(bool underline);
0090 
0091     int fontSize() const;
0092     void setFontSize(int size);
0093 
0094     Q_INVOKABLE void resetFormat();
0095 
0096     Q_INVOKABLE static bool isHtml(const QString &);
0097 
0098 Q_SIGNALS:
0099     void documentChanged();
0100     void cursorPositionChanged();
0101     void selectionStartChanged();
0102     void selectionEndChanged();
0103 
0104     void fontFamilyChanged();
0105     void textColorChanged();
0106     void alignmentChanged();
0107 
0108     void boldChanged();
0109     void italicChanged();
0110     void underlineChanged();
0111 
0112     void fontSizeChanged();
0113 
0114     void textChanged();
0115 
0116 private Q_SLOTS:
0117     void contentsChange(int position, int charsRemoved, int charsAdded);
0118 
0119 private:
0120     void reset();
0121     QTextCharFormat charFormat() const;
0122     QTextCursor textCursor() const;
0123     void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
0124 
0125     QQuickTextDocument *mDocument;
0126     int mCursorPosition;
0127     int mSelectionStart;
0128     int mSelectionEnd;
0129     QSharedPointer<QTextCharFormat> mCachedTextFormat;
0130 };