[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2980: Make the position of the y-axis on the bandwidth graph varia (vidalia/trunk/src/vidalia/bwgraph)



Author: edmanm
Date: 2008-08-17 00:14:25 -0400 (Sun, 17 Aug 2008)
New Revision: 2980

Modified:
   vidalia/trunk/src/vidalia/bwgraph/graphframe.cpp
   vidalia/trunk/src/vidalia/bwgraph/graphframe.h
Log:
Make the position of the y-axis on the bandwidth graph variable based on the
width of the axis labels. This works better for languages like Farsi where
"KB/s" somehow becomes 19 characters. Fixes ticket #373.


Modified: vidalia/trunk/src/vidalia/bwgraph/graphframe.cpp
===================================================================
--- vidalia/trunk/src/vidalia/bwgraph/graphframe.cpp	2008-08-17 02:09:23 UTC (rev 2979)
+++ vidalia/trunk/src/vidalia/bwgraph/graphframe.cpp	2008-08-17 04:14:25 UTC (rev 2980)
@@ -36,6 +36,7 @@
   _showRecv = true;
   _showSend = true;
   _maxValue = MIN_SCALE;
+  _scaleWidth = 0;
 }
 
 /** Default destructor */
@@ -180,14 +181,14 @@
   points << QPointF(x, y);
   for (int i = 0; i < list->size(); i++) {
     currValue = y - (list->at(i) * scale);
-    if (x - SCROLL_STEP < SCALE_WIDTH) {
-      points << QPointF(SCALE_WIDTH, currValue);
+    if (x - SCROLL_STEP < _scaleWidth) {
+      points << QPointF(_scaleWidth, currValue);
       break;
     }
     points << QPointF(x, currValue);
     x -= SCROLL_STEP;
   }
-  points << QPointF(SCALE_WIDTH, y);
+  points << QPointF(_scaleWidth, y);
   return points; 
 }
 
@@ -221,7 +222,7 @@
 void
 GraphFrame::paintTotals()
 {
-  int x = SCALE_WIDTH + FONT_SIZE, y = 0;
+  int x = _scaleWidth + FONT_SIZE, y = 0;
   int rowHeight = FONT_SIZE;
 
 #if !defined(Q_WS_MAC)
@@ -265,30 +266,56 @@
   }
 }
 
+/** Returns the width in pixels of <b>label</b> using the current painter's
+ * font. */
+int
+GraphFrame::labelWidth(const QString &label)
+{
+  int width = 0;
+  QFontMetrics fm = fontMetrics();
+
+  for (int i = 0; i < label.length(); i++)
+    width += fm.charWidth(label, i);
+  return width;
+}
+
 /** Paints the scale on the graph. */
 void
 GraphFrame::paintScale()
 {
-  qreal markStep = _maxValue * .25;
+  QString label[4];
+  int width[4];
   int top = _rec.y();
   int bottom = _rec.height();
-  qreal paintStep = (bottom - (bottom/10)) / 4;
-  
-  /* Draw the other marks in their correctly scaled locations */
-  qreal scale;
+  int scaleWidth = 0;
   qreal pos;
-  for (int i = 1; i < 5; i++) {
-    pos = bottom - (i * paintStep);
-    scale = i * markStep;
+  qreal markStep = _maxValue * .25;
+  qreal paintStep = (bottom - (bottom/8)) / 4;
+
+  /* Compute each of the y-axis labels */
+  for (int i = 0; i < 4; i++) {
+    pos = bottom - ((i+1) * paintStep);
+    label[i] = tr("%1 KB/s").arg(markStep*(i+1), 0, 'f', 2);
+    width[i] = labelWidth(label[i]);
+    scaleWidth = qMax(scaleWidth, 2+width[i]);
+  }
+
+  /* Include a 5px margin between the y-axis and its labels */
+  _scaleWidth = scaleWidth + 5;
+
+  /* Draw the y-axis labels and horizontal marks in their correctly scaled
+   * locations */
+  for (int i = 0; i < 4; i++) {
+    pos = bottom - ((i+1) * paintStep);
     _painter->setPen(SCALE_COLOR);
-    _painter->drawText(QPointF(5, pos+FONT_SIZE), 
-                       tr("%1 KB/s").arg(scale, 0, 'f', 2));
+    _painter->drawText(QPoint(_scaleWidth-width[i]-5, pos), label[i]);
+
     _painter->setPen(GRID_COLOR);
-    _painter->drawLine(QPointF(SCALE_WIDTH, pos), 
+    _painter->drawLine(QPointF(_scaleWidth, pos),
                        QPointF(_rec.width(), pos));
   }
-  
-  /* Draw vertical separator */
-  _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom);
+
+  /* Draw the y-axis */
+  _painter->drawLine(_scaleWidth, top, _scaleWidth, bottom);
 }
 

Modified: vidalia/trunk/src/vidalia/bwgraph/graphframe.h
===================================================================
--- vidalia/trunk/src/vidalia/bwgraph/graphframe.h	2008-08-17 02:09:23 UTC (rev 2979)
+++ vidalia/trunk/src/vidalia/bwgraph/graphframe.h	2008-08-17 04:14:25 UTC (rev 2980)
@@ -25,8 +25,7 @@
 #include <QList>
 
 #define HOR_SPC       2   /** Space between data points */
-#define SCALE_WIDTH   75  /** Width of the scale */
-#define MIN_SCALE     10  /** 10 kB/s is the minimum scale */  
+#define MIN_SCALE     10  /** 10 kB/s is the minimum scale */
 #define SCROLL_STEP   4   /** Horizontal change on graph update */
 
 #define BACK_COLOR    Qt::black
@@ -68,9 +67,11 @@
   void paintEvent(QPaintEvent *event);
 
 private:
+  /** Returns the width in pixels of <b>label</b> using the current painter's
+   * font. */
+  int labelWidth(const QString &label);
   /** Gets the width of the desktop, the max # of points. */
   int getNumPoints();
-  
   /** Paints an integral and an outline of that integral for each data set
    * (send and/or receive) that is to be displayed. */
   void paintData();
@@ -109,6 +110,9 @@
   /** Show the respective lines and counters. */
   bool _showRecv;
   bool _showSend;
+  /** Width (in pixels) of the scale marker area on the left side of the
+   * graph. */
+  int _scaleWidth;
 };
 
 #endif