Example #1
0
        public void Execute()
        {
            using (var manager = new ApplicationManager())
            {
                var section = manager.CurrentSection();
                ns = section.GetNamespaceOfPrefix("one");

                // find first selected - active page

                var active =
                    section.Elements(ns + "Page")
                    .Where(e => e.Attributes("isCurrentlyViewed").Any(a => a.Value.Equals("true")))
                    .FirstOrDefault();

                if (active == null)
                {
                    UIHelper.ShowMessage(manager.Window, "At least two pages must be selected to merge");
                    return;
                }

                var selections =
                    section.Elements(ns + "Page")
                    .Where(e =>
                           e.Attributes("isCurrentlyViewed").Count() == 0 &&
                           e.Attributes("selected").Any(a => a.Value.Equals("all")));

                if (active == null)
                {
                    UIHelper.ShowMessage(manager.Window, "At least two pages must be selected to merge");
                    return;
                }


                // get first selected (active) page and reference its quick styles, outline, size

                page = manager.GetPage(active.Attribute("ID").Value);

                quickies = page.Elements(ns + "QuickStyleDef")
                           .Select(p => new QuickRef(p))
                           .ToList();

                var offset = GetPageBottomOffset();

                // track running bottom as we add new outlines
                var maxOffset = offset;

                // find maximum z-offset
                var z = page.Elements(ns + "Outline").Elements(ns + "Position")
                        .Attributes("z").Max(a => int.Parse(a.Value)) + 1;

                // merge each of the subsequently selected pages into the active page

                foreach (var selection in selections.ToList())
                {
                    var childPage = manager.GetPage(selection.Attribute("ID").Value);

                    var styles = MergeQuickStyles(childPage);

                    var topOffset = childPage.Elements(ns + "Outline")
                                    .Elements(ns + "Position").Min(p => double.Parse(p.Attribute("y").Value));

                    foreach (var childOutline in childPage.Elements(ns + "Outline"))
                    {
                        // adjust position relative to new parent page outlines
                        var position = childOutline.Elements(ns + "Position").FirstOrDefault();
                        var y        = double.Parse(position.Attribute("y").Value) - topOffset + offset + OutlineMargin;
                        position.Attribute("y").Value = y.ToString("#0.0");

                        // keep track of lowest bottom
                        var size   = childOutline.Elements(ns + "Size").FirstOrDefault();
                        var bottom = y + double.Parse(size.Attribute("height").Value);
                        if (bottom > maxOffset)
                        {
                            maxOffset = bottom;
                        }

                        position.Attribute("z").Value = z.ToString();
                        z++;

                        // remove its IDs so the page can apply its own
                        childOutline.Attributes("objectID").Remove();
                        childOutline.Descendants().Attributes("objectID").Remove();

                        AdjustQuickStyles(styles, childOutline);

                        page.Add(childOutline);
                    }

                    if (maxOffset > offset)
                    {
                        offset = maxOffset;
                    }
                }

                // update page and section hierarchy

                manager.UpdatePageContent(page);

                foreach (var selection in selections)
                {
                    manager.DeleteHierarchy(selection.Attribute("ID").Value);
                }
            }
        }