Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7780 was 7780, checked in by bburlacu, 12 years ago

#1265: Fixed zoom and small issue with selection. Added tool tips.

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