[tor-commits] [vidalia/master] Compute max with only the visible points in bwgraph

chiiph at torproject.org chiiph at torproject.org
Fri Oct 7 19:20:37 UTC 2011


commit a08aa988a29ffa4d6db93250fe7589cd61395e09
Author: Tomas Touceda <chiiph at gentoo.org>
Date:   Sat May 21 14:43:41 2011 -0300

    Compute max with only the visible points in bwgraph
    
    If the max is global instead of local, a peak in the connection will make
    the rest of the graph too small.
---
 src/vidalia/bwgraph/GraphFrame.cpp |   45 ++++++++++++++++++++++++++++++-----
 src/vidalia/bwgraph/GraphFrame.h   |    4 +++
 2 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/src/vidalia/bwgraph/GraphFrame.cpp b/src/vidalia/bwgraph/GraphFrame.cpp
index c9537a0..c5d3f60 100644
--- a/src/vidalia/bwgraph/GraphFrame.cpp
+++ b/src/vidalia/bwgraph/GraphFrame.cpp
@@ -15,7 +15,7 @@
 
 #include "GraphFrame.h"
 
-#include <QtGlobal>
+#include <QtGui>
 
 
 /** Default contructor */
@@ -31,7 +31,8 @@ GraphFrame::GraphFrame(QWidget *parent)
   /* Initialize graph values */
   _recvData->prepend(0);
   _sendData->prepend(0);
-  _maxPoints = getNumPoints();  
+  _maxPoints = getNumPoints();
+  _maxPosition = 0;
   _showRecv = true;
   _showSend = true;
   _maxValue = MIN_SCALE;
@@ -51,9 +52,7 @@ GraphFrame::~GraphFrame()
 int
 GraphFrame::getNumPoints()
 {
-  QDesktopWidget *desktop = QApplication::desktop();
-  int width = desktop->width();
-  return width;
+  return size().width() - _scaleWidth;
 }
 
 /** Adds new data points to the graph. */
@@ -66,6 +65,18 @@ GraphFrame::addPoints(qreal recv, qreal send)
     _recvData->removeLast();
   }
 
+  /* Update the displayed maximum */
+  if (_maxPosition >= _maxPoints) {
+    _maxValue = MIN_SCALE;
+    foreach(qreal send, *_sendData)
+      if(send > _maxValue)
+        _maxValue = send;
+    foreach(qreal recv, *_recvData)
+      if(recv > _maxValue)
+        _maxValue = recv;
+    _maxPosition = 0;
+  }
+
   /* Add the points to their respective lists */
   _sendData->prepend(send);
   _recvData->prepend(recv);
@@ -74,9 +85,23 @@ GraphFrame::addPoints(qreal recv, qreal send)
   _totalSend += send;
   _totalRecv += recv;
   
+  bool maxUpdated = false;
   /* Check for a new maximum value */
-  if (send > _maxValue) _maxValue = send;
-  if (recv > _maxValue) _maxValue = recv;
+  if (send > _maxValue) {
+    _maxValue = send;
+    maxUpdated = true;
+  }
+
+  if (recv > _maxValue) {
+    _maxValue = recv;
+    maxUpdated = true;
+  }
+
+  if (maxUpdated) {
+    _maxPosition = 0;
+  } else {
+    _maxPosition++;
+  }
 
   this->update();
 }
@@ -318,3 +343,9 @@ GraphFrame::paintScale()
   _painter->drawLine(_scaleWidth, top, _scaleWidth, bottom);
 }
 
+void
+GraphFrame::resizeEvent(QResizeEvent *ev)
+{
+  _maxPoints = ev->size().width() - _scaleWidth;
+  _maxPoints /= SCROLL_STEP;
+}
diff --git a/src/vidalia/bwgraph/GraphFrame.h b/src/vidalia/bwgraph/GraphFrame.h
index 4819396..8e29cc2 100644
--- a/src/vidalia/bwgraph/GraphFrame.h
+++ b/src/vidalia/bwgraph/GraphFrame.h
@@ -89,6 +89,8 @@ private:
   /** Paints an integral using the supplied data. */
   void paintIntegral(QVector<QPointF> points, QColor color, qreal alpha = 1.0);
 
+  void resizeEvent(QResizeEvent *ev);
+
   /** Style with which the bandwidth data will be graphed. */
   GraphStyle _graphStyle;
   /** A QPainter object that handles drawing the various graph elements. */
@@ -101,6 +103,8 @@ private:
   QRect _rec;
   /** The maximum data value plotted. */
   qreal _maxValue;
+  /** The position of the local maximum in the displayed bandwidth */
+  int _maxPosition;
   /** The maximum number of points to store. */
   int _maxPoints;
   /** The total data sent/recv. */





More information about the tor-commits mailing list