Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/HeuristicLab.Netron-3.0.2672.12446/View.cs @ 3514

Last change on this file since 3514 was 2868, checked in by mkommend, 14 years ago

finished mapping from OperatorGraph to GraphVisualizationInfo (ticket #867)

File size: 7.6 KB
Line 
1#region License Information
2//This end-user license agreement applies to the following software;
3
4//The Netron Diagramming Library
5//Cobalt.IDE
6//Xeon webserver
7//Neon UI Library
8
9//Copyright (C) 2007, Francois M.Vanderseypen, The Netron Project & The Orbifold
10
11//This program is free software; you can redistribute it and/or
12//modify it under the terms of the GNU General Public License
13//as published by the Free Software Foundation; either version 2
14//of the License, or (at your option) any later version.
15
16//This program is distributed in the hope that it will be useful,
17//but WITHOUT ANY WARRANTY; without even the implied warranty of
18//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19//GNU General Public License for more details.
20
21//You should have received a copy of the GNU General Public License
22//along with this program; if not, write to the Free Software
23//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24
25
26//http://www.fsf.org/licensing/licenses/gpl.html
27
28//http://www.fsf.org/licensing/licenses/gpl-faq.html
29#endregion
30
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using Netron.Diagramming.Core;
36using System.Drawing;
37using System.Drawing.Drawing2D;
38
39namespace HeuristicLab.Netron {
40  public class View : ViewBase {
41
42    public View(IDiagramControl control)
43      : base(control) {
44      control.Document.Model.Selection.OnNewSelection += new EventHandler(Selection_OnNewSelection);
45      // this.Model.OnEntityAdded += new EventHandler<EntityEventArgs>(Model_OnEntityAdded);
46      this.HorizontalRuler.Visible = true;
47      this.VerticalRuler.Visible = true;
48      GhostsFactory.View = this;
49    }
50
51    void Selection_OnNewSelection(object sender, EventArgs e) {
52      ShowTracker();
53    }
54
55    public override void Paint(Graphics g) {
56      base.Paint(g);
57      //Rectangle rectangle = WorkArea;
58      //g.SetClip(WorkArea);
59      g.Transform = ViewMatrix;
60      //draw the ghost and ants on top of the diagram
61      if (Ants != null)
62        Ants.Paint(g);
63      if (Ghost != null)
64        Ghost.Paint(g);
65      if (Tracker != null)
66        Tracker.Paint(g);
67
68      g.Transform.Reset();
69      //g.PageUnit = GraphicsUnit.Pixel;
70      //g.PageScale = 1.0F;
71    }
72
73    protected virtual void InvalidateGhostArea() {
74      if (Ghost != null) {
75        Rectangle area = Ghost.Rectangle;
76        // Inflate it a little so we make sure to get everything.
77        area.Inflate(20, 20);
78        this.Invalidate(area);
79      }
80    }
81
82    protected virtual void InvalidateTrackerArea() {
83      if (Tracker != null) {
84        Rectangle area = Tracker.Rectangle;
85        // Inflate it a little so we make sure to get everything.
86        area.Inflate(20, 20);
87        this.Invalidate(area);
88      }
89    }
90
91    protected virtual void InvalidateAntsArea() {
92      if (Ants != null) {
93        Rectangle area = Ants.Rectangle;
94        // Inflate it a little so we make sure to get everything.
95        area.Inflate(20, 20);
96        this.Invalidate(area);
97      }
98    }
99
100    public override void PaintGhostEllipse(
101        Point ltPoint,
102        Point rbPoint) {
103      // Refresh the old ghost area if needed.
104      this.InvalidateGhostArea();
105
106      Ghost = GhostsFactory.GetGhost(
107          new Point[] { ltPoint, rbPoint },
108              GhostTypes.Ellipse);
109    }
110    public override void PaintGhostRectangle(
111        Point ltPoint,
112        Point rbPoint) {
113      // Refresh the old ghost area if needed.
114      this.InvalidateGhostArea();
115
116      Ghost = GhostsFactory.GetGhost(
117          new Point[] { ltPoint, rbPoint },
118          GhostTypes.Rectangle);
119    }
120    public override void PaintAntsRectangle(
121        Point ltPoint,
122        Point rbPoint) {
123      // Refresh the old area if needed.
124      this.InvalidateAntsArea();
125      Ants = AntsFactory.GetAnts(
126          new Point[] { ltPoint, rbPoint },
127          AntTypes.Rectangle);
128    }
129    public override void PaintGhostLine(Point ltPoint, Point rbPoint) {
130      // Refresh the old ghost area if needed.
131      this.InvalidateGhostArea();
132
133      Ghost = GhostsFactory.GetGhost(
134          new Point[] { ltPoint, rbPoint },
135          GhostTypes.Line);
136    }
137    public override void PaintGhostLine(
138        MultiPointType curveType,
139        Point[] points) {
140      // Refresh the old ghost area if needed.
141      this.InvalidateGhostArea();
142
143      switch (curveType) {
144        case MultiPointType.Straight:
145          Ghost = GhostsFactory.GetGhost(points, GhostTypes.MultiLine);
146          break;
147        case MultiPointType.Polygon:
148          Ghost = GhostsFactory.GetGhost(points, GhostTypes.Polygon);
149          break;
150        case MultiPointType.Curve:
151          Ghost = GhostsFactory.GetGhost(points, GhostTypes.CurvedLine);
152          break;
153
154      }
155    }
156
157    public override void PaintTracker(Rectangle rectangle, bool showHandles) {
158      // Refresh the old area if needed.
159      this.InvalidateTrackerArea();
160      Tracker = TrackerFactory.GetTracker(rectangle, TrackerTypes.Default, showHandles);
161      rectangle.Inflate(20, 20);
162      this.Invalidate(rectangle);
163    }
164
165    #region Tracker
166
167    private enum TrackerTypes {
168      Default
169    }
170
171    private class TrackerFactory {
172
173      private static ITracker defTracker;
174
175      public static ITracker GetTracker(Rectangle rectangle, TrackerTypes type, bool showHandles) {
176        switch (type) {
177          case TrackerTypes.Default:
178            if (defTracker == null) defTracker = new DefaultTracker();
179            defTracker.Transform(rectangle);
180            defTracker.ShowHandles = showHandles;
181            return defTracker;
182          default:
183            return null;
184        }
185
186      }
187    }
188
189    private class DefaultTracker : TrackerBase {
190      private const int gripSize = 4;
191      private const int hitSize = 6;
192      float mx, my, sx, sy;
193
194      public DefaultTracker(Rectangle rectangle)
195        : base(rectangle) {
196      }
197
198      public DefaultTracker()
199        : base() { }
200
201      public override void Transform(Rectangle rectangle) {
202        this.Rectangle = rectangle;
203      }
204
205      public override void Paint(Graphics g) {
206        //the main rectangle
207        g.DrawRectangle(ArtPalette.TrackerPen, Rectangle);
208        #region Recalculate the size and location of the grips
209        mx = Rectangle.X + Rectangle.Width / 2;
210        my = Rectangle.Y + Rectangle.Height / 2;
211        sx = Rectangle.Width / 2;
212        sy = Rectangle.Height / 2;
213        #endregion
214        #region draw the grips
215        if (!ShowHandles) return;
216
217        for (int x = -1; x <= 1; x++) {
218          for (int y = -1; y <= 1; y++) {
219            if (x != 0 || y != 0) //not the middle one
220                        {
221              g.FillRectangle(ArtPalette.GripBrush, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
222              g.DrawRectangle(ArtPalette.BlackPen, mx + x * sx - gripSize / 2, my + y * sy - gripSize / 2, gripSize, gripSize);
223            }
224          }
225        }
226        #endregion
227      }
228
229      public override Point Hit(Point p) {
230        //no need to test if the handles are not shown
231        if (!ShowHandles) return Point.Empty;
232
233        for (int x = -1; x <= +1; x++)
234          for (int y = -1; y <= +1; y++)
235            if ((x != 0) || (y != 0)) {
236              if (new RectangleF(mx + x * sx - hitSize / 2, my + y * sy - hitSize / 2, hitSize, hitSize).Contains(p))
237                return new Point(x, y);
238            }
239        return Point.Empty;
240      }
241    }
242    #endregion
243  }
244}
Note: See TracBrowser for help on using the repository browser.