From 737770bfa4bb91ed96412d18d6490db3450bcb1c Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 6 Oct 2017 11:43:33 +0200 Subject: TextDocumentHandler to deal with HTML formatting --- framework/src/domain/textdocumenthandler.h | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 framework/src/domain/textdocumenthandler.h (limited to 'framework/src/domain/textdocumenthandler.h') diff --git a/framework/src/domain/textdocumenthandler.h b/framework/src/domain/textdocumenthandler.h new file mode 100644 index 00000000..b8ae5bdb --- /dev/null +++ b/framework/src/domain/textdocumenthandler.h @@ -0,0 +1,121 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#pragma once + +#include +#include +#include + +class QTextDocument; +class QQuickTextDocument; + +class TextDocumentHandler : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QQuickTextDocument *document READ document WRITE setDocument NOTIFY documentChanged) + Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) + Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged) + Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged) + + Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged) + Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged) + Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) + + Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged) + Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) + Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) + + Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) + + Q_PROPERTY(QString plainText READ plainText NOTIFY textChanged) + Q_PROPERTY(QString htmlText READ htmlText NOTIFY textChanged) + +public: + explicit TextDocumentHandler(QObject *parent = nullptr); + + QQuickTextDocument *document() const; + void setDocument(QQuickTextDocument *document); + + QString plainText() const; + QString htmlText() const; + + int cursorPosition() const; + void setCursorPosition(int position); + + int selectionStart() const; + void setSelectionStart(int position); + + int selectionEnd() const; + void setSelectionEnd(int position); + + QString fontFamily() const; + void setFontFamily(const QString &family); + + QColor textColor() const; + void setTextColor(const QColor &color); + + Qt::Alignment alignment() const; + void setAlignment(Qt::Alignment alignment); + + bool bold() const; + void setBold(bool bold); + + bool italic() const; + void setItalic(bool italic); + + bool underline() const; + void setUnderline(bool underline); + + int fontSize() const; + void setFontSize(int size); + +Q_SIGNALS: + void documentChanged(); + void cursorPositionChanged(); + void selectionStartChanged(); + void selectionEndChanged(); + + void fontFamilyChanged(); + void textColorChanged(); + void alignmentChanged(); + + void boldChanged(); + void italicChanged(); + void underlineChanged(); + + void fontSizeChanged(); + + void textChanged(); + +private Q_SLOTS: + void contentsChange(int position, int charsRemoved, int charsAdded); + +private: + void reset(); + QTextCharFormat charFormat() const; + QTextCursor textCursor() const; + void mergeFormatOnWordOrSelection(const QTextCharFormat &format); + + QQuickTextDocument *mDocument; + int mCursorPosition; + int mSelectionStart; + int mSelectionEnd; + QSharedPointer mCachedTextFormat; +}; -- cgit v1.2.3