diff options
author | chning <ext-chi.ning@nokia.com> | 2010-05-04 15:23:20 +0300 |
---|---|---|
committer | Pekka Vuorela <pekka.ta.vuorela@nokia.com> | 2010-05-17 13:01:48 +0300 |
commit | 45cb8d88099ac536db6e0c98b990638678597313 (patch) | |
tree | 7e86effb60bcd9f5b70df0e65184ba65c657dd4e | |
parent | 87f8b0f80e10b82a8b9562715da6b0c4388c44c4 (diff) |
Fixes: NB#163243 DuiTextEdit in Multi Line mode is not working properly
RevBy: Pekka Vuorela, John Tapsell
Details: MTextEditView::sizeHint() should return correct size according
which and constraint. Also update ut_mtexteditview.
-rw-r--r-- | src/views/mtexteditview.cpp | 99 | ||||
-rw-r--r-- | tests/ut_mtexteditview/ut_mtexteditview.cpp | 44 | ||||
-rw-r--r-- | tests/ut_mtexteditview/ut_mtexteditview.h | 1 |
3 files changed, 132 insertions, 12 deletions
diff --git a/src/views/mtexteditview.cpp b/src/views/mtexteditview.cpp index 1088a275..f27ec56c 100644 --- a/src/views/mtexteditview.cpp +++ b/src/views/mtexteditview.cpp @@ -852,15 +852,100 @@ void MTextEditView::updateCursorPosition(QGraphicsSceneMouseEvent *event, const QSizeF MTextEditView::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { Q_D(const MTextEditView); - - QSizeF hint = MWidgetView::sizeHint(which, constraint); - qreal minHeight = d->documentHeight + style()->paddingTop() + style()->paddingBottom(); - - if (hint.height() < minHeight || model()->line() == MTextEditModel::SingleLine) { - hint.setHeight(minHeight); + // sizeHint follows below rules: + // Qt::MinimumSize: + // Returns the size given by style system if it is valid. Otherwise returns a size, which + // width is the width of one character, the height is the minimum height of one line. + // Qt::PreferredSize: + // Returns the size given by style system or constraint if it is valid. If there are both + // valid style perferredSize and constraint, takes the smaller one. + // Otherwise uses the valid width/height from style system/constraint. Correct the invalid + // one to: + // SingleLine: + // the width is the width of the document, the height is the minimum height of one line. + // MultiLine: + // Has valid constraint width(from style or constraint): + // Resizes the current document to the given constraint's width. + // Stores that document height. Resizes current document back to old size. + // Uses the stored height (including padding). + // No valid constraint width: + // the width and height are from the size of the document (including padding). + // Qt::MaximumSize: + // Returns the size given by style system if it is valid. Otherwise returns QWIDGETSIZE_MAX + + QSizeF hint; + const QTextBlock block = d->textDocument->firstBlock(); + const QTextLayout *layout = block.layout(); + QTextLine line = layout->lineAt(0); + qreal minLineHeight = line.height() + style()->paddingTop() + style()->paddingBottom() + + 2 * d->textDocument->documentMargin(); + + switch (which) { + case Qt::MinimumSize: + hint = style()->minimumSize(); + if (hint.width() < 0) { + QFontMetrics fm(style()->font()); + qreal minWidth = fm.width('x') + style()->paddingLeft() + style()->paddingRight() + + 2 * d->textDocument->documentMargin(); + hint.setWidth(minWidth); + } + if (hint.height() < 0) { + hint.setHeight(minLineHeight); + } + break; + case Qt::PreferredSize: + { + qreal preferredWidth = hint.width(); + qreal preferredHeight = hint.height(); + if (constraint.width() >= 0 && (preferredWidth < 0 || constraint.width() < preferredWidth)) + preferredWidth = constraint.width(); + if (constraint.height() >= 0 && (preferredHeight < 0 || constraint.height() < preferredHeight)) + preferredHeight = constraint.height(); + + if (preferredWidth < 0 || preferredHeight < 0 ) { + if (model()->line() == MTextEditModel::SingleLine) { + if (preferredWidth < 0) { + preferredWidth = d->textDocument->size().width() + + style()->paddingLeft() + style()->paddingRight(); + } + if (preferredHeight < 0) { + preferredHeight = minLineHeight; + } + } else { + // multi line + if (preferredWidth > 0) { + qreal oldWidth = d->textDocument->textWidth(); + d->textDocument->setTextWidth(preferredWidth); + preferredHeight = d->textDocument->size().height() + + style()->paddingTop() + style()->paddingBottom(); + d->textDocument->setTextWidth(oldWidth); + } else { + preferredWidth = d->textDocument->size().width() + + style()->paddingLeft() + style()->paddingRight(); + if (preferredHeight < 0) + preferredHeight = d->textDocument->size().height() + + style()->paddingTop() + style()->paddingBottom(); + } + } + } + hint = QSizeF(preferredWidth, preferredHeight); + } + break; + case Qt::MaximumSize: + hint = style()->maximumSize(); + if (hint.width() < 0) { + hint.setWidth(QWIDGETSIZE_MAX); + } + if (hint.height() < 0) { + hint.setHeight(QWIDGETSIZE_MAX); + } + break; + default: + qWarning("MTextEditView::sizeHint() don't know how to handle the value of 'which' "); + hint = MWidgetView::sizeHint(which, constraint); + break; } - // FIXME: apply constraint? return hint; } diff --git a/tests/ut_mtexteditview/ut_mtexteditview.cpp b/tests/ut_mtexteditview/ut_mtexteditview.cpp index 48392f64..af74fd3c 100644 --- a/tests/ut_mtexteditview/ut_mtexteditview.cpp +++ b/tests/ut_mtexteditview/ut_mtexteditview.cpp @@ -127,26 +127,26 @@ void Ut_MTextEditView::testResizeEvent() void Ut_MTextEditView::testGrowing() { - // tests that the minimum size grows after new text is appended + // tests that the preferred size grows after new text is appended QString stringToAppend("\n\nasdf"); - QSizeF oldSize = m_subject->sizeHint(Qt::MinimumSize); + QSizeF oldSize = m_subject->sizeHint(Qt::PreferredSize); m_controller->insert(stringToAppend); - QSizeF newSize = m_subject->sizeHint(Qt::MinimumSize); + QSizeF newSize = m_subject->sizeHint(Qt::PreferredSize); QVERIFY(newSize.height() > oldSize.height()); // FIXME: first test that removing one line is in between the sizes - // test that minimum size is the same as in the start after new text is removed + // test that preferred size is the same as in the start after new text is removed QKeyEvent event(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier); for (int i = 0; i < stringToAppend.size(); ++i) { m_controller->keyPressEvent(&event); } - newSize = m_subject->sizeHint(Qt::MinimumSize); + newSize = m_subject->sizeHint(Qt::PreferredSize); QCOMPARE(newSize.height(), oldSize.height()); } @@ -223,5 +223,39 @@ void Ut_MTextEditView::testUpdateScrollWhenTextChanged() QCOMPARE(m_subject->d_ptr->hscroll, qreal(0)); } +void Ut_MTextEditView::testSizeHint() +{ + cleanup(); + m_controller = new MTextEdit(MTextEditModel::SingleLine, ""); + m_subject = new MTextEditView(m_controller); + m_controller->setView(m_subject); + m_controller->setText("This is a longish string of sample text to test size hints. Thanks!"); + + //The size hints should not depend on the current size + QSizeF preferredSize = m_controller->preferredSize(); + m_controller->resize(100,100); + QCOMPARE(preferredSize, m_controller->preferredSize()); + m_controller->resize(10,10); + QCOMPARE(preferredSize, m_controller->preferredSize()); + m_controller->resize(1000,1000); + QCOMPARE(preferredSize, m_controller->preferredSize()); + + QSizeF minimumSize = m_controller->minimumSize(); + m_controller->resize(100,100); + QCOMPARE(minimumSize, m_controller->minimumSize()); + m_controller->resize(10,10); + QCOMPARE(minimumSize, m_controller->minimumSize()); + m_controller->resize(1000,1000); + QCOMPARE(minimumSize, m_controller->minimumSize()); + + QSizeF maximumSize = m_controller->maximumSize(); + m_controller->resize(100,100); + QCOMPARE(maximumSize, m_controller->maximumSize()); + m_controller->resize(10,10); + QCOMPARE(maximumSize, m_controller->maximumSize()); + m_controller->resize(1000,1000); + QCOMPARE(maximumSize, m_controller->maximumSize()); +} + QTEST_APPLESS_MAIN(Ut_MTextEditView) diff --git a/tests/ut_mtexteditview/ut_mtexteditview.h b/tests/ut_mtexteditview/ut_mtexteditview.h index 8b754087..64f72b56 100644 --- a/tests/ut_mtexteditview/ut_mtexteditview.h +++ b/tests/ut_mtexteditview/ut_mtexteditview.h @@ -50,6 +50,7 @@ private slots: void testInputMethodQuery(); void testMaskedCharacters(); void testUpdateScrollWhenTextChanged(); + void testSizeHint(); private: MTextEdit *m_controller; |