QAGrid.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using InABox.Core;
  8. using Button = System.Windows.Controls.Button;
  9. using Color = System.Windows.Media.Color;
  10. using ComboBox = System.Windows.Controls.ComboBox;
  11. using HorizontalAlignment = System.Windows.HorizontalAlignment;
  12. using Label = System.Windows.Controls.Label;
  13. using SystemColors = System.Windows.SystemColors;
  14. using TextBox = System.Windows.Controls.TextBox;
  15. using UserControl = System.Windows.Controls.UserControl;
  16. namespace InABox.WPF
  17. {
  18. public class QAGrid : UserControl
  19. {
  20. public delegate void QAFormChangedEvent(object sender, Dictionary<Guid, object> values);
  21. private readonly Dictionary<QAQuestion, Border> borders = new();
  22. private readonly Grid MasterGrid;
  23. public QAGrid()
  24. {
  25. BorderBrush = new SolidColorBrush(Colors.Gray);
  26. BorderThickness = new Thickness(0.75);
  27. MasterGrid = new Grid();
  28. MasterGrid.Background = new SolidColorBrush(Colors.WhiteSmoke);
  29. MasterGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.25F, GridUnitType.Star) });
  30. MasterGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.25F, GridUnitType.Star) });
  31. MasterGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Auto) });
  32. MasterGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  33. MasterGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Auto) });
  34. Content = MasterGrid;
  35. }
  36. public bool Preview { get; set; }
  37. public QAFormChangedEvent OnChanged { get; set; }
  38. private object GetValue(Dictionary<Guid, object> values, Guid guid)
  39. {
  40. if (values.ContainsKey(guid))
  41. return values[guid];
  42. return "";
  43. }
  44. private Border CreateHeader(string title)
  45. {
  46. var label = new Label
  47. {
  48. Content = title,
  49. FontSize = 16.0F,
  50. HorizontalAlignment = HorizontalAlignment.Center,
  51. VerticalAlignment = VerticalAlignment.Center
  52. };
  53. var border = new Border
  54. {
  55. CornerRadius = new CornerRadius(10.0F, 10.0F, 0.0F, 0.0F),
  56. BorderThickness = new Thickness(1.0F),
  57. BorderBrush = SystemColors.WindowFrameBrush,
  58. Background = SystemColors.WindowBrush,
  59. Margin = new Thickness(5.0F, 5.0F, 5.0F, 2.0F),
  60. Padding = new Thickness(5.0F)
  61. };
  62. border.SetValue(Grid.ColumnProperty, MasterGrid.ColumnDefinitions.Count - 1);
  63. border.SetValue(Grid.RowProperty, 0);
  64. border.Child = label;
  65. MasterGrid.Children.Add(border);
  66. return border;
  67. }
  68. private Thickness GetMargin(Grid grid)
  69. {
  70. if (grid.RowDefinitions.Count == 0)
  71. return new Thickness(2.0F, 0.0F, 2.0F, 0.0F);
  72. return new Thickness(4.0F, 3.0F, 4.0F, 3.0F);
  73. }
  74. public static Color InterpolateColors(Color color1, Color color2, float percentage)
  75. {
  76. double a1 = color1.A / 255.0, r1 = color1.R / 255.0, g1 = color1.G / 255.0, b1 = color1.B / 255.0;
  77. double a2 = color2.A / 255.0, r2 = color2.R / 255.0, g2 = color2.G / 255.0, b2 = color2.B / 255.0;
  78. var a3 = Convert.ToByte((a1 + (a2 - a1) * percentage) * 255);
  79. var r3 = Convert.ToByte((r1 + (r2 - r1) * percentage) * 255);
  80. var g3 = Convert.ToByte((g1 + (g2 - g1) * percentage) * 255);
  81. var b3 = Convert.ToByte((b1 + (b2 - b1) * percentage) * 255);
  82. return Color.FromArgb(a3, r3, g3, b3);
  83. }
  84. private Color GetBorder(QAQuestion question, Dictionary<Guid, object> answers)
  85. {
  86. var parameters = question.ParseParameters();
  87. var bAnswerRequired = question.Answer != QAAnswer.Comment &&
  88. (!parameters.ContainsKey("Default") || string.IsNullOrWhiteSpace(parameters["Default"]));
  89. if (bAnswerRequired && (!answers.ContainsKey(question.ID) || answers[question.ID] == null))
  90. return Colors.Red;
  91. return Colors.Transparent;
  92. }
  93. public void CollapseMargins()
  94. {
  95. MasterGrid.ColumnDefinitions.First().Width = new GridLength(0.0F);
  96. MasterGrid.ColumnDefinitions.Last().Width = new GridLength(0.0F);
  97. }
  98. private void Changed(Dictionary<Guid, object> values)
  99. {
  100. OnChanged?.Invoke(this, values);
  101. foreach (var question in borders.Keys)
  102. {
  103. var color = GetBorder(question, values);
  104. borders[question].BorderBrush = new SolidColorBrush(color);
  105. borders[question].Background = new SolidColorBrush(InterpolateColors(color, Colors.Transparent, 0.8F));
  106. }
  107. }
  108. private Border CreateBorder(Grid grid, QAQuestion check, Dictionary<Guid, object> values)
  109. {
  110. var border = new Border();
  111. border.SetValue(Grid.ColumnProperty, 0);
  112. border.SetValue(Grid.RowProperty, grid.RowDefinitions.Count);
  113. border.Padding = new Thickness(2.0F);
  114. var color = GetBorder(check, values);
  115. border.BorderBrush = new SolidColorBrush(color);
  116. border.Background = new SolidColorBrush(InterpolateColors(color, Colors.Transparent, 0.8F));
  117. border.BorderThickness = new Thickness(1.5F);
  118. border.Margin = new Thickness(0F, 2F, 0F, 2F);
  119. border.CornerRadius = new CornerRadius(5.0F);
  120. grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Auto) });
  121. grid.Children.Add(border);
  122. return border;
  123. }
  124. private Border CreateCommentPanel(Grid grid, QAQuestion check, Dictionary<Guid, object> values, int number)
  125. {
  126. var border = CreateBorder(grid, check, values);
  127. var row = new Grid();
  128. row.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  129. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  130. var label = new TextBlock
  131. {
  132. Text = check.Question,
  133. FontSize = 14.0F,
  134. TextAlignment = TextAlignment.Center,
  135. TextWrapping = TextWrapping.Wrap,
  136. VerticalAlignment = VerticalAlignment.Center,
  137. Margin = GetMargin(grid),
  138. Padding = new Thickness(10)
  139. };
  140. label.SetValue(Grid.RowProperty, 0);
  141. label.SetValue(Grid.ColumnProperty, 0);
  142. row.Children.Add(label);
  143. border.Child = row;
  144. return border;
  145. }
  146. private Border CreateChoicePanel(Grid grid, QAQuestion check, Dictionary<Guid, object> values, int number)
  147. {
  148. var border = CreateBorder(grid, check, values);
  149. var row = new Grid();
  150. row.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Auto) });
  151. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  152. var label = new TextBlock
  153. {
  154. Text = string.Format("{0}. {1}", number, check.Question),
  155. FontSize = 14.0F,
  156. Margin = GetMargin(grid),
  157. VerticalAlignment = VerticalAlignment.Center,
  158. TextAlignment = TextAlignment.Left,
  159. TextWrapping = TextWrapping.Wrap
  160. };
  161. label.SetValue(Grid.RowProperty, 0);
  162. label.SetValue(Grid.ColumnProperty, 0);
  163. row.Children.Add(label);
  164. if (!string.IsNullOrEmpty(check.Parameters))
  165. {
  166. var parameters = check.ParseParameters();
  167. var buttons = parameters["Options"].Split(',');
  168. var colors = parameters["Colors"].Split(',');
  169. var value = GetValue(values, check.ID).ToString();
  170. if (string.IsNullOrWhiteSpace(value))
  171. value = parameters["Default"];
  172. for (var i = 0; i < buttons.Length; i++)
  173. if (!string.IsNullOrWhiteSpace(buttons[i]))
  174. {
  175. var brush = new SolidColorBrush(Colors.WhiteSmoke);
  176. try
  177. {
  178. brush = new BrushConverter().ConvertFromString(colors[i]) as SolidColorBrush;
  179. }
  180. catch (Exception e)
  181. {
  182. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  183. }
  184. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(80.0F) });
  185. var button = new Button
  186. {
  187. Content = buttons[i],
  188. FontSize = 14.0F,
  189. Margin = GetMargin(grid),
  190. Height = 40,
  191. Background = buttons[i].Equals(value) ? brush : new SolidColorBrush(Colors.WhiteSmoke),
  192. Tag = brush
  193. };
  194. button.Click += (o, e) =>
  195. {
  196. values[check.ID] = button.Content.ToString();
  197. foreach (var other in row.Children)
  198. if (other is Button)
  199. {
  200. if (other == button)
  201. ((Button)other).Background = (SolidColorBrush)button.Tag;
  202. else
  203. ((Button)other).Background = new SolidColorBrush(Colors.WhiteSmoke);
  204. }
  205. Changed(values);
  206. };
  207. button.SetValue(Grid.RowProperty, 0);
  208. button.SetValue(Grid.ColumnProperty, i + 1);
  209. row.Children.Add(button);
  210. }
  211. }
  212. border.Child = row;
  213. return border;
  214. }
  215. private Border CreateNumberPanel(Grid grid, QAQuestion check, Dictionary<Guid, object> values, int number)
  216. {
  217. var border = CreateBorder(grid, check, values);
  218. var row = new Grid();
  219. row.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  220. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  221. var label = new TextBlock
  222. {
  223. Text = string.Format("{0}. {1}", number, check.Question),
  224. FontSize = 14.0F,
  225. Margin = GetMargin(grid),
  226. VerticalAlignment = VerticalAlignment.Center,
  227. TextAlignment = TextAlignment.Left,
  228. TextWrapping = TextWrapping.Wrap
  229. };
  230. label.SetValue(Grid.RowProperty, 0);
  231. label.SetValue(Grid.ColumnProperty, 0);
  232. row.Children.Add(label);
  233. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(160.0F) });
  234. var editor = new TextBox
  235. {
  236. Margin = GetMargin(grid),
  237. TextAlignment = TextAlignment.Center,
  238. VerticalContentAlignment = VerticalAlignment.Center,
  239. Height = 40,
  240. FontSize = 14.0F
  241. };
  242. editor.SetValue(Grid.RowProperty, 0);
  243. editor.SetValue(Grid.ColumnProperty, 1);
  244. row.Children.Add(editor);
  245. var def = "";
  246. if (!string.IsNullOrEmpty(check.Parameters))
  247. {
  248. var parameters = check.ParseParameters();
  249. def = parameters["Default"];
  250. }
  251. var value = GetValue(values, check.ID).ToString();
  252. editor.Text = string.IsNullOrWhiteSpace(value) ? def : value;
  253. editor.TextChanged += (o, e) =>
  254. {
  255. values[check.ID] = editor.Text;
  256. Changed(values);
  257. };
  258. border.Child = row;
  259. return border;
  260. }
  261. private Border CreateTextPanel(Grid grid, QAQuestion check, Dictionary<Guid, object> values, int number)
  262. {
  263. var border = CreateBorder(grid, check, values);
  264. var row = new Grid();
  265. row.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  266. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  267. var label = new TextBlock
  268. {
  269. Text = string.Format("{0}. {1}", number, check.Question),
  270. FontSize = 14.0F,
  271. Margin = GetMargin(grid),
  272. VerticalAlignment = VerticalAlignment.Center,
  273. TextAlignment = TextAlignment.Left,
  274. TextWrapping = TextWrapping.Wrap
  275. };
  276. label.SetValue(Grid.RowProperty, 0);
  277. label.SetValue(Grid.ColumnProperty, 0);
  278. row.Children.Add(label);
  279. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(320.0F, GridUnitType.Pixel) });
  280. var editor = new TextBox
  281. {
  282. Margin = GetMargin(grid),
  283. TextAlignment = TextAlignment.Center,
  284. VerticalContentAlignment = VerticalAlignment.Center,
  285. Height = 40,
  286. FontSize = 14.0F
  287. };
  288. editor.SetValue(Grid.RowProperty, 0);
  289. editor.SetValue(Grid.ColumnProperty, 1);
  290. row.Children.Add(editor);
  291. var def = "";
  292. if (!string.IsNullOrEmpty(check.Parameters))
  293. {
  294. var parameters = check.ParseParameters();
  295. def = parameters["Default"];
  296. }
  297. var value = GetValue(values, check.ID).ToString();
  298. editor.Text = string.IsNullOrWhiteSpace(value) ? def : value;
  299. editor.TextChanged += (o, e) =>
  300. {
  301. values[check.ID] = editor.Text;
  302. Changed(values);
  303. };
  304. border.Child = row;
  305. return border;
  306. }
  307. private Border CreateComboPanel(Grid grid, QAQuestion check, Dictionary<Guid, object> values, int number)
  308. {
  309. var border = CreateBorder(grid, check, values);
  310. var row = new Grid();
  311. row.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0F, GridUnitType.Star) });
  312. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  313. var label = new TextBlock
  314. {
  315. Text = string.Format("{0}. {1}", number, check.Question),
  316. FontSize = 14.0F,
  317. Margin = GetMargin(grid),
  318. VerticalAlignment = VerticalAlignment.Center,
  319. TextAlignment = TextAlignment.Left,
  320. TextWrapping = TextWrapping.Wrap
  321. };
  322. label.SetValue(Grid.RowProperty, 0);
  323. label.SetValue(Grid.ColumnProperty, 0);
  324. row.Children.Add(label);
  325. row.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(320.0F, GridUnitType.Pixel) });
  326. var editor = new ComboBox
  327. {
  328. Margin = GetMargin(grid),
  329. HorizontalContentAlignment = HorizontalAlignment.Left,
  330. VerticalContentAlignment = VerticalAlignment.Center,
  331. Height = 40
  332. };
  333. editor.SetValue(Grid.RowProperty, 0);
  334. editor.SetValue(Grid.ColumnProperty, 1);
  335. row.Children.Add(editor);
  336. if (!string.IsNullOrEmpty(check.Parameters))
  337. {
  338. var parameters = check.ParseParameters();
  339. var options = parameters["Options"].Split(',');
  340. var def = parameters["Default"];
  341. for (var i = 0; i < options.Length; i++)
  342. editor.Items.Add(options[i]);
  343. var value = GetValue(values, check.ID).ToString();
  344. editor.SelectedIndex = editor.Items.IndexOf(string.IsNullOrWhiteSpace(value) ? def : value);
  345. }
  346. editor.SelectionChanged += (o, e) =>
  347. {
  348. values[check.ID] = editor.SelectedIndex >= 0 ? editor.Items[editor.SelectedIndex].ToString() : "";
  349. Changed(values);
  350. };
  351. border.Child = row;
  352. return border;
  353. }
  354. public void Clear()
  355. {
  356. borders.Clear();
  357. MasterGrid.Children.Clear();
  358. MasterGrid.ColumnDefinitions.Clear();
  359. MasterGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.25F, GridUnitType.Star) });
  360. MasterGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.25F, GridUnitType.Star) });
  361. }
  362. public void LoadChecks(string title, IEnumerable<QAQuestion> checks, Dictionary<Guid, object> values)
  363. {
  364. var sections = new List<string>();
  365. foreach (var check in checks)
  366. {
  367. var section = string.IsNullOrWhiteSpace(check.Section) ? title : check.Section;
  368. if (!sections.Contains(section))
  369. sections.Add(section);
  370. }
  371. if (!sections.Any())
  372. sections.Add(title);
  373. foreach (var section in sections)
  374. {
  375. var header = CreateHeader(section);
  376. var border = new Border
  377. {
  378. CornerRadius = new CornerRadius(0.0F, 0.0F, 0.0F, 0.0F),
  379. BorderThickness = new Thickness(1.0F),
  380. BorderBrush = SystemColors.WindowFrameBrush,
  381. Background = SystemColors.WindowBrush,
  382. Margin = new Thickness(5.0F, 0.0F, 5.0F, 5.0F),
  383. Padding = new Thickness(10.0F, 10.0F, 5.0F, 10.0F)
  384. };
  385. border.SetValue(Grid.RowProperty, 1);
  386. border.SetValue(Grid.ColumnProperty, MasterGrid.ColumnDefinitions.Count - 1);
  387. if (MasterGrid.ColumnDefinitions.Count > 2)
  388. {
  389. header.Margin = new Thickness(0.0F, 5.0F, 5.0F, 2.0F);
  390. border.Margin = new Thickness(0.0F, 0.0F, 5.0F, 5.0F);
  391. CollapseMargins();
  392. }
  393. MasterGrid.ColumnDefinitions.Insert(MasterGrid.ColumnDefinitions.Count - 1,
  394. new ColumnDefinition { Width = new GridLength(1F, GridUnitType.Star) });
  395. MasterGrid.Children.Add(border);
  396. var scroll = new ScrollViewer
  397. {
  398. VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
  399. HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled,
  400. Padding = new Thickness(0.0F, 0.0F, 5.0F, 0.0F)
  401. };
  402. border.Child = scroll;
  403. var grid = new Grid();
  404. var i = 1;
  405. if (!checks.Any())
  406. {
  407. var check = new QAQuestion
  408. {
  409. Answer = QAAnswer.Comment,
  410. Question = "There are no checks defined for this form",
  411. Parameters = ""
  412. };
  413. borders[check] = CreateCommentPanel(grid, check, values, i);
  414. }
  415. else
  416. {
  417. foreach (var check in checks)
  418. {
  419. Border element = null;
  420. if (check.Answer == QAAnswer.Comment)
  421. element = CreateCommentPanel(grid, check, values, i);
  422. else if (check.Answer == QAAnswer.Choice)
  423. element = CreateChoicePanel(grid, check, values, i++);
  424. else if (check.Answer == QAAnswer.Number)
  425. element = CreateNumberPanel(grid, check, values, i++);
  426. else if (check.Answer == QAAnswer.Text)
  427. element = CreateTextPanel(grid, check, values, i++);
  428. else if (check.Answer == QAAnswer.Combo)
  429. element = CreateComboPanel(grid, check, values, i++);
  430. if (element != null)
  431. borders[check] = element;
  432. }
  433. }
  434. scroll.Content = grid;
  435. }
  436. }
  437. }
  438. }