Factor out text building for label view items for proper sizeHint.

main
Teo Mrnjavac 9 years ago
parent 0be736acf7
commit 0af434bfc1

@ -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 void
PartitionLabelsView::drawLabels( QPainter* painter, PartitionLabelsView::drawLabels( QPainter* painter,
const QRect& rect, const QRect& rect,
@ -151,46 +196,7 @@ PartitionLabelsView::drawLabels( QPainter* painter,
int label_y = rect.y(); int label_y = rect.y();
foreach ( const QModelIndex& index, indexesToDraw ) foreach ( const QModelIndex& index, indexesToDraw )
{ {
QString firstLine, secondLine; QStringList texts = buildTexts( index );
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 };
QSize labelSize = sizeForLabel( texts ); QSize labelSize = sizeForLabel( texts );
@ -231,10 +237,8 @@ PartitionLabelsView::sizeForAllLabels( int maxLineWidth ) const
int singleLabelHeight = 0; int singleLabelHeight = 0;
foreach ( const QModelIndex& index, indexesToDraw ) foreach ( const QModelIndex& index, indexesToDraw )
{ {
QStringList texts = { index.data().toString(), QStringList texts = buildTexts( index );
index.sibling( index.row(),
PartitionModel::SizeColumn )
.data().toString() };
QSize labelSize = sizeForLabel( texts ); QSize labelSize = sizeForLabel( texts );
if ( lineLength + labelSize.width() > maxLineWidth ) if ( lineLength + labelSize.width() > maxLineWidth )

@ -66,6 +66,7 @@ private:
QSize sizeForLabel( const QStringList& text ) const; QSize sizeForLabel( const QStringList& text ) const;
void drawLabel( QPainter* painter, const QStringList& text, const QColor& color, const QPoint& pos ); void drawLabel( QPainter* painter, const QStringList& text, const QColor& color, const QPoint& pos );
QModelIndexList getIndexesToDraw( const QModelIndex& parent ) const; QModelIndexList getIndexesToDraw( const QModelIndex& parent ) const;
QStringList buildTexts( const QModelIndex& index ) const;
}; };
#endif // PARTITIONLABELSVIEW_H #endif // PARTITIONLABELSVIEW_H

Loading…
Cancel
Save