PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Main/src/DynamicDataDisplay.Controls/Palettes/DiscretePaletteEditor.xaml.cs

#
C# | 344 lines | 120 code | 62 blank | 162 comment | 2 complexity | b25995fc9d4835f06219288a55e3206a MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using Microsoft.Research.DynamicDataDisplay.Common.Palettes;
  15. using System.Windows.Markup;
  16. using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
  17. using System.Windows.Threading;
  18. using System.Diagnostics;
  19. using forms = System.Windows.Forms;
  20. using Microsoft.Research.DynamicDataDisplay.Charts;
  21. namespace Microsoft.Research.DynamicDataDisplay.Controls
  22. {
  23. /// <summary>
  24. /// Interaction logic for DiscretePaletteEditor.xaml
  25. /// </summary>
  26. [ContentProperty("Palette")]
  27. public partial class DiscretePaletteEditor : UserControl
  28. {
  29. public DiscretePaletteEditor()
  30. {
  31. InitializeComponent();
  32. paletteControl.SetBinding(PaletteControl.PaletteProperty, new Binding("Palette") { Source = this });
  33. Loaded += new RoutedEventHandler(DiscretePaletteEditor_Loaded);
  34. }
  35. private void DiscretePaletteEditor_Loaded(object sender, RoutedEventArgs e)
  36. {
  37. plotter.Children.Remove(plotter.MouseNavigation);
  38. plotter.Children.Remove(plotter.KeyboardNavigation);
  39. plotter.Children.Remove(plotter.DefaultContextMenu);
  40. }
  41. #region Properties
  42. #region Palette property
  43. public DiscretePalette Palette
  44. {
  45. get { return (DiscretePalette)GetValue(PaletteProperty); }
  46. set { SetValue(PaletteProperty, value); }
  47. }
  48. public static readonly DependencyProperty PaletteProperty = DependencyProperty.Register(
  49. "Palette",
  50. typeof(DiscretePalette),
  51. typeof(DiscretePaletteEditor),
  52. new FrameworkPropertyMetadata(null, OnPaletteReplaced));
  53. private static void OnPaletteReplaced(DependencyObject d, DependencyPropertyChangedEventArgs e)
  54. {
  55. DiscretePaletteEditor owner = (DiscretePaletteEditor)d;
  56. owner.OnPaletteReplaced();
  57. }
  58. private void OnPaletteReplaced()
  59. {
  60. RebuildSplitters();
  61. }
  62. #endregion
  63. #region Index attached DP
  64. public static int GetIndex(DependencyObject obj)
  65. {
  66. return (int)obj.GetValue(IndexProperty);
  67. }
  68. public static void SetIndex(DependencyObject obj, int value)
  69. {
  70. obj.SetValue(IndexProperty, value);
  71. }
  72. public static readonly DependencyProperty IndexProperty = DependencyProperty.RegisterAttached(
  73. "Index",
  74. typeof(int),
  75. typeof(DiscretePaletteEditor),
  76. new FrameworkPropertyMetadata(0));
  77. #endregion Index attached DP
  78. #endregion Properties
  79. private readonly List<PaletteThumb> thumbs = new List<PaletteThumb>();
  80. private void RebuildSplitters()
  81. {
  82. plotter.Children.RemoveAll<PaletteThumb>();
  83. hostPanel.Children.Clear();
  84. if (Palette == null) return;
  85. var palette = Palette;
  86. int count = palette.Steps.Count;
  87. int counter = 0;
  88. // adding thumbs
  89. foreach (var colorStep in palette.Steps.Take(count - 1))
  90. {
  91. PaletteThumb thumb = new PaletteThumb { Position = new Point(colorStep.Offset, 0.5) };
  92. SetIndex(thumb, counter);
  93. thumb.PositionChanged += new EventHandler<Charts.PositionChangedEventArgs>(thumb_PositionChanged);
  94. counter++;
  95. thumbs.Add(thumb);
  96. plotter.Children.Add(thumb);
  97. }
  98. // adding space between thumbs
  99. double prevOffset = 0;
  100. foreach (var colorStep in palette.Steps)
  101. {
  102. Rectangle rect = new Rectangle { Fill = Brushes.Transparent };
  103. Rect bounds = new Rect(prevOffset, 0, colorStep.Offset, 1);
  104. ViewportPanel.SetViewportBounds(rect, bounds);
  105. hostPanel.Children.Add(rect);
  106. prevOffset = colorStep.Offset;
  107. }
  108. }
  109. void thumb_PositionChanged(object sender, PositionChangedEventArgs e)
  110. {
  111. int index = GetIndex((DependencyObject)sender);
  112. UpdatePaletteView(index);
  113. }
  114. private void UpdatePaletteView(int index)
  115. {
  116. DiscretePalette palette = new DiscretePalette();
  117. Palette.Steps[index].Offset = thumbs[index].Position.X;
  118. foreach (var colorStep in Palette.Steps)
  119. {
  120. palette.Steps.Add(new LinearPaletteColorStep(colorStep.Color, colorStep.Offset));
  121. }
  122. paletteControl.Palette = palette;
  123. }
  124. // private ContextMenu BuildContextMenu() {
  125. // ContextMenu menu = new ContextMenu();
  126. // MenuItem changeColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_ChangeColor, Tag = rect };
  127. // changeColorMenuItem.Click += (s, e) =>
  128. // {
  129. // int index = GetIndexFromTag(s);
  130. // forms.ColorDialog dialog = new forms.ColorDialog();
  131. // if (dialog.ShowDialog() == forms.DialogResult.OK)
  132. // {
  133. // var c = dialog.Color;
  134. // Color wpfColor = Color.FromRgb(c.R, c.G, c.B);
  135. // Palette.Steps[index / 2].Color = wpfColor;
  136. // UpdatePaletteLook();
  137. // }
  138. // };
  139. // menu.Items.Add(changeColorMenuItem);
  140. // MenuItem deleteColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_DeleteColor, Tag = rect };
  141. // deleteColorMenuItem.Click += (s, e) =>
  142. // {
  143. // int index = GetIndexFromTag(s);
  144. // Palette.Steps.RemoveAt(index / 2);
  145. // RebuildSplitters();
  146. // UpdatePaletteLook();
  147. // };
  148. // menu.Items.Add(deleteColorMenuItem);
  149. // MenuItem addColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_AddColor, Tag = rect };
  150. // addColorMenuItem.Click += (s, e) =>
  151. // {
  152. // var x = Mouse.GetPosition(splitterGrid).X;
  153. // var ratio = x / (ActualWidth);
  154. // int insertIndex = Palette.Steps.Select((step, i) => new { Offset = step.Offset, Index = i }).First(step => step.Offset > ratio).Index;
  155. // forms.ColorDialog dialog = new forms.ColorDialog();
  156. // if (dialog.ShowDialog() == forms.DialogResult.OK)
  157. // {
  158. // var c = dialog.Color;
  159. // Color wpfColor = Color.FromRgb(c.R, c.G, c.B);
  160. // Palette.Steps.Insert(insertIndex, new LinearPaletteColorStep(wpfColor, ratio));
  161. // }
  162. // }
  163. //const int splitterWidth = 3;
  164. //private void RebuildSplitters()
  165. //{
  166. // splitterGrid.ColumnDefinitions.Clear();
  167. // splitterGrid.Children.Clear();
  168. // int rectColumnIndex = 0;
  169. // foreach (var step in Palette.Steps)
  170. // {
  171. // double width = step.Offset;
  172. // if (rectColumnIndex > 1)
  173. // width = Palette.Steps[rectColumnIndex / 2].Offset - Palette.Steps[rectColumnIndex / 2 - 1].Offset;
  174. // rectColumnIndex = AddColumn(rectColumnIndex, width);
  175. // ColumnDefinition splitterColumn = new ColumnDefinition { Width = new GridLength(splitterWidth) };
  176. // splitterGrid.ColumnDefinitions.Add(splitterColumn);
  177. // GridSplitter splitter = new GridSplitter
  178. // {
  179. // Width = splitterWidth,
  180. // VerticalAlignment = VerticalAlignment.Stretch,
  181. // HorizontalAlignment = HorizontalAlignment.Center,
  182. // Background = Brushes.DarkGray,
  183. // ToolTip = Properties.Resources.PaletteEditor_DragToChangeOffset
  184. // };
  185. // Grid.SetColumn(splitter, rectColumnIndex);
  186. // rectColumnIndex++;
  187. // splitterGrid.Children.Add(splitter);
  188. // }
  189. // AddColumn(rectColumnIndex, 1.0 - Palette.Steps[Palette.Steps.Count - 1].Offset);
  190. //}
  191. //private int AddColumn(int rectColumnIndex, double width)
  192. //{
  193. // Rectangle rect = new Rectangle
  194. // {
  195. // Stretch = Stretch.Fill,
  196. // Fill = Brushes.Transparent,
  197. // Tag = rectColumnIndex
  198. // };
  199. // ContextMenu menu = new ContextMenu();
  200. // MenuItem changeColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_ChangeColor, Tag = rect };
  201. // changeColorMenuItem.Click += (s, e) =>
  202. // {
  203. // int index = GetIndexFromTag(s);
  204. // forms.ColorDialog dialog = new forms.ColorDialog();
  205. // if (dialog.ShowDialog() == forms.DialogResult.OK)
  206. // {
  207. // var c = dialog.Color;
  208. // Color wpfColor = Color.FromRgb(c.R, c.G, c.B);
  209. // Palette.Steps[index / 2].Color = wpfColor;
  210. // UpdatePaletteLook();
  211. // }
  212. // };
  213. // menu.Items.Add(changeColorMenuItem);
  214. // MenuItem deleteColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_DeleteColor, Tag = rect };
  215. // deleteColorMenuItem.Click += (s, e) =>
  216. // {
  217. // int index = GetIndexFromTag(s);
  218. // Palette.Steps.RemoveAt(index / 2);
  219. // RebuildSplitters();
  220. // UpdatePaletteLook();
  221. // };
  222. // menu.Items.Add(deleteColorMenuItem);
  223. // MenuItem addColorMenuItem = new MenuItem { Header = Properties.Resources.PaletteEditor_AddColor, Tag = rect };
  224. // addColorMenuItem.Click += (s, e) =>
  225. // {
  226. // var x = Mouse.GetPosition(splitterGrid).X;
  227. // var ratio = x / (ActualWidth);
  228. // int insertIndex = Palette.Steps.Select((step, i) => new { Offset = step.Offset, Index = i }).First(step => step.Offset > ratio).Index;
  229. // forms.ColorDialog dialog = new forms.ColorDialog();
  230. // if (dialog.ShowDialog() == forms.DialogResult.OK)
  231. // {
  232. // var c = dialog.Color;
  233. // Color wpfColor = Color.FromRgb(c.R, c.G, c.B);
  234. // Palette.Steps.Insert(insertIndex, new LinearPaletteColorStep(wpfColor, ratio));
  235. // }
  236. // RebuildSplitters();
  237. // UpdatePaletteLook();
  238. // };
  239. // menu.Items.Add(addColorMenuItem);
  240. // ColumnDefinition stepColumn = new ColumnDefinition { Width = new GridLength(width, GridUnitType.Star) };
  241. // splitterGrid.ColumnDefinitions.Add(stepColumn);
  242. // rect.ContextMenu = menu;
  243. // Dispatcher.BeginInvoke(() => rect.SizeChanged += new SizeChangedEventHandler(rect_SizeChanged), DispatcherPriority.Background);
  244. // Grid.SetColumn(rect, rectColumnIndex);
  245. // splitterGrid.Children.Add(rect);
  246. // rectColumnIndex++;
  247. // return rectColumnIndex;
  248. //}
  249. //private static int GetIndexFromTag(object s)
  250. //{
  251. // FrameworkElement owner = (FrameworkElement)((FrameworkElement)s).Tag;
  252. // int index = (int)owner.Tag;
  253. // return index;
  254. //}
  255. //private void rect_SizeChanged(object sender, SizeChangedEventArgs e)
  256. //{
  257. // Rectangle rect = (Rectangle)sender;
  258. // int index = (int)rect.Tag;
  259. // index /= 2;
  260. // if (index < Palette.Steps.Count)
  261. // {
  262. // double width = rect.ActualWidth / splitterGrid.ActualWidth;
  263. // if (index > 0)
  264. // width += Palette.Steps[index - 1].Offset;
  265. // Palette.Steps[index].Offset = width;
  266. // }
  267. // UpdatePaletteLook();
  268. //}
  269. //private void UpdatePaletteLook()
  270. //{
  271. // DiscretePalette palette = new DiscretePalette();
  272. // palette.EndColor = Palette.EndColor;
  273. // double offset = 0;
  274. // for (int i = 0; i < Palette.Steps.Count; i++)
  275. // {
  276. // offset += splitterGrid.ColumnDefinitions[2 * i].Width.Value / (ActualWidth - Palette.Steps.Count * splitterWidth);
  277. // palette.Steps.Add(new LinearPaletteColorStep(Palette.Steps[i].Color, offset));
  278. // }
  279. // paletteControl.Palette = palette;
  280. //}
  281. }
  282. }