mchuca's picture
Server Monitor: six editions (C++/Qt, Python, web for Linux and Windows), requirements and developer kit
bad179a
Raw History Blame Contribute Delete
6.73 kB
// 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]);
}
}