/// <summary>
        /// Returns the color for a 2scale color formatting.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="min">The minimum.</param>
        /// <param name="max">The maximum.</param>
        /// <param name="par">The 2color parameters.</param>
        /// <returns></returns>
        public static Color ConvertTwoRange(double value, double min, double max, TwoColoursParams par)
        {
            HSVColour A = ColourToHSV(par.MinimumColour);
            HSVColour B = ColourToHSV(par.MaximumColour);

            //Ratio
            double percent;

            if (min == max)
            {
                percent = 1.0;
            }
            else
            {
                //Min can be different from 0
                percent = (value - min) / (max - min);
            }
            return(Color.FromArgb((int)Math.Round(par.MinimumColour.A + (par.MaximumColour.A - par.MinimumColour.A) * percent), (int)Math.Round(par.MinimumColour.R + (par.MaximumColour.R - par.MinimumColour.R) * percent), (int)Math.Round(par.MinimumColour.G + (par.MaximumColour.G - par.MinimumColour.G) * percent), (int)Math.Round(par.MinimumColour.B + (par.MaximumColour.B - par.MinimumColour.B) * percent)));
        }
        /// <summary>
        /// Paints the specified graphics.
        /// </summary>
        /// <param name="graphics">The graphics.</param>
        /// <param name="clipBounds">The clip bounds.</param>
        /// <param name="cellBounds">The cell bounds.</param>
        /// <param name="rowIndex">Index of the row.</param>
        /// <param name="cellState">State of the cell.</param>
        /// <param name="value">The value.</param>
        /// <param name="formattedValue">The formatted value.</param>
        /// <param name="errorText">The error text.</param>
        /// <param name="cellStyle">The cell style.</param>
        /// <param name="advancedBorderStyle">The advanced border style.</param>
        /// <param name="paintParts">The paint parts.</param>
        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, System.Windows.Forms.DataGridViewElementStates cellState, object value, object formattedValue, string errorText, System.Windows.Forms.DataGridViewCellStyle cellStyle, System.Windows.Forms.DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                      System.Windows.Forms.DataGridViewPaintParts paintParts)
        {
            if (FormatParams != null)  // null can happen when cell set to Formatting but no condition has been set !
            {
                switch (FormatType)
                {
                case EnumConditionalFormatType.BAR:
                    int       barWidth;
                    BarParams par = (BarParams)FormatParams;
                    barWidth        = (int)((cellBounds.Width - 10) * par.ProportionValue);
                    Style.BackColor = this.DataGridView.DefaultCellStyle.BackColor;
                    Style.ForeColor = this.DataGridView.DefaultCellStyle.ForeColor;

                    if (barWidth > 0)     //(double)value > 0 &&
                    {
                        Rectangle r = new Rectangle(cellBounds.X + 3, cellBounds.Y + 3, barWidth, cellBounds.Height - 8);
                        if (par.GradientFill)
                        {
                            using (LinearGradientBrush linearBrush = new LinearGradientBrush(r, par.BarColour, Color.White, LinearGradientMode.Horizontal))     //Color.FromArgb(255, 247, 251, 242)
                            {
                                graphics.FillRectangle(linearBrush, r);
                            }
                        }
                        else
                        {
                            using (SolidBrush solidBrush = new SolidBrush(par.BarColour))     //Color.FromArgb(255, 247, 251, 242)
                            {
                                graphics.FillRectangle(solidBrush, r);
                            }
                        }

                        using (Pen pen = new Pen(par.BarColour))     //Color.FromArgb(255, 140, 197, 66)))
                        {
                            graphics.DrawRectangle(pen, r);
                        }
                    }

                    break;

                case EnumConditionalFormatType.TWOCOLOURSRANGE:
                    TwoColoursParams TWCpar = (TwoColoursParams)FormatParams;
                    Style.BackColor = TWCpar.ValueColour;
                    //  if (ContrastTextColor)
                    Style.ForeColor = ContrastColour(TWCpar.ValueColour);
                    break;

                case EnumConditionalFormatType.THREECOLOURSRANGE:
                    ThreeColoursParams THCpar = (ThreeColoursParams)FormatParams;
                    Style.BackColor = THCpar.ValueColour;
                    Style.ForeColor = ContrastColour(THCpar.ValueColour);
                    break;

                default:
                    Style.BackColor = this.DataGridView.DefaultCellStyle.BackColor;
                    Style.ForeColor = this.DataGridView.DefaultCellStyle.ForeColor;
                    break;
                }
            }
            else
            {
                Style.BackColor = this.DataGridView.DefaultCellStyle.BackColor;
                Style.ForeColor = this.DataGridView.DefaultCellStyle.ForeColor;
            }

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle,
                       DataGridViewPaintParts.None | DataGridViewPaintParts.ContentForeground);
        }
Пример #3
0
        /// <summary>
        /// Loads the configuration from file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="Grid">The grid.</param>
        private void LoadConfigFromFile(string file, KryptonOutlookGrid.Classes.KryptonOutlookGrid Grid)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new Exception("Grid config file is missing !");
            }

            XDocument doc         = XDocument.Load(file);
            int       versionGrid = -1;

            int.TryParse(doc.Element("OutlookGrid").Attribute("V").Value, out versionGrid);

            //Upgrade if necessary the config file
            CheckAndUpgradeConfigFile(versionGrid, doc, Grid, LoadState.Before);
            Grid.ClearEverything();
            Grid.GroupBox.Visible     = CommonHelper.StringToBool(doc.XPathSelectElement("OutlookGrid/GroupBox").Value);
            Grid.HideColumnOnGrouping = CommonHelper.StringToBool(doc.XPathSelectElement("OutlookGrid/HideColumnOnGrouping").Value);

            //Initialize
            int NbColsInFile = doc.XPathSelectElements("//Column").Count();

            DataGridViewColumn[]  columnsToAdd        = new DataGridViewColumn[NbColsInFile];
            VaultGridColumn[]     enumCols            = new VaultGridColumn[NbColsInFile];
            OutlookGridColumn[]   OutlookColumnsToAdd = new OutlookGridColumn[columnsToAdd.Length];
            SortedList <int, int> hash = new SortedList <int, int>();// (DisplayIndex , Index)


            int i = 0;
            IOutlookGridGroup group;
            XElement          node2;

            foreach (XElement node in doc.XPathSelectElement("OutlookGrid/Columns").Nodes())
            {
                //Create the columns and restore the saved properties
                //As the OutlookGrid receives constructed DataGridViewColumns, only the parent application can recreate them (dgvcolumn not serializable)
                enumCols[i]             = (VaultGridColumn)Enum.Parse(typeof(VaultGridColumn), node.Element("Name").Value);
                columnsToAdd[i]         = SetupColumn(enumCols[i]);
                columnsToAdd[i].Width   = int.Parse(node.Element("Width").Value);
                columnsToAdd[i].Visible = CommonHelper.StringToBool(node.Element("Visible").Value);
                hash.Add(int.Parse(node.Element("DisplayIndex").Value), i);
                //Reinit the group if it has been set previously
                group = null;
                if (!node.Element("GroupingType").IsEmpty&& node.Element("GroupingType").HasElements)
                {
                    node2                    = node.Element("GroupingType");
                    group                    = (IOutlookGridGroup)Activator.CreateInstance(Type.GetType(TypeConverter.ProcessType(node2.Element("Name").Value), true));
                    group.OneItemText        = node2.Element("OneItemText").Value;
                    group.XXXItemsText       = node2.Element("XXXItemsText").Value;
                    group.SortBySummaryCount = CommonHelper.StringToBool(node2.Element("SortBySummaryCount").Value);
                    if (!string.IsNullOrEmpty((node2.Element("ItemsComparer").Value)))
                    {
                        Object comparer = Activator.CreateInstance(Type.GetType(TypeConverter.ProcessType(node2.Element("ItemsComparer").Value), true));
                        group.ItemsComparer = (IComparer)comparer;
                    }
                    if (node2.Element("Name").Value.Contains("OutlookGridDateTimeGroup"))
                    {
                        ((OutlookGridDateTimeGroup)group).Interval = (OutlookGridDateTimeGroup.DateInterval)Enum.Parse(typeof(OutlookGridDateTimeGroup.DateInterval), node2.Element("GroupDateInterval").Value);
                    }
                }
                OutlookColumnsToAdd[i] = new OutlookGridColumn(columnsToAdd[i], group, (SortOrder)Enum.Parse(typeof(SortOrder), node.Element("SortDirection").Value), int.Parse(node.Element("GroupIndex").Value), int.Parse(node.Element("SortIndex").Value));

                i += 1;
            }
            //First add the underlying DataGridViewColumns to the grid
            Grid.Columns.AddRange(columnsToAdd);
            //And then the outlookgrid columns
            Grid.AddRangeInternalColumns(OutlookColumnsToAdd);

            //Add conditionnal formatting to the grid
            EnumConditionalFormatType conditionFormatType   = default(EnumConditionalFormatType);
            IFormatParams             conditionFormatParams = default(IFormatParams);

            foreach (XElement node in doc.XPathSelectElement("OutlookGrid/ConditionalFormatting").Nodes())
            {
                conditionFormatType = (EnumConditionalFormatType)Enum.Parse(typeof(EnumConditionalFormatType), node.Element("FormatType").Value);
                XElement nodeParams = node.Element("FormatParams");
                switch (conditionFormatType)
                {
                case EnumConditionalFormatType.BAR:
                    conditionFormatParams = new BarParams(Color.FromArgb(int.Parse(nodeParams.Element("BarColor").Value)), CommonHelper.StringToBool(nodeParams.Element("GradientFill").Value));
                    break;

                case EnumConditionalFormatType.THREECOLOURSRANGE:
                    conditionFormatParams = new ThreeColoursParams(Color.FromArgb(int.Parse(nodeParams.Element("MinimumColor").Value)), Color.FromArgb(int.Parse(nodeParams.Element("MediumColor").Value)), Color.FromArgb(int.Parse(nodeParams.Element("MaximumColor").Value)));
                    break;

                case EnumConditionalFormatType.TWOCOLOURSRANGE:
                    conditionFormatParams = new TwoColoursParams(Color.FromArgb(int.Parse(nodeParams.Element("MinimumColor").Value)), Color.FromArgb(int.Parse(nodeParams.Element("MaximumColor").Value)));
                    break;

                default:
                    conditionFormatParams = null;
                    //will never happened but who knows ? throw exception ?
                    break;
                }
                Grid.ConditionalFormatting.Add(new ConditionalFormatting(node.Element("ColumnName").Value, conditionFormatType, conditionFormatParams));
            }



            //We need to loop through the columns in the order of the display order, starting at zero; otherwise the columns will fall out of order as the loop progresses.
            foreach (KeyValuePair <int, int> kvp in hash)
            {
                columnsToAdd[kvp.Value].DisplayIndex = kvp.Key;
            }

            activeColumns = enumCols;
        }