Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.QuadraticAssignment.Views/3.3/QuadraticAssignmentProblemView.cs @ 6936

Last change on this file since 6936 was 6936, checked in by abeham, 11 years ago

#1619

  • Added a new service method and a helper class for calling the service and configuring a QAP instance
  • Made parameter properties of the RTS public (so that it can be configured programmatically)
File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 System.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.Problems.QuadraticAssignment.QAPInstanceService;
31
32namespace HeuristicLab.Problems.QuadraticAssignment.Views {
33  [View("Quadratic Assignment Problem View")]
34  [Content(typeof(QuadraticAssignmentProblem), IsDefaultView = true)]
35  public sealed partial class QuadraticAssignmentProblemView : ParameterizedNamedItemView {
36    private QAPService qapService;
37
38    public new QuadraticAssignmentProblem Content {
39      get { return (QuadraticAssignmentProblem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public QuadraticAssignmentProblemView() {
44      InitializeComponent();
45      importInstanceButton.Image = VSImageLibrary.Open;
46      reloadInstancesButton.Text = String.Empty;
47      reloadInstancesButton.Image = VSImageLibrary.Refresh;
48      loadInstanceButton.Image = VSImageLibrary.Internet;
49      Controls.Remove(parameterCollectionView);
50      parameterCollectionView.Dock = DockStyle.Fill;
51      problemTabPage.Controls.Add(parameterCollectionView);
52      qapService = new QAPService();
53    }
54
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57      Content.DistancesParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
58      Content.WeightsParameter.ValueChanged += new EventHandler(WeightsParameter_ValueChanged);
59      Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
60      Content.Instances.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
61      Content.Instances.ItemsMoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
62      Content.Instances.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
63      Content.Instances.ItemsReplaced += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
64      Content.Instances.CollectionReset += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
65    }
66
67    protected override void DeregisterContentEvents() {
68      Content.DistancesParameter.ValueChanged -= new EventHandler(DistanceMatrixParameter_ValueChanged);
69      Content.WeightsParameter.ValueChanged -= new EventHandler(WeightsParameter_ValueChanged);
70      Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
71      Content.Instances.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
72      Content.Instances.ItemsMoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
73      Content.Instances.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
74      Content.Instances.ItemsReplaced -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
75      Content.Instances.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<string>>(Content_Instances_Changed);
76      base.DeregisterContentEvents();
77    }
78
79    private void Content_Instances_Changed(object sender, EventArgs e) {
80      instancesComboBox.Items.Clear();
81      foreach (string name in Content.Instances)
82        instancesComboBox.Items.Add(name);
83      if (instancesComboBox.Items.Count > 0)
84        instancesComboBox.SelectedIndex = 0;
85    }
86
87    private void DistanceMatrixParameter_ValueChanged(object sender, System.EventArgs e) {
88      qapView.Distances = Content.Distances;
89    }
90
91    private void WeightsParameter_ValueChanged(object sender, System.EventArgs e) {
92      qapView.Weights = Content.Weights;
93    }
94
95    private void BestKnownSolutionParameter_ValueChanged(object sender, System.EventArgs e) {
96      qapView.Assignment = Content.BestKnownSolution;
97    }
98
99    protected override void OnContentChanged() {
100      base.OnContentChanged();
101      if (Content != null) {
102        qapView.Distances = Content.Distances;
103        qapView.Weights = Content.Weights;
104        qapView.Assignment = Content.BestKnownSolution;
105      } else {
106        qapView.Distances = null;
107        qapView.Weights = null;
108        qapView.Assignment = null;
109      }
110    }
111
112    protected override void SetEnabledStateOfControls() {
113      base.SetEnabledStateOfControls();
114      reloadInstancesButton.Enabled = !ReadOnly && !Locked && Content != null;
115      instancesComboBox.Enabled = !ReadOnly && !Locked && Content != null;
116      loadInstanceButton.Enabled = !ReadOnly && !Locked && Content != null && !String.IsNullOrEmpty((string)instancesComboBox.SelectedItem);
117      importInstanceButton.Enabled = !ReadOnly && !Locked && Content != null;
118    }
119
120    private void instancesComboBox_SelectedValueChanged(object sender, System.EventArgs e) {
121      SetEnabledStateOfControls();
122    }
123
124    private void loadInstanceButton_Click(object sender, System.EventArgs e) {
125      ReadOnly = true;
126      string instanceStr = instancesComboBox.SelectedItem as string;
127      progressBar.Visible = true;
128      loadInstanceWorker.RunWorkerAsync(instanceStr);
129    }
130
131    private void importFileInstanceButton_Click(object sender, System.EventArgs e) {
132      if (openFileDialog.ShowDialog() == DialogResult.OK) {
133        try {
134          Content.LoadInstanceFromFile(openFileDialog.FileName);
135        } catch (Exception ex) {
136          PluginInfrastructure.ErrorHandling.ShowErrorDialog(ex);
137        }
138      }
139    }
140
141    private void reloadInstancesButton_Click(object sender, EventArgs e) {
142      ReadOnly = true;
143      progressBar.Visible = true;
144      getInstancesWorker.RunWorkerAsync();
145    }
146
147    private void loadInstanceWorker_DoWork(object sender, DoWorkEventArgs e) {
148      string instance = (string)e.Argument;
149      if (String.IsNullOrEmpty(instance)) return;
150      using (var client = new QAPClient()) {
151        qapService.Configure(Content, client, instance, (x) => { if (!Disposing && !IsDisposed) loadInstanceWorker.ReportProgress(x); });
152      }
153    }
154
155    private void getInstancesWorker_DoWork(object sender, DoWorkEventArgs e) {
156      using (var client = new QAPClient()) {
157        getInstancesWorker.ReportProgress(10);
158        var instances = client.GetProblemInstances();
159        getInstancesWorker.ReportProgress(85);
160        Content.Instances.Replace(instances);
161        getInstancesWorker.ReportProgress(100);
162      }
163    }
164
165    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
166      progressBar.Value = e.ProgressPercentage;
167    }
168
169    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
170      progressBar.Visible = false;
171      ReadOnly = false;
172      if (e.Error != null)
173        PluginInfrastructure.ErrorHandling.ShowErrorDialog(e.Error);
174    }
175
176    private void QAPLIBInstancesLabel_Click(object sender, System.EventArgs e) {
177      System.Diagnostics.Process.Start("http://www.seas.upenn.edu/qaplib/");
178    }
179
180    private void QAPLIBInstancesLabel_MouseEnter(object sender, EventArgs e) {
181      Cursor = Cursors.Hand;
182      QAPLIBInstancesLabel.ForeColor = Color.Red;
183      toolTip.SetToolTip(QAPLIBInstancesLabel, "Browse to http://www.seas.upenn.edu/qaplib/");
184    }
185
186    private void QAPLIBInstancesLabel_MouseLeave(object sender, EventArgs e) {
187      Cursor = Cursors.Default;
188      QAPLIBInstancesLabel.ForeColor = Color.Blue;
189      toolTip.SetToolTip(QAPLIBInstancesLabel, String.Empty);
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.