Changeset View
Changeset View
Standalone View
Standalone View
views/manager/kpViewManager_TextCursor.cpp
Show First 20 Lines • Show All 71 Lines • ▼ Show 20 Line(s) | 71 | { | |||
---|---|---|---|---|---|
72 | return static_cast<bool> (d->textCursorBlinkTimer); | 72 | return static_cast<bool> (d->textCursorBlinkTimer); | ||
73 | } | 73 | } | ||
74 | 74 | | |||
75 | // public | 75 | // public | ||
76 | void kpViewManager::setTextCursorEnabled (bool yes) | 76 | void kpViewManager::setTextCursorEnabled (bool yes) | ||
77 | { | 77 | { | ||
78 | qCDebug(kpLogViews) << "kpViewManager::setTextCursorEnabled(" << yes << ")"; | 78 | qCDebug(kpLogViews) << "kpViewManager::setTextCursorEnabled(" << yes << ")"; | ||
79 | 79 | | |||
80 | if (yes == textCursorEnabled ()) | 80 | if (yes == textCursorEnabled ()) { | ||
81 | return; | 81 | return; | ||
82 | } | ||||
82 | 83 | | |||
83 | delete d->textCursorBlinkTimer; | 84 | delete d->textCursorBlinkTimer; | ||
84 | d->textCursorBlinkTimer = nullptr; | 85 | d->textCursorBlinkTimer = nullptr; | ||
85 | 86 | | |||
86 | setFastUpdates (); | 87 | setFastUpdates (); | ||
87 | setQueueUpdates (); | 88 | setQueueUpdates (); | ||
88 | { | 89 | { | ||
89 | if (yes) | 90 | if (yes) | ||
Show All 20 Lines | |||||
110 | bool kpViewManager::textCursorBlinkState () const | 111 | bool kpViewManager::textCursorBlinkState () const | ||
111 | { | 112 | { | ||
112 | return d->textCursorBlinkState; | 113 | return d->textCursorBlinkState; | ||
113 | } | 114 | } | ||
114 | 115 | | |||
115 | // public | 116 | // public | ||
116 | void kpViewManager::setTextCursorBlinkState (bool on) | 117 | void kpViewManager::setTextCursorBlinkState (bool on) | ||
117 | { | 118 | { | ||
118 | if (on == d->textCursorBlinkState) | 119 | if (on == d->textCursorBlinkState) { | ||
119 | return; | 120 | return; | ||
121 | } | ||||
120 | 122 | | |||
121 | d->textCursorBlinkState = on; | 123 | d->textCursorBlinkState = on; | ||
122 | 124 | | |||
123 | updateTextCursor (); | 125 | updateTextCursor (); | ||
124 | } | 126 | } | ||
125 | 127 | | |||
126 | 128 | | |||
127 | // public | 129 | // public | ||
128 | int kpViewManager::textCursorRow () const | 130 | int kpViewManager::textCursorRow () const | ||
129 | { | 131 | { | ||
130 | return d->textCursorRow; | 132 | return d->textCursorRow; | ||
131 | } | 133 | } | ||
132 | 134 | | |||
133 | // public | 135 | // public | ||
134 | int kpViewManager::textCursorCol () const | 136 | int kpViewManager::textCursorCol () const | ||
135 | { | 137 | { | ||
136 | return d->textCursorCol; | 138 | return d->textCursorCol; | ||
137 | } | 139 | } | ||
138 | 140 | | |||
139 | // public | 141 | // public | ||
140 | void kpViewManager::setTextCursorPosition (int row, int col) | 142 | void kpViewManager::setTextCursorPosition (int row, int col) | ||
141 | { | 143 | { | ||
142 | if (row == d->textCursorRow && col == d->textCursorCol) | 144 | if (row == d->textCursorRow && col == d->textCursorCol) { | ||
143 | return; | 145 | return; | ||
146 | } | ||||
144 | 147 | | |||
145 | setFastUpdates (); | 148 | setFastUpdates (); | ||
146 | setQueueUpdates (); | 149 | setQueueUpdates (); | ||
147 | { | 150 | { | ||
148 | // Clear the old cursor. | 151 | // Clear the old cursor. | ||
149 | d->textCursorBlinkState = false; | 152 | d->textCursorBlinkState = false; | ||
150 | updateTextCursor (); | 153 | updateTextCursor (); | ||
151 | 154 | | |||
152 | d->textCursorRow = row; | 155 | d->textCursorRow = row; | ||
153 | d->textCursorCol = col; | 156 | d->textCursorCol = col; | ||
154 | 157 | | |||
155 | // Render the new cursor. | 158 | // Render the new cursor. | ||
156 | d->textCursorBlinkState = true; | 159 | d->textCursorBlinkState = true; | ||
157 | updateTextCursor (); | 160 | updateTextCursor (); | ||
158 | } | 161 | } | ||
159 | restoreQueueUpdates (); | 162 | restoreQueueUpdates (); | ||
160 | restoreFastUpdates (); | 163 | restoreFastUpdates (); | ||
161 | } | 164 | } | ||
162 | 165 | | |||
163 | 166 | | |||
164 | // public | 167 | // public | ||
165 | QRect kpViewManager::textCursorRect () const | 168 | QRect kpViewManager::textCursorRect () const | ||
166 | { | 169 | { | ||
167 | kpTextSelection *textSel = document ()->textSelection (); | 170 | kpTextSelection *textSel = document ()->textSelection (); | ||
168 | if (!textSel) | 171 | if (!textSel) { | ||
169 | return QRect (); | 172 | return {}; | ||
173 | } | ||||
170 | 174 | | |||
171 | QPoint topLeft = textSel->pointForTextRowCol (d->textCursorRow, d->textCursorCol); | 175 | QPoint topLeft = textSel->pointForTextRowCol (d->textCursorRow, d->textCursorCol); | ||
172 | if (topLeft == KP_INVALID_POINT) | 176 | if (topLeft == KP_INVALID_POINT) | ||
173 | { | 177 | { | ||
174 | // Text cursor row/col hasn't been specified yet? | 178 | // Text cursor row/col hasn't been specified yet? | ||
175 | if (textSel->hasContent ()) | 179 | if (textSel->hasContent ()) { | ||
176 | return QRect (); | 180 | return {}; | ||
181 | } | ||||
177 | 182 | | |||
178 | // Empty text box should still display a cursor so that the user | 183 | // Empty text box should still display a cursor so that the user | ||
179 | // knows where typed text will go. | 184 | // knows where typed text will go. | ||
180 | topLeft = textSel->textAreaRect ().topLeft (); | 185 | topLeft = textSel->textAreaRect ().topLeft (); | ||
181 | } | 186 | } | ||
182 | 187 | | |||
183 | Q_ASSERT (topLeft != KP_INVALID_POINT); | 188 | Q_ASSERT (topLeft != KP_INVALID_POINT); | ||
184 | 189 | | |||
185 | return QRect (topLeft.x (), topLeft.y (), | 190 | return {topLeft.x (), topLeft.y (), | ||
186 | 1, textSel->textStyle ().fontMetrics ().height ()); | 191 | 1, textSel->textStyle ().fontMetrics ().height ()}; | ||
187 | } | 192 | } | ||
188 | 193 | | |||
189 | 194 | | |||
190 | // protected | 195 | // protected | ||
191 | void kpViewManager::updateTextCursor () | 196 | void kpViewManager::updateTextCursor () | ||
192 | { | 197 | { | ||
193 | qCDebug(kpLogViews) << "kpViewManager::updateTextCursor()"; | 198 | qCDebug(kpLogViews) << "kpViewManager::updateTextCursor()"; | ||
194 | 199 | | |||
195 | const QRect r = textCursorRect (); | 200 | const QRect r = textCursorRect (); | ||
196 | if (!r.isValid ()) | 201 | if (!r.isValid ()) { | ||
197 | return; | 202 | return; | ||
203 | } | ||||
198 | 204 | | |||
199 | setFastUpdates (); | 205 | setFastUpdates (); | ||
200 | { | 206 | { | ||
201 | // If !textCursorEnabled(), this will clear. | 207 | // If !textCursorEnabled(), this will clear. | ||
202 | updateViews (r); | 208 | updateViews (r); | ||
203 | } | 209 | } | ||
204 | restoreFastUpdates (); | 210 | restoreFastUpdates (); | ||
205 | } | 211 | } | ||
206 | 212 | | |||
207 | // protected slot | 213 | // protected slot | ||
208 | void kpViewManager::slotTextCursorBlink () | 214 | void kpViewManager::slotTextCursorBlink () | ||
209 | { | 215 | { | ||
210 | qCDebug(kpLogViews) << "kpViewManager::slotTextCursorBlink() cursorBlinkState=" | 216 | qCDebug(kpLogViews) << "kpViewManager::slotTextCursorBlink() cursorBlinkState=" | ||
211 | << d->textCursorBlinkState << endl; | 217 | << d->textCursorBlinkState; | ||
212 | 218 | | |||
213 | if (d->textCursorBlinkTimer) | 219 | if (d->textCursorBlinkTimer) | ||
214 | { | 220 | { | ||
215 | // (single shot) | 221 | // (single shot) | ||
216 | d->textCursorBlinkTimer->start (QApplication::cursorFlashTime () / 2); | 222 | d->textCursorBlinkTimer->start (QApplication::cursorFlashTime () / 2); | ||
217 | } | 223 | } | ||
218 | 224 | | |||
219 | updateTextCursor (); | 225 | updateTextCursor (); | ||
220 | // TODO: Shouldn't this be done _before_ updating the text cursor | 226 | // TODO: Shouldn't this be done _before_ updating the text cursor | ||
221 | // because textCursorBlinkState() is supposed to reflect what | 227 | // because textCursorBlinkState() is supposed to reflect what | ||
222 | // updateTextCursor() just rendered, until the next timer tick? | 228 | // updateTextCursor() just rendered, until the next timer tick? | ||
223 | d->textCursorBlinkState = !d->textCursorBlinkState; | 229 | d->textCursorBlinkState = !d->textCursorBlinkState; | ||
224 | } | 230 | } |