Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Knapsack.Views/3.3/KnapsackSolutionView.cs @ 3904

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

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 7.5 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.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.Problems.Knapsack;
28using HeuristicLab.Data;
29using System.Collections.Generic;
30
31namespace HeuristicLab.Problems.Knapsack.Views {
32  /// <summary>
33  /// A view for a knapsack solution.
34  /// </summary>
35  [View("Knapsack View")]
36  [Content(typeof(KnapsackSolution), true)]
37  public partial class KnapsackSolutionView : HeuristicLab.Core.Views.ItemView {
38    public new KnapsackSolution Content {
39      get { return (KnapsackSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public KnapsackSolutionView() {
44      InitializeComponent();
45    }
46
47    protected override void DeregisterContentEvents() {
48      Content.BinaryVectorChanged -= new EventHandler(Content_BinaryVectorChanged);
49      Content.CapacityChanged -= new EventHandler(Content_CapacityChanged);
50      Content.WeightsChanged -= new EventHandler(Content_WeightsChanged);
51      Content.ValuesChanged -= new EventHandler(Content_ValuesChanged);
52      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
53      base.DeregisterContentEvents();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.BinaryVectorChanged += new EventHandler(Content_BinaryVectorChanged);
59      Content.CapacityChanged += new EventHandler(Content_CapacityChanged);
60      Content.WeightsChanged += new EventHandler(Content_WeightsChanged);
61      Content.ValuesChanged += new EventHandler(Content_ValuesChanged);
62      Content.QualityChanged += new EventHandler(Content_QualityChanged);
63    }
64
65    private void Content_BinaryVectorChanged(object sender, EventArgs e) {
66      if (InvokeRequired)
67        Invoke(new EventHandler(Content_BinaryVectorChanged), sender, e);
68      else
69        GenerateImage();
70    }
71
72    private void Content_QualityChanged(object sender, EventArgs e) {
73      if (InvokeRequired)
74        Invoke(new EventHandler(Content_QualityChanged), sender, e);
75      else
76        GenerateImage();
77    }
78
79    private void Content_CapacityChanged(object sender, EventArgs e) {
80      if (InvokeRequired)
81        Invoke(new EventHandler(Content_CapacityChanged), sender, e);
82      else
83        GenerateImage();
84    }
85
86    private void Content_WeightsChanged(object sender, EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_WeightsChanged), sender, e);
89      else
90        GenerateImage();
91    }
92
93    private void Content_ValuesChanged(object sender, EventArgs e) {
94      if (InvokeRequired)
95        Invoke(new EventHandler(Content_ValuesChanged), sender, e);
96      else
97        GenerateImage();
98    }
99
100    protected override void OnContentChanged() {
101      base.OnContentChanged();
102      if (Content == null)
103        pictureBox.Image = null;
104      else
105        GenerateImage();
106    }
107
108    protected override void SetEnabledStateOfControls() {
109      base.SetEnabledStateOfControls();
110      pictureBox.Enabled = Content != null;
111    }
112
113    private void GenerateImage() {
114      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
115        if (Content == null) {
116          pictureBox.Image = null;
117        } else {
118          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
119          using (Graphics graphics = Graphics.FromImage(bitmap)) {
120            int border = 10;
121
122            int borderX = border;
123            if (pictureBox.Width - border * 2 < 0)
124              borderX = 0;
125            int width = pictureBox.Width - borderX * 2;
126
127            int borderY = border;
128            if (pictureBox.Height - border * 2 < 0)
129              borderY = 0;
130            int height = pictureBox.Height - borderY * 2;
131
132            //preprocess
133            double capacity = Content.Capacity.Value;
134
135            if (capacity != 0) {
136              double packedCapacity = 0;
137              double maxValue = 0;
138              for (int i = 0; i < Content.BinaryVector.Length; i++) {
139                if (Content.BinaryVector[i]) {
140                  packedCapacity += Content.Weights[i];
141                }
142
143                if (Content.Values[i] > maxValue)
144                  maxValue = Content.Values[i];
145              }
146
147              int knapsackHeight = height;
148              if (packedCapacity > capacity) {
149                knapsackHeight = (int)Math.Round(capacity / packedCapacity * (double)height);
150              }
151
152              //draw knapsack
153              using (Pen pen = new Pen(Color.Black, 2)) {
154                graphics.DrawRectangle(pen,
155                  borderX - 2, pictureBox.Height - borderY - knapsackHeight - 2, width + 4, knapsackHeight + 4);
156              }
157
158              //draw items sorted by value
159              List<int> sortedIndices = new List<int>();
160              for (int i = 0; i < Content.BinaryVector.Length; i++) {
161                if (Content.BinaryVector[i]) {
162                  sortedIndices.Add(i);
163                }
164              }
165
166              sortedIndices.Sort(
167                delegate(int i, int j) {
168                  if (Content.Values[i] < Content.Values[j])
169                    return -1;
170                  else if (Content.Values[i] > Content.Values[j])
171                    return 1;
172                  else
173                    return 0;
174                });
175
176              int currentPosition = pictureBox.Height - borderY;
177              foreach (int i in sortedIndices) {
178                if (Content.BinaryVector[i]) {
179
180                  double weight = Content.Weights[i];
181                  double factor = weight / capacity;
182                  int elementHeight = (int)Math.Floor(knapsackHeight * factor);
183
184                  double value = Content.Values[i];
185                  //color according to value
186                  int colorValue = 0;
187                  if (value != 0)
188                    colorValue = (int)Math.Round(255.0 * value / maxValue);
189                  Color color = Color.FromArgb(
190                    0, 0, colorValue);
191
192                  using (Brush brush = new SolidBrush(color)) {
193                    graphics.FillRectangle(brush,
194                      borderX, currentPosition - elementHeight, width, elementHeight);
195                  }
196                  graphics.DrawRectangle(Pens.White,
197                    borderX, currentPosition - elementHeight, width, elementHeight);
198
199                  currentPosition -= elementHeight;
200                }
201              }
202            }
203          }
204          pictureBox.Image = bitmap;
205        }
206      }
207    }
208
209    private void pictureBox_SizeChanged(object sender, EventArgs e) {
210      GenerateImage();
211    }
212  }
213}
Note: See TracBrowser for help on using the repository browser.