//----------------------------------------------------------------------- // // 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.Data { using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Threading; using System.Windows.Forms; using global::SharedObjects; using global::SharedObjects.GUI.OBD; using global::SharedObjects.Misc; using global::SharedObjects.Protocol.OBD; /// /// Class for displaying data in a formatted style. /// public partial class SimpleData : UserControl, ISimpleData { /// /// Gets simple data grid view instance. /// public DataGridView simpleDataDgv { get { return this.simpleDataDgv1; } } /// /// Logger instance. /// private ILogging iLogging; /// /// Columns in data grid view. /// private DataGridViewColumnCollection cols; /// /// Initializes a new instance of the class. /// /// Logger instance. /// Title of box. public SimpleData(ILogging iLogging, string boxTitle) { this.iLogging = iLogging; this.InitializeComponent(); this.cols = this.simpleDataDgv1.Columns; this.groupBox1.Text = boxTitle; } /// /// Clear the table with simple data. /// public void clearTable() { this.simpleDataDgv1.Rows.Clear(); } /// /// Add printable controls to the list. /// /// The table will be broken up to one table per page printed. /// /// /// List with printable controls. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "pagedSimpleData is member of a list and must not be disposed here.")] public void addPrintControls(IList printControls) { if (printControls != null) { SimpleData pagedSimpleData = new SimpleData(this.iLogging, string.Empty); DataGridView newDgv = pagedSimpleData.simpleDataDgv1; int i = 0; foreach (DataGridViewRow row in this.simpleDataDgv1.Rows) { // 66 lines is a decent number of rows to print on one page. if (i++ > 66) { i = 0; printControls.Add(pagedSimpleData); pagedSimpleData = new SimpleData(this.iLogging, string.Empty); newDgv = pagedSimpleData.simpleDataDgv1; } int n = newDgv.Rows.Add(1); DataGridViewRow dgvr = newDgv.Rows[n]; DataGridViewCellCollection dgvcc = dgvr.Cells; pagedSimpleData.Height += dgvr.Height; foreach (DataGridViewCell cell in row.Cells) { dgvcc[cell.ColumnIndex].Value = cell.Value; } } if (i > 0) { printControls.Add(pagedSimpleData); } } } /// /// Add data to the table including unit text. /// /// Presentation data wrapper. private delegate void addFunc(IPresentationData presentationData); /// /// Add data to the table including unit text. /// /// Presentation data wrapper. public void add(IPresentationData presentationData) { if (presentationData != null) { if (this.simpleDataDgv1.InvokeRequired) { try { this.simpleDataDgv1.Invoke(new addFunc(this.add), new object[] { presentationData }); } 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 { if (this.simpleDataDgv1.ColumnCount > 0) { DataGridViewRowCollection dgvrc = this.simpleDataDgv1.Rows; int n = dgvrc.Add(1); DataGridViewRow dgvr = dgvrc[n]; DataGridViewCellCollection dgvcc = dgvr.Cells; dgvcc["col_pid"].Value = "0x" + presentationData.pid.ToString("X2"); dgvcc["description"].Value = presentationData.label; dgvcc["unit"].Value = presentationData.unit; dgvcc["data"].Value = presentationData.valueStr; if (presentationData.naFlag) { dgvcc["data"].Style.BackColor = Color.LightGoldenrodYellow; } if (this.Height < 445) { this.Height += dgvr.Height; this.simpleDataDgv1.Height += dgvr.Height; } } else { this.iLogging.appendText("this.simpleDataDgv.ColumnCount=" + this.simpleDataDgv1.ColumnCount + ", this.simpleDataDgv.CanFocus=" + this.simpleDataDgv1.CanFocus + "\r\n"); } } } else { this.iLogging.appendText("presentationData was 'null' - nothing to do.\r\n"); } } /// /// Save table data to CSV file for later review. /// /// Name of file to save data to. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Reviewed, intentional.")] public void saveData(string fileName) { try { using (FileStream fileStream = new FileStream(fileName, FileMode.CreateNew)) { using (StreamWriter streamWriter = new StreamWriter(fileStream, Utils.iso_8859_1)) { string csvSep = ";"; if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ".") { csvSep = ","; } streamWriter.WriteLine("\"Name\"" + csvSep + "\"Unit\"" + csvSep + "\"Value\""); DataGridViewRowCollection dgvrc = this.simpleDataDgv1.Rows; foreach (DataGridViewRow dgvr in dgvrc) { DataGridViewCellCollection dgvcc = dgvr.Cells; if (dgvcc["description"].Value != null && dgvcc["unit"].Value != null && dgvcc["data"].Value != null) { string name = (string)dgvcc["description"].Value; streamWriter.Write("\""); streamWriter.Write(name); streamWriter.Write("\"" + csvSep + "\""); string unitValue = (string)dgvcc["unit"].Value; streamWriter.Write(unitValue); streamWriter.Write("\"" + csvSep); string val = dgvcc["data"].Value.ToString(); try { Convert.ToDouble(val); // Test if numeric. streamWriter.Write(val); } catch { // Value isn't numeric, print as string. streamWriter.Write("\""); streamWriter.Write(val); streamWriter.Write("\""); } streamWriter.WriteLine(); } } } } } catch (Exception ex) { MessageBox.Show( "Could not open file '" + fileName + "'\r\n\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } /// /// Adjust for changed size. /// /// Sending object. /// Event data. private void simpleDataDgv_SizeChanged(object sender, EventArgs e) { if (this.simpleDataDgv1 != null) { Size sz = this.simpleDataDgv1.Size; if (sz != null) { int width = sz.Width; width -= 159; // Units column, scrollbar etc. int descWidth = (int)(width / 1.3); int valueWidth = width - descWidth; // To cover for rounding error. descWidth = descWidth < 100 ? 100 : descWidth; valueWidth = valueWidth < 100 ? 100 : valueWidth; this.cols["description"].Width = descWidth; this.cols["data"].Width = valueWidth; } } } } }