From cacdb7b634fa8fbe15cb98cc6f5b17fd6a122a9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Fri, 8 May 2020 15:09:02 +0100 Subject: [PATCH] Fix some issues spotted by David Faure. In particular, add a check for partition capacity to be positive. --- src/gui/partwidget.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/gui/partwidget.cpp b/src/gui/partwidget.cpp index 57ebf23..59dd7d4 100644 --- a/src/gui/partwidget.cpp +++ b/src/gui/partwidget.cpp @@ -93,21 +93,22 @@ void PartWidget::paintEvent(QPaintEvent*) if (partition() == nullptr) return; - const int usedPercentage = static_cast(partition()->used() * 100 / partition()->capacity()); + auto partitionCapacity = partition()->capacity(); + if (partitionCapacity <= 0) + return; + + const int usedPercentage = static_cast(partition()->used() * 100 / partitionCapacity); const int w = width() * usedPercentage / 100; QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing); + const QColor base = activeColor(m_fileSystemColorCode[ static_cast(partition()->fileSystem().type()) ]); if (partition()->roles().has(PartitionRole::Extended)) { - drawGradient(&painter, activeColor( - m_fileSystemColorCode[ static_cast(partition()->fileSystem().type()) ]), - QRect(0, 0, width(), height())); + drawGradient(&painter, base, QRect(0, 0, width(), height())); return; } - const QColor base = activeColor(m_fileSystemColorCode[ static_cast(partition()->fileSystem().type()) ]); - if (!partition()->roles().has(PartitionRole::Unallocated)) { const QColor dark = base.darker(105); const QColor light = base.lighter(120); @@ -124,11 +125,11 @@ void PartWidget::paintEvent(QPaintEvent*) QString text = partition()->deviceNode().remove(QStringLiteral("/dev/")) + QStringLiteral("\n") + QString(Capacity::formatByteSize(partition()->capacity())); const QRect textRect(0, 0, width() - 1, height() - 1); - const QRect boundingRect = painter.boundingRect(textRect, Qt::AlignVCenter | Qt::AlignHCenter, text); + const QRect boundingRect = painter.boundingRect(textRect, Qt::AlignCenter, text); if (boundingRect.x() > PartWidgetBase::borderWidth() && boundingRect.y() > PartWidgetBase::borderHeight()) { if (isActive()) - painter.setPen(QColor(255, 255, 255)); - painter.drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, text); + painter.setPen(Qt::white); + painter.drawText(textRect, Qt::AlignCenter, text); } }