Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK.Views/BoolMatrixBitmapView.cs @ 7654

Last change on this file since 7654 was 7141, checked in by epitzer, 13 years ago

#1696 correct output paths and add VRP analysis project

File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.MainForm;
10using HeuristicLab.Core.Views;
11using HeuristicLab.Data;
12using System.Threading;
13
14namespace HeuristicLab.Problems.NK {
15
16  [View("BoolMatrixBitmapView")]
17  [Content(typeof(BoolMatrix), IsDefaultView = false)]
18  public sealed partial class BoolMatrixView : ItemView {
19
20    public new BoolMatrix Content {
21      get { return (BoolMatrix)base.Content; }
22      set { base.Content = value; }
23    }
24
25    public BoolMatrixView() {
26      InitializeComponent();
27    }   
28
29    protected override void DeregisterContentEvents() {     
30      Content.ToStringChanged -= new EventHandler(Content_ToStringChanged);
31      base.DeregisterContentEvents();
32    }   
33
34    protected override void RegisterContentEvents() {
35      base.RegisterContentEvents();
36      Content.ToStringChanged += new EventHandler(Content_ToStringChanged);
37    }
38
39    #region Event Handlers (Content)
40    void Content_ToStringChanged(object sender, EventArgs e) {
41      Rebuild();
42    }
43    #endregion
44
45    private BackgroundWorker worker;
46    private void Rebuild() {
47      if (worker != null)
48        worker.CancelAsync();
49      worker = new BackgroundWorker();
50      worker.WorkerSupportsCancellation = true;
51      Thread.Sleep(1);
52      var bitmap = new Bitmap(Content.Rows*4, Content.Columns*4);     
53      worker.DoWork += (s, a) => {
54        using (Graphics g = Graphics.FromImage(bitmap)) {
55          for (int i = 0; i<Content.Rows; i++) {
56            for (int j = 0; j<Content.Columns; j++) {
57              if (worker.CancellationPending) {
58                a.Cancel = true;
59                break;
60              }
61              g.FillRectangle(Content[i, j] ? Brushes.Green : Brushes.Red,
62                i*4, j*4, 3, 3);             
63            }
64          }
65          g.Flush();
66        }
67      };
68      worker.RunWorkerCompleted += (s, a) => {
69        if (!a.Cancelled)
70          pictureBox.Image = bitmap;
71        if (!worker.CancellationPending)
72          worker = null;
73      };
74      worker.RunWorkerAsync();
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        pictureBox.Image = new Bitmap(1, 1);       
81      } else {
82        Rebuild();
83      }
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();     
88    }
89
90    #region Event Handlers (child controls)   
91    #endregion
92  }
93}
Note: See TracBrowser for help on using the repository browser.