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-2023 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 
39 #include <QtGui/QTextCharFormat>
40 
41 #include <qqmlfile.h>
42 
43 QT_BEGIN_NAMESPACE
44 class QTextDocument;
45 QT_END_NAMESPACE
46 
47 class QmlRichText : public QObject
48 {
49  Q_OBJECT
50 
51  Q_ENUMS(HAlignment)
52 
53  Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
54  Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY
55  cursorPositionChanged)
56  Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY
57  selectionStartChanged)
58  Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged)
59  Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
60  Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged)
61  Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
62  Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged)
63  Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged)
64  Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged)
65  Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
66  Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged)
67  Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
68  Q_PROPERTY(QSizeF size READ size NOTIFY sizeChanged)
69 
70 public:
71  QmlRichText();
72 
73  QQuickItem *target()
74  {
75  return m_target;
76  }
77  void setTarget(QQuickItem *target);
78  void setCursorPosition(int position);
79  void setSelectionStart(int position);
80  void setSelectionEnd(int position);
81  int cursorPosition() const
82  {
83  return m_cursorPosition;
84  }
85  int selectionStart() const
86  {
87  return m_selectionStart;
88  }
89  int selectionEnd() const
90  {
91  return m_selectionEnd;
92  }
93  QString fontFamily() const;
94  QColor textColor() const;
95  Qt::Alignment alignment() const;
96  void setAlignment(Qt::Alignment a);
97  bool bold() const;
98  bool italic() const;
99  bool underline() const;
100  int fontSize() const;
101  QUrl fileUrl() const;
102  QString text() const;
103  QSizeF size() const
104  {
105  return m_doc->size();
106  }
107 
108 public slots:
109  void setBold(bool arg);
110  void setItalic(bool arg);
111  void setUnderline(bool arg);
112  void setFontSize(int arg);
113  void setTextColor(const QColor &arg);
114  void setFontFamily(const QString &arg);
115  void setFileUrl(const QUrl &arg);
116  void setText(const QString &arg);
117  void saveAs(const QUrl &arg, QString fileType = QString());
118  void insertTable(int rows = 1, int columns = 2, int border = 0);
119  void indentLess();
120  void indentMore();
121  void pastePlain();
122  void reset();
123 
124 signals:
125  void targetChanged();
126  void cursorPositionChanged();
127  void selectionStartChanged();
128  void selectionEndChanged();
129  void fontFamilyChanged();
130  void textColorChanged();
131  void alignmentChanged();
132  void boldChanged();
133  void italicChanged();
134  void underlineChanged();
135  void fontSizeChanged();
136  void fileUrlChanged();
137  void textChanged();
138  void error(QString message);
139  void sizeChanged();
140 
141 private:
142  QTextCursor textCursor() const;
143  void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
144  QQuickItem *m_target;
145  QTextDocument *m_doc;
146  int m_cursorPosition;
147  int m_selectionStart;
148  int m_selectionEnd;
149  QFont m_font;
150  int m_fontSize;
151  QUrl m_fileUrl;
152  QString m_text;
153  QString m_documentTitle;
154 };
155 
156 #endif // QMLRICHTEXT_H