test
This commit is contained in:
@@ -7,6 +7,7 @@ qml_module(ZShell-internal
|
|||||||
circularindicatormanager.hpp circularindicatormanager.cpp
|
circularindicatormanager.hpp circularindicatormanager.cpp
|
||||||
circularbuffer.hpp circularbuffer.cpp
|
circularbuffer.hpp circularbuffer.cpp
|
||||||
sparklineitem.hpp sparklineitem.cpp
|
sparklineitem.hpp sparklineitem.cpp
|
||||||
|
arcgauge.hpp arcgauge.cpp
|
||||||
LIBRARIES
|
LIBRARIES
|
||||||
Qt::Gui
|
Qt::Gui
|
||||||
Qt::Quick
|
Qt::Quick
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
#include "arcgauge.hpp"
|
||||||
|
|
||||||
|
#include <QtMath>
|
||||||
|
#include <qpainter.h>
|
||||||
|
#include <qpen.h>
|
||||||
|
|
||||||
|
namespace caelestia::internal {
|
||||||
|
|
||||||
|
ArcGauge::ArcGauge(QQuickItem* parent)
|
||||||
|
: QQuickPaintedItem(parent) {
|
||||||
|
setAntialiasing(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::paint(QPainter* painter) {
|
||||||
|
const qreal w = width();
|
||||||
|
const qreal h = height();
|
||||||
|
const qreal side = qMin(w, h);
|
||||||
|
const qreal radius = (side - m_lineWidth - 2.0) / 2.0;
|
||||||
|
const qreal cx = w / 2.0;
|
||||||
|
const qreal cy = h / 2.0;
|
||||||
|
|
||||||
|
const QRectF arcRect(cx - radius, cy - radius, radius * 2.0, radius * 2.0);
|
||||||
|
|
||||||
|
// Convert from Canvas convention (CW radians from 3 o'clock) to QPainter (CCW 1/16th degrees)
|
||||||
|
const int startAngle16 = qRound(-(m_startAngle * 180.0 / M_PI) * 16.0);
|
||||||
|
const int sweepAngle16 = qRound(-(m_sweepAngle * 180.0 / M_PI) * 16.0);
|
||||||
|
|
||||||
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||||
|
|
||||||
|
// Draw track arc
|
||||||
|
QPen trackPen(m_trackColor, m_lineWidth);
|
||||||
|
trackPen.setCapStyle(Qt::RoundCap);
|
||||||
|
painter->setPen(trackPen);
|
||||||
|
painter->setBrush(Qt::NoBrush);
|
||||||
|
painter->drawArc(arcRect, startAngle16, sweepAngle16);
|
||||||
|
|
||||||
|
// Draw value arc
|
||||||
|
if (m_percentage > 0.0) {
|
||||||
|
const int valueSweep16 = qRound(static_cast<qreal>(sweepAngle16) * m_percentage);
|
||||||
|
QPen valuePen(m_accentColor, m_lineWidth);
|
||||||
|
valuePen.setCapStyle(Qt::RoundCap);
|
||||||
|
painter->setPen(valuePen);
|
||||||
|
painter->drawArc(arcRect, startAngle16, valueSweep16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal ArcGauge::percentage() const {
|
||||||
|
return m_percentage;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setPercentage(qreal percentage) {
|
||||||
|
if (qFuzzyCompare(m_percentage, percentage))
|
||||||
|
return;
|
||||||
|
m_percentage = percentage;
|
||||||
|
emit percentageChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor ArcGauge::accentColor() const {
|
||||||
|
return m_accentColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setAccentColor(const QColor& color) {
|
||||||
|
if (m_accentColor == color)
|
||||||
|
return;
|
||||||
|
m_accentColor = color;
|
||||||
|
emit accentColorChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor ArcGauge::trackColor() const {
|
||||||
|
return m_trackColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setTrackColor(const QColor& color) {
|
||||||
|
if (m_trackColor == color)
|
||||||
|
return;
|
||||||
|
m_trackColor = color;
|
||||||
|
emit trackColorChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal ArcGauge::startAngle() const {
|
||||||
|
return m_startAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setStartAngle(qreal angle) {
|
||||||
|
if (qFuzzyCompare(m_startAngle, angle))
|
||||||
|
return;
|
||||||
|
m_startAngle = angle;
|
||||||
|
emit startAngleChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal ArcGauge::sweepAngle() const {
|
||||||
|
return m_sweepAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setSweepAngle(qreal angle) {
|
||||||
|
if (qFuzzyCompare(m_sweepAngle, angle))
|
||||||
|
return;
|
||||||
|
m_sweepAngle = angle;
|
||||||
|
emit sweepAngleChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal ArcGauge::lineWidth() const {
|
||||||
|
return m_lineWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ArcGauge::setLineWidth(qreal width) {
|
||||||
|
if (qFuzzyCompare(m_lineWidth, width))
|
||||||
|
return;
|
||||||
|
m_lineWidth = width;
|
||||||
|
emit lineWidthChanged();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace caelestia::internal
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <qcolor.h>
|
||||||
|
#include <qobject.h>
|
||||||
|
#include <qqmlintegration.h>
|
||||||
|
#include <qquickpainteditem.h>
|
||||||
|
|
||||||
|
namespace caelestia::internal {
|
||||||
|
|
||||||
|
class ArcGauge : public QQuickPaintedItem {
|
||||||
|
Q_OBJECT
|
||||||
|
QML_ELEMENT
|
||||||
|
|
||||||
|
Q_PROPERTY(qreal percentage READ percentage WRITE setPercentage NOTIFY percentageChanged)
|
||||||
|
Q_PROPERTY(QColor accentColor READ accentColor WRITE setAccentColor NOTIFY accentColorChanged)
|
||||||
|
Q_PROPERTY(QColor trackColor READ trackColor WRITE setTrackColor NOTIFY trackColorChanged)
|
||||||
|
Q_PROPERTY(qreal startAngle READ startAngle WRITE setStartAngle NOTIFY startAngleChanged)
|
||||||
|
Q_PROPERTY(qreal sweepAngle READ sweepAngle WRITE setSweepAngle NOTIFY sweepAngleChanged)
|
||||||
|
Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ArcGauge(QQuickItem* parent = nullptr);
|
||||||
|
|
||||||
|
void paint(QPainter* painter) override;
|
||||||
|
|
||||||
|
[[nodiscard]] qreal percentage() const;
|
||||||
|
void setPercentage(qreal percentage);
|
||||||
|
|
||||||
|
[[nodiscard]] QColor accentColor() const;
|
||||||
|
void setAccentColor(const QColor& color);
|
||||||
|
|
||||||
|
[[nodiscard]] QColor trackColor() const;
|
||||||
|
void setTrackColor(const QColor& color);
|
||||||
|
|
||||||
|
[[nodiscard]] qreal startAngle() const;
|
||||||
|
void setStartAngle(qreal angle);
|
||||||
|
|
||||||
|
[[nodiscard]] qreal sweepAngle() const;
|
||||||
|
void setSweepAngle(qreal angle);
|
||||||
|
|
||||||
|
[[nodiscard]] qreal lineWidth() const;
|
||||||
|
void setLineWidth(qreal width);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void percentageChanged();
|
||||||
|
void accentColorChanged();
|
||||||
|
void trackColorChanged();
|
||||||
|
void startAngleChanged();
|
||||||
|
void sweepAngleChanged();
|
||||||
|
void lineWidthChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
qreal m_percentage = 0.0;
|
||||||
|
QColor m_accentColor;
|
||||||
|
QColor m_trackColor;
|
||||||
|
qreal m_startAngle = 0.75 * M_PI;
|
||||||
|
qreal m_sweepAngle = 1.5 * M_PI;
|
||||||
|
qreal m_lineWidth = 10.0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace caelestia::internal
|
||||||
Reference in New Issue
Block a user