diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt --- a/libs/ui/CMakeLists.txt +++ b/libs/ui/CMakeLists.txt @@ -263,6 +263,7 @@ widgets/KoStrokeConfigWidget.cpp widgets/KoFillConfigWidget.cpp widgets/KisLayerStyleAngleSelector.cpp + widgets/KisMemoryReportButton.cpp KisPaletteEditor.cpp dialogs/KisDlgPaletteEditor.cpp diff --git a/libs/ui/kis_statusbar.h b/libs/ui/kis_statusbar.h --- a/libs/ui/kis_statusbar.h +++ b/libs/ui/kis_statusbar.h @@ -34,6 +34,7 @@ class KisViewManager; class KisProgressWidget; class KoProgressUpdater; +class KisMemoryReportButton; #include "kritaui_export.h" @@ -122,7 +123,7 @@ QScopedPointer m_progressUpdater; QToolButton *m_selectionStatus; - QPushButton *m_memoryReportBox; + KisMemoryReportButton *m_memoryReportBox; QLabel *m_pointerPositionLabel; KSqueezedTextLabel *m_statusBarStatusLabel; diff --git a/libs/ui/kis_statusbar.cc b/libs/ui/kis_statusbar.cc --- 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 @@ 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 @@ 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/widgets/KisMemoryReportButton.h b/libs/ui/widgets/KisMemoryReportButton.h new file mode 100644 --- /dev/null +++ b/libs/ui/widgets/KisMemoryReportButton.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2019 Wolthera van Hövell tot Westerflier + * + * 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 +#include + +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 diff --git a/libs/ui/widgets/KisMemoryReportButton.cpp b/libs/ui/widgets/KisMemoryReportButton.cpp new file mode 100644 --- /dev/null +++ b/libs/ui/widgets/KisMemoryReportButton.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2019 Wolthera van Hövell tot Westerflier + * + * 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 +#include +#include +#include +#include + +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