QTextEdit - mouse events to override input context

- Selection can start on top of preedit
- Mouse press outside preedit commits
- Double click to commit on top of preedit

Change-Id: Ia2698d97d354a677d935de1a8fd9ed53a161ca5e
Reviewed-by: Joona Petrell <joona.t.petrell@nokia.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Pekka Vuorela 2011-10-07 13:48:29 +03:00 committed by Qt by Nokia
parent 15d25dc676
commit 4b3d88a9c6
2 changed files with 123 additions and 102 deletions

View File

@ -1506,6 +1506,13 @@ void QWidgetTextControlPrivate::mousePressEvent(QEvent *e, Qt::MouseButton butto
{
Q_Q(QWidgetTextControl);
mousePressed = (interactionFlags & Qt::TextSelectableByMouse);
mousePressPos = pos.toPoint();
#ifndef QT_NO_DRAGANDDROP
mightStartDrag = false;
#endif
if (sendMouseEventToInputContext(
e, QEvent::MouseButtonPress, button, pos, modifiers, buttons, globalPos)) {
return;
@ -1530,10 +1537,8 @@ void QWidgetTextControlPrivate::mousePressEvent(QEvent *e, Qt::MouseButton butto
const QTextCursor oldSelection = cursor;
const int oldCursorPos = cursor.position();
mousePressed = (interactionFlags & Qt::TextSelectableByMouse);
#ifndef QT_NO_DRAGANDDROP
mightStartDrag = false;
#endif
if (isPreediting())
qApp->inputPanel()->commit();
if (trippleClickTimer.isActive()
&& ((pos - trippleClickPoint).toPoint().manhattanLength() < QApplication::startDragDistance())) {
@ -1575,7 +1580,6 @@ void QWidgetTextControlPrivate::mousePressEvent(QEvent *e, Qt::MouseButton butto
&& q->hitTest(pos, Qt::ExactHit) != -1) {
#ifndef QT_NO_DRAGANDDROP
mightStartDrag = true;
dragStartPos = pos.toPoint();
#endif
return;
}
@ -1605,11 +1609,6 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
{
Q_Q(QWidgetTextControl);
if (sendMouseEventToInputContext(
e, QEvent::MouseMove, button, mousePos, modifiers, buttons, globalPos)) {
return;
}
if (interactionFlags & Qt::LinksAccessibleByMouse) {
QString anchor = q->anchorAt(mousePos);
if (anchor != highlightedAnchor) {
@ -1618,9 +1617,7 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
}
}
if (!(buttons & Qt::LeftButton))
return;
if (buttons & Qt::LeftButton) {
const bool editable = interactionFlags & Qt::TextEditable;
if (!(mousePressed
@ -1634,7 +1631,7 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
const int oldCursorPos = cursor.position();
if (mightStartDrag) {
if ((mousePos.toPoint() - dragStartPos).manhattanLength() > QApplication::startDragDistance())
if ((mousePos.toPoint() - mousePressPos).manhattanLength() > QApplication::startDragDistance())
startDrag();
return;
}
@ -1645,6 +1642,20 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
const qreal mouseX = qreal(mousePos.x());
int newCursorPos = q->hitTest(mousePos, Qt::FuzzyHit);
if (isPreediting()) {
// note: oldCursorPos not including preedit
int selectionStartPos = q->hitTest(mousePressPos, Qt::FuzzyHit);
if (newCursorPos != selectionStartPos) {
qApp->inputPanel()->commit();
// commit invalidates positions
newCursorPos = q->hitTest(mousePos, Qt::FuzzyHit);
selectionStartPos = q->hitTest(mousePressPos, Qt::FuzzyHit);
setCursorPosition(selectionStartPos);
}
}
if (newCursorPos == -1)
return;
@ -1657,7 +1668,7 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
extendBlockwiseSelection(newCursorPos);
else if (selectedWordOnDoubleClick.hasSelection())
extendWordwiseSelection(newCursorPos, mouseX);
else
else if (!isPreediting())
setCursorPosition(newCursorPos, QTextCursor::KeepAnchor);
if (interactionFlags & Qt::TextEditable) {
@ -1681,6 +1692,9 @@ void QWidgetTextControlPrivate::mouseMoveEvent(QEvent *e, Qt::MouseButton button
}
selectionChanged(true);
repaintOldAndNewSelection(oldSelection);
}
sendMouseEventToInputContext(e, QEvent::MouseMove, button, mousePos, modifiers, buttons, globalPos);
}
void QWidgetTextControlPrivate::mouseReleaseEvent(QEvent *e, Qt::MouseButton button, const QPointF &pos, Qt::KeyboardModifiers modifiers,
@ -1748,25 +1762,21 @@ void QWidgetTextControlPrivate::mouseReleaseEvent(QEvent *e, Qt::MouseButton but
}
}
void QWidgetTextControlPrivate::mouseDoubleClickEvent(QEvent *e, Qt::MouseButton button, const QPointF &pos, Qt::KeyboardModifiers modifiers,
Qt::MouseButtons buttons, const QPoint &globalPos)
void QWidgetTextControlPrivate::mouseDoubleClickEvent(QEvent *e, Qt::MouseButton button, const QPointF &pos,
Qt::KeyboardModifiers modifiers, Qt::MouseButtons buttons,
const QPoint &globalPos)
{
Q_Q(QWidgetTextControl);
if (sendMouseEventToInputContext(
e, QEvent::MouseButtonDblClick, button, pos, modifiers, buttons, globalPos)) {
return;
}
if (button != Qt::LeftButton
|| !(interactionFlags & Qt::TextSelectableByMouse)) {
e->ignore();
return;
}
if (button == Qt::LeftButton
&& (interactionFlags & Qt::TextSelectableByMouse)) {
#ifndef QT_NO_DRAGANDDROP
mightStartDrag = false;
#endif
if (isPreediting())
qApp->inputPanel()->commit();
const QTextCursor oldSelection = cursor;
setCursorPosition(pos);
QTextLine line = currentTextLine(cursor);
@ -1789,6 +1799,10 @@ void QWidgetTextControlPrivate::mouseDoubleClickEvent(QEvent *e, Qt::MouseButton
#endif
emit q->cursorPositionChanged();
}
} else if (!sendMouseEventToInputContext(e, QEvent::MouseButtonDblClick, button, pos,
modifiers, buttons, globalPos)) {
e->ignore();
}
}
bool QWidgetTextControlPrivate::sendMouseEventToInputContext(
@ -1798,27 +1812,23 @@ bool QWidgetTextControlPrivate::sendMouseEventToInputContext(
#if !defined(QT_NO_IM)
Q_Q(QWidgetTextControl);
if (contextWidget && isPreediting()) {
QTextLayout *layout = cursor.block().layout();
if (contextWidget && layout && !layout->preeditAreaText().isEmpty()) {
QInputContext *ctx = qApp->inputContext();
int cursorPos = q->hitTest(pos, Qt::FuzzyHit) - cursor.position();
if (cursorPos < 0 || cursorPos > layout->preeditAreaText().length()) {
if (cursorPos < 0 || cursorPos > layout->preeditAreaText().length())
cursorPos = -1;
// don't send move events outside the preedit area
if (eventType == QEvent::MouseMove)
return true;
}
if (ctx) {
if (ctx && cursorPos >= 0) {
QMouseEvent ev(eventType, contextWidget->mapFromGlobal(globalPos),
contextWidget->topLevelWidget()->mapFromGlobal(globalPos), globalPos,
button, buttons, modifiers);
ctx->mouseHandler(cursorPos, &ev);
e->setAccepted(ev.isAccepted());
}
if (!layout->preeditAreaText().isEmpty())
return true;
}
}
#else
Q_UNUSED(e);
Q_UNUSED(eventType);
@ -2730,6 +2740,15 @@ void QWidgetTextControlPrivate::showToolTip(const QPoint &globalPos, const QPoin
}
#endif // QT_NO_TOOLTIP
bool QWidgetTextControlPrivate::isPreediting() const
{
QTextLayout *layout = cursor.block().layout();
if (layout && !layout->preeditAreaText().isEmpty())
return true;
return false;
}
bool QWidgetTextControl::setFocusToNextOrPreviousAnchor(bool next)
{
Q_D(QWidgetTextControl);

View File

@ -169,6 +169,8 @@ public:
void showToolTip(const QPoint &globalPos, const QPointF &pos, QWidget *contextWidget);
#endif
bool isPreediting() const;
void append(const QString &text, Qt::TextFormat format = Qt::AutoText);
QTextDocument *doc;
@ -190,7 +192,7 @@ public:
bool mousePressed;
bool mightStartDrag;
QPoint dragStartPos;
QPoint mousePressPos;
QPointer<QWidget> contextWidget;
int lastSelectionPosition;