Index: Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj =================================================================== diff -u -r23d1e296e2da4364fbfe346e68d582dfcf966bb0 -r5e6189fbdb1d72d1ac13c1c5471315778f812561 --- Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj (.../Core.Common.Controls.csproj) (revision 23d1e296e2da4364fbfe346e68d582dfcf966bb0) +++ Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj (.../Core.Common.Controls.csproj) (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -80,6 +80,8 @@ + + Component Index: Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorCell.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorCell.cs (revision 0) +++ Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorCell.cs (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -0,0 +1,80 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System.Drawing; +using System.Windows.Forms; + +namespace Core.Common.Controls.DataGrid +{ + /// + /// This class defines cells for a data grid view in which a color is shown based + /// on the value of that cell. + /// + public class DataGridViewColorCell : DataGridViewTextBoxCell + { + protected override void Paint( + Graphics graphics, + Rectangle clipBounds, + Rectangle cellBounds, + int rowIndex, + DataGridViewElementStates cellState, + object value, + object formattedValue, + string errorText, + DataGridViewCellStyle cellStyle, + DataGridViewAdvancedBorderStyle advancedBorderStyle, + DataGridViewPaintParts paintParts) + { + base.Paint( + graphics, + clipBounds, + cellBounds, + rowIndex, + cellState, + value, + formattedValue, + errorText, + cellStyle, + advancedBorderStyle, + paintParts); + + if (paintParts.HasFlag(DataGridViewPaintParts.ContentBackground) && value is Color) + { + using (var cellBackground = new SolidBrush((Color)value)) + using(var cellBackgroundBorder = new Pen(Color.DarkSlateGray, 1)) + { + var rectangleWithMargin = CreateRectangleWithMargin(cellBounds, 3); + graphics.FillRectangle(cellBackground, rectangleWithMargin); + graphics.DrawRectangle(cellBackgroundBorder, rectangleWithMargin); + } + } + } + + private Rectangle CreateRectangleWithMargin(Rectangle cellBounds, int i) + { + return new Rectangle( + cellBounds.X + i, + cellBounds.Y + i, + cellBounds.Width - i * 2 - 2, + cellBounds.Height - i * 2 - 2); + } + } +} \ No newline at end of file Index: Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorColumn.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorColumn.cs (revision 0) +++ Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewColorColumn.cs (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -0,0 +1,54 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Windows.Forms; + +namespace Core.Common.Controls.DataGrid +{ + /// + /// This class defines a column of a data grid view in which the cells + /// show a color. + /// + public class DataGridViewColorColumn : DataGridViewColumn + { + /// + /// Creates a new instance of . + /// + public DataGridViewColorColumn() : base(new DataGridViewColorCell()) { } + + public override DataGridViewCell CellTemplate + { + get + { + return base.CellTemplate; + } + set + { + if (value is DataGridViewColorCell) + { + base.CellTemplate = value; + } + else throw new ArgumentException($"Given template must be of type {typeof(DataGridViewColorCell)}", nameof(value)); + } + } + } +} \ No newline at end of file Index: Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs =================================================================== diff -u -ra246db9c5134d2c12ee5a37b19bde5e442acce38 -r5e6189fbdb1d72d1ac13c1c5471315778f812561 --- Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs (.../DataGridViewControl.cs) (revision a246db9c5134d2c12ee5a37b19bde5e442acce38) +++ Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewControl.cs (.../DataGridViewControl.cs) (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -220,6 +220,18 @@ dataGridView.Columns.Add(dataGridViewComboBoxColumn); } + public void AddColorColumn(string dataPropertyName, string headerText, DataGridViewAutoSizeColumnMode autoSizeMode = DataGridViewAutoSizeColumnMode.AllCells) + { + var colorColumn = new DataGridViewColorColumn + { + DataPropertyName = dataPropertyName, + HeaderText = headerText, + AutoSizeMode = autoSizeMode + }; + + dataGridView.Columns.Add(colorColumn); + } + /// /// Sets the datasource on the . /// Index: Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj =================================================================== diff -u -r23d1e296e2da4364fbfe346e68d582dfcf966bb0 -r5e6189fbdb1d72d1ac13c1c5471315778f812561 --- Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj (.../Core.Common.Controls.Test.csproj) (revision 23d1e296e2da4364fbfe346e68d582dfcf966bb0) +++ Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj (.../Core.Common.Controls.Test.csproj) (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -57,6 +57,8 @@ Properties\GlobalAssembly.cs + + Index: Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorCellTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorCellTest.cs (revision 0) +++ Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorCellTest.cs (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -0,0 +1,105 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.Windows.Forms; +using Core.Common.Controls.DataGrid; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Core.Common.Controls.Test.DataGrid +{ + [TestFixture] + public class DataGridViewColorCellTest + { + [Test] + public void DefaultConstructor_CreatesNewInstance() + { + // Call + var cell = new DataGridViewColorCell(); + + // Assert + Assert.IsInstanceOf(cell); + } + + [Test] + public void GivenGridWithColorCell_WhenGridCellIsMadeVisibleAndUnselected_ThenOutlinedSquareOfColorDrawnAsExpected() + { + // Given + var expectedColor = Color.FromKnownColor(new Random(21).NextEnumValue()); + var view = new DataGridView + { + DataSource = new[] + { + new ColorRow(expectedColor) + } + }; + var cell = new DataGridViewColorCell(); + view.Columns.Add(new DataGridViewColorColumn + { + CellTemplate = cell, + DataPropertyName = nameof(ColorRow.Color) + }); + + // When + WindowsFormsTestHelper.ShowModal(view, f => + { + view.ClearSelection(); + + // Then + var cellDisplayRectangle = view.GetCellDisplayRectangle(0, 0, false); + + using (Bitmap viewDrawCanvas = new Bitmap(view.Width, view.Height)) + using (Image expectedImage = new Bitmap(cellDisplayRectangle.Width, cellDisplayRectangle.Height)) + { + view.DrawToBitmap(viewDrawCanvas, new Rectangle(0, 0, view.Width, view.Height)); + using (var actualImage = viewDrawCanvas.Clone(cellDisplayRectangle, viewDrawCanvas.PixelFormat)) + { + var expectedWidth = cellDisplayRectangle.Width; + var expectedHeight = cellDisplayRectangle.Height; + var colorRectangle = new Rectangle(3, 3, expectedWidth - 8, expectedHeight - 8); + + var expectedGraphic = Graphics.FromImage(expectedImage); + expectedGraphic.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, expectedWidth, expectedHeight)); + expectedGraphic.FillRectangle(new SolidBrush(expectedColor), colorRectangle); + expectedGraphic.DrawRectangle(new Pen(Color.DarkSlateGray), colorRectangle); + expectedGraphic.DrawLine(new Pen(view.GridColor), expectedWidth - 1, 0, expectedWidth - 1, expectedHeight - 1); + expectedGraphic.DrawLine(new Pen(view.GridColor), 0, expectedHeight - 1, expectedWidth - 1, expectedHeight - 1); + + TestHelper.AssertImagesAreEqual(expectedImage, actualImage); + } + } + }); + } + } + + public class ColorRow + { + public ColorRow(Color color) + { + Color = color; + } + + public Color Color { get; } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorColumnTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorColumnTest.cs (revision 0) +++ Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewColorColumnTest.cs (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -0,0 +1,74 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Windows.Forms; +using Core.Common.Controls.DataGrid; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Core.Common.Controls.Test.DataGrid +{ + [TestFixture] + public class DataGridViewColorColumnTest + { + [Test] + public void DefaultConstructor_CreatesColumnInstance() + { + // Call + var column = new DataGridViewColorColumn(); + + // Assert + Assert.IsInstanceOf(column); + } + + [Test] + public void CellTemplate_WithColorCell_CellTemplateSet() + { + // Setup + var column = new DataGridViewColorColumn(); + var dataGridViewCell = new DataGridViewColorCell(); + + // Call + column.CellTemplate = dataGridViewCell; + + // Assert + Assert.AreSame(dataGridViewCell, column.CellTemplate); + } + + [Test] + public void CellTemplate_WithOtherCell_ThrowsArgumentException() + { + // Setup + var column = new DataGridViewColorColumn(); + var dataGridViewCell = new DataGridViewTextBoxCell(); + + // Call + TestDelegate test = () =>column.CellTemplate = dataGridViewCell; + + // Assert + ArgumentException exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage( + test, + $"Given template must be of type { typeof(DataGridViewColorCell) }"); + Assert.AreEqual("value", exception.ParamName); + } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.TestUtil/TestHelper.cs =================================================================== diff -u -r065af7e201b59ec19a17c42e9d772f5e86b31338 -r5e6189fbdb1d72d1ac13c1c5471315778f812561 --- Core/Common/test/Core.Common.TestUtil/TestHelper.cs (.../TestHelper.cs) (revision 065af7e201b59ec19a17c42e9d772f5e86b31338) +++ Core/Common/test/Core.Common.TestUtil/TestHelper.cs (.../TestHelper.cs) (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -416,7 +416,7 @@ { for (int j = 0; j < bitmap.Width; j++) { - imageColors[index++] = bitmap.GetPixel(i, j); + imageColors[index++] = bitmap.GetPixel(j, i); } } return imageColors; Index: Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingSoilLayerTable.cs =================================================================== diff -u -ra246db9c5134d2c12ee5a37b19bde5e442acce38 -r5e6189fbdb1d72d1ac13c1c5471315778f812561 --- Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingSoilLayerTable.cs (.../PipingSoilLayerTable.cs) (revision a246db9c5134d2c12ee5a37b19bde5e442acce38) +++ Ringtoets/Piping/src/Ringtoets.Piping.Forms/Views/PipingSoilLayerTable.cs (.../PipingSoilLayerTable.cs) (revision 5e6189fbdb1d72d1ac13c1c5471315778f812561) @@ -19,15 +19,21 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. -using System; using System.Collections.Generic; using Core.Common.Controls.DataGrid; using Ringtoets.Piping.Primitives; namespace Ringtoets.Piping.Forms.Views { + /// + /// This class defines a table in which properties of instances + /// are shown as rows. + /// public class PipingSoilLayerTable : DataGridViewControl { + /// + /// Creates a new instance of . + /// public PipingSoilLayerTable() { AddColumns(); @@ -46,7 +52,7 @@ private void AddColumns() { AddTextBoxColumn(nameof(PipingSoilLayer.MaterialName), "Naam", true); - AddTextBoxColumn(nameof(PipingSoilLayer.Color), "Kleur", true); + AddColorColumn(nameof(PipingSoilLayer.Color), "Kleur"); AddTextBoxColumn(nameof(PipingSoilLayer.Top), "Topniveau [m+NAP]", true); AddCheckBoxColumn(nameof(PipingSoilLayer.IsAquifer), "Is aquifer", true); AddTextBoxColumn(nameof(PipingSoilLayer.PermeabilityMean), "Doorlatendheid (verwachtingswaarde) [m/s]", true);