Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/LinearPrimitiveBase.cs @ 9415

Last change on this file since 9415 was 9415, checked in by bburlacu, 11 years ago

#1265: Added save file functionality in emf and bitmap formats.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
23using System.Drawing.Drawing2D;
24using System.Windows.Forms;
25
26namespace HeuristicLab.Visualization {
27  public abstract class LinearPrimitiveBase : PrimitiveBase {
28    protected IGroup selectionRectangles;
29
30    private PointD myStart;
31    public virtual PointD Start {
32      get { return myStart; }
33    }
34    private PointD myEnd;
35    public virtual PointD End {
36      get { return myEnd; }
37    }
38    public virtual SizeD Size {
39      get { return new SizeD(End.X - Start.X, End.Y - Start.Y); }
40    }
41    public virtual double Length {
42      get { return (Start - End).Length; }
43    }
44
45    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end)
46      : base(chart) {
47      selectionRectangles = new Group(chart);
48      myStart = start;
49      myEnd = end;
50    }
51    protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2)
52      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
53    }
54    protected LinearPrimitiveBase(IChart chart, PointD start, PointD end, Pen pen, Brush brush)
55      : base(chart, pen, brush) {
56      selectionRectangles = new Group(chart);
57      myStart = start;
58      myEnd = end;
59    }
60    protected LinearPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
61      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
62    }
63
64    public virtual void SetPosition(PointD start, PointD end) {
65      myStart = start;
66      myEnd = end;
67      OnUpdate();
68    }
69    public void SetPosition(double x1, double y1, double x2, double y2) {
70      SetPosition(new PointD(x1, y1), new PointD(x2, y2));
71    }
72    public override void Move(Offset delta) {
73      SetPosition(Start + delta, End + delta);
74    }
75
76    public override bool ContainsPoint(PointD point) {
77      if (Selected) {
78        if (selectionRectangles.GetPrimitive(point) != null) return true;
79      }
80      return false;
81    }
82
83    public override void MouseDrag(PointD point, Offset offset, MouseButtons button) {
84      if (button == MouseButtons.Left) {
85        if (Selected) {
86          if (selectionRectangles.ContainsPoint(point)) {
87            SelectionRectangle rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
88            if (rect.Point == Start) {
89              SetPosition(Start + offset, End);
90            } else if (rect.Point == End) {
91              SetPosition(Start, End + offset);
92            }
93          } else {
94            base.MouseDrag(point, offset, button);
95          }
96        }
97      }
98    }
99
100    public override Cursor GetCursor(PointD point) {
101      if (Selected) {
102        Cursor cursor = selectionRectangles.GetCursor(point);
103        if (cursor != null) return cursor;
104      }
105      return base.GetCursor(point);
106    }
107
108    public override void PostDraw(Graphics graphics) {
109      selectionRectangles.Clear();
110      if (Selected) {
111        Pen pen = new Pen(Color.LightGray, 3);
112        pen.DashStyle = DashStyle.Dash;
113        graphics.DrawLine(pen,
114                          Chart.TransformWorldToPixel(Start),
115                          Chart.TransformWorldToPixel(End));
116        selectionRectangles.Add(new SelectionRectangle(Chart, Start, Cursors.SizeAll));
117        selectionRectangles.Add(new SelectionRectangle(Chart, End, Cursors.SizeAll));
118
119        selectionRectangles.Draw(graphics);
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.