summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoona Petrell <joona.t.petrell@nokia.com>2011-03-01 12:51:27 +1000
committerJoona Petrell <joona.t.petrell@nokia.com>2011-03-01 17:40:23 +1000
commit88253db8a7d7910e1393b1948fb3747117538c92 (patch)
tree038cb2a758150312a6dfd1d4e6cd5f4bf548e215
parentd5c72c6fb75357061c5f9e0d0d2efdaff9140741 (diff)
Make sure horizontal QML editor text aligment always returns the actual alignment
Also, implicit empty text alignment now follows the Application's default layout direction traditionally set by the locale. Task-number: QTBUG-15880 Reviewed-by: Martin Jones Change-Id: I88340513d489290bafd393072786a19731097b77
-rw-r--r--src/declarative/graphicsitems/qdeclarativetext.cpp67
-rw-r--r--src/declarative/graphicsitems/qdeclarativetext_p.h3
-rw-r--r--src/declarative/graphicsitems/qdeclarativetext_p_p.h2
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextedit.cpp52
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextedit_p.h3
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextedit_p_p.h7
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextinput.cpp73
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextinput_p.h3
-rw-r--r--src/declarative/graphicsitems/qdeclarativetextinput_p_p.h24
-rw-r--r--tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp34
-rw-r--r--tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp37
-rw-r--r--tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp37
12 files changed, 274 insertions, 68 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp
index 4c6c34f7ee..af7e4a1d8b 100644
--- a/src/declarative/graphicsitems/qdeclarativetext.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetext.cpp
@@ -97,17 +97,18 @@ DEFINE_BOOL_CONFIG_OPTION(enableImageCache, QML_ENABLE_TEXT_IMAGE_CACHE);
QString QDeclarativeTextPrivate::elideChar = QString(0x2026);
QDeclarativeTextPrivate::QDeclarativeTextPrivate()
-: color((QRgb)0), style(QDeclarativeText::Normal), hAlign(QDeclarativeText::AlignLeft),
+: color((QRgb)0), style(QDeclarativeText::Normal), hAlign(QDeclarativeText::AlignLeft),
vAlign(QDeclarativeText::AlignTop), elideMode(QDeclarativeText::ElideNone),
format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), lineHeight(1),
- lineHeightMode(QDeclarativeText::ProportionalHeight),
- lineCount(1), truncated(false), maximumLineCount(INT_MAX),
+ lineHeightMode(QDeclarativeText::ProportionalHeight), lineCount(1), truncated(false), maximumLineCount(INT_MAX),
maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false),
- cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), naturalWidth(0), doc(0)
+ cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), hAlignImplicit(true), naturalWidth(0), doc(0)
{
cacheAllTextAsImage = enableImageCache();
QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton;
QGraphicsItemPrivate::flags = QGraphicsItemPrivate::flags & ~QGraphicsItem::ItemHasNoContents;
+ if (QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeText::AlignRight;
}
QTextDocumentWithImageResources::QTextDocumentWithImageResources(QDeclarativeText *parent)
@@ -211,6 +212,21 @@ qreal QDeclarativeTextPrivate::implicitWidth() const
return mImplicitWidth;
}
+void QDeclarativeTextPrivate::determineHorizontalAlignment()
+{
+ Q_Q(QDeclarativeText);
+ if (hAlignImplicit && q->isComponentComplete()) {
+ // if no explicit alignment has been set, follow the natural layout direction of the text
+ QDeclarativeText::HAlignment previousAlign = hAlign;
+ if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeText::AlignRight;
+ else
+ hAlign = text.isRightToLeft() ? QDeclarativeText::AlignRight : QDeclarativeText::AlignLeft;
+ if (previousAlign != hAlign)
+ emit q->horizontalAlignmentChanged(hAlign);
+ }
+}
+
void QDeclarativeTextPrivate::updateLayout()
{
Q_Q(QDeclarativeText);
@@ -367,17 +383,6 @@ QSize QDeclarativeTextPrivate::setupTextLayout()
textOption.setWrapMode(QTextOption::WrapMode(wrapMode));
layout.setTextOption(textOption);
- QDeclarativeText::HAlignment hAlignment = hAlign;
- if(text.isRightToLeft()) {
- if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) {
- hAlignment = QDeclarativeText::AlignRight;
- } else if (hAlign == QDeclarativeText::AlignRight) {
- hAlignment = QDeclarativeText::AlignLeft;
- } else {
- hAlignment = hAlign;
- }
- }
-
bool elideText = false;
bool truncate = false;
@@ -423,10 +428,10 @@ QSize QDeclarativeTextPrivate::setupTextLayout()
// Need to correct for alignment
line.setLineWidth(lineWidth-elideWidth);
int x = line.naturalTextWidth();
- if (hAlignment == QDeclarativeText::AlignRight) {
+ if (hAlign == QDeclarativeText::AlignRight) {
x = q->width()-elideWidth;
- } else if (hAlignment == QDeclarativeText::AlignHCenter) {
- x = (q->width()+line.naturalTextWidth()-elideWidth)/2;
+ } else if (hAlign == QDeclarativeText::AlignHCenter) {
+ x = (q->width()+line.naturalTextWidth() - elideWidth)/2;
}
elidePos = QPointF(x, y + fm.ascent());
elideText = true;
@@ -472,13 +477,13 @@ QSize QDeclarativeTextPrivate::setupTextLayout()
height += (lineHeightMode == QDeclarativeText::FixedHeight) ? lineHeight : line.height() * lineHeight;
if (!cacheAllTextAsImage) {
- if ((hAlignment == QDeclarativeText::AlignLeft) || (hAlignment == QDeclarativeText::AlignJustify)) {
+ if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) {
x = 0;
- } else if (hAlignment == QDeclarativeText::AlignRight) {
+ } else if (hAlign == QDeclarativeText::AlignRight) {
x = layoutWidth - line.naturalTextWidth();
if (elideText && i == layout.lineCount()-1)
x -= elideWidth; // Correct for when eliding multilines
- } else if (hAlignment == QDeclarativeText::AlignHCenter) {
+ } else if (hAlign == QDeclarativeText::AlignHCenter) {
x = (layoutWidth - line.naturalTextWidth()) / 2;
if (elideText && i == layout.lineCount()-1)
x -= elideWidth/2; // Correct for when eliding multilines
@@ -946,6 +951,7 @@ void QDeclarativeText::setText(const QString &n)
}
d->text = n;
+ d->determineHorizontalAlignment();
d->updateLayout();
emit textChanged(d->text);
@@ -1064,7 +1070,9 @@ void QDeclarativeText::setStyleColor(const QColor &color)
\qmlproperty enumeration Text::verticalAlignment
Sets the horizontal and vertical alignment of the text within the Text items
- width and height. By default, the text is top-left aligned.
+ width and height. By default, the text is vertically aligned to the top. Horizontal
+ alignment follows the natural alignment of the text, for example text that is read
+ from left to right will be aligned to the left.
The valid values for \c horizontalAlignment are \c Text.AlignLeft, \c Text.AlignRight, \c Text.AlignHCenter and
\c Text.AlignJustify. The valid values for \c verticalAlignment are \c Text.AlignTop, \c Text.AlignBottom
@@ -1084,6 +1092,7 @@ QDeclarativeText::HAlignment QDeclarativeText::hAlign() const
void QDeclarativeText::setHAlign(HAlignment align)
{
Q_D(QDeclarativeText);
+ d->hAlignImplicit = false;
if (d->hAlign == align)
return;
@@ -1096,6 +1105,19 @@ void QDeclarativeText::setHAlign(HAlignment align)
emit horizontalAlignmentChanged(align);
}
+void QDeclarativeText::resetHAlign()
+{
+ Q_D(QDeclarativeText);
+ d->hAlignImplicit = true;
+ QDeclarativeText::HAlignment oldAlignment = d->hAlign;
+ d->determineHorizontalAlignment();
+ if (oldAlignment != d->hAlign) {
+ prepareGeometryChange();
+ d->updateLayout();
+ emit horizontalAlignmentChanged(d->hAlign);
+ }
+}
+
QDeclarativeText::VAlignment QDeclarativeText::vAlign() const
{
Q_D(const QDeclarativeText);
@@ -1556,6 +1578,7 @@ void QDeclarativeText::componentComplete()
QDeclarativeItem::componentComplete();
if (d->updateOnComponentComplete) {
d->updateOnComponentComplete = false;
+ d->determineHorizontalAlignment();
if (d->richText) {
d->ensureDoc();
d->doc->setText(d->text);
diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h
index b8835d1d04..92c2eab8a8 100644
--- a/src/declarative/graphicsitems/qdeclarativetext_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetext_p.h
@@ -69,7 +69,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeImplici
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(TextStyle style READ style WRITE setStyle NOTIFY styleChanged)
Q_PROPERTY(QColor styleColor READ styleColor WRITE setStyleColor NOTIFY styleColorChanged)
- Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged)
+ Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged)
Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged)
Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged)
Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1)
@@ -133,6 +133,7 @@ public:
HAlignment hAlign() const;
void setHAlign(HAlignment align);
+ void resetHAlign();
VAlignment vAlign() const;
void setVAlign(VAlignment align);
diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h
index 36ae12356a..6886d915d3 100644
--- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h
@@ -76,6 +76,7 @@ public:
void updateSize();
void updateLayout();
+ void determineHorizontalAlignment();
QString text;
QFont font;
@@ -110,6 +111,7 @@ public:
bool cacheAllTextAsImage:1;
bool internalWidthUpdate:1;
bool requireImplicitWidth:1;
+ bool hAlignImplicit:1;
QSize layedOutTextSize;
QSize paintedSize;
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
index 87a49bd4e5..35716d02aa 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp
@@ -249,6 +249,7 @@ void QDeclarativeTextEdit::setText(const QString &text)
Q_D(QDeclarativeTextEdit);
if (QDeclarativeTextEdit::text() == text)
return;
+
d->richText = d->format == RichText || (d->format == AutoText && Qt::mightBeRichText(text));
if (d->richText) {
#ifndef QT_NO_TEXTHTMLPARSER
@@ -457,7 +458,9 @@ void QDeclarativeTextEdit::setSelectedTextColor(const QColor &color)
\qmlproperty enumeration TextEdit::verticalAlignment
Sets the horizontal and vertical alignment of the text within the TextEdit item's
- width and height. By default, the text is top-left aligned.
+ width and height. By default, the text alignment follows the natural alignment
+ of the text, for example text that is read from left to right will be aligned to
+ the left.
Valid values for \c horizontalAlignment are:
\list
@@ -483,6 +486,7 @@ QDeclarativeTextEdit::HAlignment QDeclarativeTextEdit::hAlign() const
void QDeclarativeTextEdit::setHAlign(QDeclarativeTextEdit::HAlignment alignment)
{
Q_D(QDeclarativeTextEdit);
+ d->hAlignImplicit = false;
if (alignment == d->hAlign)
return;
d->hAlign = alignment;
@@ -491,6 +495,19 @@ void QDeclarativeTextEdit::setHAlign(QDeclarativeTextEdit::HAlignment alignment)
emit horizontalAlignmentChanged(d->hAlign);
}
+void QDeclarativeTextEdit::resetHAlign()
+{
+ Q_D(QDeclarativeTextEdit);
+ d->hAlignImplicit = true;
+ QDeclarativeTextEdit::HAlignment oldAlignment = d->hAlign;
+ d->determineHorizontalAlignment();
+ if (oldAlignment != d->hAlign) {
+ d->updateDefaultTextOption();
+ updateSize();
+
+ }
+}
+
QDeclarativeTextEdit::VAlignment QDeclarativeTextEdit::vAlign() const
{
Q_D(const QDeclarativeTextEdit);
@@ -948,6 +965,8 @@ void QDeclarativeTextEdit::componentComplete()
Q_D(QDeclarativeTextEdit);
QDeclarativePaintedItem::componentComplete();
if (d->dirty) {
+ d->determineHorizontalAlignment();
+ d->updateDefaultTextOption();
updateSize();
d->dirty = false;
}
@@ -1438,6 +1457,9 @@ void QDeclarativeTextEditPrivate::init()
document->setDocumentMargin(textMargin);
document->setUndoRedoEnabled(false); // flush undo buffer.
document->setUndoRedoEnabled(true);
+
+ if (QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeTextEdit::AlignRight;
updateDefaultTextOption();
}
@@ -1445,6 +1467,9 @@ void QDeclarativeTextEdit::q_textChanged()
{
Q_D(QDeclarativeTextEdit);
d->text = text();
+ d->rightToLeftText = d->text.isRightToLeft();
+ d->determineHorizontalAlignment();
+ d->updateDefaultTextOption();
updateSize();
updateTotalLines();
updateMicroFocus();
@@ -1461,6 +1486,21 @@ void QDeclarativeTextEdit::moveCursorDelegate()
d->cursor->setY(cursorRect.y());
}
+void QDeclarativeTextEditPrivate::determineHorizontalAlignment()
+{
+ Q_Q(QDeclarativeTextEdit);
+ if (hAlignImplicit && q->isComponentComplete()) {
+ // if no explicit alignment has been set, follow the natural layout direction of the text
+ QDeclarativeTextEdit::HAlignment previousAlign = hAlign;
+ if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeTextEdit::AlignRight;
+ else
+ hAlign = rightToLeftText ? QDeclarativeTextEdit::AlignRight : QDeclarativeTextEdit::AlignLeft;
+ if (previousAlign != hAlign)
+ emit q->horizontalAlignmentChanged(hAlign);
+ }
+}
+
void QDeclarativeTextEditPrivate::updateSelection()
{
Q_Q(QDeclarativeTextEdit);
@@ -1611,7 +1651,15 @@ void QDeclarativeTextEditPrivate::updateDefaultTextOption()
{
QTextOption opt = document->defaultTextOption();
int oldAlignment = opt.alignment();
- opt.setAlignment((Qt::Alignment)(int)(hAlign | vAlign));
+
+ QDeclarativeTextEdit::HAlignment horizontalAlignment = hAlign;
+ if (rightToLeftText) {
+ if (hAlign == QDeclarativeTextEdit::AlignLeft)
+ horizontalAlignment = QDeclarativeTextEdit::AlignRight;
+ else if (hAlign == QDeclarativeTextEdit::AlignRight)
+ horizontalAlignment = QDeclarativeTextEdit::AlignLeft;
+ }
+ opt.setAlignment((Qt::Alignment)(int)(horizontalAlignment | vAlign));
QTextOption::WrapMode oldWrapMode = opt.wrapMode();
opt.setWrapMode(QTextOption::WrapMode(wrapMode));
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
index 7785a7a2fb..c7dc2f65c1 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h
@@ -72,7 +72,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativeImplicitSizePa
Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged)
Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
- Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged)
+ Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged)
Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged)
Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged)
Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1)
@@ -152,6 +152,7 @@ public:
HAlignment hAlign() const;
void setHAlign(HAlignment align);
+ void resetHAlign();
VAlignment vAlign() const;
void setVAlign(VAlignment align);
diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
index 111cc02bcd..f4a6c0e2b1 100644
--- a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h
@@ -71,8 +71,8 @@ public:
: color("black"), hAlign(QDeclarativeTextEdit::AlignLeft), vAlign(QDeclarativeTextEdit::AlignTop),
imgDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true),
showInputPanelOnFocus(true), clickCausedFocus(false), persistentSelection(true), requireImplicitWidth(false),
- textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0),
- format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap),
+ hAlignImplicit(true), rightToLeftText(false), textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0),
+ cursorComponent(0), cursor(0), format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap),
mouseSelectionMode(QDeclarativeTextEdit::SelectCharacters), selectByMouse(false), canPaste(false),
yoff(0)
{
@@ -88,6 +88,7 @@ public:
void updateDefaultTextOption();
void relayoutDocument();
void updateSelection();
+ void determineHorizontalAlignment();
qreal implicitWidth() const;
void focusChanged(bool);
@@ -112,6 +113,8 @@ public:
bool clickCausedFocus : 1;
bool persistentSelection : 1;
bool requireImplicitWidth:1;
+ bool hAlignImplicit:1;
+ bool rightToLeftText:1;
qreal textMargin;
int lastSelectionStart;
int lastSelectionEnd;
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp
index e7c2ac7072..adc28605cd 100644
--- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp
+++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp
@@ -327,7 +327,9 @@ void QDeclarativeTextInput::setSelectedTextColor(const QColor &color)
\qmlproperty enumeration TextInput::horizontalAlignment
Sets the horizontal alignment of the text within the TextInput item's
- width and height. By default, the text is left aligned.
+ width and height. By default, the text alignment follows the natural alignment
+ of the text, for example text that is read from left to right will be aligned to
+ the left.
TextInput does not have vertical alignment, as the natural height is
exactly the height of the single line of text. If you set the height
@@ -347,7 +349,8 @@ QDeclarativeTextInput::HAlignment QDeclarativeTextInput::hAlign() const
void QDeclarativeTextInput::setHAlign(HAlignment align)
{
Q_D(QDeclarativeTextInput);
- if(align == d->hAlign)
+ d->hAlignImplicit = false;
+ if(align == d->hAlign || align > QDeclarativeTextInput::AlignHCenter) // justify not supported
return;
d->hAlign = align;
updateRect();
@@ -355,6 +358,19 @@ void QDeclarativeTextInput::setHAlign(HAlignment align)
emit horizontalAlignmentChanged(d->hAlign);
}
+void QDeclarativeTextInput::resetHAlign()
+{
+ Q_D(QDeclarativeTextInput);
+ d->hAlignImplicit = true;
+ QDeclarativeTextInput::HAlignment oldAlignment = d->hAlign;
+ d->determineHorizontalAlignment();
+ if (oldAlignment != d->hAlign) {
+ updateRect();
+ d->updateHorizontalScroll();
+ emit horizontalAlignmentChanged(d->hAlign);
+ }
+}
+
/*!
\qmlproperty bool TextInput::readOnly
@@ -961,16 +977,21 @@ void QDeclarativeTextInput::keyPressEvent(QKeyEvent* ev)
keyPressPreHandler(ev);
if (ev->isAccepted())
return;
- if (((ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier) // Don't allow MacOSX up/down support, and we don't allow a completer.
- || (((d->control->cursor() == 0 && ev->key() == Qt::Key_Left)
- || (d->control->cursor() == d->control->text().length()
- && ev->key() == Qt::Key_Right))
- && (d->lastSelectionStart == d->lastSelectionEnd)))
- {
- //ignore when moving off the end
- //unless there is a selection, because then moving will do something (deselect)
+
+ // Don't allow MacOSX up/down support, and we don't allow a completer.
+ bool ignore = (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) && ev->modifiers() == Qt::NoModifier;
+ if (!ignore && (d->lastSelectionStart == d->lastSelectionEnd) && (ev->key() == Qt::Key_Right || ev->key() == Qt::Key_Left)) {
+ // Ignore when moving off the end unless there is a selection,
+ // because then moving will do something (deselect).
+ int cursorPosition = d->control->cursor();
+ if (cursorPosition == 0)
+ ignore = ev->key() == (d->control->text().mid(cursorPosition,1).isRightToLeft() ? Qt::Key_Right : Qt::Key_Left);
+ if (cursorPosition == d->control->text().length())
+ ignore = ev->key() == (d->control->text().mid(cursorPosition-1,1).isRightToLeft() ? Qt::Key_Left : Qt::Key_Right);
+ }
+ if (ignore) {
ev->ignore();
- }else{
+ } else {
d->control->processKeyEvent(ev);
}
if (!ev->isAccepted())
@@ -1129,11 +1150,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll()
int cix = qRound(control->cursorToX(control->cursor() + preeditLength));
QRect br(q->boundingRect().toRect());
int widthUsed = calculateTextWidth();
- Qt::Alignment va = QStyle::visualAlignment(control->layoutDirection(), QFlag(Qt::Alignment(hAlign)));
+
if (autoScroll) {
if (widthUsed <= br.width()) {
// text fits in br; use hscroll for alignment
- switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) {
+ switch (hAlign & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) {
case Qt::AlignRight:
hscroll = widthUsed - br.width() - 1;
break;
@@ -1165,11 +1186,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll()
hscroll = cix;
}
} else {
- switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) {
- case Qt::AlignRight:
+ switch (hAlign) {
+ case QDeclarativeTextInput::AlignRight:
hscroll = q->width() - widthUsed;
break;
- case Qt::AlignHCenter:
+ case QDeclarativeTextInput::AlignHCenter:
hscroll = (q->width() - widthUsed) / 2;
break;
default:
@@ -1180,6 +1201,22 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll()
}
}
+void QDeclarativeTextInputPrivate::determineHorizontalAlignment()
+{
+ Q_Q(QDeclarativeTextInput);
+ if (hAlignImplicit) {
+ QString text = control->text();
+ // if no explicit alignment has been set, follow the natural layout direction of the text
+ QDeclarativeTextInput::HAlignment previousAlign = hAlign;
+ if (text.isEmpty() && QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeTextInput::AlignRight;
+ else
+ hAlign = text.isRightToLeft() ? QDeclarativeTextInput::AlignRight : QDeclarativeTextInput::AlignLeft;
+ if (previousAlign != hAlign)
+ emit q->horizontalAlignmentChanged(hAlign);
+ }
+}
+
void QDeclarativeTextInput::drawContents(QPainter *p, const QRect &r)
{
Q_D(QDeclarativeTextInput);
@@ -1671,6 +1708,9 @@ void QDeclarativeTextInputPrivate::init()
QPalette p = control->palette();
selectedTextColor = p.color(QPalette::HighlightedText);
selectionColor = p.color(QPalette::Highlight);
+
+ if (QApplication::layoutDirection() == Qt::RightToLeft)
+ hAlign = QDeclarativeTextInput::AlignRight;
}
void QDeclarativeTextInput::cursorPosChanged()
@@ -1719,6 +1759,7 @@ void QDeclarativeTextInput::q_textChanged()
Q_D(QDeclarativeTextInput);
updateSize();
d->updateHorizontalScroll();
+ d->determineHorizontalAlignment();
updateMicroFocus();
emit textChanged();
emit displayTextChanged();
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h
index e1e66a9bda..a3e8d2968e 100644
--- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h
@@ -70,7 +70,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativeImplicitSizeP
Q_PROPERTY(QColor selectionColor READ selectionColor WRITE setSelectionColor NOTIFY selectionColorChanged)
Q_PROPERTY(QColor selectedTextColor READ selectedTextColor WRITE setSelectedTextColor NOTIFY selectedTextColorChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
- Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged)
+ Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign RESET resetHAlign NOTIFY horizontalAlignmentChanged)
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly NOTIFY readOnlyChanged)
Q_PROPERTY(bool cursorVisible READ isCursorVisible WRITE setCursorVisible NOTIFY cursorVisibleChanged)
@@ -146,6 +146,7 @@ public:
HAlignment hAlign() const;
void setHAlign(HAlignment align);
+ void resetHAlign();
bool isReadOnly() const;
void setReadOnly(bool);
diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h
index f7446b479b..321d124299 100644
--- a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h
+++ b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h
@@ -74,9 +74,9 @@ public:
color((QRgb)0), style(QDeclarativeText::Normal),
styleColor((QRgb)0), hAlign(QDeclarativeTextInput::AlignLeft),
mouseSelectionMode(QDeclarativeTextInput::SelectCharacters),
- hscroll(0), oldScroll(0), focused(false), focusOnPress(true),
+ hscroll(0), oldScroll(0), oldValidity(false), focused(false), focusOnPress(true),
showInputPanelOnFocus(true), clickCausedFocus(false), cursorVisible(false),
- autoScroll(true), selectByMouse(false), canPaste(false)
+ autoScroll(true), selectByMouse(false), canPaste(false), hAlignImplicit(true)
{
#ifdef Q_OS_SYMBIAN
if (QSysInfo::symbianVersion() == QSysInfo::SV_SF_1 || QSysInfo::symbianVersion() == QSysInfo::SV_SF_3) {
@@ -103,6 +103,7 @@ public:
void startCreatingCursor();
void focusChanged(bool hasFocus);
void updateHorizontalScroll();
+ void determineHorizontalAlignment();
int calculateTextWidth();
QLineControl* control;
@@ -124,17 +125,18 @@ public:
int lastSelectionEnd;
int oldHeight;
int oldWidth;
- bool oldValidity;
int hscroll;
int oldScroll;
- bool focused;
- bool focusOnPress;
- bool showInputPanelOnFocus;
- bool clickCausedFocus;
- bool cursorVisible;
- bool autoScroll;
- bool selectByMouse;
- bool canPaste;
+ bool oldValidity:1;
+ bool focused:1;
+ bool focusOnPress:1;
+ bool showInputPanelOnFocus:1;
+ bool clickCausedFocus:1;
+ bool cursorVisible:1;
+ bool autoScroll:1;
+ bool selectByMouse:1;
+ bool canPaste:1;
+ bool hAlignImplicit:1;
static inline QDeclarativeTextInputPrivate *get(QDeclarativeTextInput *t) {
return t->d_func();
diff --git a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
index 05546cb366..33c8b89c9b 100644
--- a/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
+++ b/tests/auto/declarative/qdeclarativetext/tst_qdeclarativetext.cpp
@@ -517,18 +517,44 @@ void tst_qdeclarativetext::horizontalAlignment_RightToLeft()
QDeclarativeTextPrivate *textPrivate = QDeclarativeTextPrivate::get(text);
QVERIFY(textPrivate != 0);
+ // implicit alignment should follow the reading direction of RTL text
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight);
QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2);
- // "Right" aligned
+ // explicitly left aligned
+ text->setHAlign(QDeclarativeText::AlignLeft);
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft);
+ QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2);
+
+ // explicitly right aligned
text->setHAlign(QDeclarativeText::AlignRight);
QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight);
- QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2);
+ QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2);
- // Center aligned
+ // explicitly center aligned
text->setHAlign(QDeclarativeText::AlignHCenter);
QCOMPARE(text->hAlign(), QDeclarativeText::AlignHCenter);
QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2);
- QVERIFY(textPrivate->layout.lineAt(0).x() + textPrivate->layout.lineAt(0).width() > canvas->width()/2);
+
+ // reseted alignment should go back to following the text reading direction
+ text->resetHAlign();
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight);
+ QVERIFY(textPrivate->layout.lineAt(0).x() > canvas->width()/2);
+
+ // English text should be implicitly left aligned
+ text->setText("Hello world!");
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft);
+ QVERIFY(textPrivate->layout.lineAt(0).x() < canvas->width()/2);
+
+ // empty text should implicitly follow the layout direction
+ QApplication::setLayoutDirection(Qt::RightToLeft);
+ text->setText("");
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignRight);
+ text->setHAlign(QDeclarativeText::AlignLeft);
+ QCOMPARE(text->hAlign(), QDeclarativeText::AlignLeft);
+
+ // set layout direction back to LTR to avoid affecting other autotests
+ QApplication::setLayoutDirection(Qt::LeftToRight);
delete canvas;
}
diff --git a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
index 87c2c60b65..eb029365b4 100644
--- a/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
+++ b/tests/auto/declarative/qdeclarativetextedit/tst_qdeclarativetextedit.cpp
@@ -438,20 +438,47 @@ void tst_qdeclarativetextedit::hAlign_RightToLeft()
QVERIFY(textEdit != 0);
canvas->show();
+ // implicit alignment should follow the reading direction of RTL text
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight);
QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2);
- // "Right" align
+ // explicitly left aligned
+ textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft);
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft);
+ QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2);
+
+ // explicitly right aligned
textEdit->setHAlign(QDeclarativeTextEdit::AlignRight);
QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight);
- QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2);
+ QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2);
- // Center align
- // Note that position 0 is on the right-hand side
+ // explicitly center aligned
textEdit->setHAlign(QDeclarativeTextEdit::AlignHCenter);
QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignHCenter);
- QVERIFY(textEdit->positionToRectangle(0).x() - textEdit->width() < canvas->width()/2);
QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2);
+ // reseted alignment should go back to following the text reading direction
+ textEdit->resetHAlign();
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight);
+ QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2);
+
+ // English text should be implicitly left aligned
+ textEdit->setText("Hello world!");
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft);
+ QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2);
+
+ // empty text should implicitly follow the layout direction
+ QApplication::setLayoutDirection(Qt::RightToLeft);
+ textEdit->setText("");
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignRight);
+ QVERIFY(textEdit->positionToRectangle(0).x() > canvas->width()/2);
+ textEdit->setHAlign(QDeclarativeTextEdit::AlignLeft);
+ QCOMPARE(textEdit->hAlign(), QDeclarativeTextEdit::AlignLeft);
+ QVERIFY(textEdit->positionToRectangle(0).x() < canvas->width()/2);
+
+ // set layout direction back to LTR to avoid affecting other autotests
+ QApplication::setLayoutDirection(Qt::LeftToRight);
+
delete canvas;
}
diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
index 7753f1119f..45f2cf7dc5 100644
--- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
+++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp
@@ -914,17 +914,48 @@ void tst_qdeclarativetextinput::horizontalAlignment_RightToLeft()
QVERIFY(textInputPrivate != 0);
QVERIFY(-textInputPrivate->hscroll > canvas->width()/2);
- // "Right" Align
- textInput->setHAlign(QDeclarativeTextInput::AlignRight);
+ // implicit alignment should follow the reading direction of RTL text
QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight);
+ QVERIFY(-textInputPrivate->hscroll > canvas->width()/2);
+
+ // explicitly left aligned
+ textInput->setHAlign(QDeclarativeTextInput::AlignLeft);
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft);
QVERIFY(-textInputPrivate->hscroll < canvas->width()/2);
- // Center Align
+ // explicitly right aligned
+ textInput->setHAlign(QDeclarativeTextInput::AlignRight);
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight);
+ QVERIFY(-textInputPrivate->hscroll > canvas->width()/2);
+
+ // explicitly center aligned
textInput->setHAlign(QDeclarativeTextInput::AlignHCenter);
QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignHCenter);
QVERIFY(-textInputPrivate->hscroll < canvas->width()/2);
QVERIFY(-textInputPrivate->hscroll + textInputPrivate->width() > canvas->width()/2);
+ // reseted alignment should go back to following the text reading direction
+ textInput->resetHAlign();
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight);
+ QVERIFY(-textInputPrivate->hscroll > canvas->width()/2);
+
+ // English text should be implicitly left aligned
+ textInput->setText("Hello world!");
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft);
+ QVERIFY(-textInputPrivate->hscroll < canvas->width()/2);
+
+ // empty text should implicitly follow the layout direction
+ QApplication::setLayoutDirection(Qt::RightToLeft);
+ textInput->setText("");
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignRight);
+ QVERIFY(-textInputPrivate->hscroll > canvas->width()/2);
+ textInput->setHAlign(QDeclarativeTextInput::AlignLeft);
+ QCOMPARE(textInput->hAlign(), QDeclarativeTextInput::AlignLeft);
+ QVERIFY(-textInputPrivate->hscroll < canvas->width()/2);
+
+ // set layout direction back to LTR to avoid affecting other autotests
+ QApplication::setLayoutDirection(Qt::LeftToRight);
+
delete canvas;
}