123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.WPF;
- using NPOI.HSSF.Util;
- using Syncfusion.Windows.Tools.Controls;
- namespace PRSDesktop
- {
- public class EquipmentSelectorData
- {
- // if first == FullGuid, remainder are selected
- // Otherwise, ignore the rest
- public Guid[] Groups { get; set; }
-
- // if first == FullGuid, select all
- public Guid[] Equipment { get; set; }
-
- public EquipmentSelectorData( Guid[] groups, Guid[] equipment)
- {
- Groups = groups;
- Equipment = equipment;
- }
- public EquipmentSelectorData() : this(new[] { Guid.Empty }, new[] { CoreUtils.FullGuid })
- {
- }
- }
- public class EquipmentSelectorSettings
- {
- public double SplitPosition { get; set; }
- public EquipmentSelectorSettings()
- {
- SplitPosition = 200.0F;
- }
- }
-
- public class EquipmentSelectorSettingsChangedArgs : EventArgs
- {
- public EquipmentSelectorSettings Settings { get; private set; }
- public EquipmentSelectorSettingsChangedArgs(EquipmentSelectorSettings settings)
- {
- Settings = settings;
- }
- }
-
- public delegate void EquipmentSelectorSettingsChanged(object sender, EquipmentSelectorSettingsChangedArgs args);
-
- public class EquipmentSelectorSelectionChangedArgs : EventArgs
- {
- public EquipmentSelectorData Selection { get; private set; }
- public EquipmentSelectorSelectionChangedArgs(EquipmentSelectorData selection)
- {
- Selection = selection;
- }
- }
- public delegate void EquipmentSelectorSelectionChanged(object sender, EquipmentSelectorSelectionChangedArgs args);
- public partial class EquipmentSelector : UserControl
- {
- private enum Suppress
- {
- This
- }
- private class ListEntry
- {
- public Guid Key { get; set; }
- public String Value { get; set; }
- public BitmapImage Image { get; set; }
- public ListEntry(Guid key, String value, BitmapImage image = null)
- {
- Key = key;
- Value = value;
- Image = image;
- }
- }
- public EquipmentSelectorSettings Settings { get; set; }
-
- public event EquipmentSelectorSettingsChanged SettingsChanged;
- public event EquipmentSelectorSelectionChanged SelectionChanged;
-
- private CoreTable _equipment;
- private CoreTable _activities;
- private ListEntry[] _groups;
- private Tuple<Guid,Guid,String, BitmapImage?>[] _equipmentgroups;
- public T[] GetEquipmentData<T>(Func<CoreRow, BitmapImage?, EquipmentActivity[], T> transform)
- {
- List<T> result = new List<T>();
- var selgrps = GetSelectedGroups();
- var seleq = GetSelectedEquipment();
- var eqids = _equipmentgroups
- .Where(x => selgrps.Contains(x.Item1) && seleq.Contains(CoreUtils.FullGuid) || seleq.Contains(x.Item2))
- .Select(x => x.Item2)
- .Distinct()
- .ToArray();
- var rows = _equipment.Rows.Where(r => eqids.Contains(r.Get<Equipment, Guid>(c => c.ID)));
- foreach (var row in rows)
- {
- var activities = _activities.Rows
- .Where(r => r.Get<EquipmentActivity, Guid>(c => c.Equipment.ID) ==
- row.Get<Equipment, Guid>(c => c.ID))
- .Select(r=>r.ToObject<EquipmentActivity>())
- .ToArray();
- result.Add(
- transform.Invoke(
- row,
- _groups.FirstOrDefault(x => x.Key == row.Get<Equipment, Guid>(c => c.GroupLink.ID))?.Image,
- activities
- )
- );
- }
- return result.ToArray();
- }
-
- public EquipmentSelector()
- {
- Settings = new EquipmentSelectorSettings();
- using (new EventSuppressor(Suppress.This))
- InitializeComponent();
- }
-
- public void Setup()
- {
- using (new EventSuppressor(Suppress.This))
- {
- var results = Client.QueryMultiple(
- new KeyedQueryDef<Equipment>(LookupFactory.DefineFilter<Equipment>()),
- new KeyedQueryDef<EquipmentActivity>(),
- new KeyedQueryDef<EquipmentGroup>(LookupFactory.DefineFilter<EquipmentGroup>())
- );
- _equipment = results.Get<Equipment>();
- _activities = results.Get<EquipmentActivity>();
- var groups = results.Get<EquipmentGroup>();
- _groups = groups.Rows.Select(r => new ListEntry(
- r.Get<EquipmentGroup,Guid>(c=>c.ID),
- r.Get<Team,String>(c=>c.Name)
- )).ToArray();
- GroupList.ItemsSource = _groups;
-
- var ids = groups.Rows.Select(r => r.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)).Distinct().Where(x => x != Guid.Empty).ToArray();
- var Images = ids.Any()
- ? new Client<Document>().Load(new Filter<Document>(x=>x.ID).InList(ids))
- : new Document[] { };
-
- foreach (var row in groups.Rows)
- {
- var image = Images.FirstOrDefault(x => x.ID.Equals(row.Get<EquipmentGroup,Guid>(c=>c.Thumbnail.ID)));
- var grp = _groups.FirstOrDefault(x => x.Key == row.Get<EquipmentGroup, Guid>(c => c.Thumbnail.ID));
- if (grp != null)
- grp.Image = ImageUtils.LoadImage(image.Data) ?? PRSDesktop.Resources.truck.AsBitmapImage();
- }
-
- ObservableCollection<ListEntry> groupdata = new() { new ListEntry(Guid.Empty, "All Equipment", PRSDesktop.Resources.truck.AsBitmapImage()) };
- foreach (var group in _groups)
- groupdata.Add(group);
- groupdata.Add(new ListEntry(CoreUtils.FullGuid, "Multiple Groups", PRSDesktop.Resources.truck.AsBitmapImage()));
- Groups.ItemsSource = groupdata;
-
- _equipmentgroups = _equipment.Rows.Select(
- row => new Tuple<Guid, Guid, String, BitmapImage?>(
- row.Get<Equipment, Guid>(c => c.GroupLink.ID),
- row.Get<Equipment, Guid>(c => c.ID),
- row.Get<Equipment, String>(c => c.Description),
- _groups.FirstOrDefault(x=>x.Key == row.Get<Equipment, Guid>(c => c.GroupLink.ID))?.Image
- )
- ).Union(
- _equipment.Rows.Select(
- row => new Tuple<Guid,Guid,String, BitmapImage?>(
- Guid.Empty,
- row.Get<Equipment, Guid>(c => c.ID),
- row.Get<Equipment, String>(c => c.Description),
- PRSDesktop.Resources.truck.AsBitmapImage()
- )
- )
- ).ToArray();
- Groups.SelectedValue = Guid.Empty;
- }
- }
- private void UpdateSettings()
- {
- bool changed = false;
-
- var groups = GetSelectedGroups();
- if (groups.Contains(CoreUtils.FullGuid) && (Math.Abs(Settings.SplitPosition - GroupListRow.Height.Value) >= 1.0F))
- {
- changed = true;
- Settings.SplitPosition = GroupListRow.Height.Value;
- }
- if (changed && (!EventSuppressor.IsSet(Suppress.This)))
- SettingsChanged?.Invoke(this, new EquipmentSelectorSettingsChangedArgs(Settings));
- }
-
- private Guid[] GetSelectedGroups()
- {
- Guid groupid = Groups.SelectedValue != null ? (Guid)Groups.SelectedValue : Guid.Empty;
- var teams = (groupid == CoreUtils.FullGuid)
- ? GroupList.SelectedItems
- .OfType<ListEntry>()
- .Select(x => x.Key)
- .Prepend(groupid)
- .ToArray()
- : new Guid[] { groupid };
- return teams;
- }
- private Guid[] GetSelectedEquipment()
- {
- var emps = (SelectedEquipment.SelectedItems.Count == SelectedEquipment.Items.Count)
- ? new Guid[] { CoreUtils.FullGuid }
- : SelectedEquipment.SelectedItems
- .OfType<ListEntry>()
- .Select(x => x.Key)
- .ToArray();
- return emps;
- }
- public EquipmentSelectorData Selection
- {
- get => GetSelectionData();
- set => SelectEquipment(value);
- }
-
- private EquipmentSelectorData GetSelectionData()
- {
- return new EquipmentSelectorData(
- GetSelectedGroups(),
- GetSelectedEquipment()
- );
- }
-
- private void SelectEquipment(EquipmentSelectorData selection)
- {
- using (new EventSuppressor(Suppress.This))
- {
- Groups.SelectedValue = selection.Groups.Contains(CoreUtils.FullGuid)
- ? CoreUtils.FullGuid
- : selection.Groups.FirstOrDefault();
- LoadGroups(selection.Groups);
- var equipment = SelectedEquipment.ItemsSource as ObservableCollection<ListEntry>;
- var pairs = equipment.Where(x => selection.Equipment.Contains(CoreUtils.FullGuid) || selection.Equipment.Contains(x.Key));
- SelectedEquipment.SelectedItems.Clear();
- foreach (var pair in pairs)
- SelectedEquipment.SelectedItems.Add(pair);
- }
- }
-
- private void LoadGroups(Guid[] groupids)
- {
- using (new EventSuppressor(Suppress.This))
- {
- Guid groupid = groupids.FirstOrDefault();
- if (Guid.Equals(groupid, CoreUtils.FullGuid))
- {
- GroupListRow.Height = new GridLength(Settings.SplitPosition, GridUnitType.Pixel);
- GroupListSplitterRow.Height = new GridLength(4, GridUnitType.Pixel);
- var selected = new ObservableCollection<object>(_groups.Where(x => groupids.Contains(x.Key)));
- GroupList.SelectedItems = selected;
- LoadEquipment(groupids.Skip(1).ToArray());
- }
- else
- {
- GroupListRow.Height = new GridLength(0, GridUnitType.Pixel);
- GroupListSplitterRow.Height = new GridLength(0, GridUnitType.Pixel);
- LoadEquipment(new Guid[] { groupid });
- }
- }
- }
- private void LoadEquipment(Guid[] teamids)
- {
- using (new EventSuppressor(Suppress.This))
- {
- var eqs =_equipmentgroups
- .OrderBy(x => x.Item3)
- .Where(x => teamids.Contains(x.Item1))
- .Select(x => new ListEntry(x.Item2, x.Item3))
- .Distinct()
- .ToArray();
- SelectedEquipment.ItemsSource = new ObservableCollection<ListEntry>(eqs);
- SelectedEquipment.SelectedItems = new ObservableCollection<object>(eqs);
- }
- }
-
- public void SelectEmployee(Guid employeeid)
- {
- using (new EventSuppressor(Suppress.This))
- {
- LoadGroups(new Guid[] { Guid.Empty });
- if (SelectedEquipment.ItemsSource is ObservableCollection<ListEntry> employees)
- {
- var emp = employees.FirstOrDefault(x => x.Key == employeeid);
- if (emp != null)
- SelectedEquipment.SelectedItems = new ObservableCollection<object>() { emp };
- }
- }
- }
-
- private void TeamsSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (EventSuppressor.IsSet(Suppress.This))
- return;
- using (new EventSuppressor(Suppress.This))
- LoadGroups(GetSelectedGroups());
- UpdateSettings();
- SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
- }
-
- private void SelectedGroups_ItemChecked(object? sender, ItemCheckedEventArgs e)
- {
- if (EventSuppressor.IsSet(Suppress.This))
- return;
- using (new EventSuppressor(Suppress.This))
- LoadEquipment(GetSelectedGroups());
- UpdateSettings();
- SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
- }
-
- private void SelectedEquipment_OnItemChecked(object? sender, ItemCheckedEventArgs e)
- {
- if (EventSuppressor.IsSet(Suppress.This))
- return;
- UpdateSettings();
- SelectionChanged?.Invoke(this, new EquipmentSelectorSelectionChangedArgs(GetSelectionData()));
- }
-
- private void SelectedGroups_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- if (EventSuppressor.IsSet(Suppress.This))
- return;
- UpdateSettings();
- }
- }
- }
|