source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/RectangularPrimitiveBase.cs @ 4776

Last change on this file since 4776 was 4776, checked in by abeham, 8 years ago

#1265

  • Renamed HeuristicLab.Drawing to HeuristicLab.Visualization
File size: 5.6 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;
23using System.Collections.Generic;
24using System.Text;
25using System.Drawing;
26using System.Drawing.Drawing2D;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Visualization {
30  public abstract class RectangularPrimitiveBase : PrimitiveBase {
31    protected IGroup selectionRectangles;
32
33    private PointD myLowerLeft;
34    public virtual PointD LowerLeft {
35      get { return myLowerLeft; }
36    }
37    private PointD myUpperRight;
38    public virtual PointD UpperRight {
39      get { return myUpperRight; }
40    }
41    public virtual SizeD Size {
42      get { return new SizeD(UpperRight.X - LowerLeft.X, UpperRight.Y - LowerLeft.Y); }
43    }
44
45    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight)
46      : base(chart) {
47      selectionRectangles = new Group(chart);
48      SetPosition(lowerLeft, upperRight);
49    }
50    protected RectangularPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2)
51      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
52    }
53    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight, Pen pen, Brush brush)
54      : base(chart, pen, brush) {
55      selectionRectangles = new Group(chart);
56      SetPosition(lowerLeft, upperRight);
57    }
58    protected RectangularPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
59      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
60    }
61
62    public virtual void SetPosition(PointD lowerLeft, PointD upperRight) {
63      if ((lowerLeft.X > upperRight.X) || (lowerLeft.Y > upperRight.Y))
64        throw new ArgumentException("Lower left point is greater than upper right point");
65
66      myLowerLeft = lowerLeft;
67      myUpperRight = upperRight;
68      OnUpdate();
69    }
70    public void SetPosition(double x1, double y1, double x2, double y2) {
71      SetPosition(new PointD(x1, y1), new PointD(x2, y2));
72    }
73    public override void Move(Offset delta) {
74      SetPosition(LowerLeft + delta, UpperRight + delta);
75    }
76
77    public override bool ContainsPoint(PointD point) {
78      if (Selected) {
79        if (selectionRectangles.ContainsPoint(point)) return true;
80      }
81      return false;
82    }
83
84    public override void MouseDrag(PointD point, Offset offset, MouseButtons button) {
85      if (button == MouseButtons.Left) {
86        if (Selected) {
87          if (selectionRectangles.ContainsPoint(point)) {
88            SelectionRectangle rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
89            PointD point1 = PointD.Empty;
90            PointD point2 = PointD.Empty;
91            if (rect.Point == LowerLeft) {
92              point1 = LowerLeft + offset;
93              point2 = UpperRight;
94            } else if (rect.Point == UpperRight) {
95              point1 = LowerLeft;
96              point2 = UpperRight + offset;
97            } else if ((rect.Point.X == LowerLeft.X) && (rect.Point.Y == UpperRight.Y)) {
98              point1 = new PointD(LowerLeft.X + offset.DX, LowerLeft.Y);
99              point2 = new PointD(UpperRight.X, UpperRight.Y + offset.DY);
100            } else if ((rect.Point.X == UpperRight.X) && (rect.Point.Y == LowerLeft.Y)) {
101              point1 = new PointD(LowerLeft.X, LowerLeft.Y + offset.DY);
102              point2 = new PointD(UpperRight.X + offset.DX, UpperRight.Y);
103            }
104            SetPosition(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y),
105                        Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
106          } else {
107            base.MouseDrag(point, offset, button);
108          }
109        }
110      }
111    }
112
113    public override Cursor GetCursor(PointD point) {
114      if (Selected) {
115        Cursor cursor = selectionRectangles.GetCursor(point);
116        if (cursor != null) return cursor;
117      }
118      return base.GetCursor(point);
119    }
120
121    public override void PostDraw(Graphics graphics) {
122      selectionRectangles.Clear();
123      if (Selected) {
124        Pen pen = new Pen(Color.LightGray, 3);
125        pen.DashStyle = DashStyle.Dash;
126        Point p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
127        Size s = Chart.TransformWorldToPixel(Size);
128        graphics.DrawRectangle(pen, p.X, p.Y, s.Width, s.Height);
129        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, LowerLeft.Y, Cursors.SizeNWSE));
130        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, LowerLeft.Y, Cursors.SizeNESW));
131        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, UpperRight.Y, Cursors.SizeNESW));
132        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, UpperRight.Y, Cursors.SizeNWSE));
133
134        selectionRectangles.Draw(graphics);
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.