handling of '\n' in texts fixed

This commit is contained in:
Uwe Rathmann 2018-12-13 11:29:40 +01:00
parent 270c94f89f
commit 9ea7409f68
3 changed files with 72 additions and 26 deletions

View File

@ -82,7 +82,7 @@ int QskTextOptions::textFlags() const
{ {
case QskTextOptions::NoWrap: case QskTextOptions::NoWrap:
{ {
flags |= Qt::TextSingleLine; // flags |= Qt::TextSingleLine;
break; break;
} }
case QskTextOptions::WordWrap: case QskTextOptions::WordWrap:

View File

@ -210,14 +210,15 @@ QFont QskTextLabel::font() const
QSizeF QskTextLabel::contentsSizeHint() const QSizeF QskTextLabel::contentsSizeHint() const
{ {
const auto font = effectiveFont( Text );
if ( !m_data->text.isEmpty() ) if ( !m_data->text.isEmpty() )
{ {
const auto font = effectiveFont( Text );
return QskTextRenderer::textSize( return QskTextRenderer::textSize(
m_data->text, font, m_data->effectiveOptions() ); m_data->text, font, m_data->effectiveOptions() );
} }
return QSizeF( 0, 0 ); return QSizeF( 0, QFontMetricsF( font ).height() );
} }
qreal QskTextLabel::heightForWidth( qreal width ) const qreal QskTextLabel::heightForWidth( qreal width ) const
@ -225,30 +226,49 @@ qreal QskTextLabel::heightForWidth( qreal width ) const
const auto font = effectiveFont( Text ); const auto font = effectiveFont( Text );
const qreal lineHeight = QFontMetricsF( font ).height(); const qreal lineHeight = QFontMetricsF( font ).height();
if ( m_data->text.isEmpty() || qreal h = 0;
( m_data->textOptions.wrapMode() == QskTextOptions::NoWrap ) )
const auto m = margins();
if ( m_data->text.isEmpty() )
{ {
return lineHeight; h = lineHeight;
}
else if ( m_data->textOptions.effectiveElideMode() != Qt::ElideNone )
{
h = lineHeight;
}
else
{
/*
In case of QskTextOptions::NoWrap we could count
the line numbers and calculate the height from
lineHeight. TODO ...
*/
qreal maxHeight = std::numeric_limits< qreal >::max();
if ( maxHeight / lineHeight > m_data->textOptions.maximumLineCount() )
{
// be careful with overflows
maxHeight = m_data->textOptions.maximumLineCount() * lineHeight;
}
qreal w = width - m.left() + m.right();
QSizeF size( w, maxHeight );
size = QskTextRenderer::textSize(
m_data->text, font, m_data->effectiveOptions(), size );
h = size.height();
} }
qreal maxHeight = std::numeric_limits< qreal >::max(); h += m.top() + m.bottom();
if ( maxHeight / lineHeight > m_data->textOptions.maximumLineCount() )
{
// be careful with overflows
maxHeight = m_data->textOptions.maximumLineCount() * lineHeight;
}
QSizeF size( width, maxHeight ); return qCeil( h );
size = QskTextRenderer::textSize(
m_data->text, font, m_data->effectiveOptions(), size );
return qCeil( size.height() );
} }
qreal QskTextLabel::widthForHeight( qreal height ) const qreal QskTextLabel::widthForHeight( qreal height ) const
{ {
if ( m_data->text.isEmpty() || if ( m_data->text.isEmpty() )
( m_data->textOptions.wrapMode() == QskTextOptions::NoWrap ) )
{ {
return Inherited::widthForHeight( height ); return Inherited::widthForHeight( height );
} }
@ -256,11 +276,13 @@ qreal QskTextLabel::widthForHeight( qreal height ) const
const auto font = effectiveFont( Text ); const auto font = effectiveFont( Text );
const qreal maxWidth = std::numeric_limits< qreal >::max(); const qreal maxWidth = std::numeric_limits< qreal >::max();
QSizeF size( maxWidth, height ); const auto m = margins();
QSizeF size( maxWidth, height - m.top() + m.bottom() );
size = QskTextRenderer::textSize( m_data->text, font, size = QskTextRenderer::textSize( m_data->text, font,
m_data->effectiveOptions(), size ); m_data->effectiveOptions(), size );
return qCeil( size.width() ); return qCeil( size.width() + m.left() + m.right() );
} }
void QskTextLabel::changeEvent( QEvent* event ) void QskTextLabel::changeEvent( QEvent* event )

View File

@ -29,7 +29,7 @@ QSK_QT_PRIVATE_END
QSizeF QskPlainTextRenderer::textSize( QSizeF QskPlainTextRenderer::textSize(
const QString& text, const QFont& font, const QskTextOptions& options ) const QString& text, const QFont& font, const QskTextOptions& options )
{ {
// result differs from QskTextRenderer::implicitSizeHint ??? // result differs from QQuickText::implicitSizeHint ???
return textRect( text, font, options, QSizeF( 10e6, 10e6 ) ).size(); return textRect( text, font, options, QSizeF( 10e6, 10e6 ) ).size();
} }
@ -46,8 +46,14 @@ QRectF QskPlainTextRenderer::textRect(
static qreal qskLayoutText( QTextLayout* layout, static qreal qskLayoutText( QTextLayout* layout,
qreal lineWidth, const QskTextOptions& options ) qreal lineWidth, const QskTextOptions& options )
{ {
const auto maxLineCount = ( options.wrapMode() == QskTextOptions::NoWrap ) auto maxLineCount = options.maximumLineCount();
? 1 : options.maximumLineCount();
const auto elideMode = options.effectiveElideMode();
if ( elideMode != Qt::ElideNone )
{
// What about strings with newlines and elide mode
maxLineCount = 1;
}
int lineNumber = 0; int lineNumber = 0;
int characterPosition = 0; int characterPosition = 0;
@ -60,7 +66,6 @@ static qreal qskLayoutText( QTextLayout* layout,
if ( lineNumber == maxLineCount ) if ( lineNumber == maxLineCount )
{ {
const auto elideMode = options.elideMode();
const auto engine = layout->engine(); const auto engine = layout->engine();
const auto textLength = engine->text.length(); const auto textLength = engine->text.length();
@ -172,10 +177,29 @@ void QskPlainTextRenderer::updateNode( const QString& text,
QTextOption textOption( alignment ); QTextOption textOption( alignment );
textOption.setWrapMode( static_cast< QTextOption::WrapMode >( options.wrapMode() ) ); textOption.setWrapMode( static_cast< QTextOption::WrapMode >( options.wrapMode() ) );
QString tmp = text;
#if 0
const int pos = tmp.indexOf( QLatin1Char( '\x9c' ) );
if ( pos != -1 )
{
// ST: string termination
tmp = tmp.mid( 0, pos );
tmp.replace( QLatin1Char( '\n' ), QChar::LineSeparator );
}
else
#endif
if ( tmp.contains( QLatin1Char( '\n' ) ) )
{
tmp.replace( QLatin1Char('\n'), QChar::LineSeparator );
}
QTextLayout layout; QTextLayout layout;
layout.setFont( font ); layout.setFont( font );
layout.setTextOption( textOption ); layout.setTextOption( textOption );
layout.setText( text ); layout.setText( tmp );
layout.beginLayout(); layout.beginLayout();
const qreal textHeight = qskLayoutText( &layout, rect.width(), options ); const qreal textHeight = qskLayoutText( &layout, rect.width(), options );