Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10831 was 10831, checked in by bburlacu, 10 years ago

#1265: Added MaximumFontSize property to RectangularPrimitiveBase.

File size: 6.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    public string Text { get; set; }
44    public Font Font { get; set; }
45    public float MaximumFontSize { get; set; }
46
47    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight)
48      : base(chart) {
49      selectionRectangles = new Group(chart);
50      SetPosition(lowerLeft, upperRight);
51
52      MaximumFontSize = 12;
53    }
54    protected RectangularPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2)
55      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
56    }
57    protected RectangularPrimitiveBase(IChart chart, PointD lowerLeft, PointD upperRight, Pen pen, Brush brush)
58      : base(chart, pen, brush) {
59      selectionRectangles = new Group(chart);
60      SetPosition(lowerLeft, upperRight);
61    }
62    protected RectangularPrimitiveBase(IChart chart, double x1, double y1, double x2, double y2, Pen pen, Brush brush)
63      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen, brush) {
64    }
65
66    public virtual void SetPosition(PointD lowerLeft, PointD upperRight) {
67      if ((lowerLeft.X > upperRight.X) || (lowerLeft.Y > upperRight.Y))
68        throw new ArgumentException("Lower left point is greater than upper right point");
69
70      myLowerLeft = lowerLeft;
71      myUpperRight = upperRight;
72      OnUpdate();
73    }
74    public void SetPosition(double x1, double y1, double x2, double y2) {
75      SetPosition(new PointD(x1, y1), new PointD(x2, y2));
76    }
77    public override void Move(Offset delta) {
78      SetPosition(LowerLeft + delta, UpperRight + delta);
79    }
80
81    public override bool ContainsPoint(PointD point) {
82      if (Selected) {
83        if (selectionRectangles.ContainsPoint(point)) return true;
84      }
85      return false;
86    }
87
88    public override void MouseDrag(PointD point, Offset offset, MouseButtons button) {
89      if (button == MouseButtons.Left) {
90        if (Selected) {
91          if (selectionRectangles.ContainsPoint(point)) {
92            SelectionRectangle rect = (SelectionRectangle)selectionRectangles.GetPrimitive(point);
93            PointD point1 = PointD.Empty;
94            PointD point2 = PointD.Empty;
95            if (rect.Point == LowerLeft) {
96              point1 = LowerLeft + offset;
97              point2 = UpperRight;
98            } else if (rect.Point == UpperRight) {
99              point1 = LowerLeft;
100              point2 = UpperRight + offset;
101            } else if ((rect.Point.X == LowerLeft.X) && (rect.Point.Y == UpperRight.Y)) {
102              point1 = new PointD(LowerLeft.X + offset.DX, LowerLeft.Y);
103              point2 = new PointD(UpperRight.X, UpperRight.Y + offset.DY);
104            } else if ((rect.Point.X == UpperRight.X) && (rect.Point.Y == LowerLeft.Y)) {
105              point1 = new PointD(LowerLeft.X, LowerLeft.Y + offset.DY);
106              point2 = new PointD(UpperRight.X + offset.DX, UpperRight.Y);
107            }
108            SetPosition(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y),
109                        Math.Max(point1.X, point2.X), Math.Max(point1.Y, point2.Y));
110          } else {
111            base.MouseDrag(point, offset, button);
112          }
113        }
114      }
115    }
116
117    public override Cursor GetCursor(PointD point) {
118      if (Selected) {
119        Cursor cursor = selectionRectangles.GetCursor(point);
120        if (cursor != null) return cursor;
121      }
122      return base.GetCursor(point);
123    }
124
125    public override void PostDraw(Graphics graphics) {
126      selectionRectangles.Clear();
127      if (Selected) {
128        Pen pen = new Pen(Color.LightGray, 3);
129        pen.DashStyle = DashStyle.Dash;
130        Point p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y + Size.Height));
131        Size s = Chart.TransformWorldToPixel(Size);
132        graphics.DrawRectangle(pen, p.X, p.Y, s.Width, s.Height);
133        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, LowerLeft.Y, Cursors.SizeNWSE));
134        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, LowerLeft.Y, Cursors.SizeNESW));
135        selectionRectangles.Add(new SelectionRectangle(Chart, UpperRight.X, UpperRight.Y, Cursors.SizeNESW));
136        selectionRectangles.Add(new SelectionRectangle(Chart, LowerLeft.X, UpperRight.Y, Cursors.SizeNWSE));
137
138        selectionRectangles.Draw(graphics);
139      }
140    }
141
142    protected virtual void DrawText(Graphics graphics) {
143      // draw the text in the center of the bounding rectangle
144      var p = Chart.TransformWorldToPixel(new PointD(LowerLeft.X, LowerLeft.Y));
145      var s = Chart.TransformWorldToPixel(Size);
146
147      var stringSize = graphics.MeasureString(Text, Font);
148      float fontSize = (int)(Math.Round(Font.Size * s.Width / stringSize.Width));
149      if (fontSize > MaximumFontSize) { fontSize = MaximumFontSize; }
150      Font = new Font(Font.FontFamily, fontSize, GraphicsUnit.Pixel);
151      stringSize = graphics.MeasureString(Text, Font);
152      graphics.DrawString(Text, Font, new SolidBrush(Pen.Color), p.X + (s.Width - stringSize.Width) / 2f, p.Y - (s.Height + stringSize.Height) / 2f);
153    }
154
155    public override void Draw(Graphics graphics) {
156      if (!string.IsNullOrEmpty(Text))
157        DrawText(graphics);
158      base.Draw(graphics);
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.