// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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 . // // 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.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; namespace Deltares.Dam.Forms { /// /// TODO: class should be moved to maps assembly and should be covered by unit tests! /// public class Symbol { private readonly Color selectedFeatureBackgroundColor = Color.Red; private readonly Dictionary, Color> colorTable; private readonly Dictionary labelTable; private readonly Dictionary images; private readonly Dictionary selectedImages; protected Symbol() { this.colorTable = new Dictionary, Color>(); this.labelTable = new Dictionary(); this.images = new Dictionary(); this.selectedImages = new Dictionary(); } public Color GetColor(double value) { foreach (var keyValuePair in this.colorTable) { if (keyValuePair.Key(value)) { return keyValuePair.Value; } } return Color.Empty; } protected void RegisterColorAndLabel(string label, Color color, Func evaluationFunction) { this.colorTable.Add(evaluationFunction, color); this.labelTable.Add(label, GetImage(this.colorTable[evaluationFunction], false)); } public Dictionary GetNamedSymbolTable() { return this.labelTable; } public Image GetImage(Color color, bool selected) { if (selected) { if (!this.selectedImages.ContainsKey(color)) { const int width = 23; const int height = width; var bitmap = new Bitmap(width, height); var graphics = Graphics.FromImage(bitmap); // backround "glow" graphics.FillEllipse(new SolidBrush(selectedFeatureBackgroundColor), 0, 0, width-1, height-1); //graphics.FillRectangle(new SolidBrush(selectedFeatureBackgroundColor), 0, 0, width - 1, height - 1); // fill color const int fillOffset = 4; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.FillEllipse(new SolidBrush(color), fillOffset, fillOffset, width-fillOffset-1-3, height-fillOffset-1-3); // border const int borderThickness = 1; const int borderOffset = 3; var blackPen = new Pen(Brushes.Black) { Width = borderThickness }; graphics.DrawEllipse(blackPen, borderOffset, borderOffset, width - (1 + borderOffset+4), height - (1 + borderOffset+4)); /* const int square = 4; var leftTop = new Rectangle(1, 1, square, square); var leftBottom = new Rectangle(1, height- square -1, square, square); var rightTop = new Rectangle(width - square -1, 1, square, square); var rightBottom = new Rectangle(width - square -1, height - square -1, square, square); graphics.FillRectangle(new SolidBrush(Color.FromArgb(96, selectedFeatureBackgroundColor)), new Rectangle(1, 1, width, height)); graphics.FillRectangle(new SolidBrush(Color.White), leftTop); graphics.FillRectangle(new SolidBrush(Color.White), leftBottom); graphics.FillRectangle(new SolidBrush(Color.White), rightTop); graphics.FillRectangle(new SolidBrush(Color.White), rightBottom); graphics.DrawRectangle(new Pen(Color.Black), leftTop); graphics.DrawRectangle(new Pen(Color.Black), leftBottom); graphics.DrawRectangle(new Pen(Color.Black), rightTop); graphics.DrawRectangle(new Pen(Color.Black), rightBottom); graphics.DrawRectangle(new Pen(Color.Black), 1, 1, width - 1, height - 1); */ this.selectedImages[color] = bitmap; } return this.selectedImages[color]; } if (!this.images.ContainsKey(color)) { var bitmap = new Bitmap(18, 18); var graphics = Graphics.FromImage(bitmap); graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.FillEllipse(new SolidBrush(color), 0, 0, 16, 16); var pen = new Pen(Brushes.Black); graphics.DrawEllipse(pen, 0, 0, 16, 16); this.images[color] = bitmap; } return this.images[color]; } public Image GetPieImage(IEnumerable percentages, IEnumerable values, bool selected) { var bitmap = CreatePie(selected, percentages, values); return bitmap; } private Image CreatePie(bool selected, IEnumerable piePercentages, IEnumerable pieValues) { var sum = piePercentages.Sum(); var bitmap = new Bitmap(18, 18); var graphics = Graphics.FromImage(bitmap); var rect = new Rectangle(1, 1, 16, 16); var fDegSum = 0.0f; var percentages = piePercentages.ToArray(); var values = pieValues.ToArray(); for (int iCnt = 0; iCnt < percentages.Count(); iCnt++) { var fDegValue = (percentages[iCnt] / sum) * 360; Brush brush = new SolidBrush(GetColor(values[iCnt])); graphics.FillPie(brush, rect, (float)fDegSum, (float)fDegValue); fDegSum += (float)fDegValue; } if (selected) { // Glow const int glowThickness = 2; var glowPen = new Pen(selectedFeatureBackgroundColor) { Width = glowThickness }; graphics.DrawEllipse(glowPen, 0, 0, 18, 18); } // else { var pen = new Pen(Brushes.Black); graphics.DrawEllipse(pen, 1, 1, 16, 16); } return bitmap; } } }