ImageEditorControl.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media.Imaging;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.WPF;
  11. namespace InABox.DynamicGrid
  12. {
  13. /// <summary>
  14. /// Interaction logic for ImageEditorControl.xaml
  15. /// </summary>
  16. public partial class ImageEditorControl : UserControl, IDocumentEditor
  17. {
  18. private IEntityDocument _document;
  19. private byte[] documentdata;
  20. public ImageEditorControl()
  21. {
  22. InitializeComponent();
  23. Editor.ToolbarSettings.IsToolbarVisiblity = false;
  24. }
  25. public IEntityDocument Document
  26. {
  27. get => _document;
  28. set
  29. {
  30. UnloadImage();
  31. _document = value;
  32. LoadImage();
  33. //PDFDocumentLoaded?.Invoke(this, value);
  34. }
  35. }
  36. private bool DownloadNeeded(Guid id)
  37. {
  38. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", id.ToString()));
  39. if (!File.Exists(cachefile))
  40. return true;
  41. var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json");
  42. if (!File.Exists(indexfile))
  43. return true;
  44. var json = File.ReadAllText(indexfile);
  45. var index = Serialization.Deserialize<Dictionary<Guid, string>>(json);
  46. if (!index.ContainsKey(id))
  47. return true;
  48. var entry = new Client<Document>().Query(
  49. new Filter<Document>(x => x.ID).IsEqualTo(id),
  50. new Columns<Document>(x => x.CRC)
  51. ).Rows.FirstOrDefault();
  52. if (entry == null)
  53. return true;
  54. if (!entry.Get<Document, string>(x => x.CRC).Equals(index[id]))
  55. {
  56. MessageBox.Show("This document has been revised, and will now refresh.\n\nPlease check the drawing for any applicable changes.");
  57. return true;
  58. }
  59. return false;
  60. }
  61. private void LoadImage()
  62. {
  63. if (_document == null)
  64. return;
  65. var cachefile = Path.Combine(CoreUtils.GetPath(), string.Format("{0}.ffd", _document.DocumentLink.ID.ToString()));
  66. using (new WaitCursor())
  67. {
  68. if (DownloadNeeded(_document.DocumentLink.ID))
  69. {
  70. var dbdoc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(_document.DocumentLink.ID)).FirstOrDefault();
  71. if (dbdoc == null)
  72. {
  73. MessageBox.Show("Unable to Load File!");
  74. return;
  75. }
  76. var indexfile = Path.Combine(CoreUtils.GetPath(), "pdfindex.json");
  77. var index = new Dictionary<Guid, string>();
  78. if (File.Exists(indexfile))
  79. {
  80. var json = File.ReadAllText(indexfile);
  81. index = Serialization.Deserialize<Dictionary<Guid, string>>(json);
  82. }
  83. index[_document.DocumentLink.ID] = dbdoc.CRC;
  84. File.WriteAllText(indexfile, Serialization.Serialize(index));
  85. documentdata = dbdoc.Data;
  86. File.WriteAllBytes(cachefile, documentdata);
  87. }
  88. else
  89. {
  90. documentdata = File.ReadAllBytes(cachefile);
  91. }
  92. var img = new BitmapImage();
  93. img.LoadImage(documentdata);
  94. Editor.ImageSource = img;
  95. }
  96. }
  97. private void UnloadImage()
  98. {
  99. if (Editor.ImageSource != null)
  100. Editor.ImageSource = null;
  101. }
  102. }
  103. }