320 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Drawing;
using Volian.Base.Library;
namespace Volian.Controls.Library
{
[Serializable]
[XmlRoot("VlnGridCellShading")]
// C2021-004 Table Cell Shading
public class VlnGridCellShading
{
#region Properties
private int _Rows;
[XmlAttribute("Rows")]
public int Rows
{
get { return _Rows; }
set { _Rows = value; }
}
private int _Columns;
[XmlAttribute("Columns")]
public int Columns
{
get { return _Columns; }
set { _Columns = value; }
}
private CellShadingArray _TableShadingInfo;
public CellShadingArray TableShadingInfo
{
get { return _TableShadingInfo; }
set { _TableShadingInfo = value; }
}
#endregion
#region ctor
public VlnGridCellShading() {; }
public VlnGridCellShading(Color defaultCellshading, int rows, int columns)
{
Rows = rows;
Columns = columns;
string defCellColorARGB = GetARGBstring(defaultCellshading);
TableShadingInfo = new CellShadingArray(defCellColorARGB, Rows, Columns);
}
#endregion
#region SetRange - Set the shading color over a range of cells
public Color GetColor(int row, int col)
{
Color cellColor;
// initialize the ARGB value to white
int Alpha = 255;
int Red = 255;
int Green = 255;
int Blue = 255;
// if the given row or column is within the table range, get the stored cell color
// otherwise default to white.
// This also catches when row and col are -1 when exiting PROMS while on a table
if (row > -1 && col > -1 && row < TableShadingInfo.Rows && col < TableShadingInfo.Columns)
{
string cellShadeColor = TableShadingInfo[row, col];
// The color is saved as ARGB in a string (i.e. "[A=255, R=157, G=187, B-97]") in the table xlm definition (SQL record)
cellShadeColor = cellShadeColor.Replace("[", "").Replace("]", "").Replace(" ", ""); // remove starting and ending square brackets
// split the string on the commas and find each ARGB value
string[] argbSplit = cellShadeColor.Split(',');
foreach (string s in argbSplit)
{
if (s[0] == 'A')
Alpha = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'R')
Red = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'G')
Green = Convert.ToInt32(s.Substring(2));
else if (s[0] == 'B')
Blue = Convert.ToInt32(s.Substring(2));
}
}
cellColor = Color.FromArgb(Alpha, Red, Green, Blue);
return cellColor;
}
public string GetARGBstring(Color clr)
{
// return an ARGB string representation of the color ( i.e "[A=255, R=255, G=255, B=255]")
// The format is same as using the ToString() of a color that does not have a name
string strARGB = string.Format("[A={0}, R={1}, G={2}, B={3}]", clr.A, clr.R, clr.G, clr.B);
return strARGB;
}
public void SetColor(int row, int col, Color CellColor)
{
// Set the table cell to the ARGB representation of the passed in color
string cellShadeColor = GetARGBstring(CellColor);
TableShadingInfo[row, col] = cellShadeColor;
return;
}
#endregion
#region Insert and Remove Rows and Columns
public void InsertRow(int row)
{
TableShadingInfo.InsertRow(row);
}
public void InsertRows(int row, int count)
{
TableShadingInfo.InsertRows(row, count);
}
public void DeleteRow(int row)
{
TableShadingInfo.DeleteRow(row);
}
public void DeleteRows(int row, int count)
{
TableShadingInfo.DeleteRows(row, count);
}
public void InsertColumn(int column)
{
TableShadingInfo.InsertColumn(column);
}
public void InsertColumns(int column, int count)
{
TableShadingInfo.InsertColumns(column, count);
}
public void DeleteColumn(int column)
{
TableShadingInfo.DeleteColumn(column);
}
public void DeleteColumns(int column, int count)
{
TableShadingInfo.DeleteColumns(column, count);
}
#endregion
#region Serialize
public string ConvertToString()
{
return GenericSerializer<VlnGridCellShading>.StringSerialize(this);
}
public override string ToString()
{
return "Volian Custom Cell Shading";
}
public static VlnGridCellShading Get(string xml)
{
return GenericSerializer<VlnGridCellShading>.StringDeserialize(xml);
}
#endregion
// Adjust the cell shading array if needed
public void CheckAndFixShadingArraySize(int grdRows, int grdCols, Color defaultCellShading)
{
if (TableShadingInfo.Columns != grdCols || TableShadingInfo.Rows != grdRows)
TableShadingInfo = new CellShadingArray(defaultCellShading, grdRows, grdCols, TableShadingInfo.CellShadingColor);
}
}
[Serializable]
public class CellShadingArray
{
#region Properties
protected static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private int _Rows;
[XmlAttribute("Rows")]
public int Rows
{
get { return _Rows; }
set { _Rows = value; }
}
private int _Columns;
[XmlAttribute("Columns")]
public int Columns
{
get { return _Columns; }
set { _Columns = value; }
}
private string [] _CellShadingColor; // RGB info in a string
public string [] CellShadingColor
{
get { return _CellShadingColor; }
set { _CellShadingColor = value; }
}
private string _WhiteARGB;
public string WhiteARGB
{
get
{
if (_WhiteARGB == null)
_WhiteARGB = string.Format("[A={0}, R={1}, G={2}, B={3}]", Color.White.A, Color.White.R, Color.White.G, Color.White.B);
return _WhiteARGB;
}
}
#endregion
#region ctor
public CellShadingArray() {; }
public CellShadingArray(string defaultShadingColorARGB, int rows, int columns)
{
Rows = rows;
Columns = columns;
CellShadingColor = new string[rows * columns];
for (int r = 0; r < Rows; r++)
for (int c = 0; c < Columns; c++)
CellShadingColor[r * Columns + c] = defaultShadingColorARGB;
}
// Used to automatically adjust the cell shading color array
public CellShadingArray(Color defaultShadingColor, int rows, int columns, string[] shadeAry)
{
Rows = rows;
Columns = columns;
CellShadingColor = new string[rows * columns];
for (int r = 0; r < Rows; r++)
for (int c = 0; c < Columns; c++)
{
int idx = r * Columns + c;
if (idx < shadeAry.Length)
CellShadingColor[idx] = shadeAry[idx];
else
CellShadingColor[idx] = defaultShadingColor.ToString();
}
}
#endregion
#region Array Access
public string this[int r, int c]
{
get
{
int indx = r * Columns + c;
if (indx < CellShadingColor.Length) return CellShadingColor[r * Columns + c];
_MyLog.WarnFormat("TableCellShading: CellShadingColor Array Access out-of-bounds ({0}, {1}) within ({2}, {3})", r, c, Rows, Columns);
return WhiteARGB;
}
set
{
CellShadingColor[r * Columns + c] = value;
}
}
#endregion
#region Insert and Delete Rows and Columns
public void InsertRow(int row)
{
InsertRows(row, 1);
}
public void InsertRows(int row, int count)
{
// Create a new Array of the correct size
string[] newLines = new string[(Rows + count) * Columns];
int newRows = Rows + count;
for (int r = 0; r < newRows; r++)
{
int rSrc = r < row ? r : r > row + count ? r - count : row;
if (rSrc > Rows - 1) rSrc = Rows - 1;
for (int c = 0; c < Columns; c++)
{
newLines[r * Columns + c] = CellShadingColor[rSrc * Columns + c];
}
}
CellShadingColor = newLines;
Rows = newRows;
}
public void DeleteRow(int row)
{
DeleteRows(row, 1);
}
public void DeleteRows(int row, int count)
{
string[] newLines = new string[(Rows - count) * Columns];
int newRows = Rows - count;
for (int r = 0; r < newRows; r++)
{
int rSrc = r < row ? r : r + count;
for (int c = 0; c < Columns; c++)
{
newLines[r * Columns + c] = CellShadingColor[rSrc * Columns + c];
}
}
CellShadingColor = newLines;
Rows = newRows;
}
public void InsertColumn(int column)
{
InsertColumns(column, 1);
}
public void InsertColumns(int column, int count)
{
// Create a new Array of the correct size
string[] newLines = new string[Rows * (Columns + count)];
int newColumns = Columns + count;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < newColumns; c++)
{
int cSrc = c < column ? c : c > column + count ? c - count : column;
newLines[r * newColumns + c] = CellShadingColor[r * Columns + cSrc];
}
}
CellShadingColor = newLines;
Columns = newColumns;
}
public void DeleteColumn(int column)
{
DeleteColumns(column, 1);
}
public void DeleteColumns(int column, int count)
{
string[] newLines = new string[Rows * (Columns - count)];
int newColumns = Columns - count;
for (int r = 0; r < Rows; r++)
{
for (int c = 0; c < newColumns; c++)
{
int cSrc = c < column ? c : c + count;
newLines[r * newColumns + c] = CellShadingColor[r * Columns + cSrc];
}
}
CellShadingColor = newLines;
Columns = newColumns;
}
#endregion
}
}