tbf/Results/Output/Printers/OnePerMeter/OnePerMeterPrintDocument.cs

434 lines
17 KiB
C#
Raw Normal View History

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using Config.Entities;
using Results.Resources;
namespace Results.Output.Printers.OnePerMeter
{
2018-03-06 17:37:43 +00:00
public class OnePerMeterPrintDocument : PrintersCommon
{
const float GapBetweenTableColumns = 10;
const float GapBetweenCommonColumns = 10;
/// Size of the printed area without margins, after taking into account 'pageOrientation'
readonly int printHeight;
readonly int printWidth;
readonly int topMargin;
readonly int bottomMargin;
readonly int leftMargin;
readonly int TitleX;
readonly int TitleY; /// Depends on the size of the header
readonly int spacingOne;
readonly int spacingOne4Header;
float commonTop; /// = TitleY + 60
float bodyTop; /// = HdrTop + 160
///
float titleHeight;
float commonHeight;
/// <summary>
/// Property variable for the Font the user wishes to use
/// </summary>
Font titleFont;
Font headerFont;
Font font;
Font footerFont;
///
/// Data to print
///
Results.Entities.Batch batch;
OnePerMeterPrinterCfg cfg;
Results.Entities.WaterMeter wm;
///
/// Items to print
///
string header;
IList<WMeterRsltItemSpec> commonItems;
IList<WMeterRsltItemSpec> testItems;
IList<WMeterRsltItemSpec> testItems2;
string footer;
int nrPages;
int pageNr; /// Page number to be printed at the bottom of the page
/// <summary>
/// Constructor
/// </summary>
/// <param name="textToPrint">Text to be printed</param>
public OnePerMeterPrintDocument(Results.Entities.Batch batch, OnePerMeterPrinterCfg cfg, Results.Entities.WaterMeter wm, string documentName)
{
this.batch = batch;
this.cfg = cfg;
this.wm = wm;
DocumentName = documentName;
topMargin = cfg.TopMargin;
bottomMargin = cfg.BottomMargin;
leftMargin = cfg.LeftMargin;
header = string.IsNullOrEmpty(cfg.Header) ? string.Empty : cfg.Header.Replace("~", Environment.NewLine);
commonItems = Results.WMeterRsltItemSpec.FromStrArray(cfg.CommonItems);
testItems = Results.WMeterRsltItemSpec.FromStrArray(cfg.TestItems); /// TestID info will be overwritten later on
testItems2 = Results.WMeterRsltItemSpec.FromStrArray(cfg.TestItems2); /// TestID info will be overwritten later on
footer = string.IsNullOrEmpty(cfg.Footer) ? string.Empty : cfg.Footer.Replace("~", Environment.NewLine);
/// Initialize fonts
titleFont = new Font((cfg.TitFntFml != null ? cfg.TitFntFml : "Arial"), (cfg.TitFntSz > 0 ? cfg.TitFntSz : 10), (FontStyle)cfg.TitFntSty);
headerFont = new Font((cfg.HdrFntFml != null ? cfg.HdrFntFml : "Arial"), (cfg.HdrFntSz > 0 ? cfg.HdrFntSz : 10), (FontStyle)cfg.HdrFntSty);
font = new Font((cfg.BodyFntFml != null ? cfg.BodyFntFml : "Arial"), (cfg.BodyFntSz > 0 ? cfg.BodyFntSz : 10), (FontStyle)cfg.BodyFntSty);
footerFont = new Font((cfg.FooterFntFml != null ? cfg.FooterFntFml : "Arial"), (cfg.FooterFntSz > 0 ? cfg.FooterFntSz : 10), (FontStyle)cfg.FooterFntSty);
/// Set print area size and margins (in dots using 100 dpi)
DefaultPageSettings.Landscape = (cfg.PageOrientation == PageOrientation.Landscape);
///
if (DefaultPageSettings.Landscape)
{
printHeight = base.DefaultPageSettings.PaperSize.Width - topMargin - bottomMargin;
printWidth = base.DefaultPageSettings.PaperSize.Height - leftMargin - leftMargin;
}
else
{
printHeight = base.DefaultPageSettings.PaperSize.Height - topMargin - bottomMargin;
printWidth = base.DefaultPageSettings.PaperSize.Width - leftMargin - leftMargin;
}
spacingOne = System.Windows.Forms.TextRenderer.MeasureText("Abcgq", font).Height;
spacingOne4Header = System.Windows.Forms.TextRenderer.MeasureText("Abcgq", headerFont).Height;
/// Prepare document outline
TitleX = cfg.LeftMargin;
TitleY = cfg.TopMargin;
pageNr = 1;
nrPages = (testItems2.Count != 0) ? 2 : 1;
}
/// <summary>
/// Override the default onbeginPrint method of the PrintDocument Object
/// </summary>
/// <param name=e></param>
/// <remarks></remarks>
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
base.OnBeginPrint(e);
}
/// <summary>
/// Override the default OnPrintPage method of the PrintDocument.
/// </summary>
/// <param name=e></param>
/// <remarks>This provides the print logic for our document</remarks>
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
{
/// Run base code
base.OnPrintPage(e);
if (!string.IsNullOrEmpty(cfg.Template))
{
string templatePath = Path.Combine("C:\\TBF\\Templates\\", cfg.Template);
if (File.Exists(templatePath))
{
if (cfg.PageOrientation == PageOrientation.Portrait)
e.Graphics.DrawImage(new Bitmap(Image.FromFile(templatePath), 827, 1169), new Rectangle(0, 0, 827, 1169));
else
e.Graphics.DrawImage(new Bitmap(Image.FromFile(templatePath), 1169, 827), new Rectangle(0, 0, 1169, 827));
}
}
if (pageNr == 1)
{
///
/// Print water meter picture
///
if (cfg.ImgWid != 0 && cfg.ImgHgh != 0 && cfg.ImgFullPathName != null)
{
try
{
e.Graphics.DrawImage(new Bitmap(Image.FromFile(cfg.ImgFullPathName), cfg.ImgWid, cfg.ImgHgh),
new Rectangle(cfg.ImgLeft, cfg.ImgTop, cfg.ImgWid, cfg.ImgHgh));
}
catch (Exception) { } /// Cope with situations when file is missing or is not correct
}
titleHeight = System.Windows.Forms.TextRenderer.MeasureText(header, titleFont).Height;
commonTop = TitleY + titleHeight + spacingOne4Header;
commonHeight = commonItems.Count * spacingOne;
bodyTop = commonTop + commonHeight + spacingOne4Header;
///
/// Title
///
RectangleF printArea = new RectangleF(TitleX, TitleY, printWidth, titleHeight);
e.Graphics.DrawString(header, titleFont, Brushes.Black, printArea);
///
/// Common items
///
string[] leftColumn = new string[commonItems.Count];
string[] rightColumn = new string[commonItems.Count];
int cnt = 0;
foreach (var v in commonItems)
{
leftColumn[cnt] = v.Caption;
rightColumn[cnt] = (batch.WaterMeters.Count > 0) ? v.Print(batch.WaterMeters[0]) : string.Empty;
cnt++;
}
/// Determine left column width
float maxWidth = 0;
foreach (var str in leftColumn)
{
float width = e.Graphics.MeasureString(str, font).Width;
if (width > maxWidth) maxWidth = width;
}
/// Write aligned columns
for (int i = 0; i < Math.Min(leftColumn.Length, rightColumn.Length); i++)
{
2018-03-06 17:37:43 +00:00
PrintAt(e, leftColumn[i], font, leftMargin, commonTop + spacingOne * i, maxWidth, 0, Alignment.Left, VertAlignment.Top);
PrintAt(e, rightColumn[i], font, leftMargin + maxWidth + GapBetweenCommonColumns, commonTop + spacingOne * i, 0, 0, Alignment.Left, VertAlignment.Top);
}
}
///
/// Body
///
2018-03-06 17:37:43 +00:00
IList<string[]> tableRows;
IList<Alignment[]> horizAlignment;
IList<VertAlignment[]> vertAlignment;
if (pageNr == 1)
{
///
/// Prepare table content: tableHeader + tableRows + itemAlignment
///
if (cfg.TestsAreRows)
{
2018-03-06 17:37:43 +00:00
GetContentTestsAreRows(wm, testItems, out tableRows, out horizAlignment, out vertAlignment);
DrawTable(e, (float)leftMargin, bodyTop, tableRows, horizAlignment, vertAlignment,
new TableStyle
{
Font = font,
HeaderFont = headerFont,
MarginX = 5,
MarginY = 3,
LineWidth = 1,
TopHeadersCount = 1,
HorizLines = TableLines.All,
VertLines = TableLines.All
});
}
else
{
2018-03-06 17:37:43 +00:00
GetContentTestsAreColumns(wm, testItems, out tableRows, out horizAlignment, out vertAlignment);
DrawTable(e, (float)leftMargin, bodyTop, tableRows, horizAlignment, vertAlignment,
new TableStyle
{
Font = font,
HeaderFont = headerFont,
MarginX = 5,
MarginY = 3,
LineWidth = 1,
LeftHeadersCount = 1,
HorizLines = TableLines.All,
VertLines = TableLines.All
});
}
}
else
{
///
/// Prepare table content: tableHeader + tableRows + itemAlignment
///
if (cfg.TestsAreRows2)
{
2018-03-06 17:37:43 +00:00
GetContentTestsAreRows(wm, testItems2, out tableRows, out horizAlignment, out vertAlignment);
DrawTable(e, (float)leftMargin, bodyTop, tableRows, horizAlignment, vertAlignment,
new TableStyle
{
Font = font,
HeaderFont = headerFont,
MarginX = 5,
MarginY = 3,
LineWidth = 1,
TopHeadersCount = 1,
HorizLines = TableLines.All,
VertLines = TableLines.All
});
}
else
{
2018-03-06 17:37:43 +00:00
GetContentTestsAreColumns(wm, testItems2, out tableRows, out horizAlignment, out vertAlignment);
DrawTable(e, (float)leftMargin, bodyTop, tableRows, horizAlignment, vertAlignment,
new TableStyle
{
Font = font,
HeaderFont = headerFont,
MarginX = 5,
MarginY = 3,
LineWidth = 1,
LeftHeadersCount = 1,
HorizLines = TableLines.All,
VertLines = TableLines.All
});
}
}
if (pageNr == nrPages)
{
///
/// Footer on the last page
///
float footerWidth = e.Graphics.MeasureString(footer, footerFont).Width;
2018-03-06 17:37:43 +00:00
PrintAt(e, footer, footerFont, leftMargin, topMargin + printHeight - 2 * spacingOne, 0, 0, Alignment.Left, VertAlignment.Top);
}
///
/// Page number on each page
///
string pageNrText = string.Format("{0} {1}/{2}", Strings.Page, pageNr, nrPages);
float pageNrWidth = e.Graphics.MeasureString(pageNrText, font).Width;
2018-03-06 17:37:43 +00:00
PrintAt(e, pageNrText, font, leftMargin + (printWidth - pageNrWidth) / 2, topMargin + printHeight - spacingOne, 0, 0, Alignment.Left, VertAlignment.Top);
e.HasMorePages = !(pageNr == nrPages);
pageNr++;
}
2018-03-06 17:37:43 +00:00
void GetContentTestsAreRows(Results.Entities.WaterMeter wm, IList<WMeterRsltItemSpec> items,
out IList<string[]> tableRows, out IList<Alignment[]> horizAlignments, out IList<VertAlignment[]> vertAlignments)
{
2018-03-06 17:37:43 +00:00
/// Top row contains column captions
int tableColumnsCount = items.Count;
tableRows = new List<string[]>();
2018-03-06 17:37:43 +00:00
horizAlignments = new List<Alignment[]>();
vertAlignments = new List<VertAlignment[]>();
2018-03-06 17:37:43 +00:00
string[] tableHeader = new string[tableColumnsCount];
Alignment[] horizAlignment = new Alignment[tableColumnsCount];
VertAlignment[] vertAlignment = new VertAlignment[tableColumnsCount];
for (int i = 0; i < tableColumnsCount; i++)
{
tableHeader[i] = items[i].Caption;
2018-03-06 17:37:43 +00:00
horizAlignment[i] = Alignment.Center;
vertAlignment[i] = VertAlignment.Middle;
}
2018-03-06 17:37:43 +00:00
tableRows.Add(tableHeader);
horizAlignments.Add(horizAlignment);
vertAlignments.Add(vertAlignment);
2018-03-06 17:37:43 +00:00
WMeterRsltItemSpec.TableRowNr = 1;
foreach (var mtr in wm.MeterTestRslts)
{
if (mtr != null && mtr.IsPilotRslt() && mtr.Publish() == Config.Entities.Publish.Always)
{
string[] oneTableRow = new string[tableColumnsCount];
2018-03-06 17:37:43 +00:00
horizAlignment = new Alignment[tableColumnsCount];
vertAlignment = new VertAlignment[tableColumnsCount];
for (int i = 0; i < tableColumnsCount; i++)
{
WMeterRsltItemSpec.TableColumnNr = i + 1;
oneTableRow[i] = items[i].Print(wm, mtr.Name()).Split(new char[] { '|' })[0];
2018-03-06 17:37:43 +00:00
horizAlignment[i] = items[i].Alignment;
vertAlignment[i] = VertAlignment.Middle;
}
tableRows.Add(oneTableRow);
2018-03-06 17:37:43 +00:00
horizAlignments.Add(horizAlignment);
vertAlignments.Add(vertAlignment);
WMeterRsltItemSpec.TableRowNr++;
}
}
}
2018-03-06 17:37:43 +00:00
void GetContentTestsAreColumns(Results.Entities.WaterMeter wm, IList<WMeterRsltItemSpec> items,
out IList<string[]> tableRows, out IList<Alignment[]> horizAlignments, out IList<VertAlignment[]> vertAlignments)
{
2018-03-06 17:37:43 +00:00
/// Left column contains row captions
///
/// Calculate columns count
///
int tableColumnsCount = 1; /// Left column contains item caption
foreach (var mtr in wm.MeterTestRslts)
{
if (mtr != null && mtr.IsPilotRslt() && mtr.Publish() == Config.Entities.Publish.Always)
{
2018-03-06 17:37:43 +00:00
tableColumnsCount++; /// One additional column for each test to be published
}
}
2018-03-06 17:37:43 +00:00
///
/// Alignment of columns
///
tableRows = new List<string[]>();
2018-03-06 17:37:43 +00:00
horizAlignments = new List<Alignment[]>();
vertAlignments = new List<VertAlignment[]>();
int columnsCount = 1;
foreach (var mtr in wm.MeterTestRslts)
{
if (mtr != null && mtr.IsPilotRslt() && mtr.Publish() == Config.Entities.Publish.Always)
{
columnsCount++;
}
}
2018-03-06 17:37:43 +00:00
///
/// Table content
///
WMeterRsltItemSpec.TableRowNr = 1;
for (int i = 0; i < items.Count; i++)
{
string[] oneTableRow = new string[tableColumnsCount];
2018-03-06 17:37:43 +00:00
Alignment[] horizAlignment = new Alignment[tableColumnsCount];
VertAlignment[] vertAlignment = new VertAlignment[tableColumnsCount];
oneTableRow[0] = items[i].Caption;
2018-03-06 17:37:43 +00:00
horizAlignment[0] = Alignment.Center;
vertAlignment[0] = VertAlignment.Middle;
WMeterRsltItemSpec.TableColumnNr = 1;
foreach (var mtr in wm.MeterTestRslts)
{
if (mtr != null && mtr.IsPilotRslt() && mtr.Publish() == Config.Entities.Publish.Always)
{
/// Strip color information
oneTableRow[WMeterRsltItemSpec.TableColumnNr] =
items[i].Print(wm, mtr.Name()).Split(new char[] { '|' })[0];
2018-03-06 17:37:43 +00:00
horizAlignment[WMeterRsltItemSpec.TableColumnNr] = items[i].Alignment;
vertAlignment[WMeterRsltItemSpec.TableColumnNr] = VertAlignment.Middle;
WMeterRsltItemSpec.TableColumnNr++;
}
}
tableRows.Add(oneTableRow);
2018-03-06 17:37:43 +00:00
horizAlignments.Add(horizAlignment);
vertAlignments.Add(vertAlignment);
2018-03-06 17:37:43 +00:00
WMeterRsltItemSpec.TableRowNr++;
}
}
}
}