diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-13 09:54:39 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-13 09:54:39 +0200 |
commit | 4948d6d5cf64aaf7f1b344202dabc7ba92540e4f (patch) | |
tree | b27bbeb576479bae7ba579e9baccbdae946cd6ac | |
parent | 1cd39d6a9500aa1ed080fb0ba05386f20de00e75 (diff) | |
download | kube-4948d6d5cf64aaf7f1b344202dabc7ba92540e4f.tar.gz kube-4948d6d5cf64aaf7f1b344202dabc7ba92540e4f.zip |
Got html formatting to work with the great invisible char hack
-rw-r--r-- | framework/src/domain/documenthandler.cpp | 122 |
1 files changed, 66 insertions, 56 deletions
diff --git a/framework/src/domain/documenthandler.cpp b/framework/src/domain/documenthandler.cpp index 1a323d9f..03e5ad69 100644 --- a/framework/src/domain/documenthandler.cpp +++ b/framework/src/domain/documenthandler.cpp | |||
@@ -77,11 +77,10 @@ QQuickTextDocument *DocumentHandler::document() const | |||
77 | 77 | ||
78 | void DocumentHandler::setDocument(QQuickTextDocument *document) | 78 | void DocumentHandler::setDocument(QQuickTextDocument *document) |
79 | { | 79 | { |
80 | if (document == m_document) | 80 | if (document != m_document) { |
81 | return; | 81 | m_document = document; |
82 | 82 | emit documentChanged(); | |
83 | m_document = document; | 83 | } |
84 | emit documentChanged(); | ||
85 | } | 84 | } |
86 | 85 | ||
87 | int DocumentHandler::cursorPosition() const | 86 | int DocumentHandler::cursorPosition() const |
@@ -91,12 +90,11 @@ int DocumentHandler::cursorPosition() const | |||
91 | 90 | ||
92 | void DocumentHandler::setCursorPosition(int position) | 91 | void DocumentHandler::setCursorPosition(int position) |
93 | { | 92 | { |
94 | if (position == m_cursorPosition) | 93 | if (position != m_cursorPosition) { |
95 | return; | 94 | m_cursorPosition = position; |
96 | 95 | reset(); | |
97 | m_cursorPosition = position; | 96 | emit cursorPositionChanged(); |
98 | reset(); | 97 | } |
99 | emit cursorPositionChanged(); | ||
100 | } | 98 | } |
101 | 99 | ||
102 | int DocumentHandler::selectionStart() const | 100 | int DocumentHandler::selectionStart() const |
@@ -106,11 +104,10 @@ int DocumentHandler::selectionStart() const | |||
106 | 104 | ||
107 | void DocumentHandler::setSelectionStart(int position) | 105 | void DocumentHandler::setSelectionStart(int position) |
108 | { | 106 | { |
109 | if (position == m_selectionStart) | 107 | if (position != m_selectionStart) { |
110 | return; | 108 | m_selectionStart = position; |
111 | 109 | emit selectionStartChanged(); | |
112 | m_selectionStart = position; | 110 | } |
113 | emit selectionStartChanged(); | ||
114 | } | 111 | } |
115 | 112 | ||
116 | int DocumentHandler::selectionEnd() const | 113 | int DocumentHandler::selectionEnd() const |
@@ -120,20 +117,19 @@ int DocumentHandler::selectionEnd() const | |||
120 | 117 | ||
121 | void DocumentHandler::setSelectionEnd(int position) | 118 | void DocumentHandler::setSelectionEnd(int position) |
122 | { | 119 | { |
123 | if (position == m_selectionEnd) | 120 | if (position != m_selectionEnd) { |
124 | return; | 121 | m_selectionEnd = position; |
125 | 122 | emit selectionEndChanged(); | |
126 | m_selectionEnd = position; | 123 | } |
127 | emit selectionEndChanged(); | ||
128 | } | 124 | } |
129 | 125 | ||
130 | QString DocumentHandler::fontFamily() const | 126 | QString DocumentHandler::fontFamily() const |
131 | { | 127 | { |
132 | QTextCursor cursor = textCursor(); | 128 | auto cursor = textCursor(); |
133 | if (cursor.isNull()) | 129 | if (cursor.isNull()) { |
134 | return QString(); | 130 | return QString(); |
135 | QTextCharFormat format = cursor.charFormat(); | 131 | } |
136 | return format.font().family(); | 132 | return cursor.charFormat().font().family(); |
137 | } | 133 | } |
138 | 134 | ||
139 | void DocumentHandler::setFontFamily(const QString &family) | 135 | void DocumentHandler::setFontFamily(const QString &family) |
@@ -146,11 +142,11 @@ void DocumentHandler::setFontFamily(const QString &family) | |||
146 | 142 | ||
147 | QColor DocumentHandler::textColor() const | 143 | QColor DocumentHandler::textColor() const |
148 | { | 144 | { |
149 | QTextCursor cursor = textCursor(); | 145 | auto cursor = textCursor(); |
150 | if (cursor.isNull()) | 146 | if (cursor.isNull()) { |
151 | return QColor(Qt::black); | 147 | return QColor(Qt::black); |
152 | QTextCharFormat format = cursor.charFormat(); | 148 | } |
153 | return format.foreground().color(); | 149 | return cursor.charFormat().foreground().color(); |
154 | } | 150 | } |
155 | 151 | ||
156 | void DocumentHandler::setTextColor(const QColor &color) | 152 | void DocumentHandler::setTextColor(const QColor &color) |
@@ -163,10 +159,11 @@ void DocumentHandler::setTextColor(const QColor &color) | |||
163 | 159 | ||
164 | Qt::Alignment DocumentHandler::alignment() const | 160 | Qt::Alignment DocumentHandler::alignment() const |
165 | { | 161 | { |
166 | QTextCursor cursor = textCursor(); | 162 | auto cursor = textCursor(); |
167 | if (cursor.isNull()) | 163 | if (cursor.isNull()) { |
168 | return Qt::AlignLeft; | 164 | return Qt::AlignLeft; |
169 | return textCursor().blockFormat().alignment(); | 165 | } |
166 | return cursor.blockFormat().alignment(); | ||
170 | } | 167 | } |
171 | 168 | ||
172 | void DocumentHandler::setAlignment(Qt::Alignment alignment) | 169 | void DocumentHandler::setAlignment(Qt::Alignment alignment) |
@@ -180,10 +177,11 @@ void DocumentHandler::setAlignment(Qt::Alignment alignment) | |||
180 | 177 | ||
181 | bool DocumentHandler::bold() const | 178 | bool DocumentHandler::bold() const |
182 | { | 179 | { |
183 | QTextCursor cursor = textCursor(); | 180 | auto cursor = textCursor(); |
184 | if (cursor.isNull()) | 181 | if (cursor.isNull()) { |
185 | return false; | 182 | return false; |
186 | return textCursor().charFormat().fontWeight() == QFont::Bold; | 183 | } |
184 | return cursor.charFormat().fontWeight() == QFont::Bold; | ||
187 | } | 185 | } |
188 | 186 | ||
189 | void DocumentHandler::setBold(bool bold) | 187 | void DocumentHandler::setBold(bool bold) |
@@ -196,10 +194,11 @@ void DocumentHandler::setBold(bool bold) | |||
196 | 194 | ||
197 | bool DocumentHandler::italic() const | 195 | bool DocumentHandler::italic() const |
198 | { | 196 | { |
199 | QTextCursor cursor = textCursor(); | 197 | auto cursor = textCursor(); |
200 | if (cursor.isNull()) | 198 | if (cursor.isNull()) { |
201 | return false; | 199 | return false; |
202 | return textCursor().charFormat().fontItalic(); | 200 | } |
201 | return cursor.charFormat().fontItalic(); | ||
203 | } | 202 | } |
204 | 203 | ||
205 | void DocumentHandler::setItalic(bool italic) | 204 | void DocumentHandler::setItalic(bool italic) |
@@ -212,10 +211,11 @@ void DocumentHandler::setItalic(bool italic) | |||
212 | 211 | ||
213 | bool DocumentHandler::underline() const | 212 | bool DocumentHandler::underline() const |
214 | { | 213 | { |
215 | QTextCursor cursor = textCursor(); | 214 | auto cursor = textCursor(); |
216 | if (cursor.isNull()) | 215 | if (cursor.isNull()) { |
217 | return false; | 216 | return false; |
218 | return textCursor().charFormat().fontUnderline(); | 217 | } |
218 | return cursor.charFormat().fontUnderline(); | ||
219 | } | 219 | } |
220 | 220 | ||
221 | void DocumentHandler::setUnderline(bool underline) | 221 | void DocumentHandler::setUnderline(bool underline) |
@@ -269,32 +269,42 @@ void DocumentHandler::reset() | |||
269 | 269 | ||
270 | QTextCursor DocumentHandler::textCursor() const | 270 | QTextCursor DocumentHandler::textCursor() const |
271 | { | 271 | { |
272 | QTextDocument *doc = textDocument(); | 272 | if (QTextDocument *doc = textDocument()) { |
273 | if (!doc) | 273 | QTextCursor cursor = QTextCursor(doc); |
274 | return QTextCursor(); | 274 | if (m_selectionStart != m_selectionEnd) { |
275 | 275 | cursor.setPosition(m_selectionStart); | |
276 | QTextCursor cursor = QTextCursor(doc); | 276 | cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor); |
277 | if (m_selectionStart != m_selectionEnd) { | 277 | } else { |
278 | cursor.setPosition(m_selectionStart); | 278 | cursor.setPosition(m_cursorPosition); |
279 | cursor.setPosition(m_selectionEnd, QTextCursor::KeepAnchor); | 279 | } |
280 | } else { | 280 | return cursor; |
281 | cursor.setPosition(m_cursorPosition); | ||
282 | } | 281 | } |
283 | return cursor; | 282 | return QTextCursor(); |
284 | } | 283 | } |
285 | 284 | ||
286 | QTextDocument *DocumentHandler::textDocument() const | 285 | QTextDocument *DocumentHandler::textDocument() const |
287 | { | 286 | { |
288 | if (!m_document) | 287 | if (!m_document) { |
289 | return nullptr; | 288 | return nullptr; |
290 | 289 | } | |
291 | return m_document->textDocument(); | 290 | return m_document->textDocument(); |
292 | } | 291 | } |
293 | 292 | ||
294 | void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) | 293 | void DocumentHandler::mergeFormatOnWordOrSelection(const QTextCharFormat &format) |
295 | { | 294 | { |
296 | QTextCursor cursor = textCursor(); | 295 | QTextCursor cursor = textCursor(); |
297 | if (!cursor.hasSelection()) | 296 | |
298 | cursor.select(QTextCursor::WordUnderCursor); | ||
299 | cursor.mergeCharFormat(format); | 297 | cursor.mergeCharFormat(format); |
298 | |||
299 | /* | ||
300 | * FIXME: This is a fantastic hack to change the text format on the TextArea's internal cursor. | ||
301 | * The TextArea internally listens for the contentChanged signal of the document and then simply | ||
302 | * copies the format with QTextCursor::charFormat. This does of course not work without any text in | ||
303 | * said format. The Qt Example solution to the problem was to select the current word, which is both not | ||
304 | * the behaviour that we want, nor does it work if you don't currently have a word under the cursor. | ||
305 | * We therefore have to insert an invisible character to transport the format, which we have to clear out in the end again. | ||
306 | */ | ||
307 | if (!cursor.hasSelection()) { | ||
308 | cursor.insertText("\u2063"); | ||
309 | } | ||
300 | } | 310 | } |