Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions src/qt/trafficgraphwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,35 @@ int TrafficGraphWidget::getGraphRangeMins() const
return nMins;
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a great UI decision for end users. This functionality is hidden from users. If they discover it, it would be by accident; after having clicked on the Network Traffic widget.

Let's make this feature more accessible and documented by adding two buttons next to the reset button: one that sets the existing format and another button that enables the new format introduced in this PR.

The fToggle variable here should then be made into a persistent setting.

Additionally, when a longer sampling period has been set with the slider; it can take a minute or longer for the format to switch. There are optimizations that need to be done. I have not investigated this.

Copy link
Contributor Author

@rebroad rebroad Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarolrod What options are available so far for making GUI changes persistent? Regarding the delay when the slider is moved, I fixed that in a later update, and the delay now is no longer than the delay already present when clicking the reset button.

What more obvious way might there be to reveal the Y axis is toggleable? A tooltip on the graph perhaps? I am reluctant to add extra buttons given the slider is already providing many values in so little space, so I do prefer the clicking on the graph to change it. It may be that someone finds it by accident, but then this could be a pleasant surprise that - an "easter egg" feature!

This comment was marked as off-topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jarolrod The latest commit makes the update immediate now (using QWidget::update).

{
QWidget::mousePressEvent(event);
fToggle = !fToggle;
update();
}

void TrafficGraphWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
Expand All @@ -82,28 +95,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));
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 < fMax; y += val, count++) {
// don't overwrite lines drawn above
for(float y = val; y < (!fToggle || fMax / val < 20 ? fMax : oldval); y += val, count++) {
if(count % 10 == 0)
continue;
int yy = YMARGIN + h - h * y / fMax;
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()) {
Expand Down
3 changes: 3 additions & 0 deletions src/qt/trafficgraphwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class TrafficGraphWidget : public QWidget

protected:
void paintEvent(QPaintEvent *) override;
int y_value(float value);
void mousePressEvent(QMouseEvent *event) override;
bool fToggle = true;

public Q_SLOTS:
void updateRates();
Expand Down