diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2026-03-12 22:51:07 +1100 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2026-03-12 22:51:07 +1100 |
| commit | 27cb290423ba5c30c1856427609938c230fa9f32 (patch) | |
| tree | 037e5ede085be567b0dec680eb7a10cb423682bd /plugin | |
| parent | notifs: skip markdown parsing for plain text bodies (diff) | |
| download | caelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.tar.gz caelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.tar.bz2 caelestia-shell-27cb290423ba5c30c1856427609938c230fa9f32.zip | |
feat: replace canvas -> c++ component
Also add c++ ring buffer
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/src/Caelestia/Internal/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/arcgauge.cpp | 119 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/arcgauge.hpp | 61 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/circularbuffer.cpp | 94 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/circularbuffer.hpp | 44 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/sparklineitem.cpp | 212 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Internal/sparklineitem.hpp | 90 |
7 files changed, 623 insertions, 0 deletions
diff --git a/plugin/src/Caelestia/Internal/CMakeLists.txt b/plugin/src/Caelestia/Internal/CMakeLists.txt index bdc58db..85e85c8 100644 --- a/plugin/src/Caelestia/Internal/CMakeLists.txt +++ b/plugin/src/Caelestia/Internal/CMakeLists.txt @@ -1,11 +1,14 @@ qml_module(caelestia-internal URI Caelestia.Internal SOURCES + arcgauge.hpp arcgauge.cpp cachingimagemanager.hpp cachingimagemanager.cpp + circularbuffer.hpp circularbuffer.cpp circularindicatormanager.hpp circularindicatormanager.cpp hyprdevices.hpp hyprdevices.cpp hyprextras.hpp hyprextras.cpp logindmanager.hpp logindmanager.cpp + sparklineitem.hpp sparklineitem.cpp LIBRARIES Qt::Gui Qt::Quick diff --git a/plugin/src/Caelestia/Internal/arcgauge.cpp b/plugin/src/Caelestia/Internal/arcgauge.cpp new file mode 100644 index 0000000..d534f5f --- /dev/null +++ b/plugin/src/Caelestia/Internal/arcgauge.cpp @@ -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 diff --git a/plugin/src/Caelestia/Internal/arcgauge.hpp b/plugin/src/Caelestia/Internal/arcgauge.hpp new file mode 100644 index 0000000..4ccb1fd --- /dev/null +++ b/plugin/src/Caelestia/Internal/arcgauge.hpp @@ -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 diff --git a/plugin/src/Caelestia/Internal/circularbuffer.cpp b/plugin/src/Caelestia/Internal/circularbuffer.cpp new file mode 100644 index 0000000..9701e7f --- /dev/null +++ b/plugin/src/Caelestia/Internal/circularbuffer.cpp @@ -0,0 +1,94 @@ +#include "circularbuffer.hpp" + +#include <algorithm> + +namespace caelestia::internal { + +CircularBuffer::CircularBuffer(QObject* parent) + : QObject(parent) {} + +int CircularBuffer::capacity() const { + return m_capacity; +} + +void CircularBuffer::setCapacity(int capacity) { + if (capacity < 0) + capacity = 0; + if (m_capacity == capacity) + return; + + const auto old = values(); + + m_capacity = capacity; + m_data.resize(capacity); + m_data.fill(0.0); + m_head = 0; + m_count = 0; + + // Re-push old values, keeping the most recent ones + const auto start = old.size() > capacity ? old.size() - capacity : 0; + for (auto i = start; i < old.size(); ++i) { + m_data[m_head] = old[i]; + m_head = (m_head + 1) % m_capacity; + m_count++; + } + + emit capacityChanged(); + emit countChanged(); + emit valuesChanged(); +} + +int CircularBuffer::count() const { + return m_count; +} + +QList<qreal> CircularBuffer::values() const { + QList<qreal> result; + result.reserve(m_count); + for (int i = 0; i < m_count; ++i) + result.append(at(i)); + return result; +} + +qreal CircularBuffer::maximum() const { + if (m_count == 0) + return 0.0; + + qreal maxVal = at(0); + for (int i = 1; i < m_count; ++i) + maxVal = std::max(maxVal, at(i)); + return maxVal; +} + +void CircularBuffer::push(qreal value) { + if (m_capacity <= 0) + return; + + m_data[m_head] = value; + m_head = (m_head + 1) % m_capacity; + if (m_count < m_capacity) { + m_count++; + emit countChanged(); + } + emit valuesChanged(); +} + +void CircularBuffer::clear() { + if (m_count == 0) + return; + + m_head = 0; + m_count = 0; + emit countChanged(); + emit valuesChanged(); +} + +qreal CircularBuffer::at(int index) const { + if (index < 0 || index >= m_count) + return 0.0; + + const int actualIndex = (m_head - m_count + index + m_capacity) % m_capacity; + return m_data[actualIndex]; +} + +} // namespace caelestia::internal diff --git a/plugin/src/Caelestia/Internal/circularbuffer.hpp b/plugin/src/Caelestia/Internal/circularbuffer.hpp new file mode 100644 index 0000000..ab2dba5 --- /dev/null +++ b/plugin/src/Caelestia/Internal/circularbuffer.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include <qobject.h> +#include <qqmlintegration.h> +#include <qvector.h> + +namespace caelestia::internal { + +class CircularBuffer : public QObject { + Q_OBJECT + QML_ELEMENT + + Q_PROPERTY(int capacity READ capacity WRITE setCapacity NOTIFY capacityChanged) + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_PROPERTY(QList<qreal> values READ values NOTIFY valuesChanged) + Q_PROPERTY(qreal maximum READ maximum NOTIFY valuesChanged) + +public: + explicit CircularBuffer(QObject* parent = nullptr); + + [[nodiscard]] int capacity() const; + void setCapacity(int capacity); + + [[nodiscard]] int count() const; + [[nodiscard]] QList<qreal> values() const; + [[nodiscard]] qreal maximum() const; + + Q_INVOKABLE void push(qreal value); + Q_INVOKABLE void clear(); + Q_INVOKABLE [[nodiscard]] qreal at(int index) const; + +signals: + void capacityChanged(); + void countChanged(); + void valuesChanged(); + +private: + QVector<qreal> m_data; + int m_head = 0; + int m_count = 0; + int m_capacity = 0; +}; + +} // namespace caelestia::internal diff --git a/plugin/src/Caelestia/Internal/sparklineitem.cpp b/plugin/src/Caelestia/Internal/sparklineitem.cpp new file mode 100644 index 0000000..b4938d1 --- /dev/null +++ b/plugin/src/Caelestia/Internal/sparklineitem.cpp @@ -0,0 +1,212 @@ +#include "sparklineitem.hpp" + +#include <qpainter.h> +#include <qpainterpath.h> +#include <qpen.h> + +namespace caelestia::internal { + +SparklineItem::SparklineItem(QQuickItem* parent) + : QQuickPaintedItem(parent) { + setAntialiasing(true); +} + +void SparklineItem::paint(QPainter* painter) { + const bool has1 = m_line1 && m_line1->count() >= 2; + const bool has2 = m_line2 && m_line2->count() >= 2; + if (!has1 && !has2) + return; + + painter->setRenderHint(QPainter::Antialiasing, true); + + // Draw line1 first (behind), then line2 (in front) + if (has1) + drawLine(painter, m_line1, m_line1Color, m_line1FillAlpha); + if (has2) + drawLine(painter, m_line2, m_line2Color, m_line2FillAlpha); +} + +void SparklineItem::drawLine(QPainter* painter, CircularBuffer* buffer, const QColor& color, qreal fillAlpha) { + const qreal w = width(); + const qreal h = height(); + const int len = buffer->count(); + const qreal stepX = w / static_cast<qreal>(m_historyLength - 1); + const qreal startX = w - (len - 1) * stepX - stepX * m_slideProgress + stepX; + + // Build line path + QPainterPath linePath; + linePath.moveTo(startX, h - (buffer->at(0) / m_maxValue) * h); + for (int i = 1; i < len; ++i) { + const qreal x = startX + i * stepX; + const qreal y = h - (buffer->at(i) / m_maxValue) * h; + linePath.lineTo(x, y); + } + + // Stroke the line + QPen pen(color, m_lineWidth); + pen.setCapStyle(Qt::RoundCap); + pen.setJoinStyle(Qt::RoundJoin); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawPath(linePath); + + // Fill under the line + QPainterPath fillPath = linePath; + fillPath.lineTo(startX + (len - 1) * stepX, h); + fillPath.lineTo(startX, h); + fillPath.closeSubpath(); + + QColor fillColor = color; + fillColor.setAlphaF(static_cast<float>(fillAlpha)); + painter->setPen(Qt::NoPen); + painter->setBrush(fillColor); + painter->drawPath(fillPath); +} + +void SparklineItem::connectBuffer(CircularBuffer* buffer) { + if (!buffer) + return; + + connect(buffer, &CircularBuffer::valuesChanged, this, [this]() { + update(); + }); + connect(buffer, &QObject::destroyed, this, [this, buffer]() { + if (m_line1 == buffer) { + m_line1 = nullptr; + emit line1Changed(); + } + if (m_line2 == buffer) { + m_line2 = nullptr; + emit line2Changed(); + } + update(); + }); +} + +CircularBuffer* SparklineItem::line1() const { + return m_line1; +} + +void SparklineItem::setLine1(CircularBuffer* buffer) { + if (m_line1 == buffer) + return; + if (m_line1) + disconnect(m_line1, nullptr, this, nullptr); + m_line1 = buffer; + connectBuffer(buffer); + emit line1Changed(); + update(); +} + +CircularBuffer* SparklineItem::line2() const { + return m_line2; +} + +void SparklineItem::setLine2(CircularBuffer* buffer) { + if (m_line2 == buffer) + return; + if (m_line2) + disconnect(m_line2, nullptr, this, nullptr); + m_line2 = buffer; + connectBuffer(buffer); + emit line2Changed(); + update(); +} + +QColor SparklineItem::line1Color() const { + return m_line1Color; +} + +void SparklineItem::setLine1Color(const QColor& color) { + if (m_line1Color == color) + return; + m_line1Color = color; + emit line1ColorChanged(); + update(); +} + +QColor SparklineItem::line2Color() const { + return m_line2Color; +} + +void SparklineItem::setLine2Color(const QColor& color) { + if (m_line2Color == color) + return; + m_line2Color = color; + emit line2ColorChanged(); + update(); +} + +qreal SparklineItem::line1FillAlpha() const { + return m_line1FillAlpha; +} + +void SparklineItem::setLine1FillAlpha(qreal alpha) { + if (qFuzzyCompare(m_line1FillAlpha, alpha)) + return; + m_line1FillAlpha = alpha; + emit line1FillAlphaChanged(); + update(); +} + +qreal SparklineItem::line2FillAlpha() const { + return m_line2FillAlpha; +} + +void SparklineItem::setLine2FillAlpha(qreal alpha) { + if (qFuzzyCompare(m_line2FillAlpha, alpha)) + return; + m_line2FillAlpha = alpha; + emit line2FillAlphaChanged(); + update(); +} + +qreal SparklineItem::maxValue() const { + return m_maxValue; +} + +void SparklineItem::setMaxValue(qreal value) { + if (qFuzzyCompare(m_maxValue, value)) + return; + m_maxValue = value; + emit maxValueChanged(); + update(); +} + +qreal SparklineItem::slideProgress() const { + return m_slideProgress; +} + +void SparklineItem::setSlideProgress(qreal progress) { + if (qFuzzyCompare(m_slideProgress, progress)) + return; + m_slideProgress = progress; + emit slideProgressChanged(); + update(); +} + +int SparklineItem::historyLength() const { + return m_historyLength; +} + +void SparklineItem::setHistoryLength(int length) { + if (m_historyLength == length) + return; + m_historyLength = length; + emit historyLengthChanged(); + update(); +} + +qreal SparklineItem::lineWidth() const { + return m_lineWidth; +} + +void SparklineItem::setLineWidth(qreal width) { + if (qFuzzyCompare(m_lineWidth, width)) + return; + m_lineWidth = width; + emit lineWidthChanged(); + update(); +} + +} // namespace caelestia::internal diff --git a/plugin/src/Caelestia/Internal/sparklineitem.hpp b/plugin/src/Caelestia/Internal/sparklineitem.hpp new file mode 100644 index 0000000..945a1b3 --- /dev/null +++ b/plugin/src/Caelestia/Internal/sparklineitem.hpp @@ -0,0 +1,90 @@ +#pragma once + +#include <qcolor.h> +#include <qobject.h> +#include <qqmlintegration.h> +#include <qquickpainteditem.h> + +#include "circularbuffer.hpp" + +namespace caelestia::internal { + +class SparklineItem : public QQuickPaintedItem { + Q_OBJECT + QML_ELEMENT + + Q_PROPERTY(CircularBuffer* line1 READ line1 WRITE setLine1 NOTIFY line1Changed) + Q_PROPERTY(CircularBuffer* line2 READ line2 WRITE setLine2 NOTIFY line2Changed) + Q_PROPERTY(QColor line1Color READ line1Color WRITE setLine1Color NOTIFY line1ColorChanged) + Q_PROPERTY(QColor line2Color READ line2Color WRITE setLine2Color NOTIFY line2ColorChanged) + Q_PROPERTY(qreal line1FillAlpha READ line1FillAlpha WRITE setLine1FillAlpha NOTIFY line1FillAlphaChanged) + Q_PROPERTY(qreal line2FillAlpha READ line2FillAlpha WRITE setLine2FillAlpha NOTIFY line2FillAlphaChanged) + Q_PROPERTY(qreal maxValue READ maxValue WRITE setMaxValue NOTIFY maxValueChanged) + Q_PROPERTY(qreal slideProgress READ slideProgress WRITE setSlideProgress NOTIFY slideProgressChanged) + Q_PROPERTY(int historyLength READ historyLength WRITE setHistoryLength NOTIFY historyLengthChanged) + Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth NOTIFY lineWidthChanged) + +public: + explicit SparklineItem(QQuickItem* parent = nullptr); + + void paint(QPainter* painter) override; + + [[nodiscard]] CircularBuffer* line1() const; + void setLine1(CircularBuffer* buffer); + + [[nodiscard]] CircularBuffer* line2() const; + void setLine2(CircularBuffer* buffer); + + [[nodiscard]] QColor line1Color() const; + void setLine1Color(const QColor& color); + + [[nodiscard]] QColor line2Color() const; + void setLine2Color(const QColor& color); + + [[nodiscard]] qreal line1FillAlpha() const; + void setLine1FillAlpha(qreal alpha); + + [[nodiscard]] qreal line2FillAlpha() const; + void setLine2FillAlpha(qreal alpha); + + [[nodiscard]] qreal maxValue() const; + void setMaxValue(qreal value); + + [[nodiscard]] qreal slideProgress() const; + void setSlideProgress(qreal progress); + + [[nodiscard]] int historyLength() const; + void setHistoryLength(int length); + + [[nodiscard]] qreal lineWidth() const; + void setLineWidth(qreal width); + +signals: + void line1Changed(); + void line2Changed(); + void line1ColorChanged(); + void line2ColorChanged(); + void line1FillAlphaChanged(); + void line2FillAlphaChanged(); + void maxValueChanged(); + void slideProgressChanged(); + void historyLengthChanged(); + void lineWidthChanged(); + +private: + void drawLine(QPainter* painter, CircularBuffer* buffer, const QColor& color, qreal fillAlpha); + void connectBuffer(CircularBuffer* buffer); + + CircularBuffer* m_line1 = nullptr; + CircularBuffer* m_line2 = nullptr; + QColor m_line1Color; + QColor m_line2Color; + qreal m_line1FillAlpha = 0.15; + qreal m_line2FillAlpha = 0.2; + qreal m_maxValue = 1024.0; + qreal m_slideProgress = 0.0; + int m_historyLength = 30; + qreal m_lineWidth = 2.0; +}; + +} // namespace caelestia::internal |