File indexing completed on 2024-04-28 17:05:54

0001 /*
0002     SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "krspecialwidgets.h"
0010 #include "../krglobal.h"
0011 #include "krmaskchoice.h"
0012 #include "newftpgui.h"
0013 
0014 // QtGui
0015 #include <QPaintEvent>
0016 
0017 #include <KI18n/KLocalizedString>
0018 #include <utility>
0019 
0020 /////////////////////////////////////////////////////////////////////////////
0021 /////////////////////// Pie related widgets /////////////////////////////////
0022 /////////////////////////////////////////////////////////////////////////////
0023 // The pie-related widgets use hard-coded coordinates to create the look.
0024 // This is ok since the whole widget is fitted into an existing view and thus
0025 // no re-alignments are needed.
0026 
0027 #define LEFT 10
0028 #define BOTTOM 150
0029 #define WIDTH 120
0030 #define HEIGHT 40
0031 #define Z_HEIGHT 10
0032 #define STARTANGLE 0
0033 #define DEG(x) (16 * (x))
0034 
0035 QColor KrPie::colors[12] =
0036     {Qt::red, Qt::blue, Qt::green, Qt::cyan, Qt::magenta, Qt::gray, Qt::black, Qt::white, Qt::darkRed, Qt::darkBlue, Qt::darkMagenta, Qt::darkCyan};
0037 
0038 //////////////////////////////////////////////////////////////////////////////
0039 /////////////// KrFSDisplay - Filesystem / Freespace Display /////////////////
0040 //////////////////////////////////////////////////////////////////////////////
0041 // This is the full constructor: use it for a mounted filesystem
0042 KrFSDisplay::KrFSDisplay(QWidget *parent, QString _alias, QString _realName, KIO::filesize_t _total, KIO::filesize_t _free)
0043     : QWidget(parent)
0044     , totalSpace(_total)
0045     , freeSpace(_free)
0046     , alias(std::move(_alias))
0047     , realName(std::move(_realName))
0048     , mounted(true)
0049     , empty(false)
0050     , supermount(false)
0051 {
0052     const QMargins margins = contentsMargins();
0053     resize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0054     setMinimumSize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0055     show();
0056 }
0057 
0058 // Use this one for an unmounted filesystem
0059 KrFSDisplay::KrFSDisplay(QWidget *parent, QString _alias, QString _realName, bool sm)
0060     : QWidget(parent)
0061     , alias(std::move(_alias))
0062     , realName(std::move(_realName))
0063     , mounted(false)
0064     , empty(false)
0065     , supermount(sm)
0066 {
0067     const QMargins margins = contentsMargins();
0068     resize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0069     setMinimumSize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0070     show();
0071 }
0072 
0073 // This is used only when an empty widget needs to be displayed (for example:
0074 // when filesystem statistics haven't been calculated yet)
0075 KrFSDisplay::KrFSDisplay(QWidget *parent)
0076     : QWidget(parent)
0077     , empty(true)
0078 {
0079     const QMargins margins = contentsMargins();
0080     resize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0081     setMinimumSize(150 + margins.left() + margins.right(), 200 + margins.top() + margins.bottom());
0082     show();
0083 }
0084 
0085 // The main painter!
0086 void KrFSDisplay::paintEvent(QPaintEvent *)
0087 {
0088     QPainter paint(this);
0089     if (!empty) {
0090         const QMargins margins = contentsMargins();
0091         // create the text
0092         // first, name and location
0093         QFont font = paint.font();
0094         font.setWeight(QFont::Bold);
0095         paint.setFont(font);
0096         paint.drawText(margins.left() + 10, margins.top() + 20, alias);
0097 
0098         font.setWeight(QFont::Normal);
0099         paint.setFont(font);
0100         paint.drawText(margins.left() + 10, margins.top() + 37, '(' + realName + ')');
0101 
0102         if (mounted) { // incase the filesystem is already mounted
0103             // second, the capacity
0104             paint.drawText(margins.left() + 10, margins.top() + 70, i18n("Capacity: %1", KIO::convertSizeFromKiB(totalSpace)));
0105             // third, the 2 boxes (used, free)
0106             QPen systemPen = paint.pen();
0107             paint.setPen(Qt::black);
0108             paint.drawRect(margins.left() + 10, margins.top() + 90, 10, 10);
0109             paint.fillRect(margins.left() + 11, margins.top() + 91, 8, 8, QBrush(Qt::gray));
0110             paint.drawRect(margins.left() + 10, margins.top() + 110, 10, 10);
0111             paint.fillRect(margins.left() + 11, margins.top() + 111, 8, 8, QBrush(Qt::white));
0112             // now, the text for the boxes
0113             paint.setPen(systemPen);
0114             paint.drawText(margins.left() + 25, margins.top() + 100, i18n("Used: %1", KIO::convertSizeFromKiB(totalSpace - freeSpace)));
0115             paint.drawText(margins.left() + 25, margins.top() + 120, i18n("Free: %1", KIO::convertSizeFromKiB(freeSpace)));
0116             // first, create the empty pie
0117             // bottom...
0118             paint.setPen(Qt::black);
0119             paint.setBrush(Qt::white);
0120             paint.drawPie(margins.left() + LEFT, margins.top() + BOTTOM, WIDTH, HEIGHT, STARTANGLE, DEG(360));
0121             // body...
0122             paint.setPen(Qt::lightGray);
0123             for (int i = 1; i < Z_HEIGHT; ++i)
0124                 paint.drawPie(margins.left() + LEFT, margins.top() + BOTTOM - i, WIDTH, HEIGHT, STARTANGLE, DEG(360));
0125             // side lines...
0126             paint.setPen(Qt::black);
0127             paint.drawLine(margins.left() + LEFT, margins.top() + BOTTOM + HEIGHT / 2, LEFT, BOTTOM + HEIGHT / 2 - Z_HEIGHT);
0128             paint.drawLine(margins.left() + LEFT + WIDTH, margins.top() + BOTTOM + HEIGHT / 2, LEFT + WIDTH, BOTTOM + HEIGHT / 2 - Z_HEIGHT);
0129             // top of the pie
0130             paint.drawPie(margins.left() + LEFT, margins.top() + BOTTOM - Z_HEIGHT, WIDTH, HEIGHT, STARTANGLE, DEG(360));
0131             // the "used space" slice
0132             long double i = (static_cast<long double>(totalSpace - freeSpace) / totalSpace) * 360.0;
0133             paint.setBrush(Qt::gray);
0134             paint.drawPie(margins.left() + LEFT, margins.top() + BOTTOM - Z_HEIGHT, WIDTH, HEIGHT, STARTANGLE, (int)DEG(i));
0135             // if we need to draw a 3d stripe ...
0136             if (i > 180.0) {
0137                 for (int j = 1; j < Z_HEIGHT; ++j)
0138                     paint.drawArc(margins.left() + LEFT, margins.top() + BOTTOM - j, WIDTH, HEIGHT, STARTANGLE - 16 * 180, (int)(DEG(i - 180.0)));
0139             }
0140         } else { // if the filesystem is unmounted...
0141             font.setWeight(QFont::Bold);
0142             paint.setFont(font);
0143             paint.drawText(margins.left() + 10, margins.top() + 60, i18n("Not mounted."));
0144         }
0145     } else { // if the widget is in empty situation...
0146     }
0147 }
0148 
0149 ////////////////////////////////////////////////////////////////////////////////
0150 KrPie::KrPie(KIO::filesize_t _totalSize, QWidget *parent)
0151     : QWidget(parent)
0152     , totalSize(_totalSize)
0153 {
0154     slices.push_back(KrPieSlice(100, Qt::yellow, "DEFAULT"));
0155     sizeLeft = totalSize;
0156     resize(300, 300);
0157 }
0158 
0159 void KrPie::paintEvent(QPaintEvent *)
0160 {
0161     QPainter paint(this);
0162     // now create the slices
0163     long double sAngle = STARTANGLE;
0164     for (int ndx = 0; ndx != slices.count(); ndx++) {
0165         paint.setBrush(slices[ndx].getColor());
0166         paint.setPen(slices[ndx].getColor());
0167         // angles are negative to create a clock-wise drawing of slices
0168         long double angle = -(slices[ndx].getPerct() / 100 * 360) * 16;
0169         for (int i = 1; i < Z_HEIGHT; ++i)
0170             paint.drawPie(LEFT, BOTTOM + i, WIDTH, HEIGHT, (int)sAngle, (int)angle);
0171         sAngle += angle;
0172     }
0173     paint.setPen(Qt::yellow); // pen
0174     paint.setBrush(Qt::yellow); // fill
0175     // for (int i=1; i<Z_HEIGHT; ++i)
0176     //  paint.drawPie(LEFT,BOTTOM+i,WIDTH,HEIGHT,sAngle,360*16-(STARTANGLE-sAngle));
0177     sAngle = STARTANGLE;
0178     for (int ndx = 0; ndx != slices.count(); ndx++) {
0179         paint.setBrush(slices[ndx].getColor());
0180         paint.setPen(slices[ndx].getColor());
0181         // angles are negative to create a clock-wise drawing of slices
0182         long double angle = -(slices[ndx].getPerct() / 100 * 360) * 16;
0183         paint.drawPie(LEFT, BOTTOM, WIDTH, HEIGHT, (int)sAngle, (int)angle);
0184         sAngle += angle;
0185     }
0186 
0187     paint.setPen(Qt::black);
0188     // the pie
0189     //  paint.drawPie(LEFT,BOTTOM,WIDTH,HEIGHT,STARTANGLE,360*16);
0190     ///////////////////////// end of empty pie /////////////////////////
0191     // now, the pie is ready to draw slices on...
0192     // to make a good look on the perimeter, draw another black circle
0193     paint.setPen(Qt::black);
0194     paint.drawArc(LEFT, BOTTOM, WIDTH, HEIGHT, STARTANGLE, 360 * 16);
0195 }
0196 
0197 void KrPie::addSlice(KIO::filesize_t size, QString label)
0198 {
0199     int i = (slices.count() % 12);
0200     slices.removeLast();
0201     slices.push_back(KrPieSlice(size * 100 / totalSize, colors[i], std::move(label)));
0202     sizeLeft -= size;
0203     slices.push_back(KrPieSlice(sizeLeft * 100 / totalSize, Qt::yellow, "DEFAULT"));
0204 }