Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Data.Views/3.3/BoolMatrixView.cs @ 14579

Last change on this file since 14579 was 12602, checked in by ascheibe, 9 years ago

#2306 moved BoolMatrixView to trunk

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