CuteLogger
Fast and simple logging solution for Qt based applications
qmlrichtext.h
1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (c) 2020-2026 Meltytech, LLC
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are
8** met:
9** * Redistributions of source code must retain the above copyright
10** notice, this list of conditions and the following disclaimer.
11** * Redistributions in binary form must reproduce the above copyright
12** notice, this list of conditions and the following disclaimer in
13** the documentation and/or other materials provided with the
14** distribution.
15** * Neither the name of The Qt Company Ltd nor the names of its
16** contributors may be used to endorse or promote products derived
17** from this software without specific prior written permission.
18**
19**
20** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31**
32****************************************************************************/
33
34#ifndef QMLRICHTEXT_H
35#define QMLRICHTEXT_H
36
37#include <QQuickTextDocument>
38#include <QtGui/QTextCharFormat>
39#include <qqmlfile.h>
40
41QT_BEGIN_NAMESPACE
42class QTextDocument;
43QT_END_NAMESPACE
44
45class QmlRichText : public QObject
46{
47 Q_OBJECT
48
49 Q_ENUMS(HAlignment)
50
51 Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
52 Q_PROPERTY(
53 int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged)
54 Q_PROPERTY(
55 int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged)
56 Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
57 Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
58 Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
59 Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
60 Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
61 Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
62 Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
63 Q_PROPERTY(bool strikeout READ strikeout WRITE setStrikeout NOTIFY strikeoutChanged)
64 Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
65 Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged)
66 Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
67 Q_PROPERTY(QSizeF size READ size NOTIFY sizeChanged)
68
69public:
70 QmlRichText();
71
72 QQuickItem *target() { return m_target; }
73 void setTarget(QQuickItem *target);
74 void setCursorPosition(int position);
75 void setSelectionStart(int position);
76 void setSelectionEnd(int position);
77 int cursorPosition() const { return m_cursorPosition; }
78 int selectionStart() const { return m_selectionStart; }
79 int selectionEnd() const { return m_selectionEnd; }
80 QString fontFamily() const;
81 QColor textColor() const;
82 Qt::Alignment alignment() const;
83 void setAlignment(Qt::Alignment a);
84 bool bold() const;
85 bool italic() const;
86 bool underline() const;
87 bool strikeout() const;
88 int fontSize() const;
89 QUrl fileUrl() const;
90 QString text() const;
91 QSizeF size() const { return m_doc->size(); }
92
93public slots:
94 void setBold(bool arg);
95 void setItalic(bool arg);
96 void setUnderline(bool arg);
97 void setStrikeout(bool arg);
98 void setFontSize(int arg);
99 void setTextColor(const QColor &arg);
100 void setFontFamily(const QString &arg);
101 void setFileUrl(const QUrl &arg);
102 void setText(const QString &arg);
103 void saveAs(const QUrl &arg, QString fileType = QString());
104 void insertTable(int rows = 1, int columns = 2, int border = 0);
105 void indentLess();
106 void indentMore();
107 void pastePlain();
108 void reset();
109
110signals:
111 void targetChanged();
112 void cursorPositionChanged();
113 void selectionStartChanged();
114 void selectionEndChanged();
115 void fontFamilyChanged();
116 void textColorChanged();
117 void alignmentChanged();
118 void boldChanged();
119 void italicChanged();
120 void underlineChanged();
121 void fontSizeChanged();
122 void strikeoutChanged();
123 void fileUrlChanged();
124 void textChanged();
125 void error(QString message);
126 void sizeChanged();
127
128private:
129 QTextCursor textCursor() const;
130 void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
131 QQuickItem *m_target;
132 QTextDocument *m_doc;
133 int m_cursorPosition;
134 int m_selectionStart;
135 int m_selectionEnd;
136 QFont m_font;
137 int m_fontSize;
138 QUrl m_fileUrl;
139 QString m_text;
140 QString m_documentTitle;
141};
142
143#endif // QMLRICHTEXT_H