Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.Views/3.3/PackingPlans/PackingPlan2DView.cs @ 13466

Last change on this file since 13466 was 13466, checked in by gkronber, 8 years ago

#1966: docking of controls in (Cuboid|Rectangular)(PackingShape|PackingItem)View

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and 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 HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
26using HeuristicLab.Problems.BinPacking.PackingItem;
27using HeuristicLab.Problems.BinPacking.PackingBin;
28using HeuristicLab.Problems.BinPacking.Dimensions;
29
30namespace HeuristicLab.Problems.BinPacking.Views {
31  [View("2-dimensional packing plan view")]
32  [Content(typeof(PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>), true)]
33  public partial class PackingPlan2DView : ItemView {
34
35    public PackingPlan2DView() {
36      InitializeComponent();
37    }
38
39    protected override void DeregisterContentEvents() {
40      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
41      base.DeregisterContentEvents();
42    }
43    protected override void RegisterContentEvents() {
44      base.RegisterContentEvents();
45      Content.QualityChanged += new EventHandler(Content_QualityChanged);
46    }
47
48
49    public new PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> Content {
50      get { return (PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem>)base.Content; }
51      set { base.Content = value; }
52    }
53
54
55    protected override void OnContentChanged() {
56      base.OnContentChanged();
57      binSelection.Items.Clear();
58      if (Content == null) {
59        ClearState();
60      } else {
61        for (int i = 0; i < Content.NrOfBins; i++)
62          binSelection.Items.Add(i);
63        UpdateState(Content);
64      }
65    }
66
67
68    private void ClearState() {
69      packingPlan2D.InitializeContainer(0, 0);
70    }
71
72    private void UpdateState(PackingPlan<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> plan) {     
73      int currentBin = (binSelection != null && binSelection.SelectedItem != null) ? (int)(binSelection.SelectedItem) : 0;
74      var bin = plan.BinPackings[currentBin].BinMeasures;
75      packingPlan2D.InitializeContainer(bin.Width, bin.Height);
76      foreach (var entry in plan.BinPackings[currentBin].ItemMeasures) {
77        var position = plan.BinPackings[currentBin].ItemPositions[entry.Key];
78          packingPlan2D.AddItemToContainer(
79            position.Rotated ? entry.Value.Height : entry.Value.Width,
80            position.Rotated ? entry.Value.Width : entry.Value.Height,
81            position.X, position.Y, entry.Key.ToString());
82      }
83    }
84
85    private void binSelection_SelectedIndexChanged(object sender, EventArgs e) {
86      UpdateState(Content);
87      packingPlan2D.Refresh();
88    }
89
90
91
92    private void Content_QualityChanged(object sender, EventArgs e) {
93      if (InvokeRequired)
94        Invoke(new EventHandler(Content_QualityChanged), sender, e);
95      else {
96        binSelection.Items.Clear();
97        if (Content == null) {
98          ClearState();
99        } else {
100          for (int i = 0; i < Content.NrOfBins; i++)
101            binSelection.Items.Add(i);
102          UpdateState(Content);
103        }
104        packingPlan2D.Refresh();
105      }
106    } 
107  }
108}
Note: See TracBrowser for help on using the repository browser.