#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Core.Views;
using HeuristicLab.MainForm;
using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
using HeuristicLab.Problems.BinPacking.PackingItem;
using HeuristicLab.Problems.BinPacking.PackingBin;
using HeuristicLab.Problems.BinPacking.Dimensions;
namespace HeuristicLab.Problems.BinPacking.Views {
[View("2-dimensional packing plan view")]
[Content(typeof(PackingPlan), true)]
public partial class PackingPlan2DView : ItemView {
public PackingPlan2DView() {
InitializeComponent();
}
protected override void DeregisterContentEvents() {
Content.QualityChanged -= new EventHandler(Content_QualityChanged);
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.QualityChanged += new EventHandler(Content_QualityChanged);
}
public new PackingPlan Content {
get { return (PackingPlan)base.Content; }
set { base.Content = value; }
}
protected override void OnContentChanged() {
base.OnContentChanged();
binSelection.Items.Clear();
if (Content == null) {
Redraw();
} else {
for (int i = 0; i < Content.NrOfBins; i++)
binSelection.Items.Add(i);
Redraw(Content);
}
}
private void Redraw() {
packingPlan2D.InitializeContainer(0, 0);
}
private void Redraw(PackingPlan plan) {
int currentBin = (binSelection != null && binSelection.SelectedItem != null) ? (int)(binSelection.SelectedItem) : 0;
var bin = plan.BinPackings[currentBin].BinMeasures;
packingPlan2D.InitializeContainer(bin.Width, bin.Height);
foreach (var entry in plan.BinPackings[currentBin].ItemMeasures) {
var position = plan.BinPackings[currentBin].ItemPositions[entry.Key];
packingPlan2D.AddItemToContainer(
position.Rotated ? entry.Value.Height : entry.Value.Width,
position.Rotated ? entry.Value.Width : entry.Value.Height,
position.X, position.Y, entry.Key.ToString());
}
}
private void binSelection_SelectedIndexChanged(object sender, EventArgs e) {
Redraw(Content);
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
}
private void Content_QualityChanged(object sender, EventArgs e) {
if (InvokeRequired)
Invoke(new EventHandler(Content_QualityChanged), sender, e);
else {
binSelection.Items.Clear();
if (Content == null) {
Redraw();
} else {
for (int i = 0; i < Content.NrOfBins; i++)
binSelection.Items.Add(i);
Redraw(Content);
}
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
packingPlan2D.Refresh();
}
}
}
}