Esempio n. 1
0
        /// <summary>Validates the successfully converted quantity. For instance, for some values (e.g. scale values) a value of zero is not appropriate.</summary>
        /// <returns>The result of the validation</returns>
        private ValidationResult ValidateSuccessfullyConvertedQuantity(DimensionfulQuantity value)
        {
            if (null != _validationAfterSuccessfulConversion)
            {
                return(_validationAfterSuccessfulConversion(value));
            }
            else
            {
                if (_disallowNegativeValues && value.Value < 0)
                {
                    return(new ValidationResult(false, "A negative value is not allowed here"));
                }
                if (_disallowZeroValue && value.Value == 0)
                {
                    return(new ValidationResult(false, "A zero value is not allowed here"));
                }
                if (!_allowInfinity && double.IsInfinity(value.Value))
                {
                    return(new ValidationResult(false, "An infinite value is not allowed here"));
                }
                if (!_allowNaN && double.IsNaN(value.Value))
                {
                    return(new ValidationResult(false, "A NaN value ('Not a Number') is not allowed here"));
                }

                return(ValidationResult.ValidResult);
            }
        }
Esempio n. 2
0
		/// <summary>Initializes a new instance of the <see cref="ChangeableRelativeUnit"/> class.</summary>
		/// <param name="name">The full name of the unit (e.g. 'percent of page with').</param>
		/// <param name="shortcut">The shortcut of the unit (e.g. %PW).</param>
		/// <param name="divider">Used to calculate the relative value from the numeric value of the quantity. In the above example (percent of page width), the divider is 100.</param>
		/// <param name="referenceQuantity">The reference quantity.</param>
		public ChangeableRelativeUnit(string name, string shortcut, double divider, DimensionfulQuantity referenceQuantity)
		{
			_name = name;
			_shortCut = shortcut;
			_divider = divider;
			_referenceQuantity = referenceQuantity;
		}
Esempio n. 3
0
        protected virtual void OnSelectedQuantityAsValueInPointsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            var value = (double)args.NewValue;

            var quant = new DimensionfulQuantity(value, AUL.Point.Instance);

            if (null != UnitEnvironment)
            {
                quant = quant.AsQuantityIn(UnitEnvironment.DefaultUnit);
            }
            SelectedQuantity = quant;
        }
Esempio n. 4
0
        private void EhChangeUnitTo_Click(object sender, RoutedEventArgs e)
        {
            var mnu = sender as MenuItem;

            if (null != mnu && mnu.Tag is PrefixedUnit)
            {
                var prefixedUnit = (PrefixedUnit)mnu.Tag;
                var newQuantity  = new DimensionfulQuantity(SelectedQuantity.Value, prefixedUnit.Prefix, prefixedUnit.Unit);
                SelectedQuantity = newQuantity;
                if (null != _unitEnvironment)
                {
                    _unitEnvironment.DefaultUnit = prefixedUnit;
                }
            }
        }
Esempio n. 5
0
        private void EhConvertTo_Click(object sender, RoutedEventArgs e)
        {
            var mnu = sender as MenuItem;

            if (null != mnu && mnu.Tag is PrefixedUnit)
            {
                var prefixedUnit = (PrefixedUnit)mnu.Tag;
                var newQuantity  = SelectedQuantity.AsQuantityIn(prefixedUnit);
                SelectedQuantity = newQuantity;
                if (null != _unitEnvironment)
                {
                    _unitEnvironment.DefaultUnit = prefixedUnit;
                }
            }
        }
Esempio n. 6
0
        private void EhCurvePointsCopyTriggered()
        {
            var points = _view.CurvePoints;

            var dao  = Current.Gui.GetNewClipboardDataObject();
            var xcol = new Altaxo.Data.DoubleColumn();
            var ycol = new Altaxo.Data.DoubleColumn();

            for (int i = 0; i < points.Count; i++)
            {
                xcol[i] = new DimensionfulQuantity(points[i].X, AUL.Point.Instance).AsValueIn(PositionEnvironment.Instance.DefaultUnit);
                ycol[i] = new DimensionfulQuantity(points[i].Y, AUL.Point.Instance).AsValueIn(PositionEnvironment.Instance.DefaultUnit);
            }

            PutXYColumnsToClipboard(xcol, ycol);
        }
Esempio n. 7
0
        private static ValidationResult EhValidateQuantity(DimensionfulQuantity quantity)
        {
            string error = null;
            double val   = quantity.AsValueInSIUnits;

            if (double.IsInfinity(val))
            {
                error = "Value must not be infinity";
            }
            else if (double.IsNaN(val))
            {
                error = "Value must be a valid number";
            }
            else if (val < 0)
            {
                error = "Value must be a non-negative number";
            }
            else if (val > 1)
            {
                error = "Value must be less or equal than 1";
            }

            return(error == null ? ValidationResult.ValidResult : new ValidationResult(false, error));
        }
Esempio n. 8
0
        private ValidationResult ConvertValidate(string s, out DimensionfulQuantity result)
        {
            if (null != _lastConvertedQuantity && s == _lastConvertedString)
            {
                result = (DimensionfulQuantity)_lastConvertedQuantity;
                return(ValidateSuccessfullyConvertedQuantity(result));
            }

            result = new DimensionfulQuantity();
            double   parsedValue;
            SIPrefix prefix = null;

            s = s.Trim();

            if (_allowNaN && null != _representationOfNaN && s.Trim() == _representationOfNaN.Trim())
            {
                result = new DimensionfulQuantity(double.NaN, SIPrefix.None, _lastConvertedQuantity.HasValue ? _lastConvertedQuantity.Value.Unit : _unitEnvironment.DefaultUnit.Unit);
                return(ValidateSuccessfullyConvertedQuantity(result));
            }

            if (string.IsNullOrEmpty(s))
            {
                return(new ValidationResult(false, "The text box is empty. You have to enter a valid numeric quantity"));
            }

            foreach (IUnit u in _unitEnvironment.UnitsSortedByShortcutLengthDescending)
            {
                if (string.IsNullOrEmpty(u.ShortCut) || (!s.EndsWith(u.ShortCut)))
                {
                    continue;
                }

                s = s.Substring(0, s.Length - u.ShortCut.Length);

                if (!u.Prefixes.ContainsNonePrefixOnly && s.Length > 0)
                {
                    // try first prefixes of bigger length, then of smaller length
                    for (int pl = SIPrefix.MaxShortCutLength; pl > 0; --pl)
                    {
                        if (s.Length < pl)
                        {
                            continue;
                        }
                        prefix = SIPrefix.TryGetPrefixFromShortcut(s.Substring(s.Length - pl));
                        if (null != prefix)
                        {
                            s = s.Substring(0, s.Length - prefix.ShortCut.Length);
                            break;
                        }
                    }
                }

                if (double.TryParse(s, System.Globalization.NumberStyles.Float, _conversionCulture, out parsedValue))
                {
                    result = new DimensionfulQuantity(parsedValue, prefix, u);
                    return(ValidateSuccessfullyConvertedQuantity(result));
                }
                else
                {
                    string firstPart;
                    if (null != prefix)
                    {
                        firstPart = string.Format("The last part \"{0}\" of your entered text is recognized as prefixed unit, ", prefix.ShortCut + u.ShortCut);
                    }
                    else
                    {
                        firstPart = string.Format("The last part \"{0}\" of your entered text is recognized as unit, ", u.ShortCut);
                    }

                    string lastPart;
                    if (string.IsNullOrEmpty(s.Trim()))
                    {
                        lastPart = string.Format("but the first part is empty. You have to prepend a numeric value!");
                    }
                    else
                    {
                        lastPart = string.Format("but the first part \"{0}\" is not recognized as a numeric value!", s);
                    }

                    return(new ValidationResult(false, firstPart + lastPart));
                }
            }

            // if nothing is found in this way, we try to split the text
            var parts = s.Split(new char[] { ' ', '\t' }, 2, StringSplitOptions.RemoveEmptyEntries);

            // try to parse the first part as a number
            if (!double.TryParse(parts[0], System.Globalization.NumberStyles.Float, _conversionCulture, out parsedValue))
            {
                return(new ValidationResult(false, string.Format("The part \"{0}\" of your entered text was not recognized as a numeric value.", parts[0])));
            }

            string unitString = parts.Length >= 2 ? parts[1] : string.Empty;

            if (string.IsNullOrEmpty(unitString))
            {
                if (null != _lastConvertedQuantity)
                {
                    result = new DimensionfulQuantity(parsedValue, ((DimensionfulQuantity)_lastConvertedQuantity).Prefix, ((DimensionfulQuantity)_lastConvertedQuantity).Unit);
                    return(ValidateSuccessfullyConvertedQuantity(result));
                }
                else if (null != _unitEnvironment && null != _unitEnvironment.DefaultUnit)
                {
                    result = new DimensionfulQuantity(parsedValue, _unitEnvironment.DefaultUnit.Prefix, _unitEnvironment.DefaultUnit.Unit);
                    return(ValidateSuccessfullyConvertedQuantity(result));
                }
                else
                {
                    return(new ValidationResult(false, "No unit was given by you and no default unit could be deduced from the environment!"));
                }
            }
            else
            {
                return(new ValidationResult(false, GetErrorStringForUnrecognizedUnit(unitString)));
            }
        }
Esempio n. 9
0
		private void EhChangeUnitTo_Click(object sender, RoutedEventArgs e)
		{
			var mnu = sender as MenuItem;
			if (null != mnu && mnu.Tag is PrefixedUnit)
			{
				var prefixedUnit = (PrefixedUnit)mnu.Tag;
				var newQuantity = new DimensionfulQuantity(SelectedQuantity.Value, prefixedUnit.Prefix, prefixedUnit.Unit);
				SelectedQuantity = newQuantity;
				if (null != _unitEnvironment)
					_unitEnvironment.DefaultUnit = prefixedUnit;
			}
		}
Esempio n. 10
0
		private void EhConvertTo_Click(object sender, RoutedEventArgs e)
		{
			var mnu = sender as MenuItem;
			if (null != mnu && mnu.Tag is PrefixedUnit)
			{
				var prefixedUnit = (PrefixedUnit)mnu.Tag;
				var newQuantity = SelectedQuantity.AsQuantityIn(prefixedUnit);
				SelectedQuantity = newQuantity;
				if (null != _unitEnvironment)
					_unitEnvironment.DefaultUnit = prefixedUnit;
			}
		}
Esempio n. 11
0
		/// <summary>Validates the successfully converted quantity. For instance, for some values (e.g. scale values) a value of zero is not appropriate.</summary>
		/// <returns>The result of the validation</returns>
		private ValidationResult ValidateSuccessfullyConvertedQuantity(DimensionfulQuantity value)
		{
			if (null != _validationAfterSuccessfulConversion)
			{
				return _validationAfterSuccessfulConversion(value);
			}
			else
			{
				if (_disallowNegativeValues && value.Value < 0)
					return new ValidationResult(false, "A negative value is not allowed here");
				if (_disallowZeroValue && value.Value == 0)
					return new ValidationResult(false, "A zero value is not allowed here");
				if (!_allowInfinity && double.IsInfinity(value.Value))
					return new ValidationResult(false, "An infinite value is not allowed here");
				if (!_allowNaN && double.IsNaN(value.Value))
					return new ValidationResult(false, "A NaN value ('Not a Number') is not allowed here");

				return ValidationResult.ValidResult;
			}
		}
Esempio n. 12
0
		private ValidationResult ConvertValidate(string s, out DimensionfulQuantity result)
		{
			if (null != _lastConvertedQuantity && s == _lastConvertedString)
			{
				result = (DimensionfulQuantity)_lastConvertedQuantity;
				return ValidateSuccessfullyConvertedQuantity(result);
			}

			result = new DimensionfulQuantity();
			double parsedValue;
			SIPrefix prefix = null;
			s = s.Trim();

			if (string.IsNullOrEmpty(s))
				return new ValidationResult(false, "The text box is empty. You have to enter a valid numeric quantity");

			foreach (IUnit u in _unitEnvironment.UnitsSortedByShortcutLengthDescending)
			{
				if (string.IsNullOrEmpty(u.ShortCut) || (!s.EndsWith(u.ShortCut)))
					continue;

				s = s.Substring(0, s.Length - u.ShortCut.Length);

				if (!u.Prefixes.ContainsNonePrefixOnly && s.Length > 0)
				{
					// try first prefixes of bigger length, then of smaller length
					for (int pl = SIPrefix.MaxShortCutLength; pl > 0; --pl)
					{
						if (s.Length < pl)
							continue;
						prefix = SIPrefix.TryGetPrefixFromShortcut(s.Substring(s.Length - pl));
						if (null != prefix)
						{
							s = s.Substring(0, s.Length - prefix.ShortCut.Length);
							break;
						}
					}
				}

				if (double.TryParse(s, System.Globalization.NumberStyles.Float, _conversionCulture, out parsedValue))
				{
					result = new DimensionfulQuantity(parsedValue, prefix, u);
					return ValidateSuccessfullyConvertedQuantity(result);
				}
				else
				{
					string firstPart;
					if (null != prefix)
						firstPart = string.Format("The last part \"{0}\" of your entered text is recognized as prefixed unit, ", prefix.ShortCut + u.ShortCut);
					else
						firstPart = string.Format("The last part \"{0}\" of your entered text is recognized as unit, ", u.ShortCut);

					string lastPart;
					if (string.IsNullOrEmpty(s.Trim()))
						lastPart = string.Format("but the first part is empty. You have to prepend a numeric value!");
					else
						lastPart = string.Format("but the first part \"{0}\" is not recognized as a numeric value!", s);

					return new ValidationResult(false, firstPart + lastPart);
				}
			}

			// if nothing is found in this way, we try to split the text
			var parts = s.Split(new char[] { ' ', '\t' }, 2, StringSplitOptions.RemoveEmptyEntries);

			// try to parse the first part as a number
			if (!double.TryParse(parts[0], System.Globalization.NumberStyles.Float, _conversionCulture, out parsedValue))
				return new ValidationResult(false, string.Format("The part \"{0}\" of your entered text was not recognized as a numeric value.", parts[0]));

			string unitString = parts.Length >= 2 ? parts[1] : string.Empty;

			if (string.IsNullOrEmpty(unitString))
			{
				if (null != _lastConvertedQuantity)
				{
					result = new DimensionfulQuantity(parsedValue, ((DimensionfulQuantity)_lastConvertedQuantity).Prefix, ((DimensionfulQuantity)_lastConvertedQuantity).Unit);
					return ValidateSuccessfullyConvertedQuantity(result);
				}
				else if (null != _unitEnvironment && null != _unitEnvironment.DefaultUnit)
				{
					result = new DimensionfulQuantity(parsedValue, _unitEnvironment.DefaultUnit.Prefix, _unitEnvironment.DefaultUnit.Unit);
					return ValidateSuccessfullyConvertedQuantity(result);
				}
				else
				{
					return new ValidationResult(false, "No unit was given by you and no default unit could be deduced from the environment!");
				}
			}
			else
			{
				return new ValidationResult(false, GetErrorStringForUnrecognizedUnit(unitString));
			}
		}
Esempio n. 13
0
		/// <summary>Initializes a new instance of the <see cref="ChangeableRelativePercentUnit"/> class.</summary>
		/// <param name="name">The full name of the unit (e.g. 'percent of page with').</param>
		/// <param name="shortcut">The shortcut of the unit (e.g. %PW).</param>
		/// <param name="valueForHundredPercent">The quantity that corresponds to a value of hundred percent.</param>
		public ChangeableRelativePercentUnit(string name, string shortcut, DimensionfulQuantity valueForHundredPercent)
			: base(name, shortcut, 100, valueForHundredPercent)
		{
		}
		private void EhCurvePointsCopyTriggered()
		{
			var points = _view.CurvePoints;

			var dao = Current.Gui.GetNewClipboardDataObject();
			Altaxo.Data.DoubleColumn xcol = new Altaxo.Data.DoubleColumn();
			Altaxo.Data.DoubleColumn ycol = new Altaxo.Data.DoubleColumn();
			for (int i = 0; i < points.Count; i++)
			{
				xcol[i] = new DimensionfulQuantity(points[i].X, Units.Length.Point.Instance).AsValueIn(PositionEnvironment.Instance.DefaultUnit);
				ycol[i] = new DimensionfulQuantity(points[i].Y, Units.Length.Point.Instance).AsValueIn(PositionEnvironment.Instance.DefaultUnit);
			}

			Altaxo.Data.DataTable tb = new Altaxo.Data.DataTable();
			tb.DataColumns.Add(xcol, "XPosition", Altaxo.Data.ColumnKind.V, 0);
			tb.DataColumns.Add(ycol, "YPosition", Altaxo.Data.ColumnKind.V, 0);
			Altaxo.Worksheet.Commands.EditCommands.WriteAsciiToClipBoardIfDataCellsSelected(
				tb, new Altaxo.Collections.AscendingIntegerCollection(),
				new Altaxo.Collections.AscendingIntegerCollection(),
				new Altaxo.Collections.AscendingIntegerCollection(),
				dao);
			Current.Gui.SetClipboardDataObject(dao, true);
		}