Changeset View
Changeset View
Standalone View
Standalone View
src/qt/trafficgraphwidget.cpp
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if (sampleCount > 0) { | ||||
path.lineTo(x, YMARGIN + h); | path.lineTo(x, YMARGIN + h); | ||||
} | } | ||||
} | } | ||||
void TrafficGraphWidget::paintEvent(QPaintEvent *) { | void TrafficGraphWidget::paintEvent(QPaintEvent *) { | ||||
QPainter painter(this); | QPainter painter(this); | ||||
painter.fillRect(rect(), Qt::black); | painter.fillRect(rect(), Qt::black); | ||||
if (fMax <= 0.0f) return; | if (fMax <= 0.0f) { | ||||
return; | |||||
} | |||||
QColor axisCol(Qt::gray); | QColor axisCol(Qt::gray); | ||||
int h = height() - YMARGIN * 2; | int h = height() - YMARGIN * 2; | ||||
painter.setPen(axisCol); | painter.setPen(axisCol); | ||||
painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h); | painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h); | ||||
// decide what order of magnitude we are | // decide what order of magnitude we are | ||||
int base = floor(log10(fMax)); | int base = floor(log10(fMax)); | ||||
Show All 16 Lines | if (fMax / val <= 3.0f) { | ||||
axisCol = axisCol.darker(); | axisCol = axisCol.darker(); | ||||
val = pow(10.0f, base - 1); | val = pow(10.0f, base - 1); | ||||
painter.setPen(axisCol); | painter.setPen(axisCol); | ||||
painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax - yMarginText, | painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax - yMarginText, | ||||
QString("%1 %2").arg(val).arg(units)); | QString("%1 %2").arg(val).arg(units)); | ||||
int count = 1; | int count = 1; | ||||
for (float y = val; y < fMax; y += val, count++) { | for (float y = val; y < fMax; y += val, count++) { | ||||
// don't overwrite lines drawn above | // don't overwrite lines drawn above | ||||
if (count % 10 == 0) continue; | if (count % 10 == 0) { | ||||
continue; | |||||
} | |||||
int yy = YMARGIN + h - h * y / fMax; | int yy = YMARGIN + h - h * y / fMax; | ||||
painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); | painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); | ||||
} | } | ||||
} | } | ||||
if (!vSamplesIn.empty()) { | if (!vSamplesIn.empty()) { | ||||
QPainterPath p; | QPainterPath p; | ||||
paintPath(p, vSamplesIn); | paintPath(p, vSamplesIn); | ||||
painter.fillPath(p, QColor(0, 255, 0, 128)); | painter.fillPath(p, QColor(0, 255, 0, 128)); | ||||
painter.setPen(Qt::green); | painter.setPen(Qt::green); | ||||
painter.drawPath(p); | painter.drawPath(p); | ||||
} | } | ||||
if (!vSamplesOut.empty()) { | if (!vSamplesOut.empty()) { | ||||
QPainterPath p; | QPainterPath p; | ||||
paintPath(p, vSamplesOut); | paintPath(p, vSamplesOut); | ||||
painter.fillPath(p, QColor(255, 0, 0, 128)); | painter.fillPath(p, QColor(255, 0, 0, 128)); | ||||
painter.setPen(Qt::red); | painter.setPen(Qt::red); | ||||
painter.drawPath(p); | painter.drawPath(p); | ||||
} | } | ||||
} | } | ||||
void TrafficGraphWidget::updateRates() { | void TrafficGraphWidget::updateRates() { | ||||
if (!clientModel) return; | if (!clientModel) { | ||||
return; | |||||
} | |||||
quint64 bytesIn = clientModel->node().getTotalBytesRecv(), | quint64 bytesIn = clientModel->node().getTotalBytesRecv(), | ||||
bytesOut = clientModel->node().getTotalBytesSent(); | bytesOut = clientModel->node().getTotalBytesSent(); | ||||
float inRate = | float inRate = | ||||
(bytesIn - nLastBytesIn) / 1024.0f * 1000 / timer->interval(); | (bytesIn - nLastBytesIn) / 1024.0f * 1000 / timer->interval(); | ||||
float outRate = | float outRate = | ||||
(bytesOut - nLastBytesOut) / 1024.0f * 1000 / timer->interval(); | (bytesOut - nLastBytesOut) / 1024.0f * 1000 / timer->interval(); | ||||
vSamplesIn.push_front(inRate); | vSamplesIn.push_front(inRate); | ||||
vSamplesOut.push_front(outRate); | vSamplesOut.push_front(outRate); | ||||
nLastBytesIn = bytesIn; | nLastBytesIn = bytesIn; | ||||
nLastBytesOut = bytesOut; | nLastBytesOut = bytesOut; | ||||
while (vSamplesIn.size() > DESIRED_SAMPLES) { | while (vSamplesIn.size() > DESIRED_SAMPLES) { | ||||
vSamplesIn.pop_back(); | vSamplesIn.pop_back(); | ||||
} | } | ||||
while (vSamplesOut.size() > DESIRED_SAMPLES) { | while (vSamplesOut.size() > DESIRED_SAMPLES) { | ||||
vSamplesOut.pop_back(); | vSamplesOut.pop_back(); | ||||
} | } | ||||
float tmax = 0.0f; | float tmax = 0.0f; | ||||
for (const float f : vSamplesIn) { | for (const float f : vSamplesIn) { | ||||
if (f > tmax) tmax = f; | if (f > tmax) { | ||||
tmax = f; | |||||
} | |||||
} | } | ||||
for (const float f : vSamplesOut) { | for (const float f : vSamplesOut) { | ||||
if (f > tmax) tmax = f; | if (f > tmax) { | ||||
tmax = f; | |||||
} | |||||
} | } | ||||
fMax = tmax; | fMax = tmax; | ||||
update(); | update(); | ||||
} | } | ||||
void TrafficGraphWidget::setGraphRangeMins(int mins) { | void TrafficGraphWidget::setGraphRangeMins(int mins) { | ||||
nMins = mins; | nMins = mins; | ||||
int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES; | int msecsPerSample = nMins * 60 * 1000 / DESIRED_SAMPLES; | ||||
Show All 19 Lines |