Changeset 1045


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

#424
Did some code refactoring (created concrete MouseEventListeners)

Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Visualization.Test/MainForm.Designer.cs

    r865 r1045  
    3737      this.canvasUI.TabIndex = 3;
    3838      this.canvasUI.Text = "canvasUI";
    39       this.canvasUI.MouseDown += new System.Windows.Forms.MouseEventHandler(this.canvasUI_MouseDown);
    4039      //
    4140      // label2
  • trunk/sources/HeuristicLab.Visualization.Test/MainForm.cs

    r1038 r1045  
    44namespace HeuristicLab.Visualization.Test {
    55  public partial class MainForm : Form {
    6     private MouseEventListener dragDropListener;
    7 
    86    public MainForm() {
    97      InitializeComponent();
    10 
    11       CreateMouseEventListeners();
    128
    139      canvasUI.MainCanvas.WorldShape = new WorldShape(new RectangleD(0, 0, 800, 600), new RectangleD(0, 0, 800, 600));
     
    1915
    2016      canvasUI.Invalidate();
    21     }
    22 
    23     private void CreateMouseEventListeners() {
    24       dragDropListener = new MouseEventListener();
    25       dragDropListener.OnMouseMove += DragDrop_OnMouseMove;
    26       dragDropListener.OnMouseUp += DragDrop_OnMouseUp;
    2717    }
    2818
     
    9282    }
    9383
    94     private void canvasUI_MouseDown(object sender, MouseEventArgs e) {
    95       mouseEventDemonstrationGraphics = canvasUI.CreateGraphics();
    96 
    97       dragDropListener.StartPoint = e.Location;
    98       lastActualPoint = e.Location;
    99 
    100       canvasUI.MouseEventListener = dragDropListener;
    101     }
    102 
    103     private Point lastActualPoint;
    104     private Graphics mouseEventDemonstrationGraphics;
    105 
    106     private void DragDrop_OnMouseUp(Point startPoint, Point actualPoint) {
    107       canvasUI.MouseEventListener = null;
    108 
    109       canvasUI.Invalidate();
    110       mouseEventDemonstrationGraphics.Dispose();
    111     }
    112 
    113     private void DragDrop_OnMouseMove(Point startPoint, Point actualPoint) {
    114       mouseEventDemonstrationGraphics.DrawLine(Pens.Blue, lastActualPoint, actualPoint);
    115       lastActualPoint = actualPoint;
    116     }
    117 
    11884    private void legendButton_Click(object sender, System.EventArgs e) {
    11985      LegendForm form = new LegendForm();
  • trunk/sources/HeuristicLab.Visualization/CanvasUI.cs

    r1038 r1045  
    88  public partial class CanvasUI : Control {
    99    private readonly Canvas mainCanvas = new Canvas();
    10     private MouseEventListener mouseEventListener;
     10    private IMouseEventListener mouseEventListener;
    1111
    1212    public CanvasUI() {
     
    2020    }
    2121
    22     public MouseEventListener MouseEventListener {
     22    public IMouseEventListener MouseEventListener {
    2323      get { return mouseEventListener; }
    2424      set { mouseEventListener = value; }
     
    5151    private void CanvasUI_MouseMove(object sender, MouseEventArgs e) {
    5252      if (mouseEventListener != null) {
    53         mouseEventListener.MouseMove(e.Location);
     53        mouseEventListener.MouseMove(sender, e);
    5454      }
    5555    }
     
    5757    private void CanvasUI_MouseUp(object sender, MouseEventArgs e) {
    5858      if (mouseEventListener != null) {
    59         mouseEventListener.MouseUp(e.Location);
     59        mouseEventListener.MouseUp(sender, e);
    6060      }
    6161    }
  • 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    }
  • trunk/sources/HeuristicLab.Visualization/MouseEventListener.cs

    r863 r1045  
    11using System.Drawing;
     2using System.Windows.Forms;
    23
    34namespace HeuristicLab.Visualization {
    4   /// <summary>
    5   /// Helper class for different OnMouseMove and OnMouseUp implementations.
    6   /// </summary>
    7   public class MouseEventListener {
    8     /// <summary>
    9     /// Fired when the MouseMove method was called.
    10     /// </summary>
    11     public event MouseEventListenerHandler OnMouseMove;
     5  public interface IMouseEventListener {
     6    void MouseMove(object sender, MouseEventArgs e);
     7    void MouseUp(object sender, MouseEventArgs e);
    128
    13     /// <summary>
    14     /// Fired when the MouseUp method was called.
    15     /// </summary>
    16     public event MouseEventListenerHandler OnMouseUp;
     9    event MouseEventHandler OnMouseMove;
     10    event MouseEventHandler OnMouseUp;
     11  }
    1712
    18     private Point startPoint;
     13  public delegate void SetNewClippingAreaHandler(RectangleD newClippingArea);
    1914
    20     /// <summary>
    21     /// Call this method to fire the OnMouseMove event.
    22     /// </summary>
    23     /// <param name="actualPoint"></param>
    24     public void MouseMove(Point actualPoint) {
     15  public delegate void DrawRectangleHandler(Rectangle rectangle);
     16
     17  public class PanListener : IMouseEventListener {
     18    private readonly Rectangle viewPort;
     19    private readonly RectangleD clippingArea;
     20    private readonly Point startPoint;
     21
     22    public PanListener(Rectangle viewPort, RectangleD clippingArea, Point startPoint) {
     23      this.viewPort = viewPort;
     24      this.clippingArea = clippingArea;
     25      this.startPoint = startPoint;
     26    }
     27
     28    #region IMouseEventListener Members
     29
     30    public event SetNewClippingAreaHandler SetNewClippingArea;
     31    public event MouseEventHandler OnMouseMove;
     32    public event MouseEventHandler OnMouseUp;
     33
     34    public void MouseMove(object sender, MouseEventArgs e) {
     35      PointD worldStartPoint = Transform.ToWorld(startPoint, viewPort, clippingArea);
     36      PointD worldActualPoint = Transform.ToWorld(e.Location, viewPort, clippingArea);
     37
     38      double xDiff = worldActualPoint.X - worldStartPoint.X;
     39      double yDiff = worldActualPoint.Y - worldStartPoint.Y;
     40
     41      RectangleD newClippingArea = new RectangleD();
     42      newClippingArea.X1 = clippingArea.X1 - xDiff;
     43      newClippingArea.X2 = clippingArea.X2 - xDiff;
     44      newClippingArea.Y1 = clippingArea.Y1 - yDiff;
     45      newClippingArea.Y2 = clippingArea.Y2 - yDiff;
     46
     47      if (SetNewClippingArea != null) {
     48        SetNewClippingArea(newClippingArea);
     49      }
     50
    2551      if (OnMouseMove != null) {
    26         OnMouseMove(startPoint, actualPoint);
     52        OnMouseMove(sender, e);
    2753      }
    2854    }
    2955
    30     /// <summary>
    31     /// Call this method to fire the OnMouseUp event.
    32     /// </summary>
    33     /// <param name="actualPoint">Actual point of the mouse</param>
    34     public void MouseUp(Point actualPoint) {
     56    public void MouseUp(object sender, MouseEventArgs e) {
    3557      if (OnMouseUp != null) {
    36         OnMouseUp(startPoint, actualPoint);
     58        OnMouseUp(sender, e);
    3759      }
    3860    }
    3961
    40     /// <summary>
    41     /// Gets or sets the starting point of the mouse.
    42     /// </summary>
    43     public Point StartPoint {
    44       get { return startPoint; }
    45       set { startPoint = value; }
    46     }
     62    #endregion
    4763  }
    4864
    49   /// <summary>
    50   /// Handler for the MouseEventListener events.
    51   /// </summary>
    52   /// <param name="startPoint">Starting point of the mouse.</param>
    53   /// <param name="actualPoint">Actual point of the mouse.</param>
    54   public delegate void MouseEventListenerHandler(Point startPoint, Point actualPoint);
     65  public class ZoomListener : IMouseEventListener {
     66    private readonly Point startPoint;
     67
     68    public event DrawRectangleHandler DrawRectangle;
     69
     70    public ZoomListener(Point startPoint) {
     71      this.startPoint = startPoint;
     72    }
     73
     74    #region IMouseEventListener Members
     75
     76    public event MouseEventHandler OnMouseMove;
     77    public event MouseEventHandler OnMouseUp;
     78
     79    public void MouseMove(object sender, MouseEventArgs e) {
     80      Rectangle r = new Rectangle();
     81      Point actualPoint = e.Location;
     82
     83      if (startPoint.X < actualPoint.X) {
     84        r.X = startPoint.X;
     85        r.Width = actualPoint.X - startPoint.X;
     86      } else {
     87        r.X = actualPoint.X;
     88        r.Width = startPoint.X - actualPoint.X;
     89      }
     90
     91      if (startPoint.Y < actualPoint.Y) {
     92        r.Y = startPoint.Y;
     93        r.Height = actualPoint.Y - startPoint.Y;
     94      } else {
     95        r.Y = actualPoint.Y;
     96        r.Height = startPoint.Y - actualPoint.Y;
     97      }
     98
     99      if(DrawRectangle != null) {
     100        DrawRectangle(r);
     101      }
     102
     103      if (OnMouseMove != null) {
     104        OnMouseMove(sender, e);
     105      }
     106    }
     107
     108    public void MouseUp(object sender, MouseEventArgs e) {
     109      if (OnMouseUp != null) {
     110        OnMouseUp(sender, e);
     111      }
     112    }
     113
     114    #endregion
     115  }
    55116}
Note: See TracChangeset for help on using the changeset viewer.