]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Draw 2 Rectangles below mount points to represent a disk capacity bar
authorChris Holland <zrenfire@gmail.com>
Mon, 20 May 2019 17:47:48 +0000 (13:47 -0400)
committerElvis Angelaccio <elvis.angelaccio@kde.org>
Wed, 4 Nov 2020 23:16:12 +0000 (23:16 +0000)
Draws overtop text that's goes below the baseline like a 'j'. It's
fairly difficult to add extra space below the text as the places view
is a "details" view, so the text is vertically centered in the
widget's "row" as you'd desire it to in the main file view.

We can make the widget 8px taller, adding 4px above and below so
there's adequate room for the capacitybar, but that wastes 4px for
each mount point.

Ideally we'd use the "compact" view, which displays the text/size in a
column beside the icon. However the compact view was not designed to
take up the entire width of the viewport. It's also designed to
overflow with a horizontal scrollbar.

src/kitemviews/kstandarditemlistwidget.h
src/panels/places/placesitemlistwidget.cpp
src/panels/places/placesitemlistwidget.h

index d036aae662ffebc4f3aa0b4a2d63d9be4be6477b..cda7cbfd101e1c9b916709b8dd1084110c20b884 100644 (file)
@@ -171,6 +171,14 @@ protected:
     void hideEvent(QHideEvent* event) override;
     bool event(QEvent *event) override;
 
+protected:
+    struct TextInfo
+    {
+        QPointF pos;
+        QStaticText staticText;
+    };
+    QHash<QByteArray, TextInfo*> m_textInfo;
+
 public slots:
     void finishRoleEditing();
 
@@ -239,13 +247,6 @@ private:
     QRectF m_iconRect;          // Cache for KItemListWidget::iconRect()
     QPixmap m_hoverPixmap;      // Cache for modified m_pixmap when hovering the item
 
-    struct TextInfo
-    {
-        QPointF pos;
-        QStaticText staticText;
-    };
-    QHash<QByteArray, TextInfo*> m_textInfo;
-
     QRectF m_textRect;
 
     QList<QByteArray> m_sortedVisibleRoles;
index 0551829afd8a2d74e3f12516655ba71e4e39ec28..52b3baf309063857a7e75b1e8017407e998e11e4 100644 (file)
@@ -6,6 +6,16 @@
 
 #include "placesitemlistwidget.h"
 
+#include <QGraphicsView>
+#include <QStyleOption>
+
+#include <KDiskFreeSpaceInfo>
+#include <KMountPoint>
+
+#define CAPACITYBAR_HEIGHT 2
+#define CAPACITYBAR_MARGIN 2
+
+
 PlacesItemListWidget::PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
     KStandardItemListWidget(informant, parent)
 {
@@ -26,3 +36,78 @@ QPalette::ColorRole PlacesItemListWidget::normalTextColorRole() const
     return QPalette::WindowText;
 }
 
+void PlacesItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
+{
+    KStandardItemListWidget::paint(painter, option, widget);
+
+    bool drawCapacityBar = false;
+    const QUrl url = data().value("url").toUrl();
+    if (url.isLocalFile()) {
+        const QString mountPointPath = url.toLocalFile();
+        KMountPoint::Ptr mp = KMountPoint::currentMountPoints().findByPath(mountPointPath);
+        bool isMountPoint = (mp && mp->mountPoint() == mountPointPath);
+
+        if (isMountPoint) {
+            const KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(mountPointPath);
+            drawCapacityBar = info.size() != 0;
+            if (drawCapacityBar) {
+                const TextInfo* textInfo = m_textInfo.value("text");
+                if (textInfo) { // See KStandarItemListWidget::paint() for info on why we check textInfo.
+                    painter->save();
+
+                    QRect capacityRect(
+                        textInfo->pos.x(),
+                        option->rect.top() + option->rect.height() - CAPACITYBAR_HEIGHT - CAPACITYBAR_MARGIN,
+                        qMin((qreal)option->rect.width(), selectionRect().width()) - (textInfo->pos.x() - option->rect.left()),
+                        CAPACITYBAR_HEIGHT
+                    );
+
+                    const qreal ratio = (qreal)info.used() / (qreal)info.size();
+                    // qDebug() << "ratio:" << ratio << "(" << info.used() << "/" << info.size() << ")";
+
+                    const QPalette pal = palette();
+                    const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
+                    // 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;
+                    // 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;
+                    QColor normalUsedColor = styleOption().palette.color(group, role);
+
+                    QColor dangerUsedColor = QColor::fromRgb(218, 38, 38);
+
+                    // Background
+                    painter->fillRect(capacityRect, bgColor);
+
+                    // Outline
+                    // const QRect outlineRect(capacityRect.x(), capacityRect.y(), capacityRect.width() - 1, capacityRect.height() - 1);
+                    // painter->setPen(outlineColor);
+                    // 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();
+                }
+            }
+        }
+    }
+}
index 82372d622049c1203d8b50c9d1a17451bbc5cf7c..e5763ad07d0219139c2a21e2e50e1bb840fde84f 100644 (file)
@@ -21,6 +21,8 @@ public:
     PlacesItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent);
     ~PlacesItemListWidget() override;
 
+    void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = nullptr) override;
+
 protected:
     bool isHidden() const override;
     QPalette::ColorRole normalTextColorRole() const override;