Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartMode.cs @ 13763

Last change on this file since 13763 was 13763, checked in by jkarder, 8 years ago

#1265: worked on visualization

  • added null check for shortcut actions
File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27
28namespace HeuristicLab.Visualization {
29  public abstract class ChartMode {
30    protected readonly ChartControl chartControl;
31    protected readonly IList<Shortcut> globalShortcuts = new List<Shortcut>();
32    protected readonly IList<Shortcut> localShortcuts;
33    protected Shortcut activeShortcut = null;
34    protected Cursor cursor;
35    protected Point buttonDownPoint;
36    protected Point previousLocation;
37    protected int mouseClickCount;
38
39    public abstract Image Image { get; }
40    public abstract string Text { get; }
41
42    public virtual string ToolTipText { get { return Text; } }
43
44    public virtual Cursor Cursor {
45      get { return cursor ?? (cursor = Cursors.Default); }
46    }
47
48    private bool selected;
49    public bool Selected {
50      get { return selected; }
51      set {
52        if (selected == value) return;
53        selected = value;
54        if (selected) {
55          chartControl.Mode = this;
56          OnSelected();
57        } else {
58          if (chartControl.Mode == this)
59            chartControl.Mode = null;
60          OnDeselected();
61        }
62        OnSelectedChanged();
63      }
64    }
65
66    public IEnumerable<Shortcut> Shortcuts { get { return globalShortcuts; } }
67
68    protected ChartMode(ChartControl control) {
69      chartControl = control;
70      chartControl.ModeChanged += (sender, args) => Selected = chartControl.Mode == this;
71      localShortcuts = new List<Shortcut> {
72        new Shortcut(Keys.Escape) { DownAction = Deselect }
73      };
74    }
75
76    public void Select() { Selected = true; }
77    public void Deselect() { Selected = false; }
78
79    protected virtual void OnSelected() { }
80    protected virtual void OnDeselected() { }
81
82    public virtual void HandleOnKeyDown(object sender, KeyEventArgs e) {
83      if (activeShortcut != null) return;
84
85      foreach (var shortcut in localShortcuts) {
86        if (e.KeyCode == shortcut.Key && e.Modifiers == shortcut.Modifiers) {
87          activeShortcut = shortcut;
88          var downAction = shortcut.DownAction;
89          if (downAction != null) downAction();
90          break;
91        }
92      }
93    }
94
95    public virtual void HandleOnKeyUp(object sender, KeyEventArgs e) {
96      if (activeShortcut == null) return;
97
98      var modifiers = new List<Keys>(from key in Enum.GetValues(typeof(Keys)).Cast<Keys>()
99                                     where e.Modifiers.HasFlag(key)
100                                     select key);
101
102      if (activeShortcut.Key == e.KeyCode || activeShortcut.GetModifiers().Except(modifiers).Any()) {
103        var upAction = activeShortcut.UpAction;
104        if (upAction != null) upAction();
105        activeShortcut = null;
106      }
107    }
108
109    public virtual void HandleOnMouseClick(object sender, MouseEventArgs e) { }
110    public virtual void HandleOnMouseDoubleClick(object sender, MouseEventArgs e) { }
111
112    public virtual void HandleOnMouseDown(object sender, MouseEventArgs e) {
113      buttonDownPoint = e.Location;
114      mouseClickCount = e.Clicks;
115    }
116
117    public virtual void HandleOnMouseUp(object sender, MouseEventArgs e) { }
118
119    public virtual void HandleOnMouseMove(object sender, MouseEventArgs e) {
120      previousLocation = e.Location;
121    }
122
123    public virtual void HandleOnMouseWheel(object sender, MouseEventArgs e) { }
124    public virtual void HandleOnMouseEnter(object sender, EventArgs e) { }
125    public virtual void HandleOnMouseLeave(object sender, EventArgs e) { }
126    public virtual void HandleOnClick(object sender, EventArgs e) { }
127
128    public event EventHandler SelectedChanged;
129    protected virtual void OnSelectedChanged() {
130      var handler = SelectedChanged;
131      if (handler != null) handler(this, EventArgs.Empty);
132    }
133
134    public class Shortcut {
135      public Keys Key { get; private set; }
136      public Keys Modifiers { get; private set; }
137      public Action DownAction { get; set; }
138      public Action UpAction { get; set; }
139
140      public Shortcut(Keys key, Keys modifiers = Keys.None) {
141        Key = key;
142        Modifiers = modifiers;
143      }
144
145      public IEnumerable<Keys> GetModifiers() {
146        var keys = new List<Keys>(from key in Enum.GetValues(typeof(Keys)).Cast<Keys>()
147                                  where Modifiers.HasFlag(key)
148                                  select key);
149        return keys;
150      }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.