Пример #1
0
 public override string ToString()
 {
     return
         (FieldToSet == null
         ? base.ToString()
         : FieldToSet.Value + " = " + (ClearValue ? "(null)" : ValueToSet?.ToString()));
 }
Пример #2
0
 /// <summary>
 /// Clones this instance.
 /// </summary>
 /// <returns>A deep copy of this instance.</returns>
 public override ActionViewModelBase Clone()
 {
     return(new SetVariableValueActionViewModel
                (new SetVariableValueAction(), ActionDescriptor, ParameterDescriptions, AllActionDescriptions)
     {
         VariableName = VariableName,
         SilentSet = SilentSet,
         ValueToSet = ValueToSet.Clone()
     });
 }
Пример #3
0
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics   G     = e.Graphics;
            HatchBrush Hatch = new HatchBrush(HatchStyle.WideDownwardDiagonal, Color.FromArgb(20, Color.Black), Color.Transparent);

            G.Clear(Parent.BackColor);
            G.SmoothingMode = SmoothingMode.AntiAlias;

            PipeBorder = RoundRectangle.RoundRect(1, 6, Width - 3, 8, 3);

            try
            {
                ValueDrawer = Convert.ToInt32((_Value - _Minimum) / (_Maximum - _Minimum) * (Width - 11));
            }
            catch (Exception ex)
            {
            }

            TrackBarHandleRect = new Rectangle(ValueDrawer, 0, 10, 20);

            G.SetClip(PipeBorder); // Set the clipping region of this Graphics to the specified GraphicsPath

            ValueRect = new Rectangle(1, 7, TrackBarHandleRect.X + TrackBarHandleRect.Width - 2, 7);
            VlaueLGB  = new LinearGradientBrush(ValueRect, _ValueColour, _ValueColour, 90.0F);

            G.FillRectangle(VlaueLGB, ValueRect);

            if (_DrawHatch == true)
            {
                G.FillRectangle(Hatch, ValueRect);
            }

            G.ResetClip(); // Reset the clip region of this Graphics to an infinite region

            G.SmoothingMode = SmoothingMode.AntiAlias;
            G.DrawPath(new Pen(Color.FromArgb(180, 180, 180)), PipeBorder); // Draw pipe border

            TrackBarHandle    = RoundRectangle.RoundRect(TrackBarHandleRect, 3);
            TrackBarHandleLGB = new LinearGradientBrush(ClientRectangle, SystemColors.Control, SystemColors.Control, 90.0F);

            // Fill the handle body with the specified color gradient
            G.FillPath(TrackBarHandleLGB, TrackBarHandle);
            // Draw handle borders
            G.DrawPath(new Pen(Color.FromArgb(180, 180, 180)), TrackBarHandle);

            if (_DrawValueString == true)
            {
                G.DrawString(ValueToSet.ToString(), Font, Brushes.Gray, 0, 25);
            }
        }
Пример #4
0
        private static ValueToSet RecalculateScale(Point[] plot1, Point[] plot2, double actualWidth, double actualHeight, double currentX1, double currentY1)
        {
            ValueToSet valueToSet = new ValueToSet();

            if (plot1.Length != 0 || plot2.Length != 0)
            {
                valueToSet.setXY = true;
                valueToSet.x0    = 0;
                valueToSet.y0    = 0;

                var xMax = double.NegativeInfinity;
                var yMax = double.NegativeInfinity;
                if (plot1.Any())
                {
                    xMax = plot1.Max(n => n.X);
                    yMax = plot1.Max(n => n.Y);
                }
                if (plot2.Any())
                {
                    xMax = Math.Max(xMax, plot2.Max(n => n.X));
                    yMax = Math.Max(yMax, plot2.Max(n => n.Y));
                }

                valueToSet.x1 = xMax * 2;    // +100%
                valueToSet.y1 = yMax * 1.15; // +15%

                if (xMax < currentX1)
                {
                    valueToSet.setXY = false;
                }
                if (valueToSet.y1 != currentY1)
                {
                    valueToSet.setXY = true;
                }
            }

            if (valueToSet.setXY &&
                (valueToSet.x1 != valueToSet.x0 && valueToSet.y1 != valueToSet.y0) &&
                (!double.IsNaN(actualWidth) && !double.IsNaN(actualHeight)) &&
                (actualWidth != 0 && actualHeight != 0))
            {
                valueToSet.setScale = true;
                valueToSet.scaleX   = actualWidth / (valueToSet.x1 - valueToSet.x0);
                valueToSet.scaleY   = actualHeight / (valueToSet.y1 - valueToSet.y0);
            }

            return(valueToSet);
        }
Пример #5
0
        private void RecalculateScale()
        {
            Point[] plot1        = new Point[0];
            Point[] plot2        = new Point[0];
            double  actualWidth  = 0;
            double  actualHeight = 0;

            try
            {
                Dispatcher.Invoke(() =>
                {
                    plot1        = Plot1.ToArray();
                    plot2        = Plot2.ToArray();
                    actualWidth  = Plots.ActualWidth;
                    actualHeight = Plots.ActualHeight;
                });
            }
            catch { }

            ValueToSet valueToSet = RecalculateScale(plot1, plot2, actualWidth, actualHeight, X1, Y1);

            try
            {
                if (!valueToSet.setXY && !valueToSet.setScale)
                {
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    if (valueToSet.setXY)
                    {
                        X0 = valueToSet.x0;
                        Y0 = valueToSet.y0;
                        X1 = valueToSet.x1;
                        Y1 = valueToSet.y1;
                    }

                    if (valueToSet.setScale)
                    {
                        ScaleX = valueToSet.scaleX;
                        ScaleY = valueToSet.scaleY;
                    }
                });
            }
            catch { }
        }
        private void RecalculateScale()
        {
            MyPoint[] plot1        = new MyPoint[0];
            double    actualWidth  = 0;
            double    actualHeight = 0;

            try
            {
                Dispatcher.Invoke(() =>
                {
                    plot1        = Generations.SelectMany(n => n.Points).ToArray();
                    actualWidth  = Plots.ActualWidth;
                    actualHeight = Plots.ActualHeight;
                });
            }
            catch { }

            ValueToSet valueToSet = RecalculateScale(plot1, actualWidth, actualHeight, X1, Y1);

            try
            {
                if (!valueToSet.setXY && !valueToSet.setScale)
                {
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    if (valueToSet.setXY)
                    {
                        X0 = valueToSet.x0;
                        Y0 = valueToSet.y0;
                        X1 = valueToSet.x1;
                        Y1 = valueToSet.y1;
                    }

                    if (valueToSet.setScale)
                    {
                        ScaleX = valueToSet.scaleX;
                        ScaleY = valueToSet.scaleY;
                    }
                });
            }
            catch { }
        }
Пример #7
0
        /// <summary>
        /// Sets parameters from a data form in an object.
        /// </summary>
        /// <param name="e">IQ Event Arguments describing the request.</param>
        /// <param name="EditableObject">Object whose parameters will be set.</param>
        /// <param name="Form">Data Form.</param>
        /// <param name="OnlySetChanged">If only changed parameters are to be set.</param>
        /// <returns>Any errors encountered, or null if parameters was set properly.</returns>
        public static async Task <SetEditableFormResult> SetEditableForm(IqEventArgs e, object EditableObject, DataForm Form, bool OnlySetChanged)
        {
            Type   T = EditableObject.GetType();
            string DefaultLanguageCode = GetDefaultLanguageCode(T);
            List <KeyValuePair <string, string> > Errors = null;
            PropertyInfo PropertyInfo;
            FieldInfo    FieldInfo;
            Language     Language = await ConcentratorServer.GetLanguage(e.Query, DefaultLanguageCode);

            Namespace Namespace             = null;
            Namespace ConcentratorNamespace = await Language.GetNamespaceAsync(typeof(ConcentratorServer).Namespace);

            LinkedList <Tuple <PropertyInfo, FieldInfo, object> > ToSet = null;
            ValidationMethod           ValidationMethod;
            OptionAttribute            OptionAttribute;
            RegularExpressionAttribute RegularExpressionAttribute;
            RangeAttribute             RangeAttribute;
            DataType DataType;
            Type     PropertyType;
            string   NamespaceStr;
            string   LastNamespaceStr = null;
            object   ValueToSet;
            object   ValueToSet2;

            object[] Parsed;
            bool     ReadOnly;
            bool     Alpha;
            bool     DateOnly;
            bool     HasHeader;
            bool     HasOptions;
            bool     ValidOption;
            bool     Nullable;

            if (Namespace is null)
            {
                Namespace = await Language.CreateNamespaceAsync(T.Namespace);
            }

            if (ConcentratorNamespace is null)
            {
                ConcentratorNamespace = await Language.CreateNamespaceAsync(typeof(ConcentratorServer).Namespace);
            }

            foreach (Field Field in Form.Fields)
            {
                PropertyInfo = T.GetRuntimeProperty(Field.Var);
                FieldInfo    = PropertyInfo is null?T.GetRuntimeField(Field.Var) : null;

                if (PropertyInfo is null && FieldInfo is null)
                {
                    AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(1, "Property not found."));
                    continue;
                }

                if (PropertyInfo != null && (!PropertyInfo.CanRead || !PropertyInfo.CanWrite))
                {
                    AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable."));
                    continue;
                }

                NamespaceStr = (PropertyInfo?.DeclaringType ?? FieldInfo.DeclaringType).Namespace;
                if (Namespace is null || NamespaceStr != LastNamespaceStr)
                {
                    Namespace = await Language.GetNamespaceAsync(NamespaceStr);

                    LastNamespaceStr = NamespaceStr;
                }

                ValidationMethod = null;
                ReadOnly         = Alpha = DateOnly = HasHeader = HasOptions = ValidOption = false;

                foreach (Attribute Attr in (PropertyInfo?.GetCustomAttributes() ?? FieldInfo.GetCustomAttributes()))
                {
                    if (Attr is HeaderAttribute)
                    {
                        HasHeader = true;
                    }
                    else if ((OptionAttribute = Attr as OptionAttribute) != null)
                    {
                        HasOptions = true;
                        if (Field.ValueString == OptionAttribute.Option.ToString())
                        {
                            ValidOption = true;
                        }
                    }
                    else if ((RegularExpressionAttribute = Attr as RegularExpressionAttribute) != null)
                    {
                        ValidationMethod = new RegexValidation(RegularExpressionAttribute.Pattern);
                    }
                    else if ((RangeAttribute = Attr as RangeAttribute) != null)
                    {
                        ValidationMethod = new RangeValidation(RangeAttribute.Min, RangeAttribute.Max);
                    }
                    else if (Attr is OpenAttribute)
                    {
                        ValidationMethod = new OpenValidation();
                    }
                    else if (Attr is ReadOnlyAttribute)
                    {
                        ReadOnly = true;
                    }
                    else if (Attr is AlphaChannelAttribute)
                    {
                        Alpha = true;
                    }
                    else if (Attr is DateOnlyAttribute)
                    {
                        DateOnly = true;
                    }
                }

                if (!HasHeader)
                {
                    AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(2, "Property not editable."));
                    continue;
                }

                if (ReadOnly)
                {
                    if (Field.ValueString != (PropertyInfo?.GetValue(EditableObject) ?? FieldInfo?.GetValue(EditableObject))?.ToString())
                    {
                        AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(3, "Property is read-only."));
                    }

                    continue;
                }

                if (HasOptions && !ValidOption)
                {
                    AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option."));
                    continue;
                }

                PropertyType = PropertyInfo?.PropertyType ?? FieldInfo.FieldType;
                ValueToSet   = null;
                ValueToSet2  = null;
                Parsed       = null;
                DataType     = null;
                Nullable     = false;

                if (PropertyType.GetTypeInfo().IsGenericType)
                {
                    Type GT = PropertyType.GetGenericTypeDefinition();
                    if (GT == typeof(Nullable <>))
                    {
                        Nullable     = true;
                        PropertyType = PropertyType.GenericTypeArguments[0];
                    }
                }

                if (Nullable && string.IsNullOrEmpty(Field.ValueString))
                {
                    ValueToSet2 = null;
                }
                else
                {
                    if (PropertyType == typeof(string[]))
                    {
                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new BasicValidation();
                        }

                        ValueToSet = ValueToSet2 = Parsed = Field.ValueStrings;
                        DataType   = StringDataType.Instance;
                    }
                    else if (PropertyType.GetTypeInfo().IsEnum)
                    {
                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new BasicValidation();
                        }

                        try
                        {
                            ValueToSet = ValueToSet2 = Enum.Parse(PropertyType, Field.ValueString);
                        }
                        catch (Exception)
                        {
                            AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(4, "Select a valid option."));
                            continue;
                        }
                    }
                    else if (PropertyType == typeof(bool))
                    {
                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new BasicValidation();
                        }

                        if (!CommonTypes.TryParse(Field.ValueString, out bool b))
                        {
                            AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(5, "Invalid boolean value."));
                            continue;
                        }

                        DataType   = BooleanDataType.Instance;
                        ValueToSet = ValueToSet2 = b;
                    }
                    else
                    {
                        if (PropertyType == typeof(string))
                        {
                            DataType = StringDataType.Instance;
                        }
                        else if (PropertyType == typeof(sbyte))
                        {
                            DataType = ByteDataType.Instance;
                        }
                        else if (PropertyType == typeof(short))
                        {
                            DataType = ShortDataType.Instance;
                        }
                        else if (PropertyType == typeof(int))
                        {
                            DataType = IntDataType.Instance;
                        }
                        else if (PropertyType == typeof(long))
                        {
                            DataType = LongDataType.Instance;
                        }
                        else if (PropertyType == typeof(byte))
                        {
                            DataType = ShortDataType.Instance;

                            if (ValidationMethod is null)
                            {
                                ValidationMethod = new RangeValidation(byte.MinValue.ToString(), byte.MaxValue.ToString());
                            }
                        }
                        else if (PropertyType == typeof(ushort))
                        {
                            DataType = IntDataType.Instance;

                            if (ValidationMethod is null)
                            {
                                ValidationMethod = new RangeValidation(ushort.MinValue.ToString(), ushort.MaxValue.ToString());
                            }
                        }
                        else if (PropertyType == typeof(uint))
                        {
                            DataType = LongDataType.Instance;

                            if (ValidationMethod is null)
                            {
                                ValidationMethod = new RangeValidation(uint.MinValue.ToString(), uint.MaxValue.ToString());
                            }
                        }
                        else if (PropertyType == typeof(ulong))
                        {
                            DataType = IntegerDataType.Instance;

                            if (ValidationMethod is null)
                            {
                                ValidationMethod = new RangeValidation(ulong.MinValue.ToString(), ulong.MaxValue.ToString());
                            }
                        }
                        else if (PropertyType == typeof(DateTime))
                        {
                            if (DateOnly)
                            {
                                DataType = DateDataType.Instance;
                            }
                            else
                            {
                                DataType = DateTimeDataType.Instance;
                            }
                        }
                        else if (PropertyType == typeof(decimal))
                        {
                            DataType = DecimalDataType.Instance;
                        }
                        else if (PropertyType == typeof(double))
                        {
                            DataType = DoubleDataType.Instance;
                        }
                        else if (PropertyType == typeof(float))
                        {
                            DataType = DoubleDataType.Instance;                                // Use xs:double anyway
                        }
                        else if (PropertyType == typeof(TimeSpan))
                        {
                            DataType = TimeDataType.Instance;
                        }
                        else if (PropertyType == typeof(Uri))
                        {
                            DataType = AnyUriDataType.Instance;
                        }
                        else if (PropertyType == typeof(SKColor))
                        {
                            if (Alpha)
                            {
                                DataType = ColorAlphaDataType.Instance;
                            }
                            else
                            {
                                DataType = ColorDataType.Instance;
                            }
                        }
                        else
                        {
                            DataType = null;
                        }

                        if (ValidationMethod is null)
                        {
                            ValidationMethod = new BasicValidation();
                        }

                        try
                        {
                            if (DataType is null)
                            {
                                ValueToSet  = Field.ValueString;
                                ValueToSet2 = Activator.CreateInstance(PropertyType, ValueToSet);
                            }
                            else
                            {
                                ValueToSet = DataType.Parse(Field.ValueString);

                                if (ValueToSet.GetType() == PropertyType)
                                {
                                    ValueToSet2 = ValueToSet;
                                }
                                else
                                {
                                    ValueToSet2 = Convert.ChangeType(ValueToSet, PropertyType);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            AddError(ref Errors, Field.Var, await ConcentratorNamespace.GetStringAsync(6, "Invalid value."));
                            continue;
                        }
                    }

                    if (Parsed is null)
                    {
                        Parsed = new object[] { ValueToSet }
                    }
                    ;

                    ValidationMethod.Validate(Field, DataType, Parsed, Field.ValueStrings);
                    if (Field.HasError)
                    {
                        AddError(ref Errors, Field.Var, Field.Error);
                        continue;
                    }
                }

                if (ToSet is null)
                {
                    ToSet = new LinkedList <Tuple <PropertyInfo, FieldInfo, object> >();
                }

                ToSet.AddLast(new Tuple <PropertyInfo, FieldInfo, object>(PropertyInfo, FieldInfo, ValueToSet2));
            }

            if (Errors is null)
            {
                SetEditableFormResult Result = new SetEditableFormResult()
                {
                    Errors = null,
                    Tags   = new List <KeyValuePair <string, object> >()
                };

                foreach (Tuple <PropertyInfo, FieldInfo, object> P in ToSet)
                {
                    try
                    {
                        if (OnlySetChanged)
                        {
                            object Current = P.Item1?.GetValue(EditableObject) ?? P.Item2?.GetValue(EditableObject);

                            if (Current is null)
                            {
                                if (P.Item3 is null)
                                {
                                    continue;
                                }
                            }
                            else if (P.Item3 != null && Current.Equals(P.Item3))
                            {
                                continue;
                            }
                        }

                        if (P.Item1 != null)
                        {
                            P.Item1.SetValue(EditableObject, P.Item3);
                            Result.Tags.Add(new KeyValuePair <string, object>(P.Item1.Name, P.Item3));
                        }
                        else
                        {
                            P.Item2.SetValue(EditableObject, P.Item3);
                            Result.Tags.Add(new KeyValuePair <string, object>(P.Item2.Name, P.Item3));
                        }
                    }
                    catch (Exception ex)
                    {
                        AddError(ref Errors, P.Item1?.Name ?? P.Item2.Name, ex.Message);
                    }
                }

                return(Result);
            }
            else
            {
                return(new SetEditableFormResult()
                {
                    Errors = Errors.ToArray(),
                    Tags = null
                });
            }
        }