Skip to content

Commit

Permalink
Merge pull request #147 from libyui/huha-fix-disk-usage-list
Browse files Browse the repository at this point in the history
Fixed Segfault in Disk Usage List
  • Loading branch information
shundhammer committed Feb 22, 2021
2 parents 790aea1 + 493bd3a commit 6dfc178
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 66 deletions.
2 changes: 1 addition & 1 deletion VERSION.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SET( VERSION_MAJOR "4")
SET( VERSION_MINOR "0" )
SET( VERSION_PATCH "1" )
SET( VERSION_PATCH "2" )
SET( VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" )

SET( SONAME_MAJOR "15" )
Expand Down
2 changes: 1 addition & 1 deletion package/libyui-qt-doc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

Name: %{parent}-doc
# DO NOT manually bump the version here; instead, use rake version:bump
Version: 4.0.1
Version: 4.0.2
Release: 0
BuildArch: noarch

Expand Down
7 changes: 7 additions & 0 deletions package/libyui-qt.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon Feb 22 14:42:14 UTC 2021 - Stefan Hundhammer <shundhammer@suse.com>

- Fixed segfault in disk usage list: Removed code for disabled
columns completely (bsc#1182555)
- 4.0.2

-------------------------------------------------------------------
Thu Feb 11 15:30:08 UTC 2021 - Ladislav Slezák <lslezak@suse.cz>

Expand Down
2 changes: 1 addition & 1 deletion package/libyui-qt.spec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Name: libyui-qt

# DO NOT manually bump the version here; instead, use rake version:bump
Version: 4.0.1
Version: 4.0.2
Release: 0

%define so_version 15
Expand Down
105 changes: 53 additions & 52 deletions src/QY2DiskUsageList.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
**/
QColor
contrastingColor( const QColor & desiredColor,
const QColor & contrastColor )
const QColor & contrastColor )
{
if ( desiredColor != contrastColor )
{
Expand All @@ -65,14 +65,15 @@ contrastingColor( const QColor & desiredColor,
}
}


/**
* Interpolate ( translate ) a value 'from' in the range between 'minFrom'
* Interpolate (translate) a value 'from' in the range between 'minFrom'
* and 'maxFrom' to a range between 'minTo' and 'maxTo'.
**/
static int
interpolate( int from,
int minFrom, int maxFrom,
int minTo, int maxTo )
int minFrom, int maxFrom,
int minTo, int maxTo )
{
if ( minFrom > maxFrom )
{
Expand Down Expand Up @@ -102,6 +103,7 @@ interpolate( int from,
return (int) x;
}


/**
* Interpolate ( in the HSV color space ) a color between 'minColor' and
* 'maxColor' for a current value 'val' so that 'minVal' corresponds to
Expand All @@ -110,11 +112,11 @@ interpolate( int from,
* Returns the interpolated color.
**/
static QColor
interpolateColor( int val,
int minVal,
int maxVal,
const QColor & minColor,
const QColor & maxColor )
interpolateColor( int val,
int minVal,
int maxVal,
const QColor & minColor,
const QColor & maxColor )
{
int minH, maxH;
int minS, maxS;
Expand All @@ -124,8 +126,8 @@ interpolateColor( int val,
maxColor.getHsv( &maxH, &maxS, &maxV );

return QColor::fromHsv( interpolate( val, minVal, maxVal, minH, maxH ),
interpolate( val, minVal, maxVal, minS, maxS ),
interpolate( val, minVal, maxVal, minV, maxV ) );
interpolate( val, minVal, maxVal, minS, maxS ),
interpolate( val, minVal, maxVal, minV, maxV ) );
}


Expand All @@ -134,25 +136,32 @@ class QY2DiskUsagePercentageItem : public QItemDelegate
QY2DiskUsageList *_view;

public:
QY2DiskUsagePercentageItem( QY2DiskUsageList *parent ) : QItemDelegate( parent ), _view( parent ) {
}

virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
QY2DiskUsagePercentageItem( QY2DiskUsageList * parent )
: QItemDelegate( parent ),
_view( parent )
{}

virtual void paint ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
painter->save();
QColor background = option.palette.color(QPalette::Window);
QColor background = option.palette.color( QPalette::Window );
painter->setBackground( background );

QY2DiskUsageListItem *item = dynamic_cast<QY2DiskUsageListItem *>(_view->itemFromIndex(index));
QY2DiskUsageListItem *item = dynamic_cast<QY2DiskUsageListItem *>( _view->itemFromIndex( index ) );

if ( item )
{
item->paintPercentageBar( painter,
option,
interpolateColor( item->usedPercent(),
60, 95,
QColor( 0, 0xa0, 0 ), // Medium dark green
QColor( 0xFF, 0, 0 ) ) ); // Bright red
item->paintPercentageBar( painter,
option,
interpolateColor( item->usedPercent(),
60, 95,
QColor( 0, 0xa0, 0 ), // Medium dark green
QColor( 0xFF, 0, 0 ) ) ); // Bright red
}

painter->restore();
}
};
Expand Down Expand Up @@ -203,7 +212,9 @@ QY2DiskUsageList::~QY2DiskUsageList()
}


void QY2DiskUsageList::drawRow( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
void QY2DiskUsageList::drawRow( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
// Intentionally bypassing the direct parent class method, use the grandparent's:
// Don't let QY2ListViewItem::_textColor / _backgroundColor interfere with our colors.
Expand All @@ -212,44 +223,38 @@ void QY2DiskUsageList::drawRow( QPainter * painter, const QStyleOptionViewItem &
}




QY2DiskUsageListItem::QY2DiskUsageListItem( QY2DiskUsageList * parent )
: QY2ListViewItem( parent )
, _diskUsageList( parent )
{
}




QY2DiskUsageListItem::~QY2DiskUsageListItem()
{
// NOP
}




void
QY2DiskUsageListItem::init( bool allFields )
{
setSizeHint( percentageBarCol(), QSize( 20, 10 ) );

setTextAlignment( usedSizeCol(), Qt::AlignRight );
setTextAlignment( freeSizeCol(), Qt::AlignRight );
setTextAlignment( freeSizeCol(), Qt::AlignRight );
setTextAlignment( totalSizeCol(), Qt::AlignRight );

if ( usedSizeCol() >= 0 ) setText( usedSizeCol(), usedSize() );
if ( freeSizeCol() >= 0 ) setText( freeSizeCol(), freeSize() );
if ( freeSizeCol() >= 0 ) setText( freeSizeCol(), freeSize() );

if ( allFields )
{
if ( totalSizeCol() >= 0 ) setText( totalSizeCol(), totalSize() );
if ( nameCol() >= 0 ) setText( nameCol(), name() );
if ( deviceNameCol() >= 0 ) setText( deviceNameCol(), deviceName() );
if ( totalSizeCol() >= 0 ) setText( totalSizeCol(), totalSize() );
if ( nameCol() >= 0 ) setText( nameCol(), name() );
}

if ( usedSizeCol() < 0 )
setToolTip( freeSizeCol(), _( "Used %1" ).arg( usedSize().form( 0, 1, true ).c_str() ) );
setToolTip( freeSizeCol(), _( "Used %1" ).arg( usedSize().form( 0, 1, true ).c_str() ) );
}


Expand All @@ -274,7 +279,7 @@ QY2DiskUsageListItem::usedPercent() const
int percent = 0;

if ( totalSize() != 0 )
percent = int(( 100 * usedSize() ) / totalSize());
percent = int( ( 100 * usedSize() ) / totalSize() );

return percent;
}
Expand All @@ -294,13 +299,10 @@ QY2DiskUsageListItem::updateData()
}





/**
* Comparison function used for sorting the list.
* Reimplemented from QTreeWidgetItem
**/
* Comparison function used for sorting the list.
* Reimplemented from QTreeWidgetItem
**/
bool
QY2DiskUsageListItem::operator<( const QTreeWidgetItem & otherListViewItem ) const
{
Expand All @@ -314,10 +316,6 @@ QY2DiskUsageListItem::operator<( const QTreeWidgetItem & otherListViewItem ) con
// Intentionally reverting sort order: Fullest first
return ( this->usedPercent() < other->usedPercent() );
}
else if ( col == usedSizeCol() )
{
return ( this->usedSize() < other->usedSize() );
}
else if ( col == freeSizeCol() )
{
return ( this->freeSize() < other->freeSize() );
Expand All @@ -331,20 +329,21 @@ QY2DiskUsageListItem::operator<( const QTreeWidgetItem & otherListViewItem ) con
return QY2ListViewItem::operator<( otherListViewItem );
}


/**
* Stolen from KDirStat::KDirTreeView with the author's permission.
**/
void
QY2DiskUsageListItem::paintPercentageBar( QPainter * painter,
QStyleOptionViewItem option,
QStyleOptionViewItem option,
const QColor & fillColor )
{
float percent = usedPercent();
if ( percent > 100.0 ) percent = 100.0;
if ( percent < 0.0 ) percent = 0.0;
int x = option.rect.left() + 1;
int y = option.rect.top() + 1;
int w = option.rect.width() - 2;
int y = option.rect.top() + 1;
int w = option.rect.width() - 2;
int h = option.rect.height() - 2;
int fillWidth = 0;

Expand All @@ -364,12 +363,14 @@ QY2DiskUsageListItem::paintPercentageBar( QPainter * painter,
// a literal percent sign to be duplicated; that would result in two
// percent signs in the output.

if ( usedPercent() > 50 ) {
if ( usedPercent() > 50 )
{
painter->setPen( treeWidget()->palette().color( QPalette::Base ) );
painter->drawText( QRect( x, y,
fillWidth - 3, h ),
Qt::AlignRight, percentageText );
} else {
} else
{
painter->setPen( treeWidget()->palette().color( QPalette::Text ) );
painter->drawText( QRect( x + fillWidth + 3, y,
w - fillWidth - 3, h ),
Expand Down
17 changes: 6 additions & 11 deletions src/QY2DiskUsageList.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,24 @@ class QY2DiskUsageList : public QY2ListView

int nameCol() const { return _nameCol; }
int percentageBarCol() const { return _percentageBarCol; }
int usedSizeCol() const { return _usedSizeCol; }
int freeSizeCol() const { return _freeSizeCol; }
int totalSizeCol() const { return _totalSizeCol; }
int deviceNameCol() const { return _deviceNameCol; }


virtual void drawRow ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
virtual void drawRow ( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const;

// make it public
// Reimplemented to make this public for use in the QItemDelegate
QTreeWidgetItem * itemFromIndex ( const QModelIndex & index ) const
{ return QY2ListView::itemFromIndex(index); }
{ return QY2ListView::itemFromIndex(index); }

protected:

int _nameCol;
int _percentageBarCol;
int _usedSizeCol;
int _freeSizeCol;
int _totalSizeCol;
int _deviceNameCol;
};


Expand All @@ -107,7 +105,6 @@ class QY2DiskUsageListItem: public QY2ListViewItem
**/
QY2DiskUsageListItem( QY2DiskUsageList * parent );


/**
* Destructor.
**/
Expand Down Expand Up @@ -197,18 +194,16 @@ class QY2DiskUsageListItem: public QY2ListViewItem

/**
* Comparison function used for sorting the list.
* Reimplemented from QTreeWidgetItem
* Reimplemented from QTreeWidgetItem.
**/
virtual bool operator< ( const QTreeWidgetItem & other ) const;

// Columns

int nameCol() const { return _diskUsageList->nameCol(); }
int percentageBarCol() const { return _diskUsageList->percentageBarCol(); }
int usedSizeCol() const { return _diskUsageList->usedSizeCol(); }
int freeSizeCol() const { return _diskUsageList->freeSizeCol(); }
int totalSizeCol() const { return _diskUsageList->totalSizeCol(); }
int deviceNameCol() const { return _diskUsageList->deviceNameCol(); }


protected:
Expand Down

0 comments on commit 6dfc178

Please sign in to comment.