[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: KDE/kdelibs/kdeui
From: David Faure <faure () kde ! org>
Date: 2008-11-12 12:10:29
Message-ID: 1226491829.673612.16466.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 883155 by dfaure:
Fix KWordWrap eating some characters when the input string has newlines already.
M +1 -1 tests/CMakeLists.txt
M +41 -19 tests/kwordwraptest.cpp
M +6 -4 util/kwordwrap.cpp
M +1 -1 util/kwordwrap.h
--- trunk/KDE/kdelibs/kdeui/tests/CMakeLists.txt #883154:883155
@@ -36,6 +36,7 @@
kstandardactiontest
kstandardshortcuttest
kuniqueapptest
+ kwordwraptest
kapplication_unittest
kcolorutilstest
kxmlgui_unittest
@@ -81,7 +82,6 @@
kdatetabletest
kinputdialogtest
ktabwidgettest
- kwordwraptest
ksqueezedtextlabeltest
kpixmapregionselectordialogtest
ktoolbarlabelactiontest
--- trunk/KDE/kdelibs/kdeui/tests/kwordwraptest.cpp #883154:883155
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003 David Faure <faure@kde.org>
+ * Copyright 2003, 2008 David Faure <faure@kde.org>
*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
@@ -17,30 +17,52 @@
* Boston, MA 02110-1301, USA.
*/
-#include <kcmdlineargs.h>
-#include <kapplication.h>
+#include <QFontMetrics>
+#include <qtest_kde.h>
+
#include <kdebug.h>
-#include <QtGui/QWidget>
+#include <QObject>
#include "kwordwrap.h"
-int main(int argc, char *argv[])
+class KWordWrap_UnitTest : public QObject
{
- KCmdLineArgs::init( argc, argv, "test", 0, ki18n("Test"), "1.0", ki18n("test \
app"));
- KApplication app;
+ Q_OBJECT
- QFont font( "helvetica", 12 ); // let's hope we all have the same...
- QFontMetrics fm( font );
- QRect r( 0, 0, 100, -1 );
- QString str = "test wadabada [/foo/bar/waba]";
- KWordWrap* ww = KWordWrap::formatText( fm, r, 0, str );
- kDebug() << str << " => " << ww->truncatedString();
- delete ww;
-
- str = "</p></p></p></p>";
- for ( ; r.width() > 0 ; r.setWidth( r.width()-10 ) )
+private Q_SLOTS:
+ void oldTruncationTest()
{
- ww = KWordWrap::formatText( fm, r, 0, str );
+ QFont font( "helvetica", 12 ); // let's hope we all have the same...
+ QFontMetrics fm( font );
+ QRect r( 0, 0, 100, -1 );
+ QString str = "test wadabada [/foo/bar/waba]";
+ KWordWrap* ww = KWordWrap::formatText( fm, r, 0, str );
kDebug() << str << " => " << ww->truncatedString();
+ QVERIFY(ww->truncatedString().endsWith("..."));
delete ww;
+
+ str = "</p></p></p></p>";
+ for ( ; r.width() > 0 ; r.setWidth( r.width()-10 ) )
+ {
+ ww = KWordWrap::formatText( fm, r, 0, str );
+ kDebug() << str << " => " << ww->truncatedString();
+ QVERIFY(ww->truncatedString().endsWith("..."));
+ delete ww;
+ }
}
-}
+
+ void testWithExistingNewlines() // when the input string has \n already
+ {
+ QRect r( 0, 0, 1000, -1 ); // very wide
+ QFont font( "helvetica", 12 ); // let's hope we all have the same...
+ QFontMetrics fm( font );
+ QString inputString = "The title here\nFoo (bar)\nFoo2 (bar2)";
+ KWordWrap* ww = KWordWrap::formatText( fm, r, 0, inputString );
+ QString str = ww->wrappedString();
+ QCOMPARE(str, inputString);
+ delete ww;
+ }
+};
+
+QTEST_KDEMAIN(KWordWrap_UnitTest, GUI)
+
+#include "kwordwraptest.moc"
--- trunk/KDE/kdelibs/kdeui/util/kwordwrap.cpp #883154:883155
@@ -61,11 +61,12 @@
bool wasBreakable = false; // value of isBreakable for last char (i-1)
bool isParens = false; // true if one of ({[
bool wasParens = false; // value of isParens for last char (i-1)
+ QString inputString = str;
for ( int i = 0 ; i < len; ++i )
{
- QChar c = str.at(i);
- int ww = fm.charWidth( str, i );
+ const QChar c = inputString.at(i);
+ const int ww = fm.charWidth(inputString, i);
isParens = ( c == QLatin1Char('(') || c == QLatin1Char('[')
|| c == QLatin1Char('{') );
@@ -74,7 +75,7 @@
// Special case for '(', '[' and '{': we want to break before them
if ( !isBreakable && i < len-1 ) {
- QChar nextc = str.at(i + 1); // look at next char
+ const QChar nextc = inputString.at(i + 1); // look at next char
isBreakable = ( nextc == QLatin1Char('(')
|| nextc == QLatin1Char('[')
|| nextc == QLatin1Char('{') );
@@ -93,7 +94,7 @@
breakAt = lastBreak;
if ( x + ww > w - 4 && lastBreak == -1 ) // time to break but found nowhere \
[-> break here] breakAt = i;
- if ( i == len - 2 && x + ww + fm.charWidth( str, i+1 ) > w ) // don't leave \
the last char alone + if (i == len - 2 && x + ww + fm.charWidth(inputString, \
i+1) > w) // don't leave the last char alone breakAt = lastBreak == -1 ? i - 1 : \
lastBreak; if ( c == QLatin1Char('\n') ) // Forced break here
{
@@ -104,6 +105,7 @@
}
// remove the line feed from the string
kw->d->m_text.remove(i, 1);
+ inputString.remove(i, 1);
len--;
}
if ( breakAt != -1 )
--- trunk/KDE/kdelibs/kdeui/util/kwordwrap.h #883154:883155
@@ -66,7 +66,7 @@
* @param len Length of text to wrap (default is -1 for all).
* @return a KWordWrap instance. The caller is responsible for storing and \
deleting the result.
*/
- static KWordWrap* formatText( QFontMetrics &fm, const QRect & r, int flags, \
const QString & str, int len = -1 ); + static KWordWrap* formatText( QFontMetrics \
&fm, const QRect & r, int flags, const QString & str, int len = -1 ); // KDE5 TODO: \
return a value, not a pointer, and use QSharedDataPointer.
/**
* @return the bounding rect, calculated by formatText. The width is the
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic