Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/FunctionView.cs @ 442

Last change on this file since 442 was 410, checked in by gkronber, 16 years ago

fixed #180 by adding an event-handler for the mouse-up event that selects the clicked node on right-click.

(had to rename variable functionTreeView to funTreeView because the designer was unhappy. )

File size: 9.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Data;
33
34namespace HeuristicLab.Functions {
35  public partial class FunctionTreeView : ViewBase {
36    private IFunctionTree functionTree;
37
38    private IFunctionTree selectedBranch;
39    private IVariable selectedVariable;
40
41    private FunctionNameVisitor functionNameVisitor;
42    public FunctionTreeView() {
43      InitializeComponent();
44      functionNameVisitor = new FunctionNameVisitor();
45    }
46
47    public FunctionTreeView(IFunctionTree functionTree)
48      : this() {
49      this.functionTree = functionTree;
50      Refresh();
51    }
52
53    protected override void UpdateControls() {
54      funTreeView.Nodes.Clear();
55      functionNameVisitor.Visit(functionTree);
56      TreeNode rootNode = new TreeNode();
57      rootNode.Name = functionTree.Function.Name;
58      rootNode.Text = functionNameVisitor.Name;
59      rootNode.Tag = functionTree;
60      rootNode.ContextMenuStrip = treeNodeContextMenu;
61      funTreeView.Nodes.Add(rootNode);
62
63      foreach(IFunctionTree subTree in functionTree.SubTrees) {
64        CreateTree(rootNode, subTree);
65      }
66      funTreeView.ExpandAll();
67    }
68
69    private void CreateTree(TreeNode rootNode, IFunctionTree functionTree) {
70      TreeNode node = new TreeNode();
71      functionNameVisitor.Visit(functionTree);
72      node.Name = functionTree.Function.Name;
73      node.Text = functionNameVisitor.Name;
74      node.Tag = functionTree;
75      node.ContextMenuStrip = treeNodeContextMenu;
76      rootNode.Nodes.Add(node);
77      foreach(IFunctionTree subTree in functionTree.SubTrees) {
78        CreateTree(node, subTree);
79      }
80    }
81
82    private void functionTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
83      variablesListBox.Items.Clear();
84      variablesSplitContainer.Panel2.Controls.Clear();
85      templateTextBox.Clear();
86      editButton.Enabled = false;
87      if(funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
88        IFunctionTree selectedBranch = (IFunctionTree)funTreeView.SelectedNode.Tag;
89        UpdateVariablesList(selectedBranch);
90        templateTextBox.Text = selectedBranch.Function.Name;
91        this.selectedBranch = selectedBranch;
92        editButton.Enabled = true;
93      }
94    }
95
96    private void UpdateVariablesList(IFunctionTree functionTree) {
97      foreach(IVariable variable in functionTree.LocalVariables) {
98        variablesListBox.Items.Add(variable.Name);
99      }
100    }
101
102    private void variablesListBox_SelectedIndexChanged(object sender, EventArgs e) {
103      // in case we had an event-handler registered for a different variable => unregister the event-handler
104      if(selectedVariable != null) {
105        selectedVariable.Value.Changed -= new EventHandler(selectedVariable_ValueChanged);
106      }
107      if(variablesListBox.SelectedItem != null) {
108        string selectedVariableName = (string)variablesListBox.SelectedItem;
109        selectedVariable = selectedBranch.GetLocalVariable(selectedVariableName);
110        variablesSplitContainer.Panel2.Controls.Clear();
111        Control editor = (Control)selectedVariable.CreateView();
112        variablesSplitContainer.Panel2.Controls.Add(editor);
113        editor.Dock = DockStyle.Fill;
114        // register an event handler that updates the treenode when the value of the variable is changed by the user
115        selectedVariable.Value.Changed += new EventHandler(selectedVariable_ValueChanged);
116      } else {
117        variablesSplitContainer.Panel2.Controls.Clear();
118      }
119    }
120
121    void selectedVariable_ValueChanged(object sender, EventArgs e) {
122      if(funTreeView.SelectedNode != null && funTreeView.SelectedNode.Tag != null) {
123        TreeNode node = funTreeView.SelectedNode;
124        functionNameVisitor.Visit(selectedBranch);
125        node.Text = functionNameVisitor.Name;
126      }
127    }
128
129    protected virtual void editButton_Click(object sender, EventArgs e) {
130      PluginManager.ControlManager.ShowControl(selectedBranch.Function.CreateView());
131    }
132
133    private void copyToClipboardMenuItem_Click(object sender, EventArgs e) {
134      TreeNode node = funTreeView.SelectedNode;
135      if(node == null || node.Tag == null) return;
136
137      ModelAnalyzerExporter visitor = new ModelAnalyzerExporter();
138      visitor.Visit((IFunctionTree)node.Tag);
139      Clipboard.SetText(visitor.ModelAnalyzerPrefix);
140    }
141    private void funTreeView_MouseUp(object sender, MouseEventArgs e) {
142      if(e.Button == MouseButtons.Right) {
143        // Select the clicked node
144        funTreeView.SelectedNode = funTreeView.GetNodeAt(e.X, e.Y);
145      }
146    }
147
148    private class FunctionNameVisitor : IFunctionVisitor {
149      string name;
150      IFunctionTree currentBranch;
151
152      public string Name {
153        get { return name; }
154      }
155
156      public void Visit(IFunctionTree tree) {
157        currentBranch = tree;
158        tree.Function.Accept(this);
159      }
160
161      #region IFunctionVisitor Members
162      public void Visit(IFunction function) {
163        name = function.Name;
164      }
165
166      public void Visit(Addition addition) {
167        name = "+";
168      }
169
170      public void Visit(Constant constant) {
171        name = ((ConstrainedDoubleData)(currentBranch.GetLocalVariable(HeuristicLab.Functions.Constant.VALUE).Value)).Data + "";
172      }
173
174      public void Visit(Cosinus cosinus) {
175        name = "Sin";
176      }
177
178      public void Visit(Differential diff) {
179        string timeOffset = "";
180        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.OFFSET).Value).Data;
181        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.INDEX).Value).Data;
182        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Differential.WEIGHT).Value).Data;
183        if(sampleOffset < 0) {
184          timeOffset = "(t" + sampleOffset + ")";
185        } else if(sampleOffset > 0) {
186          timeOffset = "(t+" + sampleOffset + ")";
187        } else {
188          timeOffset = "";
189        }
190        name = "Diff" + variableIndex + timeOffset + " * " + weight;
191      }
192
193      public void Visit(Division division) {
194        name = "/";
195      }
196
197      public void Visit(Exponential exponential) {
198        name = "Exp";
199      }
200
201      public void Visit(Logarithm logarithm) {
202        name = "Log";
203      }
204
205      public void Visit(Multiplication multiplication) {
206        name = "*";
207      }
208
209      public void Visit(Power power) {
210        name = "Pow";
211      }
212
213      public void Visit(Signum signum) {
214        name = "Sign";
215      }
216
217      public void Visit(Sinus sinus) {
218        name = "Sin";
219      }
220
221      public void Visit(Sqrt sqrt) {
222        name = "Sqrt";
223      }
224
225      public void Visit(Subtraction substraction) {
226        name = "-";
227      }
228
229      public void Visit(Tangens tangens) {
230        name = "Tan";
231      }
232
233      public void Visit(Variable variable) {
234        string timeOffset = "";
235        int sampleOffset = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.OFFSET).Value).Data;
236        int variableIndex = ((ConstrainedIntData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.INDEX).Value).Data;
237        double weight = ((ConstrainedDoubleData)currentBranch.GetLocalVariable(HeuristicLab.Functions.Variable.WEIGHT).Value).Data;
238        if(sampleOffset < 0) {
239          timeOffset = "(t" + sampleOffset + ")";
240        } else if(sampleOffset > 0) {
241          timeOffset = "(t+" + sampleOffset + ")";
242        } else {
243          timeOffset = "";
244        }
245        name = "Var" + variableIndex + timeOffset + " * " + weight;
246      }
247
248      public void Visit(And and) {
249        name = "And";
250      }
251
252      public void Visit(Average average) {
253        name = "Avg";
254      }
255
256      public void Visit(IfThenElse ifThenElse) {
257        name = "IFTE";
258      }
259
260      public void Visit(Not not) {
261        name = "Not";
262      }
263
264      public void Visit(Or or) {
265        name = "Or";
266      }
267
268      public void Visit(Xor xor) {
269        name = "Xor";
270      }
271
272      public void Visit(Equal equal) {
273        name = "eq?";
274      }
275
276      public void Visit(LessThan lessThan) {
277        name = "<";
278      }
279
280      public void Visit(GreaterThan greaterThan) {
281        name = ">";
282      }
283      #endregion
284    }
285  }
286}
Note: See TracBrowser for help on using the repository browser.