ProductLookupDock.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. new Columns<Product>(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. new Columns<Document>(x => x.ID, x => x.Data),
  120. null,
  121. (docs, error) =>
  122. {
  123. var row = docs.Rows.FirstOrDefault();
  124. if (row != null)
  125. {
  126. var id = row.Get<Document, Guid>(x => x.ID);
  127. var ms = new MemoryStream(row.Get<Document, byte[]>(x => x.Data));
  128. var bmp = new Bitmap(ms);
  129. Dispatcher.BeginInvoke(
  130. new Action<Guid>(o =>
  131. {
  132. if (Items.SelectedValue != null)
  133. {
  134. var sel = (Product)Items.SelectedValue;
  135. if (sel.Image.ID == o)
  136. Image.Source = bmp.AsBitmapImage(false);
  137. }
  138. }), id
  139. );
  140. }
  141. }
  142. );
  143. else
  144. Image.Source = null;
  145. if (Security.CanView<StockLocation>())
  146. {
  147. _holdings.Product = item;
  148. _holdings.Refresh(false, true);
  149. }
  150. }
  151. else
  152. {
  153. Image.Source = null;
  154. }
  155. }
  156. private void Items_KeyUp(object sender, KeyEventArgs e)
  157. {
  158. }
  159. private void Items_KeyDown(object sender, KeyEventArgs e)
  160. {
  161. if (e.Key == Key.Up)
  162. if (Items.SelectedIndex == 0)
  163. Search.Focus();
  164. }
  165. private void ImageMenu_Opened(object sender, RoutedEventArgs e)
  166. {
  167. var pdi = Items.SelectedItem as Product;
  168. var anyproducts = pdi != null;
  169. var validimage = pdi != null && pdi.Image.ID != Guid.Empty;
  170. LoadImageFromFile.IsEnabled = anyproducts;
  171. SaveImageToFile.IsEnabled = validimage;
  172. CopyImageToClipboard.IsEnabled = validimage;
  173. PasteImageFromClipboard.IsEnabled = anyproducts && (Clipboard.ContainsData("ProductImage") || Clipboard.ContainsImage());
  174. ClearImage.IsEnabled = anyproducts;
  175. }
  176. private string SelectedProducts()
  177. {
  178. var product = Items.SelectedItem as Product;
  179. return product != null ? product.Code : "";
  180. }
  181. private void UpdateProductImages(Guid id, string filename, Bitmap? bitmap)
  182. {
  183. var product = Items.SelectedItem as Product;
  184. if (product == null)
  185. return;
  186. var docid = id;
  187. if (bitmap != null && docid == Guid.Empty)
  188. {
  189. byte[] data = null;
  190. using (var ms = new MemoryStream())
  191. {
  192. bitmap.Save(ms, ImageFormat.Png);
  193. data = ms.GetBuffer();
  194. }
  195. var crc = CoreUtils.CalculateCRC(data);
  196. var doc = new Client<Document>().Query(
  197. new Filter<Document>(x => x.FileName).IsEqualTo(Path.GetFileName(filename)).And(x => x.CRC).IsEqualTo(crc),
  198. new Columns<Document>(x => x.ID)
  199. ).Rows.FirstOrDefault()?.ToObject<Document>();
  200. if (doc == null)
  201. {
  202. doc = new Document
  203. {
  204. FileName = Path.GetFileName(filename),
  205. CRC = crc,
  206. TimeStamp = DateTime.Now,
  207. Data = data
  208. };
  209. new Client<Document>().Save(doc, "");
  210. }
  211. docid = doc.ID;
  212. }
  213. product.Image.ID = CoreUtils.FullGuid;
  214. product.CommitChanges();
  215. product.Image.ID = docid;
  216. product.Image.FileName = Path.GetFileName(filename);
  217. new Client<Product>().Save(product, "Cleared Product Image", (p, err) => { });
  218. Image.Source = bitmap?.AsBitmapImage();
  219. }
  220. private Bitmap? ConvertDXFFile(string filename)
  221. {
  222. Bitmap? result = null;
  223. using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  224. {
  225. //String newfile = Path.ChangeExtension(Path.GetFileName(filename), "png");
  226. try
  227. {
  228. result = DxfUtils.ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
  229. }
  230. catch (Exception e)
  231. {
  232. MessageWindow.ShowError("Could not load DXF file", e);
  233. result = null;
  234. }
  235. }
  236. return result;
  237. }
  238. private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
  239. {
  240. var product = Items.SelectedItem as Product;
  241. if (product == null)
  242. return;
  243. var dlg = new OpenFileDialog();
  244. dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
  245. if (dlg.ShowDialog() == true)
  246. {
  247. var filename = dlg.FileName.ToLower();
  248. Progress.Show("Updating Images: " + Path.GetFileName(filename));
  249. Bitmap? bmp = null;
  250. if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
  251. bmp = ConvertDXFFile(filename);
  252. else
  253. bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
  254. UpdateProductImages(Guid.Empty, filename, bmp);
  255. Progress.Close();
  256. MessageBox.Show(string.Format("Imported [{0}] into [{1}]", Path.GetFileName(dlg.FileName), SelectedProducts()));
  257. }
  258. }
  259. private void SaveImageToFile_Click(object sender, RoutedEventArgs e)
  260. {
  261. var product = Items.SelectedItem as Product;
  262. if (product == null)
  263. return;
  264. var bmp = (Image.Source as BitmapImage).AsBitmap();
  265. var dlg = new SaveFileDialog();
  266. dlg.Filter = "Image Files (*.png)|*.png";
  267. dlg.FileName = product.Image.FileName;
  268. if (dlg.ShowDialog() == true)
  269. bmp.Save(dlg.FileName);
  270. }
  271. private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
  272. {
  273. var product = Items.SelectedItem as Product;
  274. if (product == null)
  275. return;
  276. var bmp = (Image.Source as BitmapImage).AsBitmap();
  277. var data = new Tuple<Guid, string, Bitmap>(
  278. product.Image.ID,
  279. product.Image.FileName,
  280. bmp
  281. );
  282. Clipboard.SetData("ProductImage", data);
  283. }
  284. private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
  285. {
  286. if (Items.SelectedItem is not Product product)
  287. return;
  288. if (MessageBox.Show("Are you sure you wish to update the image for the selected product?", "Update Image", MessageBoxButton.YesNo,
  289. MessageBoxImage.Question) != MessageBoxResult.Yes)
  290. return;
  291. Progress.Show("Updating Images");
  292. var id = Guid.Empty;
  293. var filename = "";
  294. Bitmap? bitmap = null;
  295. if (Clipboard.ContainsData("ProductImage"))
  296. {
  297. if(Clipboard.GetData("ProductImage") is Tuple<Guid, string, Bitmap> data)
  298. {
  299. id = data.Item1;
  300. filename = data.Item2;
  301. bitmap = data.Item3;
  302. }
  303. }
  304. else if (Clipboard.ContainsImage())
  305. {
  306. var data = Clipboard.GetImage();
  307. bitmap = data.AsBitmap2();
  308. filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
  309. if (Clipboard.ContainsFileDropList())
  310. {
  311. var list = Clipboard.GetFileDropList();
  312. if (list.Count > 0)
  313. filename = Path.ChangeExtension(Path.GetFileName(list[0]), ".png");
  314. }
  315. //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
  316. //bitmap.Save(filename);
  317. id = Guid.Empty;
  318. }
  319. if (bitmap == null)
  320. {
  321. MessageBox.Show("Unable to paste data from clipboard");
  322. return;
  323. }
  324. Progress.Show("");
  325. UpdateProductImages(id, filename, bitmap);
  326. Progress.Close();
  327. MessageBox.Show(string.Format("Pasted [{0}] into [{1}]", Path.GetFileName(filename), SelectedProducts()));
  328. }
  329. private void ClearImage_Click(object sender, RoutedEventArgs e)
  330. {
  331. if (MessageWindow.ShowYesNo("Are you sure you wish to clear the image for the selected product?", "Clear Image"))
  332. return;
  333. Progress.Show("Clearing Image");
  334. UpdateProductImages(Guid.Empty, "", null);
  335. Progress.Close();
  336. MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
  337. }
  338. private void Grid_MouseMove(object sender, MouseEventArgs e)
  339. {
  340. if(sender is not Grid grid || grid.Tag is not Product product || e.LeftButton != MouseButtonState.Pressed)
  341. {
  342. return;
  343. }
  344. DragDrop.DoDragDrop(grid, product, DragDropEffects.Copy);
  345. }
  346. private void FilterButton_OnFilterRefresh()
  347. {
  348. Reload();
  349. }
  350. }