File indexing completed on 2024-04-21 15:12:14

0001 /* This file is part of the KDE Project
0002    Copyright (C) 2000 Klaas Freitag <freitag@suse.de>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "sizeindicator.h"
0021 
0022 #include <qpainter.h>
0023 #include <qevent.h>
0024 #include <QLinearGradient>
0025 
0026 #include <klocalizedstring.h>
0027 #include <kcolorscheme.h>
0028 #include <kformat.h>
0029 
0030 #include "libkookascan_logging.h"
0031 
0032 
0033 SizeIndicator::SizeIndicator(QWidget *parent, long thresh, long crit)
0034     : QLabel(parent)
0035 {
0036     setFrameStyle(QFrame::Box | QFrame::Sunken);
0037     setMinimumWidth(fontMetrics().horizontalAdvance("MMMM.MM MiB"));
0038 
0039     mThreshold = thresh;
0040     mCritical = crit;
0041     mSizeInByte = -1;
0042 }
0043 
0044 SizeIndicator::~SizeIndicator()
0045 {
0046 }
0047 
0048 void SizeIndicator::setCritical(long crit)
0049 {
0050     mCritical = crit;
0051     if (mSizeInByte >= 0 && isVisible()) {
0052         update();
0053     }
0054 }
0055 
0056 void SizeIndicator::setThreshold(long thresh)
0057 {
0058     mThreshold = thresh;
0059     if (mSizeInByte >= 0 && isVisible()) {
0060         update();
0061     }
0062 }
0063 
0064 void SizeIndicator::setSizeInByte(long newSize)
0065 {
0066     //qCDebug(LIBKOOKASCAN_LOG) << "New size" << newSize << "bytes";
0067     mSizeInByte = newSize;
0068 
0069     if (newSize < 0) {
0070         setText("");
0071     } else {
0072         setText(KFormat().formatByteSize(newSize));
0073     }
0074     // TODO: do we want JEDEC units here?
0075 }
0076 
0077 void SizeIndicator::paintEvent(QPaintEvent *ev)
0078 {
0079     QFrame::paintEvent(ev);             // draw the frame
0080 
0081     QPainter p(this);
0082 
0083     const QRect cr = contentsRect();
0084     p.setClipRect(cr);                  // paint the contents
0085     int w = cr.width();
0086     int h = cr.height();
0087 
0088     KColorScheme sch(QPalette::Normal, KColorScheme::Button);
0089 
0090     QLinearGradient g;
0091 
0092     if (mSizeInByte < DEFAULT_SMALL) {
0093         p.fillRect(0, 0, w, h, sch.background(KColorScheme::PositiveBackground).color());
0094     } else if (mSizeInByte < mThreshold) {
0095         int t = w * (1.0 - (double(mSizeInByte - DEFAULT_SMALL) / (mThreshold - DEFAULT_SMALL)));
0096         g.setStart(t - w / 5, h / 2);
0097         g.setFinalStop(t + w / 5, h / 2);
0098         g.setColorAt(0, sch.background(KColorScheme::PositiveBackground).color());
0099         g.setColorAt(1, sch.background(KColorScheme::NeutralBackground).color());
0100         p.fillRect(0, 0, w, h, QBrush(g));
0101     } else if (mSizeInByte < mCritical) {
0102         int t = w * (1.0 - (double(mSizeInByte - mThreshold) / (mCritical - mThreshold)));
0103         g.setStart(t - w / 5, h / 2);
0104         g.setFinalStop(t + w / 5, h / 2);
0105         g.setColorAt(0, sch.background(KColorScheme::NeutralBackground).color());
0106         g.setColorAt(1, sch.background(KColorScheme::NegativeBackground).color());
0107         p.fillRect(0, 0, w, h, QBrush(g));
0108     } else {
0109         p.fillRect(0, 0, w, h, sch.background(KColorScheme::NegativeBackground).color());
0110     }
0111     // display the text
0112     p.drawText(0, 0, w, h, Qt::AlignHCenter | Qt::AlignVCenter, text());
0113 }