ProductLookupDock.xaml.cs 14 KB

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