| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #ifndef DOCUMENTHANDLER_H |
| 52 | #define DOCUMENTHANDLER_H |
| 53 | |
| 54 | #include <QQuickTextDocument> |
| 55 | |
| 56 | #include <QtGui/QTextCharFormat> |
| 57 | #include <QtCore/QTextCodec> |
| 58 | |
| 59 | #include <qqmlfile.h> |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | class QTextDocument; |
| 63 | QT_END_NAMESPACE |
| 64 | |
| 65 | class DocumentHandler : public QObject |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | |
| 69 | Q_ENUMS(HAlignment) |
| 70 | |
| 71 | Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged) |
| 72 | Q_PROPERTY(int cursorPosition READ cursorPosition WRITE setCursorPosition NOTIFY cursorPositionChanged) |
| 73 | Q_PROPERTY(int selectionStart READ selectionStart WRITE setSelectionStart NOTIFY selectionStartChanged) |
| 74 | Q_PROPERTY(int selectionEnd READ selectionEnd WRITE setSelectionEnd NOTIFY selectionEndChanged) |
| 75 | |
| 76 | Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged) |
| 77 | Q_PROPERTY(QString fontFamily READ fontFamily WRITE setFontFamily NOTIFY fontFamilyChanged) |
| 78 | Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) |
| 79 | |
| 80 | Q_PROPERTY(bool bold READ bold WRITE setBold NOTIFY boldChanged) |
| 81 | Q_PROPERTY(bool italic READ italic WRITE setItalic NOTIFY italicChanged) |
| 82 | Q_PROPERTY(bool underline READ underline WRITE setUnderline NOTIFY underlineChanged) |
| 83 | |
| 84 | Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged) |
| 85 | |
| 86 | Q_PROPERTY(QStringList defaultFontSizes READ defaultFontSizes NOTIFY defaultFontSizesChanged) |
| 87 | |
| 88 | Q_PROPERTY(QUrl fileUrl READ fileUrl WRITE setFileUrl NOTIFY fileUrlChanged) |
| 89 | Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) |
| 90 | Q_PROPERTY(QString documentTitle READ documentTitle WRITE setDocumentTitle NOTIFY documentTitleChanged) |
| 91 | |
| 92 | public: |
| 93 | DocumentHandler(); |
| 94 | |
| 95 | QQuickItem *target() { return m_target; } |
| 96 | |
| 97 | void setTarget(QQuickItem *target); |
| 98 | |
| 99 | void setCursorPosition(int position); |
| 100 | void setSelectionStart(int position); |
| 101 | void setSelectionEnd(int position); |
| 102 | |
| 103 | int cursorPosition() const { return m_cursorPosition; } |
| 104 | int selectionStart() const { return m_selectionStart; } |
| 105 | int selectionEnd() const { return m_selectionEnd; } |
| 106 | |
| 107 | QString fontFamily() const; |
| 108 | |
| 109 | QColor textColor() const; |
| 110 | |
| 111 | Qt::Alignment alignment() const; |
| 112 | void setAlignment(Qt::Alignment a); |
| 113 | |
| 114 | bool bold() const; |
| 115 | bool italic() const; |
| 116 | bool underline() const; |
| 117 | int fontSize() const; |
| 118 | |
| 119 | QStringList defaultFontSizes() const; |
| 120 | QUrl fileUrl() const; |
| 121 | QString text() const; |
| 122 | |
| 123 | QString documentTitle() const; |
| 124 | |
| 125 | public Q_SLOTS: |
| 126 | void setBold(bool arg); |
| 127 | void setItalic(bool arg); |
| 128 | void setUnderline(bool arg); |
| 129 | void setFontSize(int arg); |
| 130 | void setTextColor(const QColor &arg); |
| 131 | void setFontFamily(const QString &arg); |
| 132 | |
| 133 | void setFileUrl(const QUrl &arg); |
| 134 | void setText(const QString &arg); |
| 135 | void saveAs(const QUrl &arg, const QString &fileType); |
| 136 | |
| 137 | void setDocumentTitle(QString arg); |
| 138 | |
| 139 | Q_SIGNALS: |
| 140 | void targetChanged(); |
| 141 | void cursorPositionChanged(); |
| 142 | void selectionStartChanged(); |
| 143 | void selectionEndChanged(); |
| 144 | |
| 145 | void fontFamilyChanged(); |
| 146 | void textColorChanged(); |
| 147 | void alignmentChanged(); |
| 148 | |
| 149 | void boldChanged(); |
| 150 | void italicChanged(); |
| 151 | void underlineChanged(); |
| 152 | |
| 153 | void fontSizeChanged(); |
| 154 | void defaultFontSizesChanged(); |
| 155 | |
| 156 | void fileUrlChanged(); |
| 157 | |
| 158 | void textChanged(); |
| 159 | void documentTitleChanged(); |
| 160 | void error(QString message); |
| 161 | |
| 162 | private: |
| 163 | void reset(); |
| 164 | QTextCursor textCursor() const; |
| 165 | void mergeFormatOnWordOrSelection(const QTextCharFormat &format); |
| 166 | |
| 167 | QQuickItem *m_target; |
| 168 | QTextDocument *m_doc; |
| 169 | |
| 170 | int m_cursorPosition; |
| 171 | int m_selectionStart; |
| 172 | int m_selectionEnd; |
| 173 | |
| 174 | QFont m_font; |
| 175 | int m_fontSize; |
| 176 | QUrl m_fileUrl; |
| 177 | QString m_text; |
| 178 | QString m_documentTitle; |
| 179 | }; |
| 180 | |
| 181 | #endif |
| 182 | |