Changeset 2980

Show
Ignore:
Timestamp:
08/17/08 00:14:25 (3 months ago)
Author:
edmanm
Message:

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.

Location:
vidalia/trunk/src/vidalia/bwgraph
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • vidalia/trunk/src/vidalia/bwgraph/graphframe.cpp

    r2362 r2980  
    3737  _showSend = true; 
    3838  _maxValue = MIN_SCALE; 
     39  _scaleWidth = 0; 
    3940} 
    4041 
     
    181182  for (int i = 0; i < list->size(); i++) { 
    182183    currValue = y - (list->at(i) * scale); 
    183     if (x - SCROLL_STEP < SCALE_WIDTH) { 
    184       points << QPointF(SCALE_WIDTH, currValue); 
     184    if (x - SCROLL_STEP < _scaleWidth) { 
     185      points << QPointF(_scaleWidth, currValue); 
    185186      break; 
    186187    } 
     
    188189    x -= SCROLL_STEP; 
    189190  } 
    190   points << QPointF(SCALE_WIDTH, y); 
     191  points << QPointF(_scaleWidth, y); 
    191192  return points;  
    192193} 
     
    222223GraphFrame::paintTotals() 
    223224{ 
    224   int x = SCALE_WIDTH + FONT_SIZE, y = 0; 
     225  int x = _scaleWidth + FONT_SIZE, y = 0; 
    225226  int rowHeight = FONT_SIZE; 
    226227 
     
    266267} 
    267268 
     269/** Returns the width in pixels of <b>label</b> using the current painter's 
     270 * font. */ 
     271int 
     272GraphFrame::labelWidth(const QString &label) 
     273{ 
     274  int width = 0; 
     275  QFontMetrics fm = fontMetrics(); 
     276 
     277  for (int i = 0; i < label.length(); i++) 
     278    width += fm.charWidth(label, i); 
     279  return width; 
     280} 
     281 
    268282/** Paints the scale on the graph. */ 
    269283void 
    270284GraphFrame::paintScale() 
    271285{ 
    272   qreal markStep = _maxValue * .25; 
     286  QString label[4]; 
     287  int width[4]; 
    273288  int top = _rec.y(); 
    274289  int bottom = _rec.height(); 
    275   qreal paintStep = (bottom - (bottom/10)) / 4; 
    276    
    277   /* Draw the other marks in their correctly scaled locations */ 
    278   qreal scale; 
     290  int scaleWidth = 0; 
    279291  qreal pos; 
    280   for (int i = 1; i < 5; i++) { 
    281     pos = bottom - (i * paintStep); 
    282     scale = i * markStep; 
     292  qreal markStep = _maxValue * .25; 
     293  qreal paintStep = (bottom - (bottom/8)) / 4; 
     294 
     295  /* Compute each of the y-axis labels */ 
     296  for (int i = 0; i < 4; i++) { 
     297    pos = bottom - ((i+1) * paintStep); 
     298    label[i] = tr("%1 KB/s").arg(markStep*(i+1), 0, 'f', 2); 
     299    width[i] = labelWidth(label[i]); 
     300    scaleWidth = qMax(scaleWidth, 2+width[i]); 
     301  } 
     302 
     303  /* Include a 5px margin between the y-axis and its labels */ 
     304  _scaleWidth = scaleWidth + 5; 
     305 
     306  /* Draw the y-axis labels and horizontal marks in their correctly scaled 
     307   * locations */ 
     308  for (int i = 0; i < 4; i++) { 
     309    pos = bottom - ((i+1) * paintStep); 
    283310    _painter->setPen(SCALE_COLOR); 
    284     _painter->drawText(QPointF(5, pos+FONT_SIZE),  
    285                        tr("%1 KB/s").arg(scale, 0, 'f', 2)); 
     311    _painter->drawText(QPoint(_scaleWidth-width[i]-5, pos), label[i]); 
     312 
    286313    _painter->setPen(GRID_COLOR); 
    287     _painter->drawLine(QPointF(SCALE_WIDTH, pos),  
     314    _painter->drawLine(QPointF(_scaleWidth, pos), 
    288315                       QPointF(_rec.width(), pos)); 
    289316  } 
    290    
    291   /* Draw vertical separator */ 
    292   _painter->drawLine(SCALE_WIDTH, top, SCALE_WIDTH, bottom); 
    293 } 
    294  
     317 
     318  /* Draw the y-axis */ 
     319  _painter->drawLine(_scaleWidth, top, _scaleWidth, bottom); 
     320} 
     321 
  • vidalia/trunk/src/vidalia/bwgraph/graphframe.h

    r2362 r2980  
    2626 
    2727#define HOR_SPC       2   /** Space between data points */ 
    28 #define SCALE_WIDTH   75  /** Width of the scale */ 
    29 #define MIN_SCALE     10  /** 10 kB/s is the minimum scale */   
     28#define MIN_SCALE     10  /** 10 kB/s is the minimum scale */ 
    3029#define SCROLL_STEP   4   /** Horizontal change on graph update */ 
    3130 
     
    6968 
    7069private: 
     70  /** Returns the width in pixels of <b>label</b> using the current painter's 
     71   * font. */ 
     72  int labelWidth(const QString &label); 
    7173  /** Gets the width of the desktop, the max # of points. */ 
    7274  int getNumPoints(); 
    73    
    7475  /** Paints an integral and an outline of that integral for each data set 
    7576   * (send and/or receive) that is to be displayed. */ 
     
    110111  bool _showRecv; 
    111112  bool _showSend; 
     113  /** Width (in pixels) of the scale marker area on the left side of the 
     114   * graph. */ 
     115  int _scaleWidth; 
    112116}; 
    113117