[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[2210] | 27 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 28 |
|
---|
| 29 | namespace HeuristicLab.GP {
|
---|
[2202] | 30 | public partial class FunctionLibraryEditor : EditorBase {
|
---|
[2212] | 31 | private ChooseItemDialog chooseFunctionDialog;
|
---|
[2202] | 32 | public FunctionLibrary FunctionLibrary {
|
---|
| 33 | get { return (FunctionLibrary)Item; }
|
---|
[645] | 34 | set { base.Item = value; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[2202] | 37 | public FunctionLibraryEditor(FunctionLibrary library)
|
---|
[645] | 38 | : base() {
|
---|
| 39 | InitializeComponent();
|
---|
[2202] | 40 | FunctionLibrary = library;
|
---|
[2212] | 41 | }
|
---|
[645] | 42 |
|
---|
[2212] | 43 | protected override void AddItemEvents() {
|
---|
| 44 | base.AddItemEvents();
|
---|
| 45 | base.Item.Changed += (sender, args) => UpdateControls();
|
---|
| 46 | }
|
---|
[645] | 47 |
|
---|
[2212] | 48 | protected override void UpdateControls() {
|
---|
| 49 | base.UpdateControls();
|
---|
[2701] | 50 | functionsListView.Clear();
|
---|
| 51 | functionsComboBox.Items.Clear();
|
---|
[2212] | 52 | foreach (IFunction fun in FunctionLibrary.Functions) {
|
---|
[2701] | 53 | functionsListView.Items.Add(CreateListViewItem(fun));
|
---|
| 54 | functionsComboBox.Items.Add(fun);
|
---|
[2202] | 55 | if (fun.Manipulator != null) {
|
---|
[2212] | 56 | mutationListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 57 | }
|
---|
[2202] | 58 | if (fun.Initializer != null) {
|
---|
[2212] | 59 | initListView.Items.Add(CreateListViewItem(fun));
|
---|
[645] | 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[2212] | 64 | private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 65 | if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
|
---|
| 66 | IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
|
---|
| 67 | mutationVariableView.Enabled = true;
|
---|
| 68 | mutationVariableView.Variable = new Variable("Manipulator", manipulator);
|
---|
| 69 | } else {
|
---|
| 70 | mutationVariableView.Enabled = false;
|
---|
| 71 | }
|
---|
[2202] | 72 | }
|
---|
[645] | 73 |
|
---|
[2212] | 74 | private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 75 | if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
|
---|
| 76 | IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
|
---|
| 77 | initVariableView.Enabled = true;
|
---|
| 78 | initVariableView.Variable = new Variable("Initializer", initializer);
|
---|
| 79 | } else {
|
---|
| 80 | initVariableView.Enabled = false;
|
---|
| 81 | }
|
---|
[2202] | 82 | }
|
---|
[645] | 83 |
|
---|
[2212] | 84 | private void addButton_Click(object sender, EventArgs e) {
|
---|
| 85 | if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
|
---|
| 86 | if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 87 | FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[645] | 90 |
|
---|
[2212] | 91 | private void removeButton_Click(object sender, EventArgs e) {
|
---|
| 92 | // delete from the end of the list
|
---|
[2701] | 93 | List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList();
|
---|
[2212] | 94 | foreach (int selectedIndex in removeIndices) {
|
---|
[2701] | 95 | FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);
|
---|
[2212] | 96 | }
|
---|
| 97 | }
|
---|
[645] | 98 |
|
---|
[2212] | 99 | private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[2701] | 100 | if (functionsListView.SelectedIndices.Count > 0) {
|
---|
| 101 | removeButton.Enabled = true;
|
---|
| 102 | } else {
|
---|
| 103 | removeButton.Enabled = false;
|
---|
| 104 | }
|
---|
[645] | 105 | }
|
---|
| 106 |
|
---|
[2212] | 107 | private ListViewItem CreateListViewItem(IFunction function) {
|
---|
| 108 | ListViewItem item = new ListViewItem();
|
---|
| 109 | item.Name = function.Name;
|
---|
| 110 | item.Text = function.Name;
|
---|
| 111 | item.Tag = function;
|
---|
| 112 | return item;
|
---|
[645] | 113 | }
|
---|
[2701] | 114 |
|
---|
| 115 | private void functionsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 116 | ListViewItem item = (ListViewItem)e.Item;
|
---|
| 117 | IFunction fun = (IFunction)item.Tag;
|
---|
| 118 | DataObject data = new DataObject();
|
---|
| 119 | data.SetData("IFunction", fun);
|
---|
| 120 | data.SetData("DragSource", functionsListView);
|
---|
| 121 | DoDragDrop(data, DragDropEffects.Link);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | private void functionsComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 125 | if (functionsComboBox.SelectedItem != null) {
|
---|
| 126 | IFunction selectedFun = (IFunction)functionsComboBox.SelectedItem;
|
---|
| 127 | Control funView = (Control)selectedFun.CreateView();
|
---|
| 128 | funView.Dock = DockStyle.Fill;
|
---|
| 129 | functionDetailsPanel.Controls.Clear();
|
---|
| 130 | functionDetailsPanel.Controls.Add(funView);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
[645] | 133 | }
|
---|
| 134 | }
|
---|