ProductLookupDock.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. using Comal.Classes;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.Dxf;
  16. using InABox.Wpf;
  17. using InABox.WPF;
  18. using Microsoft.Win32;
  19. namespace PRSDesktop;
  20. public class ProductDockFilterButton : FilterButton<Product>
  21. {
  22. public ProductDockFilterButton() : base(
  23. new GlobalConfiguration<CoreFilterDefinitions>("ProductDock.Product"),
  24. new UserConfiguration<CoreFilterDefinitions>("ProductDock.Product"))
  25. {
  26. }
  27. }
  28. /// <summary>
  29. /// Interaction logic for ProductLookup.xaml
  30. /// </summary>
  31. public partial class ProductLookupDock : UserControl, IDockPanel
  32. {
  33. private ProductHoldingControl? _holdings;
  34. private Product[] Products = Array.Empty<Product>();
  35. public ProductLookupDock()
  36. {
  37. InitializeComponent();
  38. }
  39. public void Setup()
  40. {
  41. if (!Security.CanView<StockLocation>())
  42. return;
  43. if (_holdings == null)
  44. {
  45. _holdings = new ProductHoldingControl();
  46. HoldingsTab.Content = _holdings;
  47. HoldingsTab.Visibility = Visibility.Visible;
  48. }
  49. _holdings.Refresh(true, false);
  50. }
  51. public void Refresh()
  52. {
  53. }
  54. private Filter<Product>? GetSearchFilter()
  55. {
  56. var search = Search.Text;
  57. if (!string.IsNullOrEmpty(search))
  58. {
  59. return new Filter<Product>(x => x.Code).Contains(Search.Text).Or(x => x.Name).Contains(search);
  60. }
  61. else
  62. {
  63. return null;
  64. }
  65. }
  66. private void Reload()
  67. {
  68. var filters = new Filters<Product>();
  69. filters.Add(GetSearchFilter());
  70. filters.Add(FilterButton.GetFilter());
  71. var filter = filters.Combine() ?? new Filter<Product>().None();
  72. using (new WaitCursor())
  73. {
  74. Products = Client.Query(
  75. filter,
  76. Columns.Required<Product>().Add(x => x.ID, x => x.Code, x => x.Name, x => x.Image.ID, x => x.Image.FileName),
  77. new SortOrder<Product>(x => x.Code))
  78. .ToObjects<Product>().ToArray();
  79. Items.ItemsSource = Products;
  80. if (Products.Any())
  81. {
  82. Items.Focus();
  83. Items.SelectedIndex = 0;
  84. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  85. listBoxItem?.Focus();
  86. }
  87. }
  88. }
  89. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  90. {
  91. }
  92. private void Search_KeyUp(object sender, KeyEventArgs e)
  93. {
  94. if (e.Key == Key.Enter)
  95. {
  96. Reload();
  97. }
  98. else if (e.Key == Key.Down)
  99. {
  100. if (Products.Any())
  101. {
  102. Items.Focus();
  103. //Items.SelectedValue = items.First();
  104. Items.SelectedIndex = 0;
  105. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  106. listBoxItem.Focus();
  107. }
  108. }
  109. }
  110. private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. Image.Source = null;
  113. if (e.AddedItems.Count > 0)
  114. {
  115. var item = e.AddedItems[0] as Product;
  116. if (!item.Image.ID.Equals(Guid.Empty))
  117. new Client<Document>().Query(
  118. new Filter<Document>(x => x.ID).IsEqualTo(item.Image.ID),
  119. Columns.None<Document>().Add(x => x.ID, x => x.Data),
  120. null,
  121. CoreRange.All,
  122. (docs, error) =>
  123. {
  124. var row = docs.Rows.FirstOrDefault();
  125. if (row != null)
  126. {
  127. var id = row.Get<Document, Guid>(x => x.ID);
  128. var data = row.Get<Document, byte[]>(x => x.Data);
  129. if (data?.Any() == true)
  130. {
  131. var ms = new MemoryStream(data);
  132. var bmp = new Bitmap(ms);
  133. Dispatcher.BeginInvoke(
  134. new Action<Guid>(o =>
  135. {
  136. if (Items.SelectedValue != null)
  137. {
  138. var sel = (Product)Items.SelectedValue;
  139. if (sel.Image.ID == o)
  140. Image.Source = bmp.AsBitmapImage(false);
  141. }
  142. }), id
  143. );
  144. }
  145. }
  146. }
  147. );
  148. else
  149. Image.Source = null;
  150. if (Security.CanView<StockLocation>())
  151. {
  152. _holdings.Product = item;
  153. _holdings.Refresh(false, true);
  154. }
  155. }
  156. else
  157. {
  158. Image.Source = null;
  159. }
  160. }
  161. private void Items_KeyUp(object sender, KeyEventArgs e)
  162. {
  163. }
  164. private void Items_KeyDown(object sender, KeyEventArgs e)
  165. {
  166. if (e.Key == Key.Up)
  167. if (Items.SelectedIndex == 0)
  168. Search.Focus();
  169. }
  170. private void ImageMenu_Opened(object sender, RoutedEventArgs e)
  171. {
  172. var pdi = Items.SelectedItem as Product;
  173. var anyproducts = pdi != null;
  174. var validimage = pdi != null && pdi.Image.ID != Guid.Empty;
  175. LoadImageFromFile.IsEnabled = anyproducts;
  176. SaveImageToFile.IsEnabled = validimage;
  177. CopyImageToClipboard.IsEnabled = validimage;
  178. PasteImageFromClipboard.IsEnabled = anyproducts && (Clipboard.ContainsData("ProductImage") || Clipboard.ContainsImage());
  179. ClearImage.IsEnabled = anyproducts;
  180. }
  181. private string SelectedProducts()
  182. {
  183. var product = Items.SelectedItem as Product;
  184. return product != null ? product.Code : "";
  185. }
  186. private void UpdateProductImages(Guid id, string filename, Bitmap? bitmap)
  187. {
  188. var product = Items.SelectedItem as Product;
  189. if (product == null)
  190. return;
  191. var docid = id;
  192. if (bitmap != null && docid == Guid.Empty)
  193. {
  194. byte[] data = null;
  195. using (var ms = new MemoryStream())
  196. {
  197. bitmap.Save(ms, ImageFormat.Png);
  198. data = ms.GetBuffer();
  199. }
  200. var crc = CoreUtils.CalculateCRC(data);
  201. var doc = new Client<Document>().Query(
  202. new Filter<Document>(x => x.FileName).IsEqualTo(Path.GetFileName(filename)).And(x => x.CRC).IsEqualTo(crc),
  203. Columns.None<Document>().Add(x => x.ID)
  204. ).Rows.FirstOrDefault()?.ToObject<Document>();
  205. if (doc == null)
  206. {
  207. doc = new Document
  208. {
  209. FileName = Path.GetFileName(filename),
  210. CRC = crc,
  211. TimeStamp = DateTime.Now,
  212. Data = data
  213. };
  214. new Client<Document>().Save(doc, "");
  215. }
  216. docid = doc.ID;
  217. }
  218. product.Image.ID = CoreUtils.FullGuid;
  219. product.CommitChanges();
  220. product.Image.ID = docid;
  221. product.Image.FileName = Path.GetFileName(filename);
  222. new Client<Product>().Save(product, "Cleared Product Image", (p, err) => { });
  223. Image.Source = bitmap?.AsBitmapImage();
  224. }
  225. private Bitmap? ConvertDXFFile(string filename)
  226. {
  227. Bitmap? result = null;
  228. using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  229. {
  230. //String newfile = Path.ChangeExtension(Path.GetFileName(filename), "png");
  231. try
  232. {
  233. result = DxfUtils.ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
  234. }
  235. catch (Exception e)
  236. {
  237. MessageWindow.ShowError("Could not load DXF file", e);
  238. result = null;
  239. }
  240. }
  241. return result;
  242. }
  243. private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
  244. {
  245. var product = Items.SelectedItem as Product;
  246. if (product == null)
  247. return;
  248. var dlg = new OpenFileDialog();
  249. dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
  250. if (dlg.ShowDialog() == true)
  251. {
  252. var filename = dlg.FileName.ToLower();
  253. Progress.Show("Updating Images: " + Path.GetFileName(filename));
  254. Bitmap? bmp = null;
  255. if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
  256. bmp = ConvertDXFFile(filename);
  257. else
  258. bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
  259. UpdateProductImages(Guid.Empty, filename, bmp);
  260. Progress.Close();
  261. MessageBox.Show(string.Format("Imported [{0}] into [{1}]", Path.GetFileName(dlg.FileName), SelectedProducts()));
  262. }
  263. }
  264. private void SaveImageToFile_Click(object sender, RoutedEventArgs e)
  265. {
  266. var product = Items.SelectedItem as Product;
  267. if (product == null)
  268. return;
  269. var bmp = (Image.Source as BitmapImage).AsBitmap();
  270. var dlg = new SaveFileDialog();
  271. dlg.Filter = "Image Files (*.png)|*.png";
  272. dlg.FileName = product.Image.FileName;
  273. if (dlg.ShowDialog() == true)
  274. bmp.Save(dlg.FileName);
  275. }
  276. private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
  277. {
  278. var product = Items.SelectedItem as Product;
  279. if (product == null)
  280. return;
  281. var bmp = (Image.Source as BitmapImage).AsBitmap();
  282. var data = new Tuple<Guid, string, Bitmap>(
  283. product.Image.ID,
  284. product.Image.FileName,
  285. bmp
  286. );
  287. Clipboard.SetData("ProductImage", data);
  288. }
  289. private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
  290. {
  291. if (Items.SelectedItem is not Product product)
  292. return;
  293. if (MessageBox.Show("Are you sure you wish to update the image for the selected product?", "Update Image", MessageBoxButton.YesNo,
  294. MessageBoxImage.Question) != MessageBoxResult.Yes)
  295. return;
  296. Progress.Show("Updating Images");
  297. var id = Guid.Empty;
  298. var filename = "";
  299. Bitmap? bitmap = null;
  300. if (Clipboard.ContainsData("ProductImage"))
  301. {
  302. if(Clipboard.GetData("ProductImage") is Tuple<Guid, string, Bitmap> data)
  303. {
  304. id = data.Item1;
  305. filename = data.Item2;
  306. bitmap = data.Item3;
  307. }
  308. }
  309. else if (Clipboard.ContainsImage())
  310. {
  311. var data = Clipboard.GetImage();
  312. bitmap = data.AsBitmap2();
  313. filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
  314. if (Clipboard.ContainsFileDropList())
  315. {
  316. var list = Clipboard.GetFileDropList();
  317. if (list.Count > 0)
  318. filename = Path.ChangeExtension(Path.GetFileName(list[0]), ".png");
  319. }
  320. //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
  321. //bitmap.Save(filename);
  322. id = Guid.Empty;
  323. }
  324. if (bitmap == null)
  325. {
  326. MessageBox.Show("Unable to paste data from clipboard");
  327. return;
  328. }
  329. Progress.Show("");
  330. UpdateProductImages(id, filename, bitmap);
  331. Progress.Close();
  332. MessageBox.Show(string.Format("Pasted [{0}] into [{1}]", Path.GetFileName(filename), SelectedProducts()));
  333. }
  334. private void ClearImage_Click(object sender, RoutedEventArgs e)
  335. {
  336. if (MessageWindow.ShowYesNo("Are you sure you wish to clear the image for the selected product?", "Clear Image"))
  337. return;
  338. Progress.Show("Clearing Image");
  339. UpdateProductImages(Guid.Empty, "", null);
  340. Progress.Close();
  341. MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
  342. }
  343. private void Grid_MouseMove(object sender, MouseEventArgs e)
  344. {
  345. if(sender is not Grid grid || grid.Tag is not Product product || e.LeftButton != MouseButtonState.Pressed)
  346. {
  347. return;
  348. }
  349. DragDrop.DoDragDrop(grid, product, DragDropEffects.Copy);
  350. }
  351. private void FilterButton_OnFilterRefresh()
  352. {
  353. Reload();
  354. }
  355. }