[calamares] Reformat the progresstree

- add { } to single-line blocks
 - group some includes better
 - lines broken at a different length, some shuffling around of
   parameter lists.
This commit is contained in:
Adriaan de Groot 2019-06-11 13:19:53 +02:00
parent 8053bf6f3a
commit 9235499f7f
7 changed files with 94 additions and 60 deletions

View File

@ -20,23 +20,28 @@
#include "ProgressTreeDelegate.h" #include "ProgressTreeDelegate.h"
#include "ProgressTreeModel.h" #include "ProgressTreeModel.h"
#include "Branding.h"
#include "CalamaresApplication.h" #include "CalamaresApplication.h"
#include "CalamaresWindow.h" #include "CalamaresWindow.h"
#include "Branding.h"
#include "utils/CalamaresUtilsGui.h" #include "utils/CalamaresUtilsGui.h"
#include <QPainter> #include <QPainter>
static constexpr int const item_margin = 8; static constexpr int const item_margin = 8;
static inline int item_fontsize() { return CalamaresUtils::defaultFontSize() + 4; } static inline int
item_fontsize()
{
return CalamaresUtils::defaultFontSize() + 4;
}
QSize QSize
ProgressTreeDelegate::sizeHint( const QStyleOptionViewItem& option, ProgressTreeDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
const QModelIndex& index ) const
{ {
if ( !index.isValid() ) if ( !index.isValid() )
{
return option.rect.size(); return option.rect.size();
}
QFont font = qApp->font(); QFont font = qApp->font();
@ -51,9 +56,7 @@ ProgressTreeDelegate::sizeHint( const QStyleOptionViewItem& option,
void void
ProgressTreeDelegate::paint( QPainter* painter, ProgressTreeDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{ {
QStyleOptionViewItem opt = option; QStyleOptionViewItem opt = option;
@ -62,10 +65,9 @@ ProgressTreeDelegate::paint( QPainter* painter,
initStyleOption( &opt, index ); initStyleOption( &opt, index );
opt.text.clear(); opt.text.clear();
painter->setBrush( QColor( Calamares::Branding::instance()-> painter->setBrush(
styleString( Calamares::Branding::SidebarBackground ) ) ); QColor( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarBackground ) ) );
painter->setPen( QColor( Calamares::Branding::instance()-> painter->setPen( QColor( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarText ) ) );
styleString( Calamares::Branding::SidebarText ) ) );
paintViewStep( painter, opt, index ); paintViewStep( painter, opt, index );
@ -89,15 +91,18 @@ ProgressTreeDelegate::paintViewStep( QPainter* painter,
if ( isCurrent ) if ( isCurrent )
{ {
painter->setPen( Calamares::Branding::instance()-> painter->setPen( Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarTextSelect ) );
styleString( Calamares::Branding::SidebarTextSelect ) ); QString textHighlight
QString textHighlight = Calamares::Branding::instance()-> = Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarTextHighlight );
styleString( Calamares::Branding::SidebarTextHighlight );
if ( textHighlight.isEmpty() ) if ( textHighlight.isEmpty() )
{
painter->setBrush( CalamaresApplication::instance()->mainWindow()->palette().background() ); painter->setBrush( CalamaresApplication::instance()->mainWindow()->palette().background() );
}
else else
{
painter->setBrush( QColor( textHighlight ) ); painter->setBrush( QColor( textHighlight ) );
} }
}
// Draw the text at least once. If it doesn't fit, then shrink the font // Draw the text at least once. If it doesn't fit, then shrink the font
@ -114,17 +119,19 @@ ProgressTreeDelegate::paintViewStep( QPainter* painter,
shrinkSteps++; shrinkSteps++;
QRectF boundingBox; QRectF boundingBox;
painter->drawText( textRect, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, index.data().toString(), &boundingBox ); painter->drawText(
textRect, Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextSingleLine, index.data().toString(), &boundingBox );
// The extra check here is to avoid the changing-font-size if we're not going to use // The extra check here is to avoid the changing-font-size if we're not going to use
// it in the next iteration of the loop anyway. // it in the next iteration of the loop anyway.
if ( ( shrinkSteps <= maximumShrink ) && (boundingBox.width() > textRect.width() ) ) if ( ( shrinkSteps <= maximumShrink ) && ( boundingBox.width() > textRect.width() ) )
{ {
font.setPointSize( item_fontsize() - shrinkSteps ); font.setPointSize( item_fontsize() - shrinkSteps );
painter->setFont( font ); painter->setFont( font );
} }
else else
{
break; // It fits break; // It fits
} }
while ( shrinkSteps <= maximumShrink ); } while ( shrinkSteps <= maximumShrink );
} }

View File

@ -33,16 +33,11 @@ public:
using QStyledItemDelegate::QStyledItemDelegate; using QStyledItemDelegate::QStyledItemDelegate;
protected: protected:
QSize sizeHint( const QStyleOptionViewItem& option, QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
const QModelIndex& index ) const override; void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
void paint( QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index ) const override;
private: private:
void paintViewStep( QPainter* painter, void paintViewStep( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
const QStyleOptionViewItem& option,
const QModelIndex& index ) const;
}; };
#endif // PROGRESSTREEDELEGATE_H #endif // PROGRESSTREEDELEGATE_H

View File

@ -65,8 +65,9 @@ int
ProgressTreeItem::row() const ProgressTreeItem::row() const
{ {
if ( m_parentItem ) if ( m_parentItem )
return m_parentItem->m_childItems.indexOf( {
const_cast< ProgressTreeItem* >( this ) ); return m_parentItem->m_childItems.indexOf( const_cast< ProgressTreeItem* >( this ) );
}
return 0; return 0;
} }
@ -80,7 +81,8 @@ ProgressTreeItem::parent()
ProgressTreeRoot::ProgressTreeRoot() ProgressTreeRoot::ProgressTreeRoot()
: ProgressTreeItem() : ProgressTreeItem()
{} {
}
QVariant QVariant

View File

@ -19,7 +19,8 @@
#include "ProgressTreeModel.h" #include "ProgressTreeModel.h"
#include "progresstree/ViewStepItem.h" #include "ViewStepItem.h"
#include "ViewManager.h" #include "ViewManager.h"
ProgressTreeModel::ProgressTreeModel( QObject* parent ) ProgressTreeModel::ProgressTreeModel( QObject* parent )
@ -40,7 +41,9 @@ Qt::ItemFlags
ProgressTreeModel::flags( const QModelIndex& index ) const ProgressTreeModel::flags( const QModelIndex& index ) const
{ {
if ( !index.isValid() ) if ( !index.isValid() )
{
return Qt::ItemFlags(); return Qt::ItemFlags();
}
return Qt::ItemIsEnabled; return Qt::ItemIsEnabled;
} }
@ -50,20 +53,30 @@ QModelIndex
ProgressTreeModel::index( int row, int column, const QModelIndex& parent ) const ProgressTreeModel::index( int row, int column, const QModelIndex& parent ) const
{ {
if ( !hasIndex( row, column, parent ) ) if ( !hasIndex( row, column, parent ) )
{
return QModelIndex(); return QModelIndex();
}
ProgressTreeItem* parentItem; ProgressTreeItem* parentItem;
if ( !parent.isValid() ) if ( !parent.isValid() )
{
parentItem = m_rootItem; parentItem = m_rootItem;
}
else else
{
parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() ); parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() );
}
ProgressTreeItem* childItem = parentItem->child( row ); ProgressTreeItem* childItem = parentItem->child( row );
if ( childItem ) if ( childItem )
{
return createIndex( row, column, childItem ); return createIndex( row, column, childItem );
}
else else
{
return QModelIndex(); return QModelIndex();
}
} }
@ -71,13 +84,17 @@ QModelIndex
ProgressTreeModel::parent( const QModelIndex& index ) const ProgressTreeModel::parent( const QModelIndex& index ) const
{ {
if ( !index.isValid() ) if ( !index.isValid() )
{
return QModelIndex(); return QModelIndex();
}
ProgressTreeItem* childItem = static_cast< ProgressTreeItem* >( index.internalPointer() ); ProgressTreeItem* childItem = static_cast< ProgressTreeItem* >( index.internalPointer() );
ProgressTreeItem* parentItem = childItem->parent(); ProgressTreeItem* parentItem = childItem->parent();
if ( parentItem == m_rootItem ) if ( parentItem == m_rootItem )
{
return QModelIndex(); return QModelIndex();
}
return createIndex( parentItem->row(), 0, parentItem ); return createIndex( parentItem->row(), 0, parentItem );
} }
@ -87,7 +104,9 @@ QVariant
ProgressTreeModel::data( const QModelIndex& index, int role ) const ProgressTreeModel::data( const QModelIndex& index, int role ) const
{ {
if ( !index.isValid() ) if ( !index.isValid() )
{
return QVariant(); return QVariant();
}
ProgressTreeItem* item = static_cast< ProgressTreeItem* >( index.internalPointer() ); ProgressTreeItem* item = static_cast< ProgressTreeItem* >( index.internalPointer() );
@ -111,12 +130,18 @@ ProgressTreeModel::rowCount( const QModelIndex& parent ) const
{ {
ProgressTreeItem* parentItem; ProgressTreeItem* parentItem;
if ( parent.column() > 0 ) if ( parent.column() > 0 )
{
return 0; return 0;
}
if ( !parent.isValid() ) if ( !parent.isValid() )
{
parentItem = m_rootItem; parentItem = m_rootItem;
}
else else
{
parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() ); parentItem = static_cast< ProgressTreeItem* >( parent.internalPointer() );
}
return parentItem->childCount(); return parentItem->childCount();
} }
@ -126,9 +151,13 @@ int
ProgressTreeModel::columnCount( const QModelIndex& parent ) const ProgressTreeModel::columnCount( const QModelIndex& parent ) const
{ {
if ( parent.isValid() ) if ( parent.isValid() )
{
return static_cast< ProgressTreeItem* >( parent.internalPointer() )->columnCount(); return static_cast< ProgressTreeItem* >( parent.internalPointer() )->columnCount();
}
else else
{
return m_rootItem->columnCount(); return m_rootItem->columnCount();
}
} }
@ -152,7 +181,9 @@ QModelIndex
ProgressTreeModel::indexFromItem( ProgressTreeItem* item ) ProgressTreeModel::indexFromItem( ProgressTreeItem* item )
{ {
if ( !item || !item->parent() ) if ( !item || !item->parent() )
{
return QModelIndex(); return QModelIndex();
}
// Reconstructs a QModelIndex from a ProgressTreeItem that is somewhere in the tree. // Reconstructs a QModelIndex from a ProgressTreeItem that is somewhere in the tree.
// Traverses the item to the root node, then rebuilds the qmodelindices from there // Traverses the item to the root node, then rebuilds the qmodelindices from there
@ -172,10 +203,13 @@ ProgressTreeModel::indexFromItem( ProgressTreeItem* item )
**/ **/
QList< int > childIndexList; QList< int > childIndexList;
ProgressTreeItem* curItem = item; ProgressTreeItem* curItem = item;
while ( curItem != m_rootItem ) { while ( curItem != m_rootItem )
{
int row = curItem->row(); //relative to its parent int row = curItem->row(); //relative to its parent
if ( row < 0 ) // something went wrong, bail if ( row < 0 ) // something went wrong, bail
{
return QModelIndex(); return QModelIndex();
}
childIndexList << row; childIndexList << row;
@ -184,7 +218,7 @@ ProgressTreeModel::indexFromItem( ProgressTreeItem* item )
// Now we rebuild the QModelIndex we need // Now we rebuild the QModelIndex we need
QModelIndex idx; QModelIndex idx;
for ( int i = childIndexList.size() - 1; i >= 0 ; i-- ) for ( int i = childIndexList.size() - 1; i >= 0; i-- )
{ {
idx = index( childIndexList[ i ], 0, idx ); idx = index( childIndexList[ i ], 0, idx );
} }

View File

@ -19,8 +19,9 @@
#include "ProgressTreeView.h" #include "ProgressTreeView.h"
#include "ProgressTreeDelegate.h" #include "ProgressTreeDelegate.h"
#include "ViewManager.h"
#include "Branding.h" #include "Branding.h"
#include "ViewManager.h"
ProgressTreeView* ProgressTreeView::s_instance = nullptr; ProgressTreeView* ProgressTreeView::s_instance = nullptr;
@ -35,7 +36,7 @@ ProgressTreeView::ProgressTreeView( QWidget* parent )
{ {
s_instance = this; //FIXME: should assert when s_instance gets written and it wasn't nullptr s_instance = this; //FIXME: should assert when s_instance gets written and it wasn't nullptr
this->setObjectName("sidebarMenuApp"); this->setObjectName( "sidebarMenuApp" );
setFrameShape( QFrame::NoFrame ); setFrameShape( QFrame::NoFrame );
setContentsMargins( 0, 0, 0, 0 ); setContentsMargins( 0, 0, 0, 0 );
@ -55,31 +56,29 @@ ProgressTreeView::ProgressTreeView( QWidget* parent )
setItemDelegate( m_delegate ); setItemDelegate( m_delegate );
QPalette plt = palette(); QPalette plt = palette();
plt.setColor( QPalette::Base, Calamares::Branding::instance()-> plt.setColor( QPalette::Base,
styleString( Calamares::Branding::SidebarBackground ) ); Calamares::Branding::instance()->styleString( Calamares::Branding::SidebarBackground ) );
setPalette( plt ); setPalette( plt );
} }
ProgressTreeView::~ProgressTreeView() ProgressTreeView::~ProgressTreeView() {}
{
}
void void
ProgressTreeView::setModel( QAbstractItemModel* model ) ProgressTreeView::setModel( QAbstractItemModel* model )
{ {
if ( ProgressTreeView::model() ) if ( ProgressTreeView::model() )
{
return; return;
}
QTreeView::setModel( model ); QTreeView::setModel( model );
expandAll(); expandAll();
connect( Calamares::ViewManager::instance(), connect( Calamares::ViewManager::instance(),
&Calamares::ViewManager::currentStepChanged, &Calamares::ViewManager::currentStepChanged,
this, [this]() this,
{ [this]() { viewport()->update(); },
viewport()->update();
},
Qt::UniqueConnection ); Qt::UniqueConnection );
} }

View File

@ -19,6 +19,7 @@
#include "ViewStepItem.h" #include "ViewStepItem.h"
#include "ProgressTreeModel.h" #include "ProgressTreeModel.h"
#include "Settings.h" #include "Settings.h"
#include "ViewManager.h" #include "ViewManager.h"
#include "viewpages/ViewStep.h" #include "viewpages/ViewStep.h"
@ -35,8 +36,7 @@ ViewStepItem::ViewStepItem( std::function< QString() > prettyName,
} }
ViewStepItem::ViewStepItem( const Calamares::ViewStep* step, ViewStepItem::ViewStepItem( const Calamares::ViewStep* step, ProgressTreeItem* parent )
ProgressTreeItem* parent )
: ProgressTreeItem( parent ) : ProgressTreeItem( parent )
{ {
m_step = step; m_step = step;
@ -55,8 +55,7 @@ ViewStepItem::data( int role ) const
{ {
if ( role == Qt::DisplayRole ) if ( role == Qt::DisplayRole )
{ {
return m_step ? m_step->prettyName() return m_step ? m_step->prettyName() : m_prettyName();
: m_prettyName();
} }
if ( Calamares::Settings::instance()->debugMode() && role == Qt::ToolTipRole ) if ( Calamares::Settings::instance()->debugMode() && role == Qt::ToolTipRole )
{ {
@ -66,9 +65,9 @@ ViewStepItem::data( int role ) const
toolTip.append( "<br/>Type:\tViewStep" ); toolTip.append( "<br/>Type:\tViewStep" );
toolTip.append( QString( "<br/>Pretty:\t%1" ).arg( m_step->prettyName() ) ); toolTip.append( QString( "<br/>Pretty:\t%1" ).arg( m_step->prettyName() ) );
toolTip.append( QString( "<br/>Status:\t%1" ).arg( m_step->prettyStatus() ) ); toolTip.append( QString( "<br/>Status:\t%1" ).arg( m_step->prettyStatus() ) );
toolTip.append( QString( "<br/>Source:\t%1" ).arg( toolTip.append(
m_step->moduleInstanceKey().isEmpty() ? "built-in" QString( "<br/>Source:\t%1" )
: m_step->moduleInstanceKey() ) ); .arg( m_step->moduleInstanceKey().isEmpty() ? "built-in" : m_step->moduleInstanceKey() ) );
} }
else else
{ {
@ -78,8 +77,7 @@ ViewStepItem::data( int role ) const
return toolTip; return toolTip;
} }
if ( role == ProgressTreeModel::ProgressTreeItemCurrentRole ) if ( role == ProgressTreeModel::ProgressTreeItemCurrentRole )
return m_step ? return m_step ? ( Calamares::ViewManager::instance()->currentStep() == m_step )
( Calamares::ViewManager::instance()->currentStep() == m_step ) : : ( Calamares::ViewManager::instance()->currentStep() == m_accessor() );
( Calamares::ViewManager::instance()->currentStep() == m_accessor() );
return QVariant(); return QVariant();
} }

View File

@ -37,8 +37,7 @@ public:
std::function< const Calamares::ViewStep*() > accessor, std::function< const Calamares::ViewStep*() > accessor,
ProgressTreeItem* parent = nullptr ); ProgressTreeItem* parent = nullptr );
explicit ViewStepItem( const Calamares::ViewStep* step, explicit ViewStepItem( const Calamares::ViewStep* step, ProgressTreeItem* parent = nullptr );
ProgressTreeItem* parent = nullptr );
void appendChild( ProgressTreeItem* item ) override; void appendChild( ProgressTreeItem* item ) override;