PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Navigation/Navigation/KeyboardNavigation.cs

#
C# | 300 lines | 229 code | 66 blank | 5 comment | 1 complexity | 02b3942a28154930f6e520fde56e0b21 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1using System.Windows;
  2using System.Windows.Input;
  3using System.Windows.Media;
  4using Microsoft.Win32;
  5
  6namespace Microsoft.Research.DynamicDataDisplay.Navigation
  7{
  8	/// <summary>Provides keyboard navigation around viewport</summary>
  9	public class KeyboardNavigation : NavigationBase
 10	{
 11
 12		public KeyboardNavigation()
 13		{
 14			Focusable = true;
 15			Loaded += KeyboardNavigation_Loaded;
 16		}
 17
 18		private void KeyboardNavigation_Loaded(object sender, RoutedEventArgs e)
 19		{
 20			Keyboard.Focus(this);
 21			InitCommands();
 22		}
 23
 24		public void InitCommands()
 25		{
 26			var zoomWithParamCommandBinding = new CommandBinding(
 27				ChartCommands.ZoomWithParameter,
 28				ZoomWithParamExecute,
 29				ZoomWithParamCanExecute);
 30			Plotter2D.CommandBindings.Add(zoomWithParamCommandBinding);
 31
 32			var zoomInCommandBinding = new CommandBinding(
 33				ChartCommands.ZoomIn,
 34				ZoomInExecute,
 35				ZoomInCanExecute);
 36			Plotter2D.CommandBindings.Add(zoomInCommandBinding);
 37
 38			var zoomOutCommandBinding = new CommandBinding(
 39				ChartCommands.ZoomOut,
 40				ZoomOutExecute,
 41				ZoomOutCanExecute);
 42			Plotter2D.CommandBindings.Add(zoomOutCommandBinding);
 43
 44			var fitToViewCommandBinding = new CommandBinding(
 45				ChartCommands.FitToView,
 46				FitToViewExecute,
 47				FitToViewCanExecute);
 48			Plotter2D.CommandBindings.Add(fitToViewCommandBinding);
 49
 50			var ScrollLeftCommandBinding = new CommandBinding(
 51					ChartCommands.ScrollLeft,
 52					ScrollLeftExecute,
 53					ScrollLeftCanExecute);
 54			Plotter2D.CommandBindings.Add(ScrollLeftCommandBinding);
 55
 56			var ScrollRightCommandBinding = new CommandBinding(
 57				ChartCommands.ScrollRight,
 58				ScrollRightExecute,
 59				ScrollRightCanExecute);
 60			Plotter2D.CommandBindings.Add(ScrollRightCommandBinding);
 61
 62			var ScrollUpCommandBinding = new CommandBinding(
 63				ChartCommands.ScrollUp,
 64				ScrollUpExecute,
 65				ScrollUpCanExecute);
 66			Plotter2D.CommandBindings.Add(ScrollUpCommandBinding);
 67
 68			var ScrollDownCommandBinding = new CommandBinding(
 69					ChartCommands.ScrollDown,
 70					ScrollDownExecute,
 71					ScrollDownCanExecute);
 72			Plotter2D.CommandBindings.Add(ScrollDownCommandBinding);
 73
 74			var SaveScreenshotCommandBinding = new CommandBinding(
 75				ChartCommands.SaveScreenshot,
 76				SaveScreenshotExecute,
 77				SaveScreenshotCanExecute);
 78			Plotter2D.CommandBindings.Add(SaveScreenshotCommandBinding);
 79
 80			var CopyScreenshotCommandBinding = new CommandBinding(
 81				ChartCommands.CopyScreenshot,
 82				CopyScreenshotExecute,
 83				CopyScreenshotCanExecute);
 84			Plotter2D.CommandBindings.Add(CopyScreenshotCommandBinding);
 85
 86			var ShowHelpCommandBinding = new CommandBinding(
 87				ChartCommands.ShowHelp,
 88				ShowHelpExecute,
 89				ShowHelpCanExecute);
 90			Plotter2D.CommandBindings.Add(ShowHelpCommandBinding);
 91		}
 92
 93		#region Zoom With param
 94
 95		private void ZoomWithParamExecute(object target, ExecutedRoutedEventArgs e)
 96		{
 97			// TODO: add working code
 98			//double zoomParam = (double)e.Parameter;
 99			//Plotter2d.ZoomVisible(zoomParam);
100			e.Handled = true;
101		}
102
103		private void ZoomWithParamCanExecute(object target, CanExecuteRoutedEventArgs e)
104		{
105			e.CanExecute = true;
106		}
107
108		#endregion
109
110		#region Zoom in
111
112		private void ZoomInExecute(object target, ExecutedRoutedEventArgs e)
113		{
114			Viewport.Zoom(0.9);
115			e.Handled = true;
116		}
117
118		private void ZoomInCanExecute(object target, CanExecuteRoutedEventArgs e)
119		{
120			e.CanExecute = true;
121		}
122
123		#endregion
124
125		#region Zoom out
126
127		private void ZoomOutExecute(object target, ExecutedRoutedEventArgs e)
128		{
129			Viewport.Zoom(1.1);
130			e.Handled = true;
131		}
132
133		private void ZoomOutCanExecute(object target, CanExecuteRoutedEventArgs e)
134		{
135			e.CanExecute = true;
136		}
137
138		#endregion
139
140		#region Fit to view
141
142		private void FitToViewExecute(object target, ExecutedRoutedEventArgs e)
143		{
144			Viewport.FitToView();
145			e.Handled = true;
146		}
147
148		private void FitToViewCanExecute(object target, CanExecuteRoutedEventArgs e)
149		{
150			e.CanExecute = true;
151		}
152
153		#endregion
154
155		#region Scroll
156
157		private double scrollCoeff = 0.05;
158		private void ScrollVisibleProportionally(double xShiftCoeff, double yShiftCoeff)
159		{
160			Rect visible = Viewport.Visible;
161			double width = visible.Width;
162			double height = visible.Height;
163
164			visible.Offset(xShiftCoeff * width, yShiftCoeff * height);
165
166			Viewport.Visible = visible;
167		}
168
169		#region ScrollLeft
170
171		private void ScrollLeftExecute(object target, ExecutedRoutedEventArgs e)
172		{
173			ScrollVisibleProportionally(scrollCoeff, 0);
174			e.Handled = true;
175		}
176
177		private void ScrollLeftCanExecute(object target, CanExecuteRoutedEventArgs e)
178		{
179			e.CanExecute = true;
180		}
181
182		#endregion
183
184		#region ScrollRight
185
186		private void ScrollRightExecute(object target, ExecutedRoutedEventArgs e)
187		{
188			ScrollVisibleProportionally(-scrollCoeff, 0);
189			e.Handled = true;
190		}
191
192		private void ScrollRightCanExecute(object target, CanExecuteRoutedEventArgs e)
193		{
194			e.CanExecute = true;
195		}
196
197		#endregion
198
199		#region ScrollUp
200
201		private void ScrollUpExecute(object target, ExecutedRoutedEventArgs e)
202		{
203			ScrollVisibleProportionally(0, -scrollCoeff);
204			e.Handled = true;
205		}
206
207		private void ScrollUpCanExecute(object target, CanExecuteRoutedEventArgs e)
208		{
209			e.CanExecute = true;
210		}
211
212		#endregion
213
214		#region ScrollDown
215
216		private void ScrollDownExecute(object target, ExecutedRoutedEventArgs e)
217		{
218			ScrollVisibleProportionally(0, scrollCoeff);
219			e.Handled = true;
220		}
221
222		private void ScrollDownCanExecute(object target, CanExecuteRoutedEventArgs e)
223		{
224			e.CanExecute = true;
225		}
226
227		#endregion
228
229		#endregion
230
231		#region SaveScreenshot
232
233		private void SaveScreenshotExecute(object target, ExecutedRoutedEventArgs e)
234		{
235			SaveFileDialog dlg = new SaveFileDialog();
236			dlg.Filter = "JPEG(*.jpg)|*.jpg|BMP(*.bmp)|*.bmp|GIF(*.gif)|.gif|All files (*.*)|*.*";
237			dlg.FilterIndex = 1;
238			if (dlg.ShowDialog().GetValueOrDefault(false))
239			{
240				string filePath = dlg.FileName;
241				Plotter2D.SaveScreenshot(filePath);
242				e.Handled = true;
243			}
244		}
245
246		private void SaveScreenshotCanExecute(object target, CanExecuteRoutedEventArgs e)
247		{
248			e.CanExecute = true;
249		}
250
251		#endregion
252
253		#region CopyScreenshot
254
255		private void CopyScreenshotExecute(object target, ExecutedRoutedEventArgs e)
256		{
257			Plotter2D.CopyScreenshotToClipboard();
258			e.Handled = true;
259		}
260
261		private void CopyScreenshotCanExecute(object target, CanExecuteRoutedEventArgs e)
262		{
263			e.CanExecute = true;
264		}
265
266		#endregion
267
268		#region ShowHelp
269
270		private void ShowHelpExecute(object target, ExecutedRoutedEventArgs e)
271		{
272			string helpText =
273@"Dynamic Data Display is in ""Auto Fit"" mode by default.
274By using the mouse you can manually select an area to display:
275
276Mouse wheel, '+' and '-' — Zoom in/Zoom out
277Mouse drag or arrow keys — Pan the display area
278Ctrl + Mouse drag — Select an area to zoom in
279Shift + Mouse drag — Select an area with fixed aspect ratio
280Home — Fit to view
281F11 — Copy chart to clipboard";
282
283			MessageBox.Show(helpText, "Quick Help", MessageBoxButton.OK, MessageBoxImage.Information);
284
285			e.Handled = true;
286		}
287
288		private void ShowHelpCanExecute(object target, CanExecuteRoutedEventArgs e)
289		{
290			e.CanExecute = true;
291		}
292
293		#endregion
294
295		protected override void OnRenderCore(DrawingContext dc, RenderState state)
296		{
297			// do nothing here
298		}
299	}
300}