| 77 | |
| 78 | |
| 79 | void TrafficGraphWidget::paintEvent(QPaintEvent*) |
| 80 | { |
| 81 | QPainter painter(this); |
| 82 | painter.fillRect(rect(), Qt::black); |
| 83 | |
| 84 | if (fMax <= 0.0f) return; |
| 85 | |
| 86 | QColor axisCol(Qt::gray); |
| 87 | int h = height() - YMARGIN * 2; |
| 88 | painter.setPen(axisCol); |
| 89 | painter.drawLine(XMARGIN, YMARGIN + h, width() - XMARGIN, YMARGIN + h); |
| 90 | |
| 91 | // decide what order of magnitude we are |
| 92 | int base = floor(log10(fMax)); |
| 93 | float val = pow(10.0f, base); |
| 94 | |
| 95 | const QString units = tr("KB/s"); |
| 96 | const float yMarginText = 2.0; |
| 97 | |
| 98 | // draw lines |
| 99 | painter.setPen(axisCol); |
| 100 | painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax - yMarginText, QString("%1 %2").arg(val).arg(units)); |
| 101 | for (float y = val; y < fMax; y += val) { |
| 102 | int yy = YMARGIN + h - h * y / fMax; |
| 103 | painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); |
| 104 | } |
| 105 | // if we drew 3 or fewer lines, break them up at the next lower order of magnitude |
| 106 | if (fMax / val <= 3.0f) { |
| 107 | axisCol = axisCol.darker(); |
| 108 | val = pow(10.0f, base - 1); |
| 109 | painter.setPen(axisCol); |
| 110 | painter.drawText(XMARGIN, YMARGIN + h - h * val / fMax - yMarginText, QString("%1 %2").arg(val).arg(units)); |
| 111 | int count = 1; |
| 112 | for (float y = val; y < fMax; y += val, count++) { |
| 113 | // don't overwrite lines drawn above |
| 114 | if (count % 10 == 0) |
| 115 | continue; |
| 116 | int yy = YMARGIN + h - h * y / fMax; |
| 117 | painter.drawLine(XMARGIN, yy, width() - XMARGIN, yy); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | const TrafficGraphData::SampleQueue& queue = trafficGraphData.getCurrentRangeQueue(); |
| 122 | |
| 123 | if(!queue.empty()) { |
| 124 | QPainterPath pIn; |
| 125 | paintPath(pIn, queue, boost::bind(chooseIn,_1)); |
| 126 | painter.fillPath(pIn, QColor(0, 255, 0, 128)); |
| 127 | painter.setPen(Qt::green); |
| 128 | painter.drawPath(pIn); |
| 129 | |
| 130 | QPainterPath pOut; |
| 131 | paintPath(pOut, queue, boost::bind(chooseOut,_1)); |
| 132 | painter.fillPath(pOut, QColor(255, 0, 0, 128)); |
| 133 | painter.setPen(Qt::red); |
| 134 | painter.drawPath(pOut); |
| 135 | } |
| 136 | } |