[12503] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Input;
|
---|
| 6 | using System.Windows.Controls.Primitives;
|
---|
| 7 | using System.Windows;
|
---|
| 8 | using System.Windows.Controls;
|
---|
| 9 | using System.Windows.Shapes;
|
---|
| 10 | using System.Windows.Media;
|
---|
| 11 | using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
---|
| 12 |
|
---|
| 13 | namespace Microsoft.Research.DynamicDataDisplay.Charts
|
---|
| 14 | {
|
---|
| 15 | public abstract class PopupTipElement : IPlotterElement
|
---|
| 16 | {
|
---|
| 17 | /// <summary>Shows tooltips.</summary>
|
---|
| 18 | private PopupTip popup;
|
---|
| 19 |
|
---|
| 20 | protected PopupTip GetPopupTipWindow()
|
---|
| 21 | {
|
---|
| 22 | if (popup != null)
|
---|
| 23 | return popup;
|
---|
| 24 |
|
---|
| 25 | foreach (var item in Plotter.Children)
|
---|
| 26 | {
|
---|
| 27 | if (item is ViewportUIContainer)
|
---|
| 28 | {
|
---|
| 29 | ViewportUIContainer container = (ViewportUIContainer)item;
|
---|
| 30 | if (container.Content is PopupTip)
|
---|
| 31 | return popup = (PopupTip)container.Content;
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | popup = new PopupTip();
|
---|
| 36 | popup.Placement = PlacementMode.Relative;
|
---|
| 37 | popup.PlacementTarget = plotter.CentralGrid;
|
---|
| 38 | Plotter.Children.Add(popup);
|
---|
| 39 | return popup;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | private void OnMouseLeave(object sender, MouseEventArgs e)
|
---|
| 43 | {
|
---|
| 44 | GetPopupTipWindow().Hide();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private void OnMouseMove(object sender, MouseEventArgs e)
|
---|
| 48 | {
|
---|
| 49 | var popup = GetPopupTipWindow();
|
---|
| 50 | if (popup.IsOpen)
|
---|
| 51 | popup.Hide();
|
---|
| 52 |
|
---|
| 53 | Point screenPoint = e.GetPosition(plotter.CentralGrid);
|
---|
| 54 | Point viewportPoint = screenPoint.ScreenToData(plotter.Transform);
|
---|
| 55 |
|
---|
| 56 | var tooltip = GetTooltipForPoint(viewportPoint);
|
---|
| 57 | if (tooltip == null) return;
|
---|
| 58 |
|
---|
| 59 | popup.VerticalOffset = screenPoint.Y + 20;
|
---|
| 60 | popup.HorizontalOffset = screenPoint.X;
|
---|
| 61 |
|
---|
| 62 | popup.ShowDelayed(TimeSpan.FromSeconds(0));
|
---|
| 63 |
|
---|
| 64 | Grid grid = new Grid();
|
---|
| 65 |
|
---|
| 66 | Rectangle rect = new Rectangle
|
---|
| 67 | {
|
---|
| 68 | Stroke = Brushes.Black,
|
---|
| 69 | Fill = SystemColors.InfoBrush
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | StackPanel panel = new StackPanel();
|
---|
| 73 | panel.Orientation = Orientation.Vertical;
|
---|
| 74 | panel.Children.Add(tooltip);
|
---|
| 75 | panel.Margin = new Thickness(4, 2, 4, 2);
|
---|
| 76 |
|
---|
| 77 | var textBlock = new TextBlock();
|
---|
| 78 | textBlock.Text = String.Format("Location: {0:F2}, {1:F2}", viewportPoint.X, viewportPoint.Y);
|
---|
| 79 | textBlock.Foreground = SystemColors.GrayTextBrush;
|
---|
| 80 | panel.Children.Add(textBlock);
|
---|
| 81 |
|
---|
| 82 | grid.Children.Add(rect);
|
---|
| 83 | grid.Children.Add(panel);
|
---|
| 84 | grid.Measure(SizeHelper.CreateInfiniteSize());
|
---|
| 85 | popup.Child = grid;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | protected virtual UIElement GetTooltipForPoint(Point viewportPosition)
|
---|
| 89 | {
|
---|
| 90 | return null;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | #region IPlotterElement Members
|
---|
| 94 |
|
---|
| 95 | private Plotter2D plotter;
|
---|
| 96 | public void OnPlotterAttached(Plotter plotter)
|
---|
| 97 | {
|
---|
| 98 | this.plotter = (Plotter2D)plotter;
|
---|
| 99 | plotter.CentralGrid.MouseMove += OnMouseMove;
|
---|
| 100 | plotter.CentralGrid.MouseLeave += OnMouseLeave;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public void OnPlotterDetaching(Plotter plotter)
|
---|
| 104 | {
|
---|
| 105 | plotter.CentralGrid.MouseMove -= OnMouseMove;
|
---|
| 106 | plotter.CentralGrid.MouseLeave -= OnMouseLeave;
|
---|
| 107 | this.plotter = null;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public Plotter Plotter
|
---|
| 111 | {
|
---|
| 112 | get { return plotter; }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | #endregion
|
---|
| 116 | }
|
---|
| 117 | }
|
---|