Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Charting/Primitives/LinearPrimitiveBase.cs @ 329

Last change on this file since 329 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Drawing;
26using System.Drawing.Drawing2D;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Charting {
30  public abstract class LinearPrimitiveBase : PrimitiveBase {
31    protected IGroup selectionRectangles;
32
33    private PointD myStart;
34    public virtual PointD Start {
35      get { return myStart; }
36    }
37    private PointD myEnd;
38    public virtual PointD End {
39      get { return myEnd; }
40    }
41    public virtual SizeD Size {
42      get { return new SizeD(End.X - Start.X, End.Y - Start.Y); }
43    }
44    public virtual double Length {
45      get { return (Start - End).Length; }
46    }
47
48    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end)
49      : base(chart) {
50      selectionRectangles = new Group(chart);
51      myStart = start;
52      myEnd = end;
53    }
54    protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2)
55      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
56    }
57    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end, Pen pen, Brush brush)
58      : base(chart, pen, brush) {
59      selectionRectangles = new Group(chart);
60      myStart = start;
61      myEnd = end;
62    }
63    protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
64      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
65    }
66
67    public virtual void SetPosition(PointD start, PointD end) {
68      myStart = start;
69      myEnd = end;
70      OnUpdate();
71    }
72    public void SetPosition(double x1, double y1, double x2, double y2) {
73      SetPosition(new PointD(x1, y1), new PointD(x2, y2));
74    }
75    public override void Move(Offset delta) {
76      SetPosition(Start + delta, End + delta);
77    }
78
79    public override bool ContainsPoint(PointD point) {
80      if (Selected) {
81        if (selectionRectangles.GetPrimitive(point) != null) return true;
82      }
83      return false;
84    }
85
86    public override void MouseDrag(PointD point, Offset offset, MouseButtons button) {
87      if (button == MouseButtons.Left) {
88        if (Selected) {
89          if (selectionRectangles.ContainsPoint(point)) {
90            SelectionRectangle rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
91            if (rect.Point == Start) {
92              SetPosition(Start + offset, End);
93            } else if (rect.Point == End) {
94              SetPosition(Start, End + offset);
95            }
96          } else {
97            base.MouseDrag(point, offset, button);
98          }
99        }
100      }
101    }
102
103    public override Cursor GetCursor(PointD point) {
104      if (Selected) {
105        Cursor cursor = selectionRectangles.GetCursor(point);
106        if (cursor != null) return cursor;
107      }
108      return base.GetCursor(point);
109    }
110
111    public override void PostDraw(Graphics graphics) {
112      selectionRectangles.Clear();
113      if (Selected) {
114        Pen pen = new Pen(Color.LightGray, 3);
115        pen.DashStyle = DashStyle.Dash;
116        graphics.DrawLine(pen,
117                          Chart.TransformWorldToPixel(Start),
118                          Chart.TransformWorldToPixel(End));
119        selectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll));
120        selectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll));
121
122        selectionRectangles.Draw(graphics);
123      }
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.