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