Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Data.Views/3.3/BoolMatrixView.cs @ 13332

Last change on this file since 13332 was 12689, checked in by dglaser, 9 years ago

#2388: Merged trunk into HiveStatistics branch

File size: 3.6 KB
RevLine 
[12574]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;
[7141]23using System.ComponentModel;
24using System.Drawing;
25using HeuristicLab.Core.Views;
[12574]26using HeuristicLab.MainForm;
[7141]27
[12602]28namespace HeuristicLab.Data.Views {
[7141]29
[12574]30  [View("BoolMatrixView")]
[7141]31  [Content(typeof(BoolMatrix), IsDefaultView = false)]
32  public sealed partial class BoolMatrixView : ItemView {
[12574]33    private BackgroundWorker worker;
[7141]34
35    public new BoolMatrix Content {
36      get { return (BoolMatrix)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public BoolMatrixView() {
41      InitializeComponent();
[12574]42    }
[7141]43
[12574]44    protected override void DeregisterContentEvents() {
45      Content.ToStringChanged -= Content_ToStringChanged;
[7141]46      base.DeregisterContentEvents();
[12574]47    }
[7141]48
49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
[12574]51      Content.ToStringChanged += Content_ToStringChanged;
[7141]52    }
53
[12601]54    private void Content_ToStringChanged(object sender, EventArgs e) {
[7141]55      Rebuild();
56    }
57
58    private void Rebuild() {
[12574]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
[7141]66      if (worker != null)
67        worker.CancelAsync();
[12574]68
69      if (tileWidth == 0 || tileHeight == 0) {
70        using (Graphics g = Graphics.FromImage(bitmap)) {
[12596]71          g.DrawString("BoolMatrix is too big to draw.", DefaultFont, new SolidBrush(Color.Black), 10.0f, height / 2.0f);
[12574]72          g.Flush();
73        }
74        pictureBox.Image = bitmap;
75        return;
76      }
77
[7141]78      worker = new BackgroundWorker();
79      worker.WorkerSupportsCancellation = true;
80      worker.DoWork += (s, a) => {
81        using (Graphics g = Graphics.FromImage(bitmap)) {
[12574]82          for (int i = 0; i < Content.Rows; i++) {
83            for (int j = 0; j < Content.Columns; j++) {
[7141]84              if (worker.CancellationPending) {
85                a.Cancel = true;
86                break;
87              }
[12574]88              g.FillRectangle(Content[i, j] ? Brushes.Black : Brushes.White, i * tileWidth, j * tileHeight,
89                                                                    tileWidth - border, tileHeight - border);
[7141]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) {
[12574]107        pictureBox.Image = new Bitmap(1, 1);
[7141]108      } else {
109        Rebuild();
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.