File: ColorRangeIntervalSlider.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (335 lines) | stat: -rw-r--r-- 10,574 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QuickRoute.BusinessEntities;
using QuickRoute.BusinessEntities.Numeric;

namespace QuickRoute.Controls
{
  public partial class ColorRangeIntervalSlider : UserControl
  {
    private double minValue = 0.0;
    private double maxValue = 1.0;
    private ColorRange colorRange = new ColorRange(new Gradient(Color.White, Color.Black), 0.0, 1.0);
    private Rectangle colorRangeRectangle;
    private SliderType currentSlider = SliderType.None;
    private Bitmap backBufferBitmap;
    private Graphics backBufferGraphics;
    private ScaleCreatorBase scaleCreator = new DoubleScaleCreator(0, 1, 11, false);
    private const int MOUSE_CLOSENESS = 16;
    private Size sliderSize = new Size(11, 6);
    private NumericConverter numericConverter;
    private double alphaAdjustment;
    private bool preventRedraw;

    public event EventHandler ColorRangeStartValueChanged;
    public event EventHandler ColorRangeEndValueChanged;
    public event EventHandler ColorRangeStartValueChanging;
    public event EventHandler ColorRangeEndValueChanging;
    public event MouseEventHandler ColorRangeClicked;

    public ColorRangeIntervalSlider()
    {
      InitializeComponent();
      colorRangeRectangle = new Rectangle(new Point(0, 0), Size);
      SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
      CreateColorRangeRectangle();
      CreateBackBuffer();
    }

    ~ColorRangeIntervalSlider()
    {
      if (backBufferBitmap != null) backBufferBitmap.Dispose();
      if (backBufferGraphics != null) backBufferGraphics.Dispose();
    }

    public double MinValue
    {
      get { return minValue; }
      set { minValue = value; }
    }

    public double MaxValue
    {
      get { return maxValue; }
      set { maxValue = value; }
    }

    public ColorRange ColorRange
    {
      get { return colorRange; }
      set 
      {
        if (colorRange != null)
        {
          colorRange.StartValueChanged -= new EventHandler(ColorRange_StartValueChanged);
          colorRange.EndValueChanged -= new EventHandler(ColorRange_EndValueChanged);
        }
        colorRange = value;
        colorRange.StartValueChanged += new EventHandler(ColorRange_StartValueChanged);
        colorRange.EndValueChanged += new EventHandler(ColorRange_EndValueChanged);
        Draw();
      }
    }

    public ScaleCreatorBase ScaleCreator
    {
      get { return scaleCreator; }
      set 
      { 
        scaleCreator = value;
        Draw();
      }
    }

    public NumericConverter NumericConverter
    {
      get { return numericConverter; }
      set
      {
        numericConverter = value;
        Draw();
      }
    }

    public double AlphaAdjustment
    {
      get { return alphaAdjustment; }
      set { alphaAdjustment = Math.Min(1, Math.Max(-1, value)); }
    }

    public bool PreventRedraw
    {
      get { return preventRedraw; }
      set 
      { 
        preventRedraw = value;
        Draw();
      }
    }


    private void Draw()
    {
      if (backBufferGraphics == null || preventRedraw) return;

      // use backbuffering
      Graphics g = backBufferGraphics;
      // clear background
      g.Clear(BackColor);

      // gradient
      double intervalLength = (colorRange.EndValue - colorRange.StartValue);
      double startLocation = 0.0 - (colorRange.StartValue - minValue) / intervalLength;
      double endLocation = 1.0 + (maxValue - colorRange.EndValue) / intervalLength;
      Gradient.FillCheckerboardRectangle(g, colorRangeRectangle, 8);
      ColorRange.Gradient.Draw(g, colorRangeRectangle, startLocation, endLocation, Gradient.Direction.Horizontal, alphaAdjustment);

      // start value slider
      DrawSlider(new Point(ValueToX(colorRange.StartValue), colorRangeRectangle.Bottom + 1));

      // end value slider
      DrawSlider(new Point(ValueToX(colorRange.EndValue), colorRangeRectangle.Bottom + 1));

      // border
      g.DrawRectangle(Pens.Gray, new Rectangle(colorRangeRectangle.Left - 1, colorRangeRectangle.Top - 1, colorRangeRectangle.Width + 1, colorRangeRectangle.Height + 1));

      // marker lines and labels
      SizeF labelSize;
      int startY;
      if (NumericConverter == null)
      {
        labelSize = new SizeF(0F, 0F);
        startY = colorRangeRectangle.Top;
      }
      else
      {
        labelSize = g.MeasureString(NumericConverter.ToString(scaleCreator.LastMarkerValue) + "    ", Font);
        startY = colorRangeRectangle.Top + (int)labelSize.Height;
      }
      float lastLabelX = (float)colorRangeRectangle.Left - labelSize.Width / 2;
      for (double value = scaleCreator.FirstMarkerValue; value <= scaleCreator.LastMarkerValue; value += scaleCreator.MarkerInterval)
      {
        Pen p = new Pen(Color.FromArgb(192, Color.Black));
        g.DrawLine(
          p,
          (int)ValueToX(value),
          startY,
          ValueToX(value),
          (int)colorRangeRectangle.Bottom
        );
        p.Dispose();
        if (NumericConverter != null)
        {
          float labelX = (float)ValueToX(value);
          // only draw label if it is enough space to the last one
          if (labelX > lastLabelX + labelSize.Width && labelX + labelSize.Width / 2 < (float)colorRangeRectangle.Right) 
          {
            g.DrawString(
              "  " + NumericConverter.ToString(value),
              Font,
              Brushes.Black,
              new PointF(labelX -labelSize.Width / 2, (float)colorRangeRectangle.Top)
            );
            lastLabelX = labelX;
          }
        }
      }

      // copy to screen
      sliderPanel.CreateGraphics().DrawImageUnscaled(backBufferBitmap, new Point(0, 0));
    }

    private double XToValue(int x)
    {
      return minValue + (double)(x - colorRangeRectangle.Left) / (double)(colorRangeRectangle.Width) * (maxValue - minValue);
    }

    private int ValueToX(double value)
    {
      return colorRangeRectangle.Left + (int)((value - minValue) / (maxValue - minValue) * colorRangeRectangle.Width);
    }

    private void CreateBackBuffer()
    {
      if (Width > 0 && Height > 0)
      {
        if (backBufferBitmap != null) backBufferBitmap.Dispose();
        if (backBufferGraphics != null) backBufferGraphics.Dispose();
        backBufferBitmap = new Bitmap(Width, Height);
        backBufferGraphics = Graphics.FromImage(backBufferBitmap);
        backBufferGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
      }
    }

    private void CreateColorRangeRectangle()
    {
      colorRangeRectangle = new Rectangle(
        new Point(sliderSize.Width / 2 + 1, 1),
        new Size(Width - 2 * (sliderSize.Width / 2 + 1), Height - sliderSize.Height - 2));
    }

    private void DrawSlider(Point p)
    {
      Point[] points = new Point[3];

      points[0] = new Point(p.X, p.Y);
      points[1] = new Point(p.X + sliderSize.Width / 2, p.Y + sliderSize.Height);
      points[2] = new Point(p.X - sliderSize.Width / 2, p.Y + sliderSize.Height);

      backBufferGraphics.FillPolygon(Brushes.Black, points);
    }

    #region Event handlers

    private void ColorRangeIntervalSlider_MouseDown(object sender, MouseEventArgs e)
    {
      if (colorRangeRectangle.Contains(e.Location))
      {
        if (ColorRangeClicked != null) ColorRangeClicked(this, e);
      }
      else
      {
        // get closest slider
        int startCloseness = Math.Abs(e.X - ValueToX(colorRange.StartValue));
        int endCloseness = Math.Abs(e.X - ValueToX(colorRange.EndValue));

        if (startCloseness < endCloseness && startCloseness < MOUSE_CLOSENESS)
        {
          currentSlider = SliderType.Start;
          colorRange.StartValue = Math.Max(minValue, Math.Min(maxValue, XToValue(e.X)));
          Draw();
        }
        else if (endCloseness < MOUSE_CLOSENESS)
        {
          currentSlider = SliderType.End;
          colorRange.EndValue = Math.Max(minValue, Math.Min(maxValue, XToValue(e.X)));
          Draw();
        }
        else
        {
          currentSlider = SliderType.None;
        }
      }
    }

    private void ColorRangeIntervalSlider_MouseMove(object sender, MouseEventArgs e)
    {
      switch (currentSlider)
      {
        case SliderType.Start:
          colorRange.StartValue = Math.Max(minValue, Math.Min(maxValue, XToValue(e.X)));
          Draw();
          break;
        case SliderType.End:
          colorRange.EndValue = Math.Max(minValue, Math.Min(maxValue, XToValue(e.X)));
          Draw();
          break;
      }
    }

    private void ColorRangeIntervalSlider_MouseUp(object sender, MouseEventArgs e)
    {
      if (currentSlider == SliderType.Start)
      {
        if (ColorRangeStartValueChanged != null) ColorRangeStartValueChanged(this, new EventArgs());
      }
      else if(currentSlider == SliderType.End)
      {
        if (ColorRangeEndValueChanged != null) ColorRangeEndValueChanged(this, new EventArgs());
      }
      currentSlider = SliderType.None;
    }

    private void ColorRangeIntervalSlider_Resize(object sender, EventArgs e)
    {
      CreateColorRangeRectangle();
      CreateBackBuffer();
    }

    private void ColorRange_StartValueChanged(object sender, EventArgs e)
    {
      Draw();
      if (currentSlider == SliderType.None)
      {
        if (ColorRangeStartValueChanged != null) ColorRangeStartValueChanged(this, new EventArgs());
      }
      else
      {
        if (ColorRangeStartValueChanging != null) ColorRangeStartValueChanging(this, new EventArgs());
      }
    }

    private void ColorRange_EndValueChanged(object sender, EventArgs e)
    {
      Draw();
      if (currentSlider == SliderType.None)
      {
        if (ColorRangeEndValueChanged != null) ColorRangeEndValueChanged(this, new EventArgs());
      }
      else
      {
        if (ColorRangeEndValueChanging != null) ColorRangeEndValueChanging(this, new EventArgs());
      }
    }

    private void SliderPanel_Paint(object sender, PaintEventArgs e)
    {
      Draw();
    }

    #endregion

    private enum SliderType
    {
      Start,
      End,
      None
    }

  }
}