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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Drawing;
|
---|
25 | using HeuristicLab.Core.Views;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 |
|
---|
29 | namespace 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 | #region Event Handlers (Content)
|
---|
56 | void Content_ToStringChanged(object sender, EventArgs e) {
|
---|
57 | Rebuild();
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | private void Rebuild() {
|
---|
62 | int width = pictureBox.Width;
|
---|
63 | int height = pictureBox.Height;
|
---|
64 | int tileWidth = width / Content.Columns;
|
---|
65 | int tileHeight = height / Content.Rows;
|
---|
66 | int border = (int)Math.Round((tileWidth + tileHeight) / 2.0 * 0.05);
|
---|
67 | var bitmap = new Bitmap(width, height);
|
---|
68 |
|
---|
69 | if (worker != null)
|
---|
70 | worker.CancelAsync();
|
---|
71 |
|
---|
72 | if (tileWidth == 0 || tileHeight == 0) {
|
---|
73 | using (Graphics g = Graphics.FromImage(bitmap)) {
|
---|
74 | g.DrawString("BoolMatrix is to big to draw.", DefaultFont, new SolidBrush(Color.Black), 10.0f, height / 2.0f);
|
---|
75 | g.Flush();
|
---|
76 | }
|
---|
77 | pictureBox.Image = bitmap;
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | worker = new BackgroundWorker();
|
---|
82 | worker.WorkerSupportsCancellation = true;
|
---|
83 | worker.DoWork += (s, a) => {
|
---|
84 | using (Graphics g = Graphics.FromImage(bitmap)) {
|
---|
85 | for (int i = 0; i < Content.Rows; i++) {
|
---|
86 | for (int j = 0; j < Content.Columns; j++) {
|
---|
87 | if (worker.CancellationPending) {
|
---|
88 | a.Cancel = true;
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | g.FillRectangle(Content[i, j] ? Brushes.Black : Brushes.White, i * tileWidth, j * tileHeight,
|
---|
92 | tileWidth - border, tileHeight - border);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | g.Flush();
|
---|
96 | }
|
---|
97 | };
|
---|
98 | worker.RunWorkerCompleted += (s, a) => {
|
---|
99 | if (!a.Cancelled)
|
---|
100 | pictureBox.Image = bitmap;
|
---|
101 | if (!worker.CancellationPending)
|
---|
102 | worker = null;
|
---|
103 | };
|
---|
104 | worker.RunWorkerAsync();
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected override void OnContentChanged() {
|
---|
108 | base.OnContentChanged();
|
---|
109 | if (Content == null) {
|
---|
110 | pictureBox.Image = new Bitmap(1, 1);
|
---|
111 | } else {
|
---|
112 | Rebuild();
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|