source: branches/FitnessLandscapeAnalysis/HeuristicLab.Problems.NK.Views/BoolMatrixView.cs @ 12596

Last change on this file since 12596 was 12596, checked in by ascheibe, 5 years ago

#2306 some cosmetic changes

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.ComponentModel;
24using System.Drawing;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.NK {
30
31  [View("BoolMatrixView")]
32  [Content(typeof(BoolMatrix), IsDefaultView = false)]
33  public sealed partial class BoolMatrixView : ItemView {
34    private BackgroundWorker worker;
35
36    public new BoolMatrix Content {
37      get { return (BoolMatrix)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public BoolMatrixView() {
42      InitializeComponent();
43    }
44
45    protected override void DeregisterContentEvents() {
46      Content.ToStringChanged -= Content_ToStringChanged;
47      base.DeregisterContentEvents();
48    }
49
50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      Content.ToStringChanged += Content_ToStringChanged;
53    }
54
55    void Content_ToStringChanged(object sender, EventArgs e) {
56      Rebuild();
57    }
58
59    private void Rebuild() {
60      int width = pictureBox.Width;
61      int height = pictureBox.Height;
62      int tileWidth = width / Content.Columns;
63      int tileHeight = height / Content.Rows;
64      int border = (int)Math.Round((tileWidth + tileHeight) / 2.0 * 0.05);
65      var bitmap = new Bitmap(width, height);
66
67      if (worker != null)
68        worker.CancelAsync();
69
70      if (tileWidth == 0 || tileHeight == 0) {
71        using (Graphics g = Graphics.FromImage(bitmap)) {
72          g.DrawString("BoolMatrix is too big to draw.", DefaultFont, new SolidBrush(Color.Black), 10.0f, height / 2.0f);
73          g.Flush();
74        }
75        pictureBox.Image = bitmap;
76        return;
77      }
78
79      worker = new BackgroundWorker();
80      worker.WorkerSupportsCancellation = true;
81      worker.DoWork += (s, a) => {
82        using (Graphics g = Graphics.FromImage(bitmap)) {
83          for (int i = 0; i < Content.Rows; i++) {
84            for (int j = 0; j < Content.Columns; j++) {
85              if (worker.CancellationPending) {
86                a.Cancel = true;
87                break;
88              }
89              g.FillRectangle(Content[i, j] ? Brushes.Black : Brushes.White, i * tileWidth, j * tileHeight,
90                                                                    tileWidth - border, tileHeight - border);
91            }
92          }
93          g.Flush();
94        }
95      };
96      worker.RunWorkerCompleted += (s, a) => {
97        if (!a.Cancelled)
98          pictureBox.Image = bitmap;
99        if (!worker.CancellationPending)
100          worker = null;
101      };
102      worker.RunWorkerAsync();
103    }
104
105    protected override void OnContentChanged() {
106      base.OnContentChanged();
107      if (Content == null) {
108        pictureBox.Image = new Bitmap(1, 1);
109      } else {
110        Rebuild();
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.