Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/21/08 18:42:03 (16 years ago)
Author:
bspisic
Message:

#424
Did some code refactoring (created concrete MouseEventListeners)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Visualization/LineChart.cs

    r1038 r1045  
    4949    /// <param name="model">Referenz to the model, for data</param>
    5050    public LineChart(IChartDataRowsModel model) : this() {
    51       if (model == null)
     51      if (model == null) {
    5252        throw new NullReferenceException("Model cannot be null.");
     53      }
    5354
    5455      //TODO: correct Rectangle to fit
     
    7172
    7273      UpdateLayout();
    73 
    74       CreateMouseEventListeners();
    7574
    7675      this.model = model;
     
    117116      model.ModelChanged += OnModelChanged;
    118117
    119       foreach (IDataRow row in model.Rows)
     118      foreach (IDataRow row in model.Rows) {
    120119        OnDataRowAdded(row);
     120      }
    121121    }
    122122
     
    132132      row.ValueChanged += OnRowValueChanged;
    133133      row.ValuesChanged += OnRowValuesChanged;
    134       if (row.Count > maxDataRowCount)
     134      if (row.Count > maxDataRowCount) {
    135135        maxDataRowCount = row.Count;
     136      }
    136137
    137138      InitLineShapes(row);
     
    139140
    140141    private void ZoomToFullView() {
    141       if (!zoomFullView)
     142      if (!zoomFullView) {
    142143        return;
     144      }
    143145      RectangleD newClippingArea = new RectangleD(-0.1,
    144146                                                  minDataValue - ((maxDataValue - minDataValue)*0.05),
     
    164166      List<LineShape> lineShapes = new List<LineShape>();
    165167      if (row.Count > 0) {
    166         maxDataValue = Math.Max(row[0], this.maxDataValue);
     168        maxDataValue = Math.Max(row[0], maxDataValue);
    167169        minDataValue = Math.Min(row[0], minDataValue);
    168170      }
     
    197199      minDataValue = Math.Min(value, minDataValue);
    198200
    199       if (index > lineShapes.Count + 1)
     201      if (index > lineShapes.Count + 1) {
    200202        throw new NotImplementedException();
     203      }
    201204
    202205      // new value was added
    203206      if (index > 0 && index == lineShapes.Count + 1) {
    204         if (maxDataRowCount < row.Count)
     207        if (maxDataRowCount < row.Count) {
    205208          maxDataRowCount = row.Count;
     209        }
    206210        LineShape lineShape = new LineShape(index - 1, row[index - 1], index, row[index], 0, row.Color, row.Thickness, row.Style);
    207211        lineShapes.Add(lineShape);
     
    211215
    212216      // not the first value
    213       if (index > 0)
     217      if (index > 0) {
    214218        lineShapes[index - 1].Y2 = value;
     219      }
    215220
    216221      // not the last value
    217       if (index > 0 && index < row.Count - 1)
     222      if (index > 0 && index < row.Count - 1) {
    218223        lineShapes[index].Y1 = value;
     224      }
    219225      ZoomToFullView();
    220226
     
    224230    // TODO use action parameter
    225231    private void OnRowValuesChanged(IDataRow row, double[] values, int index, Action action) {
    226       foreach (double value in values)
     232      foreach (double value in values) {
    227233        OnRowValueChanged(row, value, index++, action);
     234      }
    228235    }
    229236
     
    241248
    242249    public void EndUpdate() {
    243       if (beginUpdateCount == 0)
     250      if (beginUpdateCount == 0) {
    244251        throw new InvalidOperationException("Too many EndUpdates.");
     252      }
    245253
    246254      beginUpdateCount--;
    247255
    248       if (beginUpdateCount == 0)
     256      if (beginUpdateCount == 0) {
    249257        canvas.Invalidate();
     258      }
    250259    }
    251260
    252261    #endregion
    253262
    254     private MouseEventListener panListener;
    255     private MouseEventListener zoomListener;
    256 
    257     private void CreateMouseEventListeners() {
    258       panListener = new MouseEventListener();
    259       panListener.OnMouseMove += Pan_OnMouseMove;
    260       panListener.OnMouseUp += Pan_OnMouseUp;
    261 
    262       zoomListener = new MouseEventListener();
    263       zoomListener.OnMouseMove += Zoom_OnMouseMove;
    264       zoomListener.OnMouseUp += Zoom_OnMouseUp;
    265     }
    266 
    267     private RectangleD startClippingArea;
     263    private RectangleShape rectangleShape;
    268264
    269265    private void canvasUI1_MouseDown(object sender, MouseEventArgs e) {
    270266      if (ModifierKeys == Keys.Control) {
    271         zoomListener.StartPoint = e.Location;
    272         canvas.MouseEventListener = zoomListener;
    273 
    274         r = Rectangle.Empty;
    275         rectangleShape = new RectangleShape(e.X, e.Y, e.X, e.Y, Color.Blue);
    276         rectangleShape.Opacity = 50;
    277 
    278         linesShape.AddShape(rectangleShape);
     267        CreateZoomListener(e);
    279268      } else {
    280         panListener.StartPoint = e.Location;
    281         canvas.MouseEventListener = panListener;
    282 
    283         startClippingArea = linesShape.ClippingArea;
    284       }
    285     }
    286 
    287     private void Pan_OnMouseUp(Point startPoint, Point actualPoint) {
     269        CreatePanListener(e);
     270      }
     271    }
     272
     273    private void CreateZoomListener(MouseEventArgs e) {
     274      ZoomListener zoomListener = new ZoomListener(e.Location);
     275      zoomListener.DrawRectangle += DrawRectangle;
     276      zoomListener.OnMouseUp += OnZoom_MouseUp;
     277
     278      canvas.MouseEventListener = zoomListener;
     279
     280      rectangleShape = new RectangleShape(e.X, e.Y, e.X, e.Y, Color.Blue);
     281      rectangleShape.Opacity = 50;
     282
     283      linesShape.AddShape(rectangleShape);
     284    }
     285
     286    private void OnZoom_MouseUp(object sender, MouseEventArgs e) {
    288287      canvas.MouseEventListener = null;
    289     }
    290 
    291     private void Pan_OnMouseMove(Point startPoint, Point actualPoint) {
    292       Rectangle viewPort = canvas.ClientRectangle;
    293 
    294       PointD worldStartPoint = Transform.ToWorld(startPoint, viewPort, startClippingArea);
    295       PointD worldActualPoint = Transform.ToWorld(actualPoint, viewPort, startClippingArea);
    296 
    297       double xDiff = worldActualPoint.X - worldStartPoint.X;
    298       double yDiff = worldActualPoint.Y - worldStartPoint.Y;
    299 
    300       RectangleD newClippingArea = new RectangleD();
    301       newClippingArea.X1 = startClippingArea.X1 - xDiff;
    302       newClippingArea.X2 = startClippingArea.X2 - xDiff;
    303       newClippingArea.Y1 = startClippingArea.Y1 - yDiff;
    304       newClippingArea.Y2 = startClippingArea.Y2 - yDiff;
    305 
     288
     289      SetLineClippingArea(rectangleShape.Rectangle);
     290      linesShape.RemoveShape(rectangleShape);
     291
     292      zoomFullView = false; //user wants to zoom => no full view
     293
     294      canvas.Invalidate();
     295    }
     296
     297    private void DrawRectangle(Rectangle rectangle) {
     298      rectangleShape.Rectangle = Transform.ToWorld(rectangle, canvas.ClientRectangle, linesShape.ClippingArea);
     299      canvas.Invalidate();
     300    }
     301
     302    private void CreatePanListener(MouseEventArgs e) {
     303      PanListener panListener = new PanListener(canvas.ClientRectangle, linesShape.ClippingArea, e.Location);
     304
     305      panListener.SetNewClippingArea += SetNewClippingArea;
     306      panListener.OnMouseUp += delegate { canvas.MouseEventListener = null; };
     307
     308      canvas.MouseEventListener = panListener;
     309    }
     310
     311    private void SetNewClippingArea(RectangleD newClippingArea) {
    306312      SetLineClippingArea(newClippingArea);
    307       panListener.StartPoint = startPoint;
    308 
    309       zoomFullView = false; //user wants to pan => no full view
    310 
    311       canvas.Invalidate();
    312     }
    313 
    314     private void Zoom_OnMouseUp(Point startPoint, Point actualPoint) {
    315       canvas.MouseEventListener = null;
    316 
    317       RectangleD newClippingArea = Transform.ToWorld(r, canvas.ClientRectangle, linesShape.ClippingArea);
    318       SetLineClippingArea(newClippingArea);
    319       linesShape.RemoveShape(rectangleShape);
    320 
    321       zoomFullView = false; //user wants to pan => no full view
    322 
    323       canvas.Invalidate();
    324     }
    325 
    326     private Rectangle r;
    327     private RectangleShape rectangleShape;
    328 
    329     private void Zoom_OnMouseMove(Point startPoint, Point actualPoint) {
    330       r = new Rectangle();
    331 
    332       if (startPoint.X < actualPoint.X) {
    333         r.X = startPoint.X;
    334         r.Width = actualPoint.X - startPoint.X;
    335       } else {
    336         r.X = actualPoint.X;
    337         r.Width = startPoint.X - actualPoint.X;
    338       }
    339 
    340       if (startPoint.Y < actualPoint.Y) {
    341         r.Y = startPoint.Y;
    342         r.Height = actualPoint.Y - startPoint.Y;
    343       } else {
    344         r.Y = actualPoint.Y;
    345         r.Height = startPoint.Y - actualPoint.Y;
    346       }
    347 
    348       rectangleShape.Rectangle = Transform.ToWorld(r, canvas.ClientRectangle, linesShape.ClippingArea);
    349      
     313
     314      zoomFullView = false;
    350315      canvas.Invalidate();
    351316    }
Note: See TracChangeset for help on using the changeset viewer.