Skip to content

Commit

Permalink
Add drawCircleProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Jan 29, 2024
1 parent c05ff24 commit 0a01bbf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/powerkit_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <QSettings>
#include <QPalette>
#include <QDebug>
#include <QPainter>
#include <QPen>

#include "powerkit_def.h"
#include "powerkit_settings.h"
Expand Down Expand Up @@ -86,3 +88,60 @@ void Theme::setIconTheme()
}
#endif
}

const QPixmap Theme::drawCircleProgress(const int &progress,
const int &dimension,
const int &width,
const int &padding,
const bool dash,
const QString &text,
const QColor &color1,
const QColor &color2)
{
double value = (double)progress / 100;
if (value < 0.) { value = 0.; }
else if (value > 1.) { value = 1.; }

QRectF circle(padding / 2,
padding / 2,
dimension - padding,
dimension - padding);

int startAngle = -90 * 16;
int spanAngle = value * 360 * 16;

QPixmap pix(dimension, dimension);
pix.fill(Qt::transparent);

QPainter p(&pix);
p.setRenderHint(QPainter::Antialiasing);

QPen pen;
pen.setWidth(width);
pen.setCapStyle(Qt::FlatCap);
pen.setColor(color1);
if (dash) { pen.setDashPattern(QVector<qreal>{0.5, 1.105}); }

QPen pen2;
pen2.setWidth(width);
pen2.setColor(color2);
pen2.setCapStyle(Qt::FlatCap);

p.setPen(pen);
p.drawArc(circle, startAngle, 360 * 16);

p.setPen(pen2);
p.drawArc(circle, startAngle, spanAngle);

if (!text.isEmpty()) {
int textPadding = padding * 4;
p.drawText(QRectF(textPadding / 2,
textPadding / 2,
dimension - textPadding,
dimension - textPadding),
Qt::AlignCenter,
text);
}

return pix;
}
11 changes: 11 additions & 0 deletions src/powerkit_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
#ifndef THEME_H
#define THEME_H

#include <QPixmap>
#include <QColor>

class Theme
{
public:
static void setAppTheme();
static void setIconTheme();
static const QPixmap drawCircleProgress(const int &progress,
const int &dimension,
const int &width,
const int &padding,
const bool dash,
const QString &text,
const QColor &color1 = Qt::red,
const QColor &color2 = Qt::white);
};

#endif // THEME_H

0 comments on commit 0a01bbf

Please sign in to comment.