DynamicMapColumn.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 InABox.Wpf.DynamicGrid;
  9. using PRSDesktop.Forms;
  10. namespace PRSDesktop
  11. {
  12. internal class DynamicMapColumn<T> : DynamicImageColumn where T : Entity, new()
  13. {
  14. private readonly string Latitude;
  15. private readonly string Longitude;
  16. private readonly BitmapImage milestone = Resources.map.AsBitmapImage();
  17. private readonly string Geofence;
  18. public DynamicMapColumn(DynamicGrid<T> grid, Expression<Func<T, Address>> property) : base(r => null)
  19. {
  20. Latitude = $"{CoreUtils.GetFullPropertyName(property, ".")}.{nameof(Address.Location)}.{nameof(Address.Location.Latitude)}";
  21. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Latitude));
  22. Longitude = $"{CoreUtils.GetFullPropertyName(property, ".")}.{nameof(Address.Location)}.{nameof(Address.Location.Longitude)}";
  23. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Longitude));
  24. try
  25. {
  26. Geofence = $"{CoreUtils.GetFullPropertyName(property, ".")}.{nameof(Address.Geofence)}";
  27. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Geofence));
  28. }
  29. catch (Exception e)
  30. {
  31. Geofence = "";
  32. }
  33. Image = MapImage;
  34. Action = MapClick;
  35. GetFilter = () => new StaticColumnFilter<bool>(HasLocation, [
  36. new("(Blank)", false),
  37. new("(Not Blank)", true)
  38. ]);
  39. }
  40. public DynamicMapColumn(DynamicGrid<T> grid, Expression<Func<T, Location>> property) : base(r => null)
  41. {
  42. Latitude = $"{CoreUtils.GetFullPropertyName(property, ".")}.{nameof(Location.Latitude)}";
  43. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Latitude));
  44. Longitude = CoreUtils.GetFullPropertyName(property, ".") + ".Longitude";
  45. grid.HiddenColumns.Add(CoreUtils.GetPropertyExpression<T>(Longitude));
  46. Geofence = "";
  47. Image = MapImage;
  48. Action = MapClick;
  49. GetFilter = () => new StaticColumnFilter<bool>(HasLocation, [
  50. new("(Blank)", false),
  51. new("(Not Blank)", true)
  52. ]);
  53. }
  54. private bool HasLocation(CoreRow row)
  55. {
  56. var lat = row.Get<double>(Latitude);
  57. var lng = row.Get<double>(Longitude);
  58. return lat != 0.0F && lng != 0.0F;
  59. }
  60. private BitmapImage? MapImage(CoreRow? row)
  61. {
  62. return row is null || HasLocation(row) ? milestone : null;
  63. }
  64. private bool MapClick(CoreRow? row)
  65. {
  66. if (row is not null && HasLocation(row))
  67. {
  68. var geofence = (!String.IsNullOrWhiteSpace(Geofence)
  69. ? Serialization.Deserialize<GeoFenceDefinition>(row.Get<string>(Geofence))
  70. : null)
  71. ?? new GeoFenceDefinition();
  72. var mapform = new MapForm(row.Get<double>(Latitude), row.Get<double>(Longitude),geofence);
  73. mapform.ShowDialog();
  74. //var Address = new Address();
  75. //Address.Location.Latitude = row.Get<double>(Latitude);
  76. //Address.Location.Longitude = row.Get<double>(Longitude);
  77. //if (Geofence != null)
  78. // Address.Geofence = row.Get<string>(Geofence);
  79. //var form = new GeofenceEditor(Address, false);
  80. //form.ShowDialog();
  81. }
  82. return false;
  83. }
  84. }
  85. }