Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/RectangularPrimitiveBase.cs @ 17328

Last change on this file since 17328 was 13717, checked in by bburlacu, 9 years ago

#1265: Updated license year to 2016.

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
24using System.Drawing.Drawing2D;
25using System.Windows.Forms;
26
27namespace HeuristicLab.Visualization {
28  public abstract class RectangularPrimitiveBase : PrimitiveBase {
29    protected IGroup selectionRectangles;
30
31    private PointD myLowerLeft;
32    public virtual PointD LowerLeft {
33      get { return myLowerLeft; }
34    }
35    private PointD myUpperRight;
36    public virtual PointD UpperRight {
37      get { return myUpperRight; }
38    }
39    public virtual SizeD Size {
40      get { return new SizeD(UpperRight.X - LowerLeft.X, UpperRight.Y - LowerLeft.Y); }
41    }
42
43    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight)
44      : base(chart) {
45      selectionRectangles = new Group(chart);
46      SetPosition(lowerLeft, upperRight);
47    }
48    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight, Pen pen, Brush brush)
49      : base(chart, pen, brush) {
50      selectionRectangles = new Group(chart);
51      SetPosition(lowerLeft, upperRight);
52    }
53
54    public virtual void SetPosition(PointD lowerLeft, PointD upperRight) {
55      if ((lowerLeft.X > upperRight.X) || (lowerLeft.Y > upperRight.Y))
56        throw new ArgumentException("Lower left point is greater than upper right point");
57
58      myLowerLeft = lowerLeft;
59      myUpperRight = upperRight;
60      OnRedrawRequired();
61    }
62
63    public override void Move(Offset delta) {
64      SetPosition(LowerLeft + delta, UpperRight + delta);
65    }
66
67    public override void Move(PointD point, Offset delta) {
68      if (Selected && selectionRectangles.ContainsPoint(point)) {
69        var rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
70        var point1 = PointD.Empty;
71        var point2 = PointD.Empty;
72        if (rect.Point == LowerLeft) {
73          point1 = LowerLeft + delta;
74          point2 = UpperRight;
75        } else if (rect.Point == UpperRight) {
76          point1 = LowerLeft;
77          point2 = UpperRight + delta;
78        } else if (rect.Point.X == LowerLeft.X && rect.Point.Y == UpperRight.Y) {
79          point1 = new PointD(LowerLeft.X + delta.DX, LowerLeft.Y);
80          point2 = new PointD(UpperRight.X, UpperRight.Y + delta.DY);
81        } else if (rect.Point.X == UpperRight.X && rect.Point.Y == LowerLeft.Y) {
82          point1 = new PointD(LowerLeft.X, LowerLeft.Y + delta.DY);
83          point2 = new PointD(UpperRight.X + delta.DX, UpperRight.Y);
84        }
85        SetPosition(new PointD(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y)),
86                    new PointD(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y)));
87      } else SetPosition(LowerLeft + delta, UpperRight + delta);
88    }
89
90    public override void SnapToGrid(IGrid grid) {
91      var bl = grid.GetBottomLeftGridPoint(LowerLeft);
92      var ur = grid.GetUpperRightGridPoint(LowerLeft);
93      var sx = Math.Abs(bl.X - LowerLeft.X) <= Math.Abs(ur.X - LowerLeft.X) ? bl.X : ur.X;
94      var sy = Math.Abs(bl.Y - LowerLeft.Y) <= Math.Abs(ur.Y - LowerLeft.Y) ? bl.Y : ur.Y;
95
96      var end = new PointD(UpperRight.X + (sx - LowerLeft.X), UpperRight.Y + (sy - LowerLeft.Y));
97
98      bl = grid.GetBottomLeftGridPoint(end);
99      ur = grid.GetUpperRightGridPoint(end);
100      var ex = Math.Abs(bl.X - end.X) <= Math.Abs(ur.X - end.X) ? bl.X : ur.X;
101      var ey = Math.Abs(bl.Y - end.Y) <= Math.Abs(ur.Y - end.Y) ? bl.Y : ur.Y;
102
103      SetPosition(new PointD(sx, sy), new PointD(ex, ey));
104    }
105
106    public override void SnapToGrid(PointD point, IGrid grid) {
107      if (Selected && selectionRectangles.ContainsPoint(point)) {
108        var rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
109        var point1 = LowerLeft;
110        var point2 = UpperRight;
111        if (rect.Point == LowerLeft) {
112          var bl = grid.GetBottomLeftGridPoint(LowerLeft);
113          var ur = grid.GetUpperRightGridPoint(LowerLeft);
114          var x = Math.Abs(bl.X - LowerLeft.X) <= Math.Abs(ur.X - LowerLeft.X) ? bl.X : ur.X;
115          var y = Math.Abs(bl.Y - LowerLeft.Y) <= Math.Abs(ur.Y - LowerLeft.Y) ? bl.Y : ur.Y;
116          point1 = new PointD(x, y);
117        } else if (rect.Point == UpperRight) {
118          var bl = grid.GetBottomLeftGridPoint(UpperRight);
119          var ur = grid.GetUpperRightGridPoint(UpperRight);
120          var x = Math.Abs(bl.X - UpperRight.X) <= Math.Abs(ur.X - UpperRight.X) ? bl.X : ur.X;
121          var y = Math.Abs(bl.Y - UpperRight.Y) <= Math.Abs(ur.Y - UpperRight.Y) ? bl.Y : ur.Y;
122          point2 = new PointD(x, y);
123        } else if (rect.Point.X == LowerLeft.X && rect.Point.Y == UpperRight.Y) {
124          var upperLeft = new PointD(LowerLeft.X, UpperRight.Y);
125          var bl = grid.GetBottomLeftGridPoint(upperLeft);
126          var ur = grid.GetUpperRightGridPoint(upperLeft);
127          var x = Math.Abs(bl.X - upperLeft.X) <= Math.Abs(ur.X - upperLeft.X) ? bl.X : ur.X;
128          var y = Math.Abs(bl.Y - upperLeft.Y) <= Math.Abs(ur.Y - upperLeft.Y) ? bl.Y : ur.Y;
129          point1 = new PointD(x, LowerLeft.Y);
130          point2 = new PointD(UpperRight.X, y);
131        } else if (rect.Point.X == UpperRight.X && rect.Point.Y == LowerLeft.Y) {
132          var lowerRight = new PointD(UpperRight.X, LowerLeft.Y);
133          var bl = grid.GetBottomLeftGridPoint(lowerRight);
134          var ur = grid.GetUpperRightGridPoint(lowerRight);
135          var x = Math.Abs(bl.X - lowerRight.X) <= Math.Abs(ur.X - lowerRight.X) ? bl.X : ur.X;
136          var y = Math.Abs(bl.Y - lowerRight.Y) <= Math.Abs(ur.Y - lowerRight.Y) ? bl.Y : ur.Y;
137          point1 = new PointD(LowerLeft.X, y);
138          point2 = new PointD(x, UpperRight.Y);
139        }
140        SetPosition(new PointD(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y)),
141                    new PointD(Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y)));
142      } else SnapToGrid(grid);
143    }
144
145    public override bool ContainsPoint(PointD point) {
146      if (Selected) {
147        if (selectionRectangles.ContainsPoint(point)) return true;
148      }
149      return false;
150    }
151
152    public override Cursor GetCursor(PointD point) {
153      if (Selected) {
154        var cursor = selectionRectangles.GetCursor(point);
155        if (cursor != null) return cursor;
156      }
157      return base.GetCursor(point);
158    }
159
160    public override void PostDraw(Graphics graphics) {
161      selectionRectangles.Clear();
162      if (Selected) {
163        Pen pen = new Pen(Color.LightGray, 3);
164        pen.DashStyle = DashStyle.Dash;
165        Point p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
166        Size s = Chart.TransformWorldToPixel(Size);
167        graphics.DrawRectangle(pen, p.X, p.Y, s.Width, s.Height);
168        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, LowerLeft.Y, Cursors.SizeNWSE));
169        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, LowerLeft.Y, Cursors.SizeNESW));
170        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, UpperRight.Y, Cursors.SizeNESW));
171        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, UpperRight.Y, Cursors.SizeNWSE));
172
173        selectionRectangles.Draw(graphics);
174      }
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.