DynamicMapColumn.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Windows.Media.Imaging;
  5. using InABox.Core;
  6. using InABox.DynamicGrid;
  7. using InABox.WPF;
  8. using PRSDesktop.Forms;
  9. namespace PRSDesktop
  10. {
  11. internal class DynamicMapColumn<T> : DynamicImageColumn where T : Entity, new()
  12. {
  13. private readonly string Latitude = "";
  14. private readonly string Longitude = "";
  15. private readonly BitmapImage milestone = Resources.milestone.AsBitmapImage();
  16. private readonly string Timestamp = "";
  17. public DynamicMapColumn(DynamicGrid<T> grid, Expression<Func<T, object>> property) : base(r => null)
  18. {
  19. Latitude = CoreUtils.GetFullPropertyName(property, ".") + ".Latitude";
  20. Longitude = CoreUtils.GetFullPropertyName(property, ".") + ".Longitude";
  21. Timestamp = CoreUtils.GetFullPropertyName(property, ".") + ".Timestamp";
  22. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Latitude));
  23. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Longitude));
  24. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Timestamp));
  25. Property = property;
  26. Image = MapImage;
  27. Action = MapClick;
  28. Filters = new[] { "(Blank)", "(Not Blank)" };
  29. FilterRecord = MapFilter;
  30. }
  31. private bool MapFilter(CoreRow row, string[] filter)
  32. {
  33. if (filter.Contains("(Blank)") && !HasLocation(row))
  34. return true;
  35. if (filter.Contains("(Not Blank)") && HasLocation(row))
  36. return true;
  37. return false;
  38. }
  39. public Expression<Func<T, object>> Property { get; }
  40. private bool HasLocation(CoreRow row)
  41. {
  42. var lat = row.Get<double>(Latitude);
  43. var lng = row.Get<double>(Longitude);
  44. return lat != 0.0F && lng != 0.0F;
  45. }
  46. private BitmapImage? MapImage(CoreRow? row)
  47. {
  48. return row is null || HasLocation(row) ? milestone : null;
  49. }
  50. private bool MapClick(CoreRow? row)
  51. {
  52. if (row is not null && HasLocation(row))
  53. {
  54. var form = new MapForm(row.Get<double>(Latitude), row.Get<double>(Longitude), row.Get<DateTime>(Timestamp));
  55. form.ShowDialog();
  56. }
  57. return false;
  58. }
  59. }
  60. }