Esempio n. 1
0
        /// <summary>Creates the bibliography.</summary>
        /// <param name="tags">The tags to add to.</param>
        private void CreateBibliography(List <AutoDocumentation.ITag> tags)
        {
            if (citations.Count > 0)
            {
                // Create the heading.
                tags.Add(new AutoDocumentation.Heading("References", 1));

                citations.Sort(new BibTeX.CitationComparer());
                foreach (BibTeX.Citation citation in citations)
                {
                    string url = citation.URL;
                    string text;
                    if (url != string.Empty)
                    {
                        text = string.Format("<a href=\"{0}\">{1}</a>", url, citation.BibliographyText);
                    }
                    else
                    {
                        text = citation.BibliographyText;
                    }

                    AutoDocumentation.Paragraph paragraph = new AutoDocumentation.Paragraph(text, 0);
                    paragraph.bookmarkName  = citation.Name;
                    paragraph.handingIndent = true;
                    tags.Add(paragraph);
                }
            }
        }
Esempio n. 2
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            string html = markDown.Transform(paragraph.text);

            HtmlToMigraDoc.Convert(html, section, WorkingDirectory);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent += Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 3
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            string html = Markdown.ToHtml(paragraph.text, new MarkdownPipelineBuilder().UsePipeTables().UseEmphasisExtras().Build());

            HtmlToMigraDoc.Convert(html, section, WorkingDirectory, RelativePath);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent += Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 4
0
        /// <summary>Adds a formatted paragraph to section.</summary>
        /// <param name="section">The section.</param>
        /// <param name="paragraph">The paragraph.</param>
        private void AddFormattedParagraphToSection(Section section, AutoDocumentation.Paragraph paragraph)
        {
            MarkdownDeep.Markdown markDown = new MarkdownDeep.Markdown();
            markDown.ExtraMode = true;
            string html = markDown.Transform(paragraph.text);

            string imageDirectory = Path.GetDirectoryName(ExplorerPresenter.ApsimXFile.FileName);

            HtmlToMigraDoc.Convert(html, section, imageDirectory);

            Paragraph para = section.LastParagraph;

            para.Format.LeftIndent = Unit.FromCentimeter(paragraph.indent);
            if (paragraph.bookmarkName != null)
            {
                para.AddBookmark(paragraph.bookmarkName);
            }
            if (paragraph.handingIndent)
            {
                para.Format.LeftIndent      = "1cm";
                para.Format.FirstLineIndent = "-1cm";
            }
        }
Esempio n. 5
0
        /// <summary>
        /// For each image markdown tag in a paragraph, locate image from resource and save to temp folder.
        /// </summary>
        /// <param name="paragraph">The paragraph to scan.</param>
        private void FindImagesInParagraph(AutoDocumentation.Paragraph paragraph)
        {
            var regEx = new Regex(@"!\[(.+)\]\((.+)\)");

            foreach (Match match in regEx.Matches(paragraph.text))
            {
                var  fileName     = match.Groups[2].ToString();
                var  tempFileName = Path.Combine(Path.GetTempPath(), fileName);
                bool createImage  = true;
                if (File.Exists(tempFileName))
                {
                    var timeSinceLastAccess = DateTime.Now - File.GetLastAccessTime(tempFileName);
                    createImage = timeSinceLastAccess.Hours > 1;
                }
                if (createImage)
                {
                    using (FileStream file = new FileStream(tempFileName, FileMode.Create, FileAccess.Write))
                    {
                        var imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"ApsimNG.Resources.{fileName}");
                        imageStream?.CopyTo(file);
                    }
                }
            }
        }
Esempio n. 6
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        para.Format.KeepWithNext = true;
                        int posSpace = heading.text.IndexOf(' ');
                        if (posSpace > 0)
                        {
                            para.AddBookmark(heading.text.Substring(posSpace + 1));
                        }
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    explorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor        = OxyPlot.OxyColors.White;
                    graphView.ForegroundColour = OxyPlot.OxyColors.Black;
                    graphView.FontSize         = 12;
                    graphView.Width            = 500;
                    graphView.Height           = 500;
                    graphPresenter.Attach(tag, graphView, explorerPresenter);
                    string pngFileName = graphPresenter.ExportToPNG(WorkingDirectory);
                    section.AddImage(pngFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                    graphView.MainWidget.Destroy();
                }
                else if (tag is Map && (tag as Map).GetCoordinates().Count > 0)
                {
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView(null);
                    mapPresenter.Attach(tag, mapView, explorerPresenter);
                    string pngFileName = mapPresenter.ExportToPNG(WorkingDirectory);
                    if (!String.IsNullOrEmpty(pngFileName))
                    {
                        section.AddImage(pngFileName);
                    }
                    mapPresenter.Detach();
                    mapView.MainWidget.Destroy();
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    if (imageTag.image.Width > 700)
                    {
                        imageTag.image = ImageUtilities.ResizeImage(imageTag.image, 700, 500);
                    }
                    string pngFileName = Path.Combine(WorkingDirectory, imageTag.name);
                    imageTag.image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddImage(pngFileName);
                    figureNumber++;
                }
                else if (tag is AutoDocumentation.ModelView)
                {
                    AutoDocumentation.ModelView modelView     = tag as AutoDocumentation.ModelView;
                    ViewNameAttribute           viewName      = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(ViewNameAttribute), false) as ViewNameAttribute;
                    PresenterNameAttribute      presenterName = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(PresenterNameAttribute), false) as PresenterNameAttribute;
                    if (viewName != null && presenterName != null)
                    {
                        ViewBase owner = ViewBase.MasterView as ViewBase;
                        if (viewName.ToString() == "UserInterface.Views.MapView")
                        {
                            owner = null;
                        }

                        ViewBase   view      = Assembly.GetExecutingAssembly().CreateInstance(viewName.ToString(), false, BindingFlags.Default, null, new object[] { owner }, null, null) as ViewBase;
                        IPresenter presenter = Assembly.GetExecutingAssembly().CreateInstance(presenterName.ToString()) as IPresenter;

                        if (view != null && presenter != null)
                        {
                            explorerPresenter.ApsimXFile.Links.Resolve(presenter);
                            presenter.Attach(modelView.model, view, explorerPresenter);

                            Gtk.Window popupWin = new Gtk.Window(Gtk.WindowType.Popup);
                            popupWin.SetSizeRequest(800, 800);
                            popupWin.Add(view.MainWidget);

                            if (view is IMapView map)
                            {
                                map.HideZoomControls();
                            }

                            popupWin.ShowAll();

                            while (Gtk.Application.EventsPending())
                            {
                                Gtk.Application.RunIteration();
                            }

                            // From MapView:
                            // With WebKit, it appears we need to give it time to actually update the display
                            // Really only a problem with the temporary windows used for generating documentation
                            if (view is MapView)
                            {
                                var watch = new System.Diagnostics.Stopwatch();
                                watch.Start();
                                while (watch.ElapsedMilliseconds < 1000)
                                {
                                    Gtk.Application.RunIteration();
                                }
                            }

                            string pngFileName = (presenter as IExportable).ExportToPNG(WorkingDirectory);
                            section.AddImage(pngFileName);
                            presenter.Detach();
                            view.MainWidget.Destroy();
                            popupWin.Destroy();
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>Scans for citations.</summary>
        /// <param name="t">The tags to go through looking for citations.</param>
        private void ScanForCitations(List <AutoDocumentation.ITag> tags)
        {
            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    string text = paragraph.text;

                    // citations are of the form [Brown et al. 2014][brown_plant_2014]
                    // where the second bracketed value is the bibliography reference name. i.e.
                    // the bit we're interested in.
                    int posBracket = text.IndexOf('[');
                    while (posBracket != -1)
                    {
                        int posEndBracket = text.IndexOf(']', posBracket);
                        if (posEndBracket != -1)
                        {
                            // found a possible citation.
                            string   citationName    = text.Substring(posBracket + 1, posEndBracket - posBracket - 1);
                            string[] inTextCitations = citationName.Split("; ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                            string   replacementText = string.Empty;

                            foreach (string inTextCitation in inTextCitations)
                            {
                                // see if we have already encountered the citation.
                                BibTeX.Citation citation = citations.Find(c => c.Name == inTextCitation);

                                // If we haven't encountered it, look it up in the .bib file.
                                if (citation == null)
                                {
                                    citation = bibTeX.Lookup(inTextCitation);
                                    if (citation != null)
                                    {
                                        citations.Add(citation);
                                    }
                                }

                                if (citation != null)
                                {
                                    // Replace the in-text citation with (author et al., year)
                                    if (replacementText != string.Empty)
                                    {
                                        replacementText += "; ";
                                    }
                                    replacementText += string.Format("<a href=\"#{0}\">{1}</a>", citation.Name, citation.InTextCite);
                                }
                            }

                            if (replacementText != string.Empty)
                            {
                                text = text.Remove(posBracket, posEndBracket - posBracket + 1);
                                text = text.Insert(posBracket, replacementText);
                            }
                        }

                        // Find the next bracketed potential citation.
                        posBracket = text.IndexOf('[', posEndBracket + 1);
                    }

                    paragraph.text = text;
                }
            }
        }
Esempio n. 8
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        /// <param name="workingDirectory">The working directory.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags, string workingDirectory)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        para.Format.KeepWithNext = true;
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage, workingDirectory);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table, workingDirectory);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    explorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor        = OxyPlot.OxyColors.White;
                    graphView.ForegroundColour = OxyPlot.OxyColors.Black;
                    graphView.FontSize         = 12;
                    graphView.Width            = 500;
                    graphView.Height           = 500;
                    graphPresenter.Attach(tag, graphView, explorerPresenter);
                    string pngFileName = graphPresenter.ExportToPNG(workingDirectory);
                    section.AddImage(pngFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                    graphView.MainWidget.Destroy();
                }
                else if (tag is Map && (tag as Map).GetCoordinates().Count > 0)
                {
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView(null);
                    mapPresenter.Attach(tag, mapView, explorerPresenter);
                    string pngFileName = mapPresenter.ExportToPNG(workingDirectory);
                    if (!String.IsNullOrEmpty(pngFileName))
                    {
                        section.AddImage(pngFileName);
                    }
                    mapPresenter.Detach();
                    mapView.MainWidget.Destroy();
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    if (imageTag.image.Width > 700)
                    {
                        imageTag.image = ImageUtilities.ResizeImage(imageTag.image, 700, 500);
                    }
                    string pngFileName = Path.Combine(workingDirectory, imageTag.name);
                    imageTag.image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddImage(pngFileName);
                    figureNumber++;
                }
                else if (tag is AutoDocumentation.ModelView)
                {
                    AutoDocumentation.ModelView modelView     = tag as AutoDocumentation.ModelView;
                    ViewNameAttribute           viewName      = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(ViewNameAttribute), false) as ViewNameAttribute;
                    PresenterNameAttribute      presenterName = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(PresenterNameAttribute), false) as PresenterNameAttribute;
                    if (viewName != null && presenterName != null)
                    {
                        ViewBase   view      = Assembly.GetExecutingAssembly().CreateInstance(viewName.ToString(), false, BindingFlags.Default, null, new object[] { ViewBase.MasterView }, null, null) as ViewBase;
                        IPresenter presenter = Assembly.GetExecutingAssembly().CreateInstance(presenterName.ToString()) as IPresenter;

                        if (view != null && presenter != null)
                        {
                            explorerPresenter.ApsimXFile.Links.Resolve(presenter);
                            presenter.Attach(modelView.model, view, explorerPresenter);

                            Gtk.Window popupWin = null;
                            if (view is MapView)
                            {
                                popupWin = (view as MapView)?.GetPopupWin();
                                popupWin?.SetSizeRequest(515, 500);
                            }
                            if (popupWin == null)
                            {
                                popupWin = new Gtk.Window(Gtk.WindowType.Popup);
                                popupWin.SetSizeRequest(800, 800);
                                popupWin.Add(view.MainWidget);
                            }
                            popupWin.ShowAll();
                            while (Gtk.Application.EventsPending())
                            {
                                Gtk.Application.RunIteration();
                            }

                            string pngFileName = (presenter as IExportable).ExportToPNG(workingDirectory);
                            section.AddImage(pngFileName);
                            presenter.Detach();
                            view.MainWidget.Destroy();
                            popupWin.Destroy();
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        para.Format.KeepWithNext = true;
                        int posSpace = heading.text.IndexOf(' ');
                        if (posSpace > 0)
                        {
                            para.AddBookmark(heading.text.Substring(posSpace + 1));
                        }
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                    if ((tag as AutoDocumentation.NewPage).Portrait)
                    {
                        section.PageSetup.Orientation = Orientation.Portrait;
                    }
                    else
                    {
                        section.PageSetup.Orientation = Orientation.Landscape;
                    }
                }
                else if (tag is AutoDocumentation.PageSetup)
                {
                    if ((tag as AutoDocumentation.PageSetup).Portrait)
                    {
                        section.PageSetup.Orientation = Orientation.Portrait;
                    }
                    else
                    {
                        section.PageSetup.Orientation = Orientation.Landscape;
                    }
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    explorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor        = OxyPlot.OxyColors.White;
                    graphView.ForegroundColour = OxyPlot.OxyColors.Black;
                    graphView.FontSize         = 12;
                    graphView.Width            = 500;
                    graphView.Height           = 500;
                    graphPresenter.Attach(tag, graphView, explorerPresenter);
                    string pngFileName = graphPresenter.ExportToPNG(WorkingDirectory);
                    section.AddResizeImage(pngFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                    graphView.MainWidget.Cleanup();
                }
                else if (tag is Map && (tag as Map).GetCoordinates().Count > 0)
                {
#if NETFRAMEWORK
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView(null);
                    mapPresenter.Attach(tag, mapView, explorerPresenter);
                    var    map         = mapView.Export();
                    string pngFileName = Path.ChangeExtension(Path.GetTempFileName(), ".png");
                    if (map.Width > section.PageSetup.PageWidth)
                    {
                        map = ImageUtilities.ResizeImage(map, section.PageSetup.PageWidth, double.MaxValue);
                    }
                    map.Save(pngFileName);
                    if (!String.IsNullOrEmpty(pngFileName))
                    {
                        section.AddResizeImage(pngFileName);
                    }
                    mapPresenter.Detach();
                    mapView.MainWidget.Destroy();
#else
                    section.AddParagraph("MapView has not been implemented in gtk3. Use the framework/gtk2 build instead.");
#endif
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    string pngFileName = Path.Combine(WorkingDirectory, $"{imageTag.name}.png");
                    imageTag.image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddResizeImage(pngFileName);
                    figureNumber++;
                }
                else if (tag is AutoDocumentation.ModelView)
                {
                    try
                    {
                        AutoDocumentation.ModelView modelView     = tag as AutoDocumentation.ModelView;
                        ViewNameAttribute           viewName      = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(ViewNameAttribute), false) as ViewNameAttribute;
                        PresenterNameAttribute      presenterName = ReflectionUtilities.GetAttribute(modelView.model.GetType(), typeof(PresenterNameAttribute), false) as PresenterNameAttribute;
                        if (viewName != null && presenterName != null)
                        {
                            ViewBase owner = ViewBase.MasterView as ViewBase;
                            if (viewName.ToString() == "UserInterface.Views.MapView")
                            {
                                owner = null;
                            }

                            ViewBase   view      = Assembly.GetExecutingAssembly().CreateInstance(viewName.ToString(), false, BindingFlags.Default, null, new object[] { owner }, null, null) as ViewBase;
                            IPresenter presenter = Assembly.GetExecutingAssembly().CreateInstance(presenterName.ToString()) as IPresenter;

                            if (view != null && presenter != null)
                            {
                                explorerPresenter.ApsimXFile.Links.Resolve(presenter);
                                presenter.Attach(modelView.model, view, explorerPresenter);

#if NETFRAMEWORK
                                Gtk.Window popupWin = new Gtk.Window(Gtk.WindowType.Popup);
#endif
                                try
                                {
#if NETFRAMEWORK
                                    popupWin.SetSizeRequest(700, 700);
                                    popupWin.Add(view.MainWidget);
#endif

                                    if (view is MapView map)
                                    {
                                        map.HideZoomControls();
                                    }
#if NETFRAMEWORK
                                    popupWin.ShowAll();
#endif
                                    while (Gtk.Application.EventsPending())
                                    {
                                        Gtk.Application.RunIteration();
                                    }

                                    // From MapView:
                                    // With WebKit, it appears we need to give it time to actually update the display
                                    // Really only a problem with the temporary windows used for generating documentation
                                    string pngFileName;
                                    if (view is MapView mapView)
                                    {
                                        var img = mapView.Export();
                                        pngFileName = Path.ChangeExtension(Path.GetTempFileName(), ".png");
                                        if (section.PageSetup.PageWidth > 0 && img.Width > section.PageSetup.PageWidth)
                                        {
                                            img = ImageUtilities.ResizeImage(img, section.PageSetup.PageWidth, double.MaxValue);
                                        }
                                        img.Save(pngFileName);
                                    }
                                    else
                                    {
                                        pngFileName = (presenter as IExportable).ExportToPNG(WorkingDirectory);
                                    }
                                    section.AddResizeImage(pngFileName);
                                    presenter.Detach();
                                    view.MainWidget.Cleanup();
                                }
                                finally
                                {
#if NETFRAMEWORK
                                    popupWin.Cleanup();
#endif
                                    while (GLib.MainContext.Iteration())
                                    {
                                        ;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine(err);
                    }
                }
            }
        }
Esempio n. 10
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        /// <param name="workingDirectory">The working directory.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags, string workingDirectory)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        //if (heading.headingLevel == 1)
                        //    section.AddPageBreak();

                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage, workingDirectory);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table, workingDirectory);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    ExplorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor = OxyPlot.OxyColors.White;
                    graphView.FontSize  = 12;
                    graphView.Width     = 500;
                    graphView.Height    = 500;
                    graphPresenter.Attach(tag, graphView, ExplorerPresenter);
                    string PNGFileName = graphPresenter.ExportToPDF(workingDirectory);
                    section.AddImage(PNGFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                    graphView.MainWidget.Destroy();
                }
                else if (tag is Map && (tag as Map).GetCoordinates().Count > 0)
                {
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView(null);
                    mapPresenter.Attach(tag, mapView, ExplorerPresenter);
                    string PNGFileName = mapPresenter.ExportToPDF(workingDirectory);
                    if (!String.IsNullOrEmpty(PNGFileName))
                    {
                        section.AddImage(PNGFileName);
                    }
                    mapPresenter.Detach();
                    mapView.MainWidget.Destroy();
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    if (imageTag.image.Width > 700)
                    {
                        imageTag.image = ImageUtilities.ResizeImage(imageTag.image, 700, 500);
                    }
                    string PNGFileName = Path.Combine(workingDirectory, imageTag.name);
                    imageTag.image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddImage(PNGFileName);
                    figureNumber++;
                }
                else if (tag is DirectedGraph)
                {
                    DirectedGraphPresenter presenter = new DirectedGraphPresenter();
                    ExplorerPresenter.ApsimXFile.Links.Resolve(presenter);
                    DirectedGraphView view = new DirectedGraphView();
                    presenter.Attach(new DirectedGraphContainer(tag as DirectedGraph), view, ExplorerPresenter);

                    Gtk.Window popupWin = new Gtk.Window(Gtk.WindowType.Popup);
                    popupWin.SetSizeRequest(800, 800);
                    // Move the window offscreen; the user doesn't need to see it.
                    // This works with IE, but not with WebKit
                    // Not yet tested on OSX
                    if (ProcessUtilities.CurrentOS.IsWindows)
                    {
                        popupWin.Move(-10000, -10000);
                    }
                    popupWin.Add(view.MainWidget);
                    popupWin.ShowAll();
                    while (Gtk.Application.EventsPending())
                    {
                        Gtk.Application.RunIteration();
                    }

                    string PNGFileName = presenter.ExportToPNG(workingDirectory);
                    section.AddImage(PNGFileName);
                    presenter.Detach();
                    view.MainWidget.Destroy();
                }
            }
        }
Esempio n. 11
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        /// <param name="workingDirectory">The working directory.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags, string workingDirectory)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        //if (heading.headingLevel == 1)
                        //    section.AddPageBreak();

                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage, workingDirectory);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table, workingDirectory);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    ExplorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor = OxyPlot.OxyColors.White;
                    graphView.FontSize  = 12;
                    graphView.Width     = 500;
                    graphView.Height    = 500;
                    graphPresenter.Attach(tag, graphView, ExplorerPresenter);
                    string PNGFileName = graphPresenter.ExportToPDF(workingDirectory);
                    section.AddImage(PNGFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                    graphView.MainWidget.Destroy();
                }
                else if (tag is Map && (tag as Map).GetCoordinates().Count > 0)
                {
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView(null);
                    mapPresenter.Attach(tag, mapView, ExplorerPresenter);
                    string PNGFileName = mapPresenter.ExportToPDF(workingDirectory);
                    if (!String.IsNullOrEmpty(PNGFileName))
                    {
                        section.AddImage(PNGFileName);
                    }
                    mapPresenter.Detach();
                    mapView.MainWidget.Destroy();
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    if (imageTag.image.Width > 700)
                    {
                        imageTag.image = ImageUtilities.ResizeImage(imageTag.image, 700, 500);
                    }
                    string PNGFileName = Path.Combine(workingDirectory, imageTag.name);
                    imageTag.image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddImage(PNGFileName);
                    figureNumber++;
                }
            }
        }
Esempio n. 12
0
        /// <summary>Writes PDF for specified auto-doc commands.</summary>
        /// <param name="section">The writer to write to.</param>
        /// <param name="tags">The autodoc tags.</param>
        /// <param name="workingDirectory">The working directory.</param>
        private void TagsToMigraDoc(Section section, List <AutoDocumentation.ITag> tags, string workingDirectory)
        {
            int figureNumber = 0;

            foreach (AutoDocumentation.ITag tag in tags)
            {
                if (tag is AutoDocumentation.Heading)
                {
                    AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading;
                    if (heading.headingLevel > 0 && heading.headingLevel <= 6)
                    {
                        //if (heading.headingLevel == 1)
                        //    section.AddPageBreak();

                        Paragraph para = section.AddParagraph(heading.text, "Heading" + heading.headingLevel);
                        if (heading.headingLevel == 1)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level1;
                        }
                        else if (heading.headingLevel == 2)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level2;
                        }
                        else if (heading.headingLevel == 3)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level3;
                        }
                        else if (heading.headingLevel == 4)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level4;
                        }
                        else if (heading.headingLevel == 5)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level5;
                        }
                        else if (heading.headingLevel == 6)
                        {
                            para.Format.OutlineLevel = OutlineLevel.Level6;
                        }
                    }
                }
                else if (tag is AutoDocumentation.Paragraph)
                {
                    AutoDocumentation.Paragraph paragraph = tag as AutoDocumentation.Paragraph;
                    if (paragraph.text.Contains("![Alt Text]"))
                    {
                        figureNumber++;
                    }
                    paragraph.text = paragraph.text.Replace("[FigureNumber]", figureNumber.ToString());
                    AddFormattedParagraphToSection(section, paragraph);
                }
                else if (tag is AutoDocumentation.GraphAndTable)
                {
                    CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory);
                }
                else if (tag is GraphPage)
                {
                    CreateGraphPage(section, tag as GraphPage, workingDirectory);
                }
                else if (tag is AutoDocumentation.NewPage)
                {
                    section.AddPageBreak();
                }
                else if (tag is AutoDocumentation.Table)
                {
                    CreateTable(section, tag as AutoDocumentation.Table, workingDirectory);
                }
                else if (tag is Graph)
                {
                    GraphPresenter graphPresenter = new GraphPresenter();
                    ExplorerPresenter.ApsimXFile.Links.Resolve(graphPresenter);
                    GraphView graphView = new GraphView();
                    graphView.BackColor = System.Drawing.Color.White;
                    graphView.FontSize  = 12;
                    graphView.Width     = 500;
                    graphView.Height    = 500;
                    graphPresenter.Attach(tag, graphView, ExplorerPresenter);
                    string PNGFileName = graphPresenter.ExportToPDF(workingDirectory);
                    section.AddImage(PNGFileName);
                    string caption = (tag as Graph).Caption;
                    if (caption != null)
                    {
                        section.AddParagraph(caption);
                    }
                    graphPresenter.Detach();
                }
                else if (tag is Map)
                {
                    Form f = new Form();
                    f.FormBorderStyle = FormBorderStyle.None;
                    f.Width           = 650; // 1100;
                    f.Height          = 600; // 600;
                    MapPresenter mapPresenter = new MapPresenter();
                    MapView      mapView      = new MapView();
                    mapView.BackColor         = System.Drawing.Color.White;
                    mapView.Parent            = f;
                    (mapView as Control).Dock = DockStyle.Fill;
                    f.Show();
                    (tag as Map).Zoom = 1.4;
                    mapPresenter.Attach(tag, mapView, ExplorerPresenter);

                    Application.DoEvents();
                    Thread.Sleep(2000);
                    Application.DoEvents();

                    string PNGFileName = mapPresenter.ExportToPDF(workingDirectory);
                    var    Image       = new Bitmap(f.Width, f.Height);
                    f.DrawToBitmap(Image, new Rectangle(0, 0, f.Width, f.Height));
                    Image.Save(PNGFileName);
                    section.AddImage(PNGFileName);
                    mapPresenter.Detach();

                    f.Close();
                }
                else if (tag is AutoDocumentation.Image)
                {
                    AutoDocumentation.Image imageTag = tag as AutoDocumentation.Image;
                    if (imageTag.image.Width > 700)
                    {
                        imageTag.image = ImageUtilities.ResizeImage(imageTag.image, 700, 500);
                    }
                    string PNGFileName = Path.Combine(workingDirectory, imageTag.name);
                    imageTag.image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png);
                    section.AddImage(PNGFileName);
                    figureNumber++;
                }
            }
        }