]> git.lyx.org Git - features.git/commitdiff
Work around bug in QTextLine::xToCursor
authorJean-Marc Lasgouttes <lasgouttes@lyx.org>
Fri, 31 Mar 2017 14:56:06 +0000 (16:56 +0200)
committerJean-Marc Lasgouttes <lasgouttes@lyx.org>
Tue, 23 May 2017 08:50:08 +0000 (10:50 +0200)
With RtL text, the method can be non-monotonic wrt x value (which is a
Qt bug). To work around this, we check whether positions adjacent to
those returned by xToCursor look better. Depending on whether the new
x position is too small or too large, we look backward or forward for
a better solution.

The performance is probably not great, but this is only needed for
user interactions, so the performance penalty should not be a problem.

Fixes part of #10569.

(cherry picked from commit da590925cd6749587b9f500ecdaa457e34bc0f4a)

src/frontends/qt4/GuiFontMetrics.cpp
status.22x

index 80ff8195ec0837e0989d87ba2cb79083f1c8ca4e..eb95401f36e411be6241db9d2aaaf3d2dfb744e0 100644 (file)
@@ -265,9 +265,34 @@ int GuiFontMetrics::x2pos(docstring const & s, int & x, bool const rtl,
                           double const wordspacing) const
 {
        shared_ptr<QTextLayout const> tl = getTextLayout(s, rtl, wordspacing);
-       int const qpos = tl->lineForTextPosition(0).xToCursor(x);
+       QTextLine const & tline = tl->lineForTextPosition(0);
+       int qpos = tline.xToCursor(x);
+       int newx = static_cast<int>(tline.cursorToX(qpos));
+       // The value of qpos may be wrong in rtl text (see ticket #10569).
+       // To work around this, let's have a look at adjacent positions to
+       // see whether we find closer matches.
+       if (rtl && newx < x) {
+               while (qpos > 0) {
+                       int const xm = static_cast<int>(tline.cursorToX(qpos - 1));
+                       if (abs(xm - x) < abs(newx - x)) {
+                               --qpos;
+                               newx = xm;
+                       } else
+                               break;
+               }
+       } else if (rtl && newx > x) {
+               while (qpos < tline.textLength()) {
+                       int const xp = static_cast<int>(tline.cursorToX(qpos + 1));
+                       if (abs(xp - x) < abs(newx - x)) {
+                               ++qpos;
+                               newx = xp;
+                       } else
+                               break;
+               }
+       }
        // correct x value to the actual cursor position.
-       x = static_cast<int>(tl->lineForTextPosition(0).cursorToX(qpos));
+       x = newx;
+
        /* Since QString is UTF-16 and docstring is UCS-4, the offsets may
         * not be the same when there are high-plan unicode characters
         * (bug #10443).
index b0dfced5fda6cdc4b18e0044e8484d6c827df371..d3105f16f8087f5f19b3297a5fee070bbff6b3fa 100644 (file)
@@ -50,6 +50,9 @@ What's new
 
 - Show correct color name for green in the status bar (bug 10656).
 
+- fixes to cursor positionning on mouse click for right-to-left text
+  (bug 10569).
+
 
 * INTERNALS