//----------------------------------------------------------------------- // // Copyright © 2012 Nils Hammar. All rights reserved. // //----------------------------------------------------------------------- /* * Software to access vehicle information via the OBD-II connector. * * Copyright © 2012 Nils Hammar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Alternative licensing is possible, see the licensing document. * * The above text may not be removed or modified. */ namespace UserInterface.GUI.OBD { using System; using System.Collections.Generic; using System.Drawing; using System.Threading; using System.Windows.Forms; using global::SharedObjects; using global::SharedObjects.GUI; using global::SharedObjects.GUI.OBD; using global::SharedObjects.Protocol.OBD; using global::UserInterface.GUI.OBD.Data; /// /// Panel for ECU data. /// public partial class EcuDataPanel : AbstractUserControl, IDataPresentation { /// /// Simple data table. /// private ISimpleData simpleData; /// /// Callback for data event. /// private IDataEventCallback iDataEventCallback; /// /// Base width for the panel. /// private int baseWidth = 0; /// /// Last item printed. /// private int lastPrintedItem = 0; /// /// Controls to print. /// private List printControls = new List(); /// /// Initializes a new instance of the class. /// /// Logging instance. /// Current application tree instance. /// ECU description. /// Callback for data event. public EcuDataPanel(ILogging iLogging, IApplicationTree applicationTree, string ecuDescription, IDataEventCallback iDataEventCallback) : base(iLogging, null, applicationTree) { this.iDataEventCallback = iDataEventCallback; this.InitializeComponent(); this.ecuDescriptionTB.Text = ecuDescription; this.initDataOutputPanel(); } /// /// Clear old data. /// public void clear() { this.flowLayoutPanel1.Controls.Clear(); this.initDataOutputPanel(); } /// /// Add data to the simple data table. /// /// Presentation data wrapper. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed, intentional.")] public void simpleDataAdd(IPresentationData presentationData) { this.simpleData.add(presentationData); } /// /// Add an item of the type Control. /// /// ECU source address for data. /// Which parameter mode that is selected. /// Item to add. public void add(uint sourceAddress, byte mode, Control control) { if (control != null) { if (this.flowLayoutPanel1.InvokeRequired) { try { this.flowLayoutPanel1.Invoke(new addFunc(this.add), new object[] { sourceAddress, mode, control }); } catch (ThreadAbortException) { throw; } catch (System.Reflection.TargetParameterCountException ex) { MessageBox.Show( "Exception: " + ex.Message + "\r\n", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop); } catch (System.ObjectDisposedException) { // Ignore. } } else { control.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.flowLayoutPanel1.Controls.Add(control); } } } /// /// Initialize the data output panel. /// private void initDataOutputPanel() { SimpleData newSimpleData = new SimpleData(this.iLogging, string.Empty); this.flowLayoutPanel1.Controls.Add(newSimpleData); if (this.baseWidth == 0) { this.baseWidth = (this.Width - 6); } newSimpleData.Width = this.baseWidth; newSimpleData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.simpleData = newSimpleData; } /// /// Add an item of the type Control. /// /// ECU source address for data. /// Which parameter mode that is selected. /// Item to add. private delegate void addFunc(uint sourceAddress, byte mode, Control control); /// /// Adjust for changed size. /// /// Sending object. /// Event data. private void EcuDataPanel_SizeChanged(object sender, EventArgs e) { this.simpleData.Width = (this.Width - 22); } /// /// Get fresh data. /// /// Sending object. /// Event data. private void getButton_Click(object sender, EventArgs e) { this.iDataEventCallback.getDataEvent(); } /// /// Clear data and get fresh data. /// /// Sending object. /// Event data. private void clearAndGetButton_Click(object sender, EventArgs e) { this.clear(); this.iDataEventCallback.getDataEvent(); } /// /// Save data to CSV file. /// /// Sending object. /// Event data. private void saveButton_Click(object sender, EventArgs e) { this.saveFileDialog1.FileName = string.Empty; this.saveFileDialog1.DefaultExt = ".csv"; this.saveFileDialog1.AddExtension = true; this.saveFileDialog1.ValidateNames = true; this.saveFileDialog1.Filter = "Excel CSV Format|*.csv"; DialogResult res = this.saveFileDialog1.ShowDialog(); if (res == DialogResult.OK) { if (!string.IsNullOrEmpty(this.saveFileDialog1.FileName)) { this.simpleData.saveData(this.saveFileDialog1.FileName); } } } /// /// Print data. /// /// Sending object. /// Event data. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Reviewed, intentional.")] private void printButton_Click(object sender, EventArgs e) { this.printDialog1.Document = this.printDocument1; if (this.printDialog1.ShowDialog() == DialogResult.OK) { this.lastPrintedItem = 0; foreach (Control contr in this.flowLayoutPanel1.Controls) { if (contr.Equals(this.simpleData)) { DataGridView dgv = this.simpleData.simpleDataDgv; SimpleData sd = new SimpleData(this.iLogging, string.Empty); DataGridView newDgv = sd.simpleDataDgv; int i = 0; foreach (DataGridViewRow row in dgv.Rows) { if (i++ > 66) { i = 0; sd.Dock = DockStyle.Fill; newDgv.ClearSelection(); newDgv.DefaultCellStyle.SelectionBackColor = Color.White; newDgv.DefaultCellStyle.SelectionForeColor = Color.Black; sd.Width = 1000; this.printControls.Add(sd); sd = new SimpleData(this.iLogging, string.Empty); newDgv = sd.simpleDataDgv; } int n = newDgv.Rows.Add(1); DataGridViewRow dgvr = newDgv.Rows[n]; DataGridViewCellCollection dgvcc = dgvr.Cells; sd.Height += dgvr.Height; foreach (DataGridViewCell cell in row.Cells) { dgvcc[cell.ColumnIndex].Value = cell.Value; } } if (i > 0) { newDgv.ClearSelection(); newDgv.DefaultCellStyle.SelectionBackColor = Color.White; newDgv.DefaultCellStyle.SelectionForeColor = Color.Black; sd.Width = 1000; this.printControls.Add(sd); } } else { this.printControls.Add(contr); } } this.printDocument1.Print(); } } /// /// Print the page. /// /// Sending object. /// Event data. private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g = e.Graphics; Rectangle pageBounds = e.PageBounds; float hpos2 = 10; bool firstOnPage = true; Control contr = this.printControls[this.lastPrintedItem]; while ((contr != null) && (this.lastPrintedItem < this.printControls.Count) && (firstOnPage || (((contr.ClientRectangle.Height / 1.5) + hpos2) < pageBounds.Height))) { firstOnPage = false; Rectangle sz = contr.ClientRectangle; int width = sz.Width; int height = sz.Height; Color tmpColor = contr.BackColor; contr.BackColor = Color.White; using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height)) { contr.DrawToBitmap(bmp, contr.ClientRectangle); g.DrawImage(bmp, 80, hpos2, (float)(sz.Width / 1.5), (float)(sz.Height / 1.5)); } hpos2 += (float)(sz.Height / 1.5); contr.BackColor = tmpColor; this.lastPrintedItem++; if (this.lastPrintedItem < this.printControls.Count) { contr = this.printControls[this.lastPrintedItem]; } else { contr = null; } } if (contr != null) { e.HasMorePages = true; } } } }