Only check if place url is mount point once during polish event.

The capacity bars will no longer update when capacity changes. It only
checks the size on init.

Updating during polish event is only a temporarily solution just to
draft code flow for seperating logic from painting. Eventually this
logic update should be in a worker thread detached from blocking the
rendering thread that blocks launching the dolphin window.
This commit is contained in:
Chris Holland 2019-07-31 21:46:53 -04:00 committed by Elvis Angelaccio
parent ac6de72048
commit e75326077b
2 changed files with 95 additions and 59 deletions

View file

@ -6,6 +6,8 @@
#include "placesitemlistwidget.h" #include "placesitemlistwidget.h"
#include <QDebug>
#include <QGraphicsView> #include <QGraphicsView>
#include <QStyleOption> #include <QStyleOption>
@ -18,6 +20,9 @@
PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) : PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
KStandardItemListWidget(informant, parent) KStandardItemListWidget(informant, parent)
, m_isMountPoint(false)
, m_drawCapacityBar(false)
, m_capacityBarRatio(0)
{ {
} }
@ -36,78 +41,101 @@ QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
return QPalette::WindowText; return QPalette::WindowText;
} }
void PlacesItemListWidget::updateCapacityBar()
{
const QUrl url = data().value("url").toUrl();
if (url.isLocalFile()) {
const QString mountPointPath = url.toLocalFile();
qDebug() << "url:" << mountPointPath;
KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
m_isMountPoint = (mp && mp->mountPoint() == mountPointPath);
qDebug() << " isMountPoint:" << m_isMountPoint;
if (m_isMountPoint) {
const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
m_drawCapacityBar = info.size() != 0;
m_capacityBarRatio = (qreal)info.used() / (qreal)info.size();
qDebug() << " capacityBarRatio:" << m_capacityBarRatio << "(" << info.used() << "/" << info.size() << ")";
// update();
} else {
resetCapacityBar();
}
} else {
resetCapacityBar();
}
}
void PlacesItemListWidget::resetCapacityBar()
{
m_isMountPoint = false;
m_drawCapacityBar = false;
m_capacityBarRatio = 0;
}
void PlacesItemListWidget::polishEvent()
{
updateCapacityBar();
QGraphicsWidget::polishEvent();
}
void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{ {
KStandardItemListWidget::paint(painter, option, widget); KStandardItemListWidget::paint(painter, option, widget);
bool drawCapacityBar = false; if (m_drawCapacityBar) {
const QUrl url = data().value("url").toUrl(); const TextInfo* textInfo = m_textInfo.value("text");
if (url.isLocalFile()) { if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
const QString mountPointPath = url.toLocalFile(); painter->save();
KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
bool isMountPoint = (mp && mp->mountPoint() == mountPointPath);
if (isMountPoint) { QRect capacityRect(
const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath); textInfo->pos.x(),
drawCapacityBar = info.size() != 0; option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
if (drawCapacityBar) { qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
const TextInfo* textInfo = m_textInfo.value("text"); CAPACITYBAR_HEIGHT
if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo. );
painter->save();
QRect capacityRect( const QPalette pal = palette();
textInfo->pos.x(), const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN, // QColor bgColor = QColor::fromRgb(230, 230, 230);
qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()), // QColor outlineColor = QColor::fromRgb(208, 208, 208);
CAPACITYBAR_HEIGHT // QColor bgColor = QColor::fromRgb(0, 230, 0);
); // QColor outlineColor = QColor::fromRgb(208, 0, 0, 127);
// QColor normalUsedColor = QColor::fromRgb(38, 160, 218);
// QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
// QColor bgColor = pal.base().color().darker(130);
// QColor outlineColor = pal.base().color().darker(150);
const qreal ratio = (qreal)info.used() / (qreal)info.size(); QPalette::ColorRole role;
// qDebug() << "ratio:" << ratio << "(" << info.used() << "/" << info.size() << ")"; // role = isSelected() ? QPalette::Highlight : QPalette::Window;
// QColor bgColor = styleOption().palette.color(group, role).darker(150);
// QColor outlineColor = styleOption().palette.color(group, role).darker(170);
QColor bgColor = isSelected()
? styleOption().palette.color(group, QPalette::Highlight).darker(180)
: styleOption().palette.color(group, QPalette::Window).darker(120);
const QPalette pal = palette(); role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight;
const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive; QColor normalUsedColor = styleOption().palette.color(group, role);
// QColor bgColor = QColor::fromRgb(230, 230, 230);
// QColor outlineColor = QColor::fromRgb(208, 208, 208);
// QColor bgColor = QColor::fromRgb(0, 230, 0);
// QColor outlineColor = QColor::fromRgb(208, 0, 0, 127);
// QColor normalUsedColor = QColor::fromRgb(38, 160, 218);
// QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
// QColor bgColor = pal.base().color().darker(130);
// QColor outlineColor = pal.base().color().darker(150);
QPalette::ColorRole role; QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
// role = isSelected() ? QPalette::Highlight : QPalette::Window;
// QColor bgColor = styleOption().palette.color(group, role).darker(150);
// QColor outlineColor = styleOption().palette.color(group, role).darker(170);
QColor bgColor = isSelected()
? styleOption().palette.color(group, QPalette::Highlight).darker(180)
: styleOption().palette.color(group, QPalette::Window).darker(120);
role = isSelected() ? QPalette::HighlightedText : QPalette::Highlight; // Background
QColor normalUsedColor = styleOption().palette.color(group, role); painter->fillRect(capacityRect, bgColor);
QColor dangerUsedColor = QColor::fromRgb(218, 38, 38); // Outline
// const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
// painter->setPen(outlineColor);
// painter->drawRect(outlineRect);
// Background // Fill
painter->fillRect(capacityRect, bgColor); const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * m_capacityBarRatio, capacityRect.height());
if (m_capacityBarRatio < 0.95) { // Fill
// Outline painter->fillRect(fillRect, normalUsedColor);
// const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1); } else {
// painter->setPen(outlineColor); painter->fillRect(fillRect, dangerUsedColor);
// painter->drawRect(outlineRect);
// Fill
const QRect fillRect(capacityRect.x(), capacityRect.y(), capacityRect.width() * ratio, capacityRect.height());
if (ratio < 0.95) { // Fill
painter->fillRect(fillRect, normalUsedColor);
} else {
painter->fillRect(fillRect, dangerUsedColor);
}
painter->restore();
}
} }
painter->restore();
} }
} }
} }

View file

@ -22,10 +22,18 @@ public:
~PlacesItemListWidget() override; ~PlacesItemListWidget() override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
void polishEvent() override;
protected: protected:
bool isHidden() const override; bool isHidden() const override;
QPalette::ColorRole normalTextColorRole() const override; QPalette::ColorRole normalTextColorRole() const override;
void updateCapacityBar();
void resetCapacityBar();
private:
bool m_isMountPoint;
bool m_drawCapacityBar;
qreal m_capacityBarRatio;
}; };
#endif #endif