Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartMode.cs @ 17328

Last change on this file since 17328 was 13769, checked in by jkarder, 9 years ago

#1265: worked on visualization

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