File size: 6,733 Bytes
bad179a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Theme, hand-painted widgets (gauges, time charts, bars, tiles, core grid, weekly
// heatmap), a flow layout and small builders for labels, buttons, panels and tables.
#pragma once
#include "core/Model.h"
#include <QFrame>
#include <QLabel>
#include <QLayout>
#include <QPainter>
#include <QPushButton>
#include <QStyledItemDelegate>
#include <QTableWidget>
#include <QVariantAnimation>
#include <functional>

namespace C { // palette
inline const QColor bg{0x0d111c}, panel{0x161c2b}, card{0x1a2133}, cardHover{0x20293f}, border{0x27314a}, grid{0x242d42},
    text{0xe6ebf5}, muted{0x8b95ab}, faint{0x5a657d}, accent{0x38bdf8}, green{0x22c55e}, amber{0xf59e0b}, red{0xef4444},
    violet{0xa78bfa}, pink{0xf472b6}, teal{0x2dd4bf}, orange{0xfb923c}, blue{0x60a5fa};
QColor level(double pct, double warn = 70, double crit = 90); // green / amber / red
QColor severity(const QString &s);
QColor health(int h);
QColor mix(const QColor &a, const QColor &b, double t);
inline QColor tint(QColor c, int alpha) { return c.setAlpha(alpha), c; }
QString styleSheet();
QIcon appIcon();
QString mono();
}

using FmtFn = std::function<QString(double)>;
inline const FmtFn pctFmt = [](double v) { return QString::number(v, 'f', 0) + "%"; }, rateFmt = Fmt::rate;
inline FmtFn numFmt(int prec) { return [prec](double v) { return QString::number(v, 'f', prec); }; }

struct AA : QPainter { // antialiased painter
    explicit AA(QPaintDevice *d) : QPainter(d) { setRenderHint(Antialiasing); }
};
QFont pxFont(QFont f, int px, bool bold = false);
void paintRing(QPainter &p, const QRectF &r, double pct, const QColor &c, double thick, bool glow = true); // 270° arc

struct RingGauge : QWidget {
    explicit RingGauge(const QString &title);
    void setValue(double pct, const QString &center = {}, const QString &sub = {}, const QColor &color = {});
    double warn = 70, crit = 90;
    QSize sizeHint() const override { return {170, 190}; }
    void paintEvent(QPaintEvent *) override;
    QString m_title, m_center, m_sub;
    QColor m_color;
    double m_target = 0, m_shown = 0;
    QVariantAnimation m_anim;
};

struct Series { QString name; QColor color; QVector<double> v; bool fill = true, dashed = false; };
struct TimeChart : QWidget {
    explicit TimeChart(const QString &title, FmtFn fmt = pctFmt, double yMax = 0); // yMax 0 = auto
    void setData(const QVector<qint64> &tMs, const QVector<Series> &s, qint64 spanMs = 0); // span 0 = all
    QString title, subtitle, empty = "Waiting for data…";
    bool stacked = false;
    QSize sizeHint() const override { return {480, 230}; }
    QSize minimumSizeHint() const override { return {260, 200}; }
    void paintEvent(QPaintEvent *) override;
    void mouseMoveEvent(QMouseEvent *) override;
    void leaveEvent(QEvent *) override { m_hover = -1, update(); }
    FmtFn m_fmt;
    double m_yMax;
    QVector<qint64> m_t;
    QVector<Series> m_s;
    qint64 m_span = 0;
    int m_hover = -1;
};

struct Segment { double value; QColor color; QString label; };
struct SegmentBar : QWidget {
    SegmentBar();
    void setSegments(const QVector<Segment> &s, double total);
    QSize sizeHint() const override { return {300, 58}; }
    void paintEvent(QPaintEvent *) override;
    QVector<Segment> m_s;
    double m_total = 1;
};

struct StatTile : QFrame {
    StatTile(const QString &title, const QColor &accent);
    void set(const QString &value, const QString &sub = {}, const QColor &color = {});
    void paintEvent(QPaintEvent *) override;
    QLabel *m_value, *m_sub;
    QColor m_accent;
};
// creates tiles in a grid (cols per row) from {title, colour} pairs
QVector<StatTile *> addTiles(class QGridLayout *g, const QVector<QPair<QString, QColor>> &defs, int cols = 99);

struct CoreGrid : QWidget {
    CoreGrid();
    void setCores(const QVector<CoreStat> &c);
    bool hasHeightForWidth() const override { return true; }
    int heightForWidth(int w) const override { return (qMax(1, m_c.size()) + qMax(1, w / 92) - 1) / qMax(1, w / 92) * 66; }
    void paintEvent(QPaintEvent *) override;
    QVector<CoreStat> m_c;
};

struct WeekHeatmap : QWidget {
    WeekHeatmap();
    void setData(const double v[7][24], const int n[7][24], const QString &unit, const QColor &hot);
    QSize sizeHint() const override { return {560, 230}; }
    void paintEvent(QPaintEvent *) override;
    void mouseMoveEvent(QMouseEvent *) override;
    QRectF cell(int d, int h) const;
    double m_v[7][24];
    int m_n[7][24];
    QString m_unit;
    QColor m_hot;
};

// table cell bar: Qt::UserRole = percent, DisplayRole = text on the bar
struct BarDelegate : QStyledItemDelegate {
    using QStyledItemDelegate::QStyledItemDelegate;
    void paint(QPainter *p, const QStyleOptionViewItem &o, const QModelIndex &i) const override;
};

struct FlowLayout : QLayout { // equal-width columns of cards
    FlowLayout(QWidget *parent) : QLayout(parent) {}
    ~FlowLayout() override { qDeleteAll(m_items); }
    void addItem(QLayoutItem *i) override { m_items << i; }
    int count() const override { return m_items.size(); }
    QLayoutItem *itemAt(int i) const override { return m_items.value(i); }
    QLayoutItem *takeAt(int i) override { return i >= 0 && i < m_items.size() ? m_items.takeAt(i) : nullptr; }
    bool hasHeightForWidth() const override { return true; }
    int heightForWidth(int w) const override { return place(QRect(0, 0, w, 0), false); }
    void setGeometry(const QRect &r) override { QLayout::setGeometry(r), place(r, true); }
    QSize sizeHint() const override { return {340, 270}; }
    int place(const QRect &r, bool apply) const;
    QList<QLayoutItem *> m_items;
};

// ---- small builders ----------------------------------------------------------------
QLabel *label(const QString &text, const char *objectName = nullptr, bool wrap = false);
QPushButton *button(const QString &text, const char *style = nullptr, std::function<void()> onClick = {});
QFrame *panel(const QString &title = {}, std::initializer_list<QWidget *> content = {}); // nullptr = stretch
bool ask(QWidget *parent, const QString &title, const QString &text);

struct Cell { QString text; QColor color; bool right = false; double pct = -1; QVariant key; }; // pct = bar, key = sort value
using Row = QVector<Cell>;
QTableWidget *table(const QStringList &headers, const QVector<int> &widths = {}, const QVector<int> &barColumns = {});
void cell(QTableWidget *t, int r, int c, const Cell &x);
template <class V, class F> void fillRows(QTableWidget *t, const V &items, F rowOf) // one row per item
{
    t->setRowCount(items.size());
    for (int r = 0; r < items.size(); ++r) {
        const Row row = rowOf(items[r]);
        for (int c = 0; c < row.size(); ++c) cell(t, r, c, row[c]);
    }
}