4949 return ;
5050 }
5151
52+ root .clearSelection ();
5253 root .selectionStart = start;
5354 root .selectionEnd = end;
5455 }
@@ -144,8 +145,10 @@ Item {
144145 delegate: Shape {
145146 id: shape
146147 anchors .horizontalCenter : parent .horizontalCenter
148+ antialiasing: true
147149 layer .enabled : true
148- layer .samples : 8
150+ layer .samples : 4
151+ layer .smooth : true
149152
150153 // This draws the background
151154 ShapePath {
@@ -212,8 +215,8 @@ Item {
212215 return Qt .rect (0 , 0 , 0 , 0 );
213216 }
214217
215- const startRect = shadowText .positionToRectangle (start);
216- const endRect = shadowText .positionToRectangle (end);
218+ const startRect = textEdit .positionToRectangle (start);
219+ const endRect = textEdit .positionToRectangle (end);
217220
218221 const startX = root .margin + startRect .x ;
219222 const endX = root .margin + endRect .x + endRect .width ;
@@ -247,10 +250,10 @@ Item {
247250
248251 // This text edit is only used for hit testing, it is not shown
249252 TextEdit {
250- id: shadowText
253+ id: textEdit
251254
252- x: root .margin // This centers the text horizontally perfectly
253- anchors . verticalCenter : shape . verticalCenter
255+ x: root .margin
256+ y : root . margin - ( textMetrics . tightBoundingRect . y - textMetrics . boundingRect . y )
254257 font: root .font
255258 color: " transparent"
256259 readOnly: true
@@ -259,42 +262,53 @@ Item {
259262 wrapMode: TextEdit .NoWrap
260263 text: modelData .text
261264
265+ TextMetrics {
266+ id: textMetrics
267+ font: textEdit .font
268+ text: textEdit .text
269+ renderType: textEdit .renderType
270+ }
271+
262272 /**
263273 * Returns the index into the root.text property for the
264274 * given x-coordinate.
265- * @param x The x-coordinate in shadowText space.
275+ * @param x The x-coordinate in textEdit space.
266276 * @return The index of the given coordinates in root.text.
267277 */
268278 function getCharacterIndexAt (x ) {
269- const pos = shadowText .positionAt (x, 0 );
270- const rect = shadowText .positionToRectangle (pos);
279+ const pos = textEdit .positionAt (x, 0 );
280+ const rect = textEdit .positionToRectangle (pos);
271281 const actualIndex = (x < rect .x ) ? pos - 1 : pos;
272- const lineIndex = Math .max (0 , Math .min (shadowText .text .length - 1 , actualIndex));
282+ const lineIndex = Math .max (0 , Math .min (textEdit .text .length - 1 , actualIndex));
273283 return lineIndex + modelData .offset ;
274284 }
285+ }
275286
276- MouseArea {
277- anchors .fill : parent
278- hoverEnabled: true
279- acceptedButtons: Qt .LeftButton | Qt .MiddleButton
280- cursorShape: root .cursorShape
281- onPositionChanged : function (mouse ) {
282- let index = shadowText .getCharacterIndexAt (mouse .x );
287+ MouseArea {
288+ anchors .fill : parent
289+ hoverEnabled: true
290+ acceptedButtons: Qt .LeftButton | Qt .MiddleButton
291+ cursorShape: root .cursorShape
292+ onPositionChanged : function (mouse ) {
293+ let x = mouse .x - root .margin ;
294+ if (x >= 0 )
295+ {
296+ let index = textEdit .getCharacterIndexAt (x);
283297 root .hoverIndex = index;
284298 }
285- onClicked : function ( event ) {
286- if (event . button === Qt . LeftButton )
287- {
288- root . clicked ();
289- }
290- else if ( event . button === Qt . MiddleButton )
291- {
292- root . middleClicked ();
293- }
299+ }
300+ onClicked : function (event ) {
301+ if ( event . button === Qt . LeftButton )
302+ {
303+ root . clicked ();
304+ }
305+ else if ( event . button === Qt . MiddleButton )
306+ {
307+ root . middleClicked ();
294308 }
295- onDoubleClicked: root .doubleClicked ()
296- onExited: root .hoverIndex = - 1
297309 }
310+ onDoubleClicked: root .doubleClicked ()
311+ onExited: root .hoverIndex = - 1
298312 }
299313 }
300314 }
0 commit comments