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:
{
flags |= Qt::TextSingleLine;
// flags |= Qt::TextSingleLine;
break;
}
case QskTextOptions::WordWrap:

View File

@ -210,14 +210,15 @@ QFont QskTextLabel::font() const
QSizeF QskTextLabel::contentsSizeHint() const
{
const auto font = effectiveFont( Text );
if ( !m_data->text.isEmpty() )
{
const auto font = effectiveFont( Text );
return QskTextRenderer::textSize(
m_data->text, font, m_data->effectiveOptions() );
}
return QSizeF( 0, 0 );
return QSizeF( 0, QFontMetricsF( font ).height() );
}
qreal QskTextLabel::heightForWidth( qreal width ) const
@ -225,12 +226,25 @@ qreal QskTextLabel::heightForWidth( qreal width ) const
const auto font = effectiveFont( Text );
const qreal lineHeight = QFontMetricsF( font ).height();
if ( m_data->text.isEmpty() ||
( m_data->textOptions.wrapMode() == QskTextOptions::NoWrap ) )
{
return lineHeight;
}
qreal h = 0;
const auto m = margins();
if ( m_data->text.isEmpty() )
{
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() )
{
@ -238,17 +252,23 @@ qreal QskTextLabel::heightForWidth( qreal width ) const
maxHeight = m_data->textOptions.maximumLineCount() * lineHeight;
}
QSizeF size( width, maxHeight );
qreal w = width - m.left() + m.right();
QSizeF size( w, maxHeight );
size = QskTextRenderer::textSize(
m_data->text, font, m_data->effectiveOptions(), size );
return qCeil( size.height() );
h = size.height();
}
h += m.top() + m.bottom();
return qCeil( h );
}
qreal QskTextLabel::widthForHeight( qreal height ) const
{
if ( m_data->text.isEmpty() ||
( m_data->textOptions.wrapMode() == QskTextOptions::NoWrap ) )
if ( m_data->text.isEmpty() )
{
return Inherited::widthForHeight( height );
}
@ -256,11 +276,13 @@ qreal QskTextLabel::widthForHeight( qreal height ) const
const auto font = effectiveFont( Text );
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,
m_data->effectiveOptions(), size );
return qCeil( size.width() );
return qCeil( size.width() + m.left() + m.right() );
}
void QskTextLabel::changeEvent( QEvent* event )

View File

@ -29,7 +29,7 @@ QSK_QT_PRIVATE_END
QSizeF QskPlainTextRenderer::textSize(
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();
}
@ -46,8 +46,14 @@ QRectF QskPlainTextRenderer::textRect(
static qreal qskLayoutText( QTextLayout* layout,
qreal lineWidth, const QskTextOptions& options )
{
const auto maxLineCount = ( options.wrapMode() == QskTextOptions::NoWrap )
? 1 : options.maximumLineCount();
auto maxLineCount = 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 characterPosition = 0;
@ -60,7 +66,6 @@ static qreal qskLayoutText( QTextLayout* layout,
if ( lineNumber == maxLineCount )
{
const auto elideMode = options.elideMode();
const auto engine = layout->engine();
const auto textLength = engine->text.length();
@ -172,10 +177,29 @@ void QskPlainTextRenderer::updateNode( const QString& text,
QTextOption textOption( alignment );
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;
layout.setFont( font );
layout.setTextOption( textOption );
layout.setText( text );
layout.setText( tmp );
layout.beginLayout();
const qreal textHeight = qskLayoutText( &layout, rect.width(), options );