Files
DP44/DataPRO/Modules/DatabaseImporter/DatabaseImport/Classes/RegionsAndZones/Zone.cs

157 lines
5.3 KiB
C#
Raw Normal View History

2026-04-17 14:55:32 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace DatabaseImport
{
public class Zone //: BasePropertyChanged
{
public string Name { get; set; }
public string Description { get; set; }
public string Image { get; set; }
private readonly TestObjectTemplate _template;
public TemplateZone ISODllZone { get; }
public Zone(Zone copy, TestObjectTemplate template)
{
ISODllZone = copy.ISODllZone;
_template = template;
Description = ISODllZone?.Description ?? "";
Image = ISODllZone?.Picture;
_fileNames = new List<System.IO.FileInfo>();
PopulateFilenamesIfNeeded();
PictureIndex = copy.PictureIndex;
PictureSource = copy.PictureSource;
Name = ISODllZone.ZoneName;
_regions = new List<Region>(copy.Regions);
}
public Zone(TemplateZone z, TestObjectTemplate template)
{
ISODllZone = z;
_template = template;
Description = z?.Description ?? "";
Image = z?.Picture;
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZonePictures");
if (!string.IsNullOrEmpty(Image))
{
path = System.IO.Path.Combine(path, Image);
if (!System.IO.File.Exists(path)) { PictureIndex = -1; }
else
{
var src = new System.Windows.Media.Imaging.BitmapImage();
src.BeginInit();
src.UriSource = new Uri(path, UriKind.Absolute);
try
{
src.EndInit();
PictureSource = src;
_pictureIndex = AllPictures.ToList().IndexOf(Image);
}
catch
{
PictureIndex = -1;
}
}
}
if (z != null)
{
Name = z.ZoneName;
_regions = new List<Region>();
foreach (var r in z.TemplateRegions)
{
var region = new Region(template, r);
_regions.Add(region);
}
}
}
public class FileInfoComparer : IComparer<System.IO.FileInfo>
{
int IComparer<System.IO.FileInfo>.Compare(System.IO.FileInfo left, System.IO.FileInfo right)
{
if (left == right) { return 0; }
if (left == null) { return -1; }
return right == null ? 1 : string.Compare(left.FullName, right.FullName, StringComparison.Ordinal);
}
}
private List<System.IO.FileInfo> _fileNames;
private static object MyLock = new object();
public string[] AllPictures
{
get
{
lock (MyLock)
{
PopulateFilenamesIfNeeded();
return _fileNames.Select(fi => fi.Name).ToArray();
}
}
}
public void PopulateFilenamesIfNeeded()
{
lock (MyLock)
{
if (null == _fileNames || _fileNames.Count == 0)
{
PopulateFilenames();
}
}
}
private void PopulateFilenames()
{
lock (MyLock)
{
var files = new List<string>();
files.AddRange(System.IO.Directory.EnumerateFiles(
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZonePictures")));
_fileNames = new List<System.IO.FileInfo>();
foreach (var file in files)
{
_fileNames.Add(new System.IO.FileInfo(file));
}
_fileNames.Sort(new FileInfoComparer());
}
}
private int _pictureIndex = -1;
public int PictureIndex
{
get => _pictureIndex;
set
{
if (value < 0) { PictureSource = null; }
else
{
var src = new System.Windows.Media.Imaging.BitmapImage();
lock (MyLock)
{
PopulateFilenamesIfNeeded();
src.BeginInit();
src.UriSource = new Uri(_fileNames[value].FullName, UriKind.Absolute);
src.EndInit();
}
PictureSource = src;
}
_pictureIndex = value;
}
}
public string GetPictureName()
{
return _pictureIndex < 0 ? "" : _fileNames[_pictureIndex].Name;
}
public System.Windows.Media.ImageSource PictureSource { get; set; } = null;
private List<Region> _regions = new List<Region>();
public Region[] Regions
{
get => _regions.ToArray();
set => _regions = new List<Region>(value);
}
}
}