Merge g473 via rebroad-g/NonLinearTraffic

This commit is contained in:
Luke Dashjr 2025-03-05 03:27:08 +00:00
commit 3b46e6081b
2 changed files with 41 additions and 20 deletions

View File

@ -39,22 +39,35 @@ void TrafficGraphWidget::setClientModel(ClientModel *model)
std::chrono::minutes TrafficGraphWidget::getGraphRange() const { return m_range; }
int TrafficGraphWidget::y_value(float value)
{
int h = height() - YMARGIN * 2;
return YMARGIN + h - (h * 1.0 * (fToggle ? (pow(value, 0.30102) / pow(fMax, 0.30102)) : (value / fMax)));
}
void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
{
int sampleCount = samples.size();
if(sampleCount > 0) {
if(sampleCount > 0 && fMax > 0) {
int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
int x = XMARGIN + w;
path.moveTo(x, YMARGIN + h);
for(int i = 0; i < sampleCount; ++i) {
x = XMARGIN + w - w * i / DESIRED_SAMPLES;
int y = YMARGIN + h - (int)(h * samples.at(i) / fMax);
int y = y_value(samples.at(i));
path.lineTo(x, y);
}
path.lineTo(x, YMARGIN + h);
}
}
void TrafficGraphWidget::mousePressEvent(QMouseEvent *event)
{
QWidget::mousePressEvent(event);
fToggle = !fToggle;
update();
}
void TrafficGraphWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
@ -74,28 +87,33 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *)
const QString units = tr("kB/s");
const float yMarginText = 2.0;
// draw lines
painter.setPen(axisCol);
painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
for(float y = val; y < fMax; y += val) {
int yy = YMARGIN + h - h * y / fMax;
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
}
// if we drew 3 or fewer lines, break them up at the next lower order of magnitude
if(fMax / val <= 3.0f) {
axisCol = axisCol.darker();
// if we drew 10 or 3 fewer lines, break them up at the next lower order of magnitude
if(fMax / val <= (fToggle ? 10.0f : 3.0f)) {
float oldval = val;
val = pow(10.0f, base - 1);
painter.setPen(axisCol);
painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax-yMarginText, QString("%1 %2").arg(val).arg(units));
int count = 1;
for(float y = val; y < fMax; y += val, count++) {
// don't overwrite lines drawn above
if(count % 10 == 0)
continue;
int yy = YMARGIN + h - h * y / fMax;
painter.setPen(axisCol.darker());
painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units));
if (fToggle) {
int yy = y_value(val*0.1);
painter.drawText(XMARGIN, yy-yMarginText, QString("%1 %2").arg(val*0.1).arg(units));
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
}
int count = 1;
for(float y = val; y < (!fToggle || fMax / val < 20 ? fMax : oldval); y += val, count++) {
if(count % 10 == 0)
continue;
int yy = y_value(y);
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
}
val = oldval;
}
// draw lines
painter.setPen(axisCol);
for(float y = val; y < fMax; y += val) {
int yy = y_value(y);
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy);
}
painter.drawText(XMARGIN, y_value(val)-yMarginText, QString("%1 %2").arg(val).arg(units));
painter.setRenderHint(QPainter::Antialiasing);
if(!vSamplesIn.empty()) {

View File

@ -28,6 +28,9 @@ public:
protected:
void paintEvent(QPaintEvent *) override;
int y_value(float value);
void mousePressEvent(QMouseEvent *event) override;
bool fToggle = true;
public Q_SLOTS:
void updateRates();