Check selection handles position

This patch fixes the strange behavior when selecting text.

This patch (on a Left To Right text) makes sure that:
 - the left handle will select text only on the left & above of the right handle
 - the right handle will select text only on the right & below of the left handle

For RTL is way more complicated:
- the left handle is acuatually the right handle but on the left side and it acts as a right handle
- the right  handle is acuatually the left handle but on the right side and it acts as a left handle

Change-Id: Ifca591398103199d5aef479f0a080161c9f44c0e
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
This commit is contained in:
BogDan Vatra 2017-12-15 11:08:16 +02:00
parent b1fb4f8261
commit 5f924134ff

View File

@ -605,27 +605,63 @@ void QAndroidInputContext::handleLocationChanged(int handleId, int x, int y)
double pixelDensity = window double pixelDensity = window
? QHighDpiScaling::factor(window) ? QHighDpiScaling::factor(window)
: QHighDpiScaling::factor(QtAndroid::androidPlatformIntegration()->screen()); : QHighDpiScaling::factor(QtAndroid::androidPlatformIntegration()->screen());
QPoint point(x / pixelDensity, y / pixelDensity); QPointF point(x / pixelDensity, y / pixelDensity);
y -= leftRect.width() / 2; point.setY(point.y() - leftRect.width() / 2);
if (handleId == 1) { if (handleId == 1) {
setSelectionOnFocusObject(point, point); setSelectionOnFocusObject(point, point);
return; return;
} }
QInputMethodQueryEvent query(Qt::ImCursorPosition | Qt::ImAnchorPosition); QInputMethodQueryEvent query(Qt::ImCursorPosition | Qt::ImAnchorPosition | Qt::ImCurrentSelection);
QCoreApplication::sendEvent(m_focusObject, &query); QCoreApplication::sendEvent(m_focusObject, &query);
int cpos = query.value(Qt::ImCursorPosition).toInt(); int cpos = query.value(Qt::ImCursorPosition).toInt();
int anchor = query.value(Qt::ImAnchorPosition).toInt(); int anchor = query.value(Qt::ImAnchorPosition).toInt();
bool rtl = query.value(Qt::ImCurrentSelection).toString().isRightToLeft();
auto rightRect = im->anchorRectangle(); auto rightRect = im->anchorRectangle();
if (cpos > anchor) if (cpos > anchor)
std::swap(leftRect, rightRect); std::swap(leftRect, rightRect);
auto checkLeftHandle = [&rightRect](QPointF &handlePos) {
if (handlePos.y() > rightRect.center().y())
handlePos.setY(rightRect.center().y()); // adjust Y handle pos
if (handlePos.y() >= rightRect.y() && handlePos.y() <= rightRect.bottom() && handlePos.x() >= rightRect.x())
return false; // same line and wrong X pos ?
return true;
};
auto checkRtlRightHandle = [&rightRect](QPointF &handlePos) {
if (handlePos.y() > rightRect.center().y())
handlePos.setY(rightRect.center().y()); // adjust Y handle pos
if (handlePos.y() >= rightRect.y() && handlePos.y() <= rightRect.bottom() && rightRect.x() >= handlePos.x())
return false; // same line and wrong X pos ?
return true;
};
auto checkRightHandle = [&leftRect](QPointF &handlePos) {
if (handlePos.y() < leftRect.center().y())
handlePos.setY(leftRect.center().y()); // adjust Y handle pos
if (handlePos.y() >= leftRect.y() && handlePos.y() <= leftRect.bottom() && leftRect.x() >= handlePos.x())
return false; // same line and wrong X pos ?
return true;
};
auto checkRtlLeftHandle = [&leftRect](QPointF &handlePos) {
if (handlePos.y() < leftRect.center().y())
handlePos.setY(leftRect.center().y()); // adjust Y handle pos
if (handlePos.y() >= leftRect.y() && handlePos.y() <= leftRect.bottom() && handlePos.x() >= leftRect.x())
return false; // same line and wrong X pos ?
return true;
};
if (handleId == 2) { if (handleId == 2) {
QPoint rightPoint(rightRect.center().toPoint()); QPointF rightPoint(rightRect.center());
if ((!rtl && !checkLeftHandle(point)) || (rtl && !checkRtlRightHandle(point)))
return;
setSelectionOnFocusObject(point, rightPoint); setSelectionOnFocusObject(point, rightPoint);
} else if (handleId == 3) { } else if (handleId == 3) {
QPoint leftPoint(leftRect.center().toPoint()); QPointF leftPoint(leftRect.center());
if ((!rtl && !checkRightHandle(point)) || (rtl && !checkRtlLeftHandle(point)))
return;
setSelectionOnFocusObject(leftPoint, point); setSelectionOnFocusObject(leftPoint, point);
} }
} }