Add functionality to detect user versus code driven change events
This commit is contained in:
parent
6996261a65
commit
4fec085e14
7 changed files with 115 additions and 71 deletions
|
|
@ -35,29 +35,26 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
public class BasicField
|
||||
{
|
||||
public event EventHandler ValueChanged;
|
||||
public event EventHandler<FieldChangedEventArgs> ValueChanged;
|
||||
|
||||
private string fieldValue;
|
||||
public string Value
|
||||
public void SetValue(string newValue, bool userInitiated)
|
||||
{
|
||||
get => fieldValue;
|
||||
set
|
||||
{
|
||||
string convertedValue = this.ConvertValue(value);
|
||||
string convertedValue = this.ConvertValue(newValue);
|
||||
|
||||
if (fieldValue != convertedValue)
|
||||
{
|
||||
fieldValue = convertedValue;
|
||||
this.OnValueChanged();
|
||||
}
|
||||
else if (value != convertedValue)
|
||||
{
|
||||
// If the validated value matches the current value, then UI element values were rejected and must be discarded
|
||||
this.OnValueChanged();
|
||||
}
|
||||
if (this.Value != convertedValue)
|
||||
{
|
||||
this.Value = convertedValue;
|
||||
this.OnValueChanged(new FieldChangedEventArgs(userInitiated));
|
||||
}
|
||||
else if (newValue != convertedValue)
|
||||
{
|
||||
// If the validated value matches the current value, then UI element values were rejected and must be discarded
|
||||
this.OnValueChanged(new FieldChangedEventArgs(userInitiated));
|
||||
}
|
||||
}
|
||||
|
||||
public string Value { get; private set; }
|
||||
|
||||
public GuiWidget Content { get; protected set; }
|
||||
|
||||
public string HelpText { get; set; }
|
||||
|
|
@ -69,9 +66,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
return newValue;
|
||||
}
|
||||
|
||||
protected virtual void OnValueChanged()
|
||||
protected virtual void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
ValueChanged?.Invoke(this, new EventArgs());
|
||||
ValueChanged?.Invoke(this, fieldChangedEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -93,19 +90,21 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
if (this.Value != numberEdit.Value.ToString())
|
||||
{
|
||||
this.Value = numberEdit.Value.ToString();
|
||||
this.SetValue(
|
||||
numberEdit.Value.ToString(),
|
||||
userInitiated: true);
|
||||
}
|
||||
};
|
||||
|
||||
this.Content = numberEdit;
|
||||
}
|
||||
|
||||
protected override void OnValueChanged()
|
||||
protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
int.TryParse(this.Value, out int currentValue);
|
||||
numberEdit.ActuallNumberEdit.Value = currentValue;
|
||||
|
||||
base.OnValueChanged();
|
||||
base.OnValueChanged(fieldChangedEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -127,21 +126,24 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
if (this.Value != textEditWidget.Text)
|
||||
{
|
||||
this.Value = textEditWidget.Text;
|
||||
this.SetValue(
|
||||
textEditWidget.Text,
|
||||
userInitiated: true);
|
||||
}
|
||||
};
|
||||
|
||||
this.Content = textEditWidget;
|
||||
}
|
||||
|
||||
protected override void OnValueChanged()
|
||||
|
||||
protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
if (this.Value != textEditWidget.Text)
|
||||
{
|
||||
textEditWidget.Text = this.Value;
|
||||
}
|
||||
|
||||
base.OnValueChanged();
|
||||
base.OnValueChanged(fieldChangedEventArgs);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,11 +158,16 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
this.uiField = uiField;
|
||||
}
|
||||
|
||||
public string Value { get => uiField.Value; set => uiField.Value = value; }
|
||||
public void SetValue(string newValue, bool userInitiated)
|
||||
{
|
||||
uiField.SetValue(newValue, userInitiated);
|
||||
}
|
||||
|
||||
public string Value { get => uiField.Value; }
|
||||
|
||||
public GuiWidget Content { get; private set; }
|
||||
|
||||
public event EventHandler ValueChanged;
|
||||
public event EventHandler<FieldChangedEventArgs> ValueChanged;
|
||||
|
||||
public void Initialize(int tabIndex)
|
||||
{
|
||||
|
|
@ -181,7 +188,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
|
||||
newItem.Selected += (s, e) =>
|
||||
{
|
||||
uiField.Value = valueLocal;
|
||||
uiField.SetValue(valueLocal, userInitiated: true);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
};
|
||||
checkBoxWidget.CheckedStateChanged += (s, e) =>
|
||||
{
|
||||
this.Value = checkBoxWidget.Checked ? "1" : "0";
|
||||
this.SetValue(
|
||||
checkBoxWidget.Checked ? "1" : "0",
|
||||
userInitiated: true);
|
||||
};
|
||||
|
||||
this.Content = checkBoxWidget;
|
||||
|
|
|
|||
|
|
@ -40,8 +40,6 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
private DropDownList dropdownList;
|
||||
|
||||
public string HelpText { get; set; }
|
||||
|
||||
public void Initialize(int tabIndex)
|
||||
{
|
||||
EventHandler unregisterEvents = null;
|
||||
|
|
@ -90,12 +88,10 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
this.Content = dropdownList;
|
||||
}
|
||||
|
||||
protected override void OnValueChanged()
|
||||
protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
// Lookup the machine specific comport value rather than the passed in text value
|
||||
dropdownList.SelectedLabel = ActiveSliceSettings.Instance.Helpers.ComPort();
|
||||
|
||||
base.OnValueChanged();
|
||||
base.OnValueChanged(fieldChangedEventArgs);
|
||||
}
|
||||
|
||||
private void RebuildMenuItems()
|
||||
|
|
@ -120,7 +116,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
if (sender is MenuItem menuItem)
|
||||
{
|
||||
this.Value = menuItem.Text;
|
||||
this.SetValue(
|
||||
menuItem.Text,
|
||||
userInitiated: true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,11 +32,23 @@ using MatterHackers.Agg.UI;
|
|||
|
||||
namespace MatterHackers.MatterControl.SlicerConfiguration
|
||||
{
|
||||
public class FieldChangedEventArgs
|
||||
{
|
||||
public FieldChangedEventArgs(bool userInitiated)
|
||||
{
|
||||
this.UserInitiated = userInitiated;
|
||||
}
|
||||
|
||||
public bool UserInitiated { get; }
|
||||
}
|
||||
|
||||
public interface IUIField
|
||||
{
|
||||
event EventHandler ValueChanged;
|
||||
event EventHandler<FieldChangedEventArgs> ValueChanged;
|
||||
|
||||
string Value { get; set; }
|
||||
string Value { get; }
|
||||
|
||||
void SetValue(string newValue, bool userInitiated);
|
||||
|
||||
void Initialize(int tabIndex);
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
{
|
||||
if (sender is MenuItem menuItem)
|
||||
{
|
||||
this.Value = menuItem.Text;
|
||||
this.SetValue(
|
||||
menuItem.Text,
|
||||
userInitiated: true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -70,10 +72,10 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
this.Content = dropdownList;
|
||||
}
|
||||
|
||||
protected override void OnValueChanged()
|
||||
protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
dropdownList.SelectedLabel = this.Value;
|
||||
base.OnValueChanged();
|
||||
base.OnValueChanged(fieldChangedEventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
};
|
||||
xEditWidget.ActuallNumberEdit.EditComplete += (sender, e) =>
|
||||
{
|
||||
this.Value = string.Format("{0},{1}", xEditWidget.ActuallNumberEdit.Value.ToString(), yEditWidget.ActuallNumberEdit.Value.ToString());
|
||||
this.SetValue(
|
||||
string.Format("{0},{1}", xEditWidget.ActuallNumberEdit.Value.ToString(), yEditWidget.ActuallNumberEdit.Value.ToString()),
|
||||
userInitiated: true);
|
||||
};
|
||||
|
||||
container.AddChild(new TextWidget("X:", pointSize: 10, textColor: ActiveTheme.Instance.PrimaryTextColor)
|
||||
|
|
@ -81,7 +83,9 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
};
|
||||
yEditWidget.ActuallNumberEdit.EditComplete += (sender, e) =>
|
||||
{
|
||||
this.Value = string.Format("{0},{1}", xEditWidget.ActuallNumberEdit.Value.ToString(), yEditWidget.ActuallNumberEdit.Value.ToString());
|
||||
this.SetValue(
|
||||
string.Format("{0},{1}", xEditWidget.ActuallNumberEdit.Value.ToString(), yEditWidget.ActuallNumberEdit.Value.ToString()),
|
||||
userInitiated: true);
|
||||
};
|
||||
|
||||
container.AddChild(new TextWidget("Y:", pointSize: 10, textColor: ActiveTheme.Instance.PrimaryTextColor)
|
||||
|
|
@ -94,7 +98,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
this.Content = container;
|
||||
}
|
||||
|
||||
protected override void OnValueChanged()
|
||||
protected override void OnValueChanged(FieldChangedEventArgs fieldChangedEventArgs)
|
||||
{
|
||||
string[] xyValueStrings2 = this.Value.Split(',');
|
||||
if (xyValueStrings2.Length != 2)
|
||||
|
|
@ -108,7 +112,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration
|
|||
double.TryParse(xyValueStrings2[1], out currentValue);
|
||||
yEditWidget.ActuallNumberEdit.Value = currentValue;
|
||||
|
||||
base.OnValueChanged();
|
||||
base.OnValueChanged(fieldChangedEventArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue