From a0272302536cb028a892f68d610256c4b83ed0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramon=20Buld=C3=B3?= Date: Tue, 12 Sep 2017 16:09:37 +0200 Subject: [PATCH] kernel: right align labels, limit size of kernel name width --- src/modules/kernel/KernelListViewDelegate.cpp | 199 +++++++++--------- 1 file changed, 100 insertions(+), 99 deletions(-) diff --git a/src/modules/kernel/KernelListViewDelegate.cpp b/src/modules/kernel/KernelListViewDelegate.cpp index b9867ab..2c1ba54 100644 --- a/src/modules/kernel/KernelListViewDelegate.cpp +++ b/src/modules/kernel/KernelListViewDelegate.cpp @@ -71,29 +71,52 @@ KernelListViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& op bool isRc = qvariant_cast( index.data( KernelModel::IsRcRole ) ); bool isRealtime = qvariant_cast( index.data( KernelModel::IsRealtimeRole ) ); - // draw name - QFont nameFont = option.font; - nameFont.setPointSize( option.font.pointSize() * 1.15 ); - QFontMetrics nameFontMetrics( nameFont ); - QSize nameSize = nameFontMetrics.size( Qt::TextSingleLine, name ); - QRectF nameRect( QPointF(), nameSize ); + // draw right side buttons + QString removeStr( tr( "Remove" ) ); + QString installStr( tr( "Install" ) ); + QString infoStr( tr( "Changelog" ) ); + QStringList buttonStringList = ( QStringList() << removeStr << installStr << infoStr ); - painter->setPen( option.palette.color( QPalette::Normal, QPalette::WindowText ) ); - nameRect.moveTopLeft( option.rect.topLeft() + QPoint( padding, padding ) ); - painter->setFont( nameFont ); - painter->drawText( nameRect, Qt::TextSingleLine, name ); + QFont buttonFont = option.font; + QFontMetrics buttonFontMetrics( buttonFont ); - // draw package - QFont packageFont = option.font; - packageFont.setPointSize( option.font.pointSize() * 0.9 ); - QFontMetrics packageFontMetrics( packageFont ); - QSize packageSize = packageFontMetrics.size( Qt::TextSingleLine, package ); - QRectF packageRect( QPointF(), packageSize ); + int buttonWidth = 0; + int buttonHeight = 0; + foreach ( const QString str, buttonStringList ) + { + QSize buttonSize = buttonFontMetrics.size( Qt::TextSingleLine, str ); + if ( buttonSize.width() > buttonWidth ) + buttonWidth = buttonSize.width(); + if ( buttonSize.height() > buttonHeight ) + buttonHeight = buttonSize.height(); + } - painter->setPen( option.palette.color( QPalette::Normal, QPalette::WindowText ) ); - packageRect.moveTopLeft( nameRect.bottomLeft() ); - painter->setFont( packageFont ); - painter->drawText( packageRect, Qt::TextSingleLine, package ); + // Draw remove/install button + QRectF buttonRect( QPointF(), QSize( buttonWidth + 20, buttonHeight + 8 ) ); + buttonRect.moveTopRight( QPointF( option.rect.right() - padding, + option.rect.center().y() - buttonHeight - 10 ) ); + + QStyleOptionButton button; + button.rect = buttonRect.toRect(); + if ( isInstalled ) + button.text = removeStr; + else + button.text = installStr; + button.state = m_stateInstallButton | QStyle::State_Enabled; + painter->setFont( buttonFont ); + QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter ); + + // Draw changelog/information button + buttonRect.moveTopRight( QPointF( option.rect.right() - padding, + option.rect.center().y() + 2 ) ); + QStyleOptionButton infoButton; + infoButton.rect = buttonRect.toRect(); + infoButton.text = infoStr; + infoButton.state = m_stateInfoButton | QStyle::State_Enabled; + painter->setFont( buttonFont ); + QString changelog = QString( ":/changelogs/%1.html" ).arg( package ); + if ( QFile( changelog ).exists() ) + QApplication::style()->drawControl( QStyle::CE_PushButton, &infoButton, painter ); // draw middle labels QString ltsStr( tr( "LTS" ) ); @@ -124,8 +147,43 @@ KernelListViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& op } QRectF labelRect( QPointF(), QSize( labelWidth + padding*2, labelHeight ) ); + // draw second column (running, installed, unsupported) + labelRect.moveTopRight( QPointF( option.rect.right() - buttonWidth - padding*2 - 40, + option.rect.top() + padding ) ); + if ( isRunning ) + { + painter->fillRect( labelRect, QColor( "#d6e9c6" ) ); + painter->setPen( QColor( "#dff0d8" ) ); + painter->drawRect( labelRect ); + painter->setPen( QColor( "#3c763d" ) ); + labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); + painter->drawText( labelRect, Qt::AlignCenter, runningStr ); + labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, labelHeight + 2 ) ); + } + + if ( isInstalled ) + { + painter->fillRect( labelRect, QColor( "#d6e9c6" ) ); + painter->setPen( QColor( "#dff0d8" ) ); + painter->drawRect( labelRect ); + painter->setPen( QColor( "#3c763d" ) ); + labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); + painter->drawText( labelRect, Qt::AlignCenter, installedStr ); + labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, labelHeight + 2 ) ); + } + + if ( isUnsupported ) + { + painter->fillRect( labelRect, QColor( "#f2dede" ) ); + painter->setPen( QColor( "#ebccd1" ) ); + painter->drawRect( labelRect ); + painter->setPen( QColor( "#a94442" ) ); + labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); + painter->drawText( labelRect, Qt::AlignCenter, unsupportedStr ); + } + // draw first column (lts, recommended, experimental) - labelRect.moveTopRight( QPointF( option.rect.center().x(), + labelRect.moveTopRight( QPointF( labelRect.topLeft().x() - 5, option.rect.top() + padding ) ); painter->setFont( labelFont ); if ( isLts ) @@ -163,87 +221,30 @@ KernelListViewDelegate::paint( QPainter* painter, const QStyleOptionViewItem& op painter->drawText( labelRect, Qt::AlignCenter, realtimeStr ); } - // draw second column (running, installed, unsupported) - labelRect.moveTopLeft( QPointF( option.rect.center().x() + 5, - option.rect.top() + padding ) ); - if ( isRunning ) - { - painter->fillRect( labelRect, QColor( "#d6e9c6" ) ); - painter->setPen( QColor( "#dff0d8" ) ); - painter->drawRect( labelRect ); - painter->setPen( QColor( "#3c763d" ) ); - labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); - painter->drawText( labelRect, Qt::AlignCenter, runningStr ); - labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, labelHeight + 2 ) ); - } - - if ( isInstalled ) - { - painter->fillRect( labelRect, QColor( "#d6e9c6" ) ); - painter->setPen( QColor( "#dff0d8" ) ); - painter->drawRect( labelRect ); - painter->setPen( QColor( "#3c763d" ) ); - labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); - painter->drawText( labelRect, Qt::AlignCenter, installedStr ); - labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, labelHeight + 2 ) ); - } - - if ( isUnsupported ) - { - painter->fillRect( labelRect, QColor( "#f2dede" ) ); - painter->setPen( QColor( "#ebccd1" ) ); - painter->drawRect( labelRect ); - painter->setPen( QColor( "#a94442" ) ); - labelRect.moveTopLeft( labelRect.topLeft() + QPoint( 0, 2 ) ); - painter->drawText( labelRect, Qt::AlignCenter, unsupportedStr ); - } - - // draw right side buttons - QString removeStr( tr( "Remove" ) ); - QString installStr( tr( "Install" ) ); - QString infoStr( tr( "Changelog" ) ); - QStringList buttonStringList = ( QStringList() << removeStr << installStr << infoStr ); - - QFont buttonFont = option.font; - QFontMetrics buttonFontMetrics( buttonFont ); - - int buttonWidth = 0; - int buttonHeight = 0; - foreach ( const QString str, buttonStringList ) - { - QSize buttonSize = buttonFontMetrics.size( Qt::TextSingleLine, str ); - if ( buttonSize.width() > buttonWidth ) - buttonWidth = buttonSize.width(); - if ( buttonSize.height() > buttonHeight ) - buttonHeight = buttonSize.height(); - } + // draw name + QFont nameFont = option.font; + nameFont.setPointSize( option.font.pointSize() * 1.75 ); + QFontMetrics nameFontMetrics( nameFont ); + QSize nameSize = nameFontMetrics.size( Qt::TextSingleLine, name ); + nameSize.setWidth(labelRect.topLeft().x() - 10); + QRectF nameRect( QPointF(), nameSize ); - // Draw remove/install button - QRectF buttonRect( QPointF(), QSize( buttonWidth + 20, buttonHeight + 8 ) ); - buttonRect.moveTopRight( QPointF( option.rect.right() - padding, - option.rect.center().y() - buttonHeight - 10 ) ); + painter->setPen( option.palette.color( QPalette::Normal, QPalette::WindowText ) ); + nameRect.moveTopLeft( option.rect.topLeft() + QPoint( padding, padding ) ); + painter->setFont( nameFont ); + painter->drawText( nameRect, Qt::TextWrapAnywhere, name ); - QStyleOptionButton button; - button.rect = buttonRect.toRect(); - if ( isInstalled ) - button.text = removeStr; - else - button.text = installStr; - button.state = m_stateInstallButton | QStyle::State_Enabled; - painter->setFont( buttonFont ); - QApplication::style()->drawControl( QStyle::CE_PushButton, &button, painter ); + // draw package + QFont packageFont = option.font; + packageFont.setPointSize( option.font.pointSize() * 0.9 ); + QFontMetrics packageFontMetrics( packageFont ); + QSize packageSize = packageFontMetrics.size( Qt::TextSingleLine, package ); + QRectF packageRect( QPointF(), packageSize ); - // Draw changelog/information button - buttonRect.moveTopRight( QPointF( option.rect.right() - padding, - option.rect.center().y() + 2 ) ); - QStyleOptionButton infoButton; - infoButton.rect = buttonRect.toRect(); - infoButton.text = infoStr; - infoButton.state = m_stateInfoButton | QStyle::State_Enabled; - painter->setFont( buttonFont ); - QString changelog = QString( ":/changelogs/%1.html" ).arg( package ); - if ( QFile( changelog ).exists() ) - QApplication::style()->drawControl( QStyle::CE_PushButton, &infoButton, painter ); + painter->setPen( option.palette.color( QPalette::Normal, QPalette::WindowText ) ); + packageRect.moveTopLeft( nameRect.bottomLeft() ); + painter->setFont( packageFont ); + painter->drawText( packageRect, Qt::TextSingleLine, package ); painter->restore(); } -- GitLab