Browse Source

Added Hires Image Retrieval to DynamicDocumentGrid

frogsoftware 11 tháng trước cách đây
mục cha
commit
ef7811902d
1 tập tin đã thay đổi với 43 bổ sung15 xóa
  1. 43 15
      inabox.wpf/DynamicGrid/DynamicDocumentGrid.cs

+ 43 - 15
inabox.wpf/DynamicGrid/DynamicDocumentGrid.cs

@@ -8,6 +8,7 @@ using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Media;
+using System.Windows.Media.Imaging;
 using InABox.Clients;
 using InABox.Core;
 using InABox.WPF;
@@ -39,25 +40,12 @@ namespace InABox.DynamicGrid
                 : Set;
         }
     }
-
+    
     public class DynamicDocumentGrid<TDocument, TEntity, TEntityLink> : DynamicManyToManyGrid<TDocument, TEntity>
         where TEntity : Entity, IPersistent, IRemotable, new()
         where TDocument : Entity, IEntityDocument<TEntityLink>, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany<TEntity, Document>, new()
         where TEntityLink : EntityLink<TEntity>, new()
     {
-        // private DynamicActionColumn supercedecolumn;
-        //
-        // public bool ShowSupercededColumn
-        // {
-        //     get
-        //     {
-        //         return supercedecolumn.Position != DynamicActionColumnPosition.Hidden;
-        //     }
-        //     set
-        //     {
-        //         supercedecolumn.Position = value ? DynamicActionColumnPosition.End : DynamicActionColumnPosition.Hidden;
-        //     }
-        // }
         
         public bool ShowSupercededColumn { get; set; }
 
@@ -289,7 +277,7 @@ namespace InABox.DynamicGrid
         {
             Grid grid = new Grid()
             {
-                Height = 150,
+                //Height = 150,
                 ContextMenu = CreateContextMenu(),
                 RowDefinitions = 
                 {
@@ -306,6 +294,8 @@ namespace InABox.DynamicGrid
             {
                 Stretch = Stretch.Uniform,
                 Margin = new Thickness(5),
+                //HorizontalAlignment = HorizontalAlignment.Stretch,
+                //VerticalAlignment = VerticalAlignment.Stretch
             };
                 
             thumbnail.SetBinding(Image.SourceProperty, new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
@@ -573,5 +563,43 @@ namespace InABox.DynamicGrid
                 }
             }
         }
+
+        protected override void Reload(Filters<TDocument> criteria, Columns<TDocument> columns, ref SortOrder<TDocument>? sort, Action<CoreTable?, Exception?> action)
+        {
+            base.Reload(criteria, columns, ref sort, (t,e) =>
+            {
+                action(t,e);
+                
+                // Download Hi Res images in the background and replace them when available
+                if (t != null && SimpleTemplate)
+                {
+                    var ids = t.ExtractValues<TDocument, Guid>(x => x.DocumentLink.ID).Distinct().ToArray();
+                    Client.Query(
+                        new Filter<Document>(x => x.ID).InList(ids),
+                        Columns.None<Document>().Add(x => x.ID).Add(x => x.Data),
+                        null,
+                        (d, _) =>
+                        {
+                            if (d == null)
+                                return;
+                            var docs = d.ToDictionary<Document, Guid, byte[]>(x => x.ID, x => x.Data);
+                            foreach (var row in t.Rows)
+                            {
+                                if (docs.TryGetValue(row.Get<TDocument, Guid>(x => x.DocumentLink.ID),
+                                        out byte[]? data) && (data?.Any() == true))
+                                {
+                                    if (ImageUtils.IsPdf(data))
+                                        data = ImageUtils.PDFToBitmap(data, 0);
+                                    row.Set<TDocument, byte[]>(x => x.Thumbnail!, data);
+                                }
+                            }
+                            Dispatcher.BeginInvoke(() => Refresh(false,false));
+                        }
+                    );
+                }
+
+
+            });
+        }
     }
 }