/// <summary> /// Adds a new tab containing a page of graphs. /// </summary> /// <param name="tab">List of graphs and cached data.</param> /// <param name="numCols">Number of columns into which graphs will be divided.</param> public void AddTab(GraphPanelPresenter.GraphTab tab, int numCols) { // This code may be called from a background thread but // must be run on the main UI thread. Application.Invoke(delegate { try { int numGraphs = tab.Graphs.Count; int numRows = numGraphs / numCols; if (numGraphs % numCols > 0) { numRows++; } Grid panel = new Grid(); panel.RowHomogeneous = true; panel.ColumnHomogeneous = true; for (int n = 0; n < numGraphs; n++) { GraphPresenter presenter = new GraphPresenter(); presenter.SimulationFilter = new List <string>() { tab.SimulationName }; tab.Presenter.ApsimXFile.Links.Resolve(presenter); GraphView view = new GraphView(); presenter.Attach(tab.Graphs[n].Graph, view, tab.Presenter, tab.Graphs[n].Cache); GraphViewCreated?.Invoke(this, new CustomDataEventArgs <IGraphView>(view)); view.ShowControls(false); tab.Graphs[n].Presenter = presenter; tab.Graphs[n].View = view; int i = n / numCols; int j = n % numCols; panel.Attach(view.MainWidget, j, i, 1, 1); } Label tabLabel = new Label(tab.SimulationName); tabLabel.UseUnderline = false; notebook.AppendPage(panel, tabLabel); notebook.ShowAll(); } catch (Exception err) { ShowError(err); } }); //while (GLib.MainContext.Iteration()) ; }
/// <summary>Creates the graph page.</summary> /// <param name="section">The section to write to.</param> /// <param name="graphPage">The graph and table to convert to html.</param> private void CreateGraphPage(Section section, GraphPage graphPage) { int numGraphsToPrint = graphPage.graphs.FindAll(g => g.IncludeInDocumentation).Count; if (numGraphsToPrint > 0) { int numColumns = 2; int numRows = (numGraphsToPrint + 1) / numColumns; // Export graph to bitmap file. Bitmap image = new Bitmap(1800, numRows * 600); using (Graphics gfx = Graphics.FromImage(image)) using (SolidBrush brush = new SolidBrush(System.Drawing.Color.White)) { gfx.FillRectangle(brush, 0, 0, image.Width, image.Height); } 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 = 22; graphView.MarkerSize = MarkerSizeType.Normal; graphView.Width = image.Width / numColumns; graphView.Height = image.Height / numRows; graphView.LeftRightPadding = 0; int col = 0; int row = 0; for (int i = 0; i < graphPage.graphs.Count; i++) { if (graphPage.graphs[i].IncludeInDocumentation) { graphPresenter.Attach(graphPage.graphs[i], graphView, explorerPresenter); Rectangle r = new Rectangle(col * graphView.Width, row * graphView.Height, graphView.Width, graphView.Height); graphView.Export(ref image, r, false); graphPresenter.Detach(); col++; if (col >= numColumns) { col = 0; row++; } } } string basePngFileName = Apsim.FullPath(graphPage.graphs[0].Parent) + "." + graphPage.name + ".png"; basePngFileName = basePngFileName.TrimStart('.'); string pngFileName = Path.Combine(WorkingDirectory, basePngFileName); image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png); MigraDoc.DocumentObjectModel.Shapes.Image sectionImage = section.AddImage(pngFileName); sectionImage.LockAspectRatio = true; sectionImage.Width = "19cm"; } }
/// <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) { foreach (AutoDocumentation.ITag tag in tags) { if (tag is AutoDocumentation.Heading) { AutoDocumentation.Heading heading = tag as AutoDocumentation.Heading; if (heading.headingLevel > 0 && heading.headingLevel < 4) { 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 (tag is AutoDocumentation.Paragraph) { AddFormattedParagraphToSection(section, tag as AutoDocumentation.Paragraph); } else if (tag is AutoDocumentation.GraphAndTable) { CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory); } else if (tag is Graph) { GraphPresenter graphPresenter = new GraphPresenter(); GraphView graphView = new GraphView(); graphView.BackColor = System.Drawing.Color.White; 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(); } } }
/// <summary>Creates the graph page.</summary> /// <param name="section">The section to write to.</param> /// <param name="graphPage">The graph and table to convert to html.</param> /// <param name="workingDirectory">The working directory.</param> private void CreateGraphPage(Section section, GraphPage graphPage, string workingDirectory) { int numGraphsToPrint = graphPage.graphs.FindAll(g => g.IncludeInDocumentation).Count; if (numGraphsToPrint > 0) { int numColumns = 2; int numRows = (numGraphsToPrint + 1) / numColumns; // Export graph to bitmap file. Bitmap image = new Bitmap(1800, numRows * 600); using (Graphics gfx = Graphics.FromImage(image)) using (SolidBrush brush = new SolidBrush(System.Drawing.Color.White)) { gfx.FillRectangle(brush, 0, 0, image.Width, image.Height); } GraphPresenter graphPresenter = new GraphPresenter(); GraphView graphView = new GraphView(); graphView.BackColor = System.Drawing.Color.White; graphView.FontSize = 22; graphView.MarkerSize = 8; graphView.Width = image.Width / numColumns; graphView.Height = image.Height / numRows; graphView.LeftRightPadding = 0; int col = 0; int row = 0; for (int i = 0; i < graphPage.graphs.Count; i++) { if (graphPage.graphs[i].IncludeInDocumentation) { graphPresenter.Attach(graphPage.graphs[i], graphView, ExplorerPresenter); Rectangle r = new Rectangle(col * graphView.Width, row * graphView.Height, graphView.Width, graphView.Height); graphView.Export(image, r, false); graphPresenter.Detach(); col++; if (col >= numColumns) { col = 0; row++; } } } string PNGFileName = Path.Combine(workingDirectory, graphPage.name + ".png"); image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png); MigraDoc.DocumentObjectModel.Shapes.Image sectionImage = section.AddImage(PNGFileName); sectionImage.LockAspectRatio = true; sectionImage.Width = "19cm"; } }
/// <summary> /// Adds a new tab containing a page of graphs. /// </summary> /// <param name="tab">List of graphs and cached data.</param> /// <param name="numCols">Number of columns into which graphs will be divided.</param> public void AddTab(GraphPanelPresenter.GraphTab tab, int numCols) { // This code may be called from a background thread but // must be run on the main UI thread. Application.Invoke(delegate { int numGraphs = tab.Graphs.Count; int numRows = numGraphs / numCols; if (numGraphs % numCols > 0) { numRows++; } Table panel = new Table((uint)numRows, (uint)numCols, true); for (int n = 0; n < numGraphs; n++) { GraphPresenter presenter = new GraphPresenter(); presenter.SimulationFilter = new List <string>() { tab.SimulationName }; tab.Presenter.ApsimXFile.Links.Resolve(presenter); GraphView view = new GraphView(); presenter.Attach(tab.Graphs[n].Graph, view, tab.Presenter, tab.Graphs[n].Cache); tab.Graphs[n].Presenter = presenter; tab.Graphs[n].View = view; uint i = (uint)(n / numCols); uint j = (uint)(n % numCols); panel.Attach(view.MainWidget, j, j + 1, i, i + 1); } Label tabLabel = new Label(tab.SimulationName); tabLabel.UseUnderline = false; notebook.AppendPage(panel, tabLabel); notebook.ShowAll(); //while (GLib.MainContext.Iteration()) ; }); while (GLib.MainContext.Iteration()) { ; } }
/// <summary>Creates the graph page.</summary> /// <param name="section">The section to write to.</param> /// <param name="graphPage">The graph and table to convert to html.</param> /// <param name="workingDirectory">The working directory.</param> private void CreateGraphPage(Section section, GraphPage graphPage, string workingDirectory) { int numColumns = 2; int numRows = 3; // Export graph to bitmap file. Bitmap image = new Bitmap(700, 850); GraphPresenter graphPresenter = new GraphPresenter(); GraphView graphView = new GraphView(); graphView.BackColor = System.Drawing.Color.White; graphView.FontSize = 10; graphView.MarkerSize = 4; graphView.Width = image.Width / numColumns; graphView.Height = image.Height / numRows; int col = 0; int row = 0; for (int i = 0; i < graphPage.graphs.Count; i++) { if (graphPage.graphs[i].IncludeInDocumentation) { graphPresenter.Attach(graphPage.graphs[i], graphView, ExplorerPresenter); Rectangle r = new Rectangle(col * graphView.Width, row * graphView.Height, graphView.Width, graphView.Height); graphView.Export(image, r, false); graphPresenter.Detach(); col++; if (col >= numColumns) { col = 0; row++; } } } string PNGFileName = Path.Combine(workingDirectory, graphPage.name + ".png"); image.Save(PNGFileName, System.Drawing.Imaging.ImageFormat.Png); section.AddImage(PNGFileName); }
/// <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(); } } } } }
/// <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(); } } } } }
/// <summary>Creates the graph page.</summary> /// <param name="section">The section to write to.</param> /// <param name="graphPage">The graph and table to convert to html.</param> private void CreateGraphPage(Section section, GraphPage graphPage) { int numGraphsToPrint = graphPage.Graphs.FindAll(g => g.IncludeInDocumentation && g.Enabled).Count; if (numGraphsToPrint > 0) { int numColumns = 2; int numRows = (numGraphsToPrint + 1) / numColumns; // Export graph to bitmap file. Bitmap image = new Bitmap(1800, numRows * 600); using (Graphics gfx = Graphics.FromImage(image)) using (SolidBrush brush = new SolidBrush(System.Drawing.Color.White)) { gfx.FillRectangle(brush, 0, 0, image.Width, image.Height); } 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 = 22; graphView.MarkerSize = MarkerSizeType.Normal; int width = image.Width / numColumns; int height = image.Height / numRows; graphView.Width = width; graphView.Height = height; graphView.LeftRightPadding = 0; int col = 0; int row = 0; for (int i = 0; i < graphPage.Graphs.Count; i++) { if (graphPage.Graphs[i].IncludeInDocumentation && graphPage.Graphs[i].Enabled) { try { graphPresenter.Attach(graphPage.Graphs[i], graphView, explorerPresenter); Rectangle r = new Rectangle(col * width, row * height, width, height); graphView.Export(ref image, r, false); graphPresenter.Detach(); col++; if (col >= numColumns) { col = 0; row++; } } catch (Exception err) { throw new Exception($"Unable to draw graph '{graphPage.Graphs[i].FullPath}'", err); } } } string basePngFileName = graphPage.Graphs[0].Parent.FullPath + "." + graphPage.Name + ".png"; basePngFileName = basePngFileName.TrimStart('.'); string pngFileName = Path.Combine(WorkingDirectory, basePngFileName); image.Save(pngFileName, System.Drawing.Imaging.ImageFormat.Png); #if NETFRAMEWORK var sectionImage = section.AddImage(pngFileName); #else var sectionImage = section.AddImage(ImageSource.FromFile(pngFileName)); #endif sectionImage.LockAspectRatio = true; sectionImage.Width = "19cm"; } }
/// <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); } } } }
/// <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) { 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) { AddFormattedParagraphToSection(section, tag as AutoDocumentation.Paragraph); } else if (tag is AutoDocumentation.GraphAndTable) { CreateGraphPDF(section, tag as AutoDocumentation.GraphAndTable, workingDirectory); } else if (tag is AutoDocumentation.Table) { CreateTable(section, tag as AutoDocumentation.Table, workingDirectory); } else if (tag is Graph) { GraphPresenter graphPresenter = new GraphPresenter(); GraphView graphView = new GraphView(null); 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(); } else if (tag is Map) { /* TBI * Form f = new Form(); * f.Width = 700; // 1100; * f.Height = 500; // 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(); * * mapPresenter.Attach(tag, mapView, ExplorerPresenter); * * Application.DoEvents(); * Thread.Sleep(2000); * Application.DoEvents(); * string PNGFileName = mapPresenter.ExportToPDF(workingDirectory); * section.AddImage(PNGFileName); * mapPresenter.Detach(); * * f.Close(); */ } } }
/// <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) { 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) { AddFormattedParagraphToSection(section, tag as AutoDocumentation.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(); 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); } } }
/// <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(); } } }
/// <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++; } } }