[7141] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Drawing;
|
---|
| 5 | using System.Data;
|
---|
| 6 | using System.Linq;
|
---|
| 7 | using System.Text;
|
---|
| 8 | using System.Windows.Forms;
|
---|
| 9 | using HeuristicLab.MainForm;
|
---|
| 10 | using HeuristicLab.Core.Views;
|
---|
| 11 | using HeuristicLab.Data;
|
---|
| 12 | using System.Threading;
|
---|
| 13 |
|
---|
| 14 | namespace 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 | }
|
---|