[krita] libs/ui: Add a visual indicator to memory reporting.

Wolthera van Hövell tot Westerflier null at kde.org
Wed Feb 20 12:12:14 GMT 2019


Git commit 2baeb7890ff200bdea54700ae40a74cc60bac7b1 by Wolthera van Hövell tot Westerflier.
Committed on 20/02/2019 at 12:09.
Pushed by woltherav into branch 'master'.

Add a visual indicator to memory reporting.

This turns the memory reporting button into a slider that fills up with color
as the memory usage increases.

* From 0% to 20% it'll be the themes' highlight color.
* From 20 to 40% it'll mix to turn towards a dark yellow.
* From 40% to 80% it'll mix to turn towards a darker red.
* From 80% onwards it'll be red.

This should help less technical users determine whether their computer can handle what
they're requesting it to do.

CCMAIL:kimageshop at kde.org
Differential Revision: https://phabricator.kde.org/D19167
Ref T10368

M  +1    -0    libs/ui/CMakeLists.txt
M  +7    -1    libs/ui/kis_statusbar.cc
M  +2    -1    libs/ui/kis_statusbar.h
A  +105  -0    libs/ui/widgets/KisMemoryReportButton.cpp     [License: GPL (v2+)]
A  +46   -0    libs/ui/widgets/KisMemoryReportButton.h     [License: GPL (v2+)]

https://commits.kde.org/krita/2baeb7890ff200bdea54700ae40a74cc60bac7b1

diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt
index ff57f53158a..4d566a6fe5c 100644
--- a/libs/ui/CMakeLists.txt
+++ b/libs/ui/CMakeLists.txt
@@ -263,6 +263,7 @@ set(kritaui_LIB_SRCS
     widgets/KoStrokeConfigWidget.cpp
     widgets/KoFillConfigWidget.cpp
     widgets/KisLayerStyleAngleSelector.cpp
+    widgets/KisMemoryReportButton.cpp
 
     KisPaletteEditor.cpp
     dialogs/KisDlgPaletteEditor.cpp
diff --git a/libs/ui/kis_statusbar.cc b/libs/ui/kis_statusbar.cc
index 991beb82457..501efbe1634 100644
--- a/libs/ui/kis_statusbar.cc
+++ b/libs/ui/kis_statusbar.cc
@@ -57,6 +57,8 @@
 #include "KisMainWindow.h"
 #include "kis_config.h"
 
+#include "widgets/KisMemoryReportButton.h"
+
 enum {
     IMAGE_SIZE_ID,
     POINTER_POSITION_ID
@@ -112,7 +114,7 @@ void KisStatusBar::setup()
     m_progressUpdater.reset(new KisProgressUpdater(m_progress, m_progress->progressProxy()));
     m_progressUpdater->setAutoNestNames(true);
 
-    m_memoryReportBox = new QPushButton();
+    m_memoryReportBox = new KisMemoryReportButton();
     m_memoryReportBox->setObjectName("memoryReportBox");
     m_memoryReportBox->setFlat(true);
     m_memoryReportBox->setContentsMargins(5, 5, 5, 5);
@@ -319,6 +321,10 @@ void KisStatusBar::updateMemoryStatus()
     m_longMemoryTag = longStats;
     m_memoryStatusIcon = icon;
 
+    m_memoryReportBox->setMaximumMemory(stats.totalMemoryLimit);
+    m_memoryReportBox->setCurrentMemory(stats.totalMemorySize);
+    m_memoryReportBox->setImageWeight(stats.imageSize);
+
     emit memoryStatusUpdated();
 }
 
diff --git a/libs/ui/kis_statusbar.h b/libs/ui/kis_statusbar.h
index 14467e9180f..592b2044a80 100644
--- a/libs/ui/kis_statusbar.h
+++ b/libs/ui/kis_statusbar.h
@@ -34,6 +34,7 @@ class KSqueezedTextLabel;
 class KisViewManager;
 class KisProgressWidget;
 class KoProgressUpdater;
+class KisMemoryReportButton;
 
 #include "kritaui_export.h"
 
@@ -122,7 +123,7 @@ private:
     QScopedPointer<KoProgressUpdater> m_progressUpdater;
 
     QToolButton *m_selectionStatus;
-    QPushButton *m_memoryReportBox;
+    KisMemoryReportButton *m_memoryReportBox;
     QLabel *m_pointerPositionLabel;
 
     KSqueezedTextLabel *m_statusBarStatusLabel;
diff --git a/libs/ui/widgets/KisMemoryReportButton.cpp b/libs/ui/widgets/KisMemoryReportButton.cpp
new file mode 100644
index 00000000000..b18af3e2f44
--- /dev/null
+++ b/libs/ui/widgets/KisMemoryReportButton.cpp
@@ -0,0 +1,105 @@
+/*
+ *  Copyright (c) 2019 Wolthera van Hövell tot Westerflier <griffinvalley at gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#include "KisMemoryReportButton.h"
+#include <QPaintEvent>
+#include <QPalette>
+#include <QDebug>
+#include <QStyleOptionButton>
+#include <QStylePainter>
+
+KisMemoryReportButton::KisMemoryReportButton(QWidget *parent) :
+    QPushButton(parent)
+    , m_maxbytes(0)
+    , m_curbytes(0)
+    , m_imgbytes(0)
+{
+
+}
+
+void KisMemoryReportButton::setMaximumMemory(qint64 max)
+{
+    m_maxbytes = max;
+}
+
+void KisMemoryReportButton::setCurrentMemory(qint64 memory)
+{
+    m_curbytes = memory;
+}
+
+void KisMemoryReportButton::setImageWeight(qint64 memory)
+{
+    m_imgbytes = memory;
+}
+
+void KisMemoryReportButton::paintEvent(QPaintEvent *e)
+{
+    qreal ratioCur = qreal(m_curbytes)/qreal(m_maxbytes);
+    QStyleOptionButton buttonStyle;
+    buttonStyle.initFrom(this);
+    QRect area = this->style()->subElementRect(QStyle::SE_PushButtonFocusRect, &buttonStyle);
+
+    int totalWidth = area.width();
+
+    QStylePainter painter(this);
+
+    painter.setPen(Qt::transparent);
+    if (style()->objectName() == "breeze") {
+        painter.drawPrimitive(QStyle::PE_PanelButtonCommand, buttonStyle);
+    } else {
+        painter.drawPrimitive(QStyle::PE_Frame, buttonStyle);
+    }
+
+    QColor HL = this->palette().highlight().color();
+    QColor mid = QColor(220, 220, 0);
+    QColor warn = QColor(220, 0, 0);
+    if (ratioCur>=0.2 && ratioCur<0.4) {
+        qreal newRatio = (ratioCur-0.2)/0.2;
+        qreal negRatio = 1-newRatio;
+        HL.setRed( qMax(0, qMin(int(HL.red()*negRatio) + int(mid.red()*newRatio), 255)));
+        HL.setGreen( qMax(0, qMin(int(HL.green()*negRatio) + int(mid.green()*newRatio), 255)));
+        HL.setBlue( qMax(0, qMin(int(HL.blue()*negRatio) + int(mid.blue()*newRatio), 255)));
+    }
+    else if (ratioCur>=0.4 && ratioCur<0.8) {
+        qreal newRatio = (ratioCur-0.4)/0.4;
+        qreal negRatio = 1-newRatio;
+        HL.setRed( qMax(0, qMin(int(mid.red()*negRatio) + int(warn.red()*newRatio), 255)));
+        HL.setGreen( qMax(0, qMin(int(mid.green()*negRatio) + int(warn.green()*newRatio), 255)));
+        HL.setBlue( qMax(0, qMin(int(mid.blue()*negRatio) + int(warn.blue()*newRatio), 255)));
+    }
+    else if (ratioCur>0.8) {
+        HL = warn;
+    }
+
+    painter.setBrush(HL);
+    QRect currentBytes = area;
+    currentBytes.setWidth(int(ratioCur*totalWidth));
+    painter.setOpacity(0.5);
+
+    painter.drawRoundRect(currentBytes, 2, 2);
+
+    if (m_imgbytes<m_curbytes) {
+        QRect imageSize = area;
+        imageSize.setWidth(int((qreal(m_imgbytes)/qreal(m_maxbytes))*totalWidth));
+
+        painter.setOpacity(1.0);
+        painter.drawRoundRect(imageSize, 2, 2);
+    }
+
+    QPushButton::paintEvent(e);
+
+}
diff --git a/libs/ui/widgets/KisMemoryReportButton.h b/libs/ui/widgets/KisMemoryReportButton.h
new file mode 100644
index 00000000000..6b672fa4d74
--- /dev/null
+++ b/libs/ui/widgets/KisMemoryReportButton.h
@@ -0,0 +1,46 @@
+/*
+ *  Copyright (c) 2019 Wolthera van Hövell tot Westerflier <griffinvalley at gmail.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef KISMEMORYREPORTBUTTON_H
+#define KISMEMORYREPORTBUTTON_H
+
+#include <QPushButton>
+#include <kritaui_export.h>
+
+class KRITAUI_EXPORT KisMemoryReportButton : public QPushButton
+{
+    Q_OBJECT
+public:
+    explicit KisMemoryReportButton(QWidget *parent = 0);
+
+
+    void setMaximumMemory(qint64 max);
+
+    void setCurrentMemory(qint64 memory);
+
+    void setImageWeight(qint64 memory);
+
+    void paintEvent(QPaintEvent *e) override;
+
+private:
+    qint64 m_maxbytes;
+    qint64 m_curbytes;
+    qint64 m_imgbytes;
+};
+
+#endif // KISMEMORYREPORTBUTTON_H


More information about the kimageshop mailing list