Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Misc/ILLegend.cs @ 10903

Last change on this file since 10903 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 9.2 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Drawing;
44using System.Windows.Forms; 
45using ILNumerics.Drawing;
46using ILNumerics.Exceptions; 
47using ILNumerics.Drawing.Graphs;
48using ILNumerics.Drawing.Interfaces;
49using ILNumerics.Drawing.Labeling;
50using ILNumerics.Drawing.Platform.OpenGL;
51
52namespace ILNumerics.Drawing.Misc {
53    public abstract class ILLegend {
54
55        #region events
56        public event EventHandler Changed;
57        protected void OnChanged() {
58            if (Changed != null) {
59                Changed(this,null);
60            }
61        }
62        void m_border_Changed(object sender, EventArgs e) {
63            OnChanged();
64        }
65        #endregion
66
67        #region attributes
68        protected ILPanel m_panel;
69        protected Size m_size;
70        protected PointF m_location;
71        protected Color m_bgColor;
72        protected ILLineProperties m_border;
73        protected float m_opacity;
74        protected Padding m_padding;
75        protected bool m_visible;
76        #endregion
77
78        #region properties
79        /// <summary>
80        /// get / set visibility for legend
81        /// </summary>
82        public bool Visible {
83            get {
84                return m_visible;
85            }
86            set {
87                m_visible = value;
88                OnChanged();
89            }
90        }
91        /// <summary>
92        /// Get/ set opacity for filled area, values: 0...1.0
93        /// </summary>
94        public float Opacity {
95            get {
96                return m_opacity;
97            }
98            set {
99                m_opacity = value;
100                OnChanged();
101            }
102        }
103
104        /// <summary>
105        /// Get / set background color
106        /// </summary>
107        public Color BackgroundColor {
108            get {
109                return m_bgColor;
110            }
111            set {
112                m_bgColor = value;
113                OnChanged();
114            }
115        }
116        /// <summary>
117        /// get/ set location of legend box (upper/left corner, fraction of ClientSize [(0,0)...(1f,1F)]), empty for auto mode
118        /// </summary>
119        public PointF Location {
120            get {
121                return m_location;
122            }
123            set {
124                m_location = value;
125                OnChanged();
126            }
127        }
128        /// <summary>
129        /// get/ set size of legend box, empty for auto mode
130        /// </summary>
131        public Size Size {
132            get { return m_size; }
133            set {
134                m_size = value;
135                OnChanged();
136            }
137        }
138        /// <summary>
139        /// set properties of legend's border
140        /// </summary>
141        public ILLineProperties Border {
142            get {
143                return m_border;
144            }
145        }
146        #endregion
147
148        #region constructor
149        /// <summary>
150        /// construct new legend object
151        /// </summary>
152        internal ILLegend (ILPanel panel) {
153            m_panel = panel;
154            m_size = Size.Empty;
155            m_border = new ILLineProperties();
156            m_border.Antialiasing = false;
157            m_border.Color = Color.LightGray; 
158            m_border.Style = LineStyle.Solid;
159            m_border.Width = 2;
160            m_border.Changed += new EventHandler(m_border_Changed);
161            m_location = Point.Empty;
162            m_bgColor = Color.FromArgb(250,250,250);
163            m_opacity = 0.9f;
164            m_padding = new Padding(5);
165            m_visible = false;
166        }
167        /// <summary>
168        /// Create new instance of ILLegend, depending on graphics device type
169        /// </summary>
170        /// <param name="panel">panel hosting this legend</param>
171        /// <returns>newly created ILLegend object</returns>
172        public static ILLegend Create(ILPanel panel) {
173            if (panel is ILOGLPanel) {
174                return new ILOGLLegend(panel);
175            } else
176                throw new NotImplementedException("Currently only legends for OpenGL are supported");
177        }
178        #endregion
179
180        #region public interface
181        /// <summary>
182        /// Draw the legend onto a predefined bitmap or into GL context
183        /// </summary>
184        /// <param name="p">render properties</param>
185        /// <param name="area">rectangle area defines region to draw legend contents into.</param>
186        /// <remarks>This function does only render to a predefined bitmap, which must be given in p. Rendering to
187        /// (dvice dependent) graphic contexts is done in derived implementations.</remarks>
188        public virtual void Draw(ILRenderProperties p, Rectangle area) {
189            if (m_panel == null || m_panel.Graphs == null) return;
190            if (p.Graphics == null) {
191                throw new ILArgumentException("ILLegend:Draw: unexpected parameter (bitmap) is null. This should be handled in derived classes!");
192            }
193            // draw bg + border
194            PointF p1,p2;
195            float offsX = Math.Max(m_border.Width / 2.0f,1.0f), offsY;
196            p1 = new PointF(offsX,offsX);
197            p2 = new PointF(m_size.Width-offsX,offsX);
198            Pen pen = new Pen(m_border.Color,m_border.Width);
199            // todo: implement dash styles
200            p.Graphics.Clear(m_bgColor);
201            p.Graphics.DrawLine(pen,p1,p2);
202            p1.X = p2.X; p1.Y = m_size.Height - offsX;
203            p.Graphics.DrawLine(pen,p2,p1);
204            p2.X = offsX; p2.Y = p1.Y;
205            p.Graphics.DrawLine(pen,p1,p2);
206            p1.X = offsX; p1.Y = offsX;
207            p.Graphics.DrawLine(pen,p2,p1);
208            List<IILLegendRenderer> providers = new List<IILLegendRenderer>(10);
209            foreach (ILGraph graph in m_panel.Graphs) {
210                if (graph is IILLegendRenderer) {
211                    providers.Add(graph as IILLegendRenderer);
212                }
213            }
214            // compute final example sizes to fit into my size
215            offsX = m_size.Width * 0.1f; offsY = m_size.Height * 0.1f;
216            area.Height = (int)(m_size.Height - 2 * offsY) / providers.Count;
217            area.Width  = (int)(m_size.Width - 2 * offsX) / providers.Count;
218            area.X = (int)offsX;
219            for (int i = 0; i < providers.Count; i++) {
220                area.Y = (int)(offsY + i * area.Height);
221                // todo: not implemented!
222                providers[i].DrawToLegend(p,area,area);
223            }
224        }
225
226        #endregion
227
228        #region helper functions
229        protected List<IILLegendRenderer> getRenderers(ref Size labelSize) {
230            List<IILLegendRenderer> ret = new List<IILLegendRenderer>();
231            foreach(ILGraph graph in m_panel.Graphs) {
232                IILLegendRenderer rend = graph as IILLegendRenderer;
233                if (rend != null) {
234                    ret.Add(rend);
235                    if (rend.LabelSize.Width > labelSize.Width)
236                        labelSize.Width = rend.LabelSize.Width;
237                    labelSize.Height += rend.LabelSize.Height;
238                }
239            }
240            return ret;
241        }
242        #endregion
243    }
244}
Note: See TracBrowser for help on using the repository browser.