From 0af434bfc18614280e7e24cdad7430938a875583 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Tue, 15 Dec 2015 16:56:10 +0100 Subject: [PATCH] Factor out text building for label view items for proper sizeHint. --- .../partition/gui/PartitionLabelsView.cpp | 92 ++++++++++--------- .../partition/gui/PartitionLabelsView.h | 1 + 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/modules/partition/gui/PartitionLabelsView.cpp b/src/modules/partition/gui/PartitionLabelsView.cpp index 154ddcb97..e9a427ca5 100644 --- a/src/modules/partition/gui/PartitionLabelsView.cpp +++ b/src/modules/partition/gui/PartitionLabelsView.cpp @@ -136,6 +136,51 @@ PartitionLabelsView::getIndexesToDraw( const QModelIndex& parent ) const } +QStringList +PartitionLabelsView::buildTexts( const QModelIndex& index ) const +{ + QString firstLine, secondLine; + + if ( index.data( PartitionModel::IsPartitionNewRole ).toBool() ) + { + QString mountPoint = index.sibling( index.row(), + PartitionModel::MountPointColumn ) + .data().toString(); + if ( mountPoint == "/" ) + firstLine = tr( "New root partition" ); + else if ( mountPoint == "/home" ) + firstLine = tr( "New home partition" ); + else if ( mountPoint == "/boot" ) + firstLine = tr( "New boot partition" ); + else if ( mountPoint.contains( "/efi" ) && + index.sibling( index.row(), + PartitionModel::FileSystemColumn ) + .data().toString() == "fat32" ) + firstLine = tr( "New EFI system partition" ); + else if ( index.sibling( index.row(), + PartitionModel::FileSystemColumn ) + .data().toString() == "linuxswap" ) + firstLine = tr( "New swap partition" ); + else + firstLine = tr( "New partition for %1" ).arg( mountPoint ); + } + else if ( index.data( PartitionModel::OsproberNameRole ).toString().isEmpty() ) + firstLine = index.data().toString(); + else + firstLine = index.data( PartitionModel::OsproberNameRole ).toString(); + + secondLine = tr( "%1 %2" ) + .arg( index.sibling( index.row(), + PartitionModel::SizeColumn ) + .data().toString() ) + .arg( index.sibling( index.row(), + PartitionModel::FileSystemColumn ) + .data().toString() ); + + return { firstLine, secondLine }; +} + + void PartitionLabelsView::drawLabels( QPainter* painter, const QRect& rect, @@ -151,46 +196,7 @@ PartitionLabelsView::drawLabels( QPainter* painter, int label_y = rect.y(); foreach ( const QModelIndex& index, indexesToDraw ) { - QString firstLine, secondLine; - - if ( index.data( PartitionModel::IsPartitionNewRole ).toBool() ) - { - QString mountPoint = index.sibling( index.row(), - PartitionModel::MountPointColumn ) - .data().toString(); - if ( mountPoint == "/" ) - firstLine = tr( "New root partition" ); - else if ( mountPoint == "/home" ) - firstLine = tr( "New home partition" ); - else if ( mountPoint == "/boot" ) - firstLine = tr( "New boot partition" ); - else if ( mountPoint.contains( "/efi" ) && - index.sibling( index.row(), - PartitionModel::FileSystemColumn ) - .data().toString() == "fat32" ) - firstLine = tr( "New EFI system partition" ); - else if ( index.sibling( index.row(), - PartitionModel::FileSystemColumn ) - .data().toString() == "linuxswap" ) - firstLine = tr( "New swap partition" ); - else - firstLine = tr( "New partition for %1" ).arg( mountPoint ); - } - else if ( index.data( PartitionModel::OsproberNameRole ).toString().isEmpty() ) - firstLine = index.data().toString(); - else - firstLine = index.data( PartitionModel::OsproberNameRole ).toString(); - - secondLine = tr( "%1 %2" ) - .arg( index.sibling( index.row(), - PartitionModel::SizeColumn ) - .data().toString() ) - .arg( index.sibling( index.row(), - PartitionModel::FileSystemColumn ) - .data().toString() ); - - QStringList texts = { firstLine, - secondLine }; + QStringList texts = buildTexts( index ); QSize labelSize = sizeForLabel( texts ); @@ -231,10 +237,8 @@ PartitionLabelsView::sizeForAllLabels( int maxLineWidth ) const int singleLabelHeight = 0; foreach ( const QModelIndex& index, indexesToDraw ) { - QStringList texts = { index.data().toString(), - index.sibling( index.row(), - PartitionModel::SizeColumn ) - .data().toString() }; + QStringList texts = buildTexts( index ); + QSize labelSize = sizeForLabel( texts ); if ( lineLength + labelSize.width() > maxLineWidth ) diff --git a/src/modules/partition/gui/PartitionLabelsView.h b/src/modules/partition/gui/PartitionLabelsView.h index b3dd0d541..20b7cc7f7 100644 --- a/src/modules/partition/gui/PartitionLabelsView.h +++ b/src/modules/partition/gui/PartitionLabelsView.h @@ -66,6 +66,7 @@ private: QSize sizeForLabel( const QStringList& text ) const; void drawLabel( QPainter* painter, const QStringList& text, const QColor& color, const QPoint& pos ); QModelIndexList getIndexesToDraw( const QModelIndex& parent ) const; + QStringList buildTexts( const QModelIndex& index ) const; }; #endif // PARTITIONLABELSVIEW_H