public IEnumerable <AppliedWorkspace> MakeLayouts(ILogicalWorkspace logicalWorkspace)
        {
            var imageSets = GetImageSets(logicalWorkspace);

            foreach (var imageSet in imageSets)
            {
                StoredLayout layout = LayoutSettingsHelper.MinimumLayout;
                foreach (IDisplaySet displaySet in imageSet.DisplaySets)
                {
                    if (displaySet.PresentationImages.Count <= 0)
                    {
                        continue;
                    }
                    StoredLayout storedLayout = LayoutSettingsHelper.GetLayout(displaySet.PresentationImages[0] as IImageSopProvider);
                    layout.ImageBoxRows    = Math.Max(layout.ImageBoxRows, storedLayout.ImageBoxRows);
                    layout.ImageBoxColumns = Math.Max(layout.ImageBoxColumns, storedLayout.ImageBoxColumns);
                    layout.TileRows        = Math.Max(layout.TileRows, storedLayout.TileRows);
                    layout.TileColumns     = Math.Max(layout.TileColumns, storedLayout.TileColumns);
                }
                var imageBoxCount          = layout.ImageBoxColumns * layout.ImageBoxRows;
                var displaySetPerWorkspace = imageSet.DisplaySets.Select((item, index) => new { index, item })
                                             .GroupBy(x => x.index / imageBoxCount)
                                             .Select(x => x.Select(y => y.item));

                var imageBoxLayout = new ImageBoxLayout()
                {
                    TileColumns = layout.TileColumns, TileRows = layout.TileRows
                };
                foreach (var displaySets in displaySetPerWorkspace)
                {
                    var workspaceDisplaySets = displaySets.Select((displaySet) => Tuple.Create(imageBoxLayout, displaySet)).ToList();
                    for (int i = workspaceDisplaySets.Count; i < imageBoxCount; i++)
                    {
                        workspaceDisplaySets.Add(Tuple.Create <ImageBoxLayout, IDisplaySet>(imageBoxLayout, null));
                    }
                    yield return(new AppliedWorkspace()
                    {
                        WorkspaceLayout =
                            new WorkspaceLayout()
                        {
                            Columns = layout.ImageBoxColumns,
                            Rows = layout.ImageBoxRows
                        },
                        DisplaySets = workspaceDisplaySets
                    });
                }
            }
        }
 public IDisplaySet FindDisplaySet(HangingProtocol hangingProtocol, IImageSet imageSet, ImageBoxLayout imageBox)
 {
     if (null == imageSet)
     {
         return(null);
     }
     foreach (var displaySet in imageSet.DisplaySets)
     {
         foreach (var presentationImage in displaySet.PresentationImages)
         {
             var filter = hangingProtocol.GetImageFilter(imageBox.FilterName);
             if (null == filter ||
                 filter.Test(presentationImage).Success)
             {
                 return(displaySet);
             }
         }
     }
     return(null);
 }
        public IEnumerable <AppliedWorkspace> MakeLayouts(ILogicalWorkspace logicalWorkspace)
        {
            var primaryStudy    = logicalWorkspace.ImageViewer.StudyTree.Studies.First();
            var hangingProtocol = hangingProtocolsContainer.Value.HangingProtocols.Find(h => h.StudyFilter.Test(primaryStudy).Success);

            if (null == hangingProtocol)
            {
                throw new ArgumentException(string.Format(SR.MessageNoMatchingHangingProtocol, primaryStudy.ParentPatient, primaryStudy));
            }

            var imageSets           = GetImageSets(logicalWorkspace, hangingProtocol);
            var primaryImageSet     = imageSets.First();
            var residualDisplaySets = primaryImageSet.DisplaySets.ToList();
            var studyHasPriors      = imageSets.Count() > 1;
            var priorImageSets      = studyHasPriors ? imageSets.Skip(1) : new List <IImageSet>()
            {
                null
            };

            foreach (var iw in priorImageSets.Zip(
                         OneWorkspacesWithAndTheRestWithoutPrimary(hangingProtocol.Workspaces), (i, w) => new { Workspaces = w, priorImageSet = i }))
            {
                foreach (var workspace in iw.Workspaces)
                {
                    if (workspace.ImageBoxes.Count != workspace.Rows * workspace.Columns)
                    {
                        throw new ArgumentException(string.Format(SR.MessageAmountOfImageBoxesMismatch, workspace.ImageBoxes.Count, workspace.Rows, workspace.Columns, workspace.Name));
                    }
                    var appliedWorkspace = new AppliedWorkspace()
                    {
                        WorkspaceLayout = workspace
                    };
                    bool atLeastOneMatchingDisplaySet = false;
                    if (!studyHasPriors && workspace.ImageBoxes.Any(i => i.PriorStudy))
                    {
                        continue;
                    }

                    foreach (var imageBox in workspace.ImageBoxes)
                    {
                        var imageSet   = imageBox.PriorStudy ? iw.priorImageSet : primaryImageSet;
                        var displaySet = FindDisplaySet(hangingProtocol, imageSet, imageBox);
                        if (null != displaySet)
                        {
                            residualDisplaySets.Remove(displaySet);
                            atLeastOneMatchingDisplaySet = true;
                        }
                        else
                        {
                            displaySet = MessagePresentationImage.CreateDisplaySet(
                                string.Format(SR.MessageNoMatchingDisplaySet, imageBox.FilterName, null != imageSet ? imageSet.Name : string.Empty, workspace.Name));
                        }
                        appliedWorkspace.DisplaySets.Add(new Tuple <ImageBoxLayout, IDisplaySet>(imageBox, displaySet));
                    }
                    if (!workspace.Optional || atLeastOneMatchingDisplaySet)
                    {
                        yield return(appliedWorkspace);
                    }
                }
            }

            if (hangingProtocol.ShowResidualWorkspace && 0 != residualDisplaySets.Count)
            {
                var workspace = new WorkspaceLayout()
                {
                    Columns = hangingProtocol.ResidualWorkspaceColumns,
                    Rows    = hangingProtocol.ResidualWorkspaceRows
                };
                var residualImageBoxCount = workspace.Columns * workspace.Rows;
                var imageBox         = new ImageBoxLayout();
                var appliedWorkspace = new AppliedWorkspace()
                {
                    WorkspaceLayout = workspace
                };
                var index = 0;

                var missingImageBoxCount = residualImageBoxCount - (residualDisplaySets.Count % residualImageBoxCount);
                for (int i = 0; i < missingImageBoxCount; i++)
                {
                    residualDisplaySets.Add(null);
                }

                foreach (var displaySet in residualDisplaySets)
                {
                    appliedWorkspace.DisplaySets.Add(new Tuple <ImageBoxLayout, IDisplaySet>(imageBox, displaySet));
                    index++;
                    if (index % residualImageBoxCount == 0)
                    {
                        yield return(appliedWorkspace);

                        appliedWorkspace = new AppliedWorkspace()
                        {
                            WorkspaceLayout = workspace
                        };
                    }
                }
            }
        }