Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1265: worked on visualization

  • added shortcut handling prototype
File size: 5.1 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          shortcut.DownAction();
89          break;
90        }
91      }
92    }
93
94    public virtual void HandleOnKeyUp(object sender, KeyEventArgs e) {
95      if (activeShortcut == null) return;
96
97      var modifiers = new List<Keys>(from key in Enum.GetValues(typeof(Keys)).Cast<Keys>()
98                                     where e.Modifiers.HasFlag(key)
99                                     select key);
100
101      if (activeShortcut.Key == e.KeyCode || activeShortcut.GetModifiers().Except(modifiers).Any()) {
102        activeShortcut.UpAction();
103        activeShortcut = null;
104      }
105    }
106
107    public virtual void HandleOnMouseClick(object sender, MouseEventArgs e) { }
108    public virtual void HandleOnMouseDoubleClick(object sender, MouseEventArgs e) { }
109
110    public virtual void HandleOnMouseDown(object sender, MouseEventArgs e) {
111      buttonDownPoint = e.Location;
112      mouseClickCount = e.Clicks;
113    }
114
115    public virtual void HandleOnMouseUp(object sender, MouseEventArgs e) { }
116
117    public virtual void HandleOnMouseMove(object sender, MouseEventArgs e) {
118      previousLocation = e.Location;
119    }
120
121    public virtual void HandleOnMouseWheel(object sender, MouseEventArgs e) { }
122    public virtual void HandleOnMouseEnter(object sender, EventArgs e) { }
123    public virtual void HandleOnMouseLeave(object sender, EventArgs e) { }
124    public virtual void HandleOnClick(object sender, EventArgs e) { }
125
126    public event EventHandler SelectedChanged;
127    protected virtual void OnSelectedChanged() {
128      var handler = SelectedChanged;
129      if (handler != null) handler(this, EventArgs.Empty);
130    }
131
132    public class Shortcut {
133      public Keys Key { get; private set; }
134      public Keys Modifiers { get; private set; }
135      public Action DownAction { get; set; }
136      public Action UpAction { get; set; }
137
138      public Shortcut(Keys key, Keys modifiers = Keys.None) {
139        Key = key;
140        Modifiers = modifiers;
141      }
142
143      public IEnumerable<Keys> GetModifiers() {
144        var keys = new List<Keys>(from key in Enum.GetValues(typeof(Keys)).Cast<Keys>()
145                                  where Modifiers.HasFlag(key)
146                                  select key);
147        return keys;
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.