102 lines
3.9 KiB
C#
102 lines
3.9 KiB
C#
using bts.utils;
|
|
using mbts.collections;
|
|
using mbts.ui;
|
|
using mbts.ui.controls;
|
|
using mbts.ui.shell;
|
|
using System;
|
|
using System.ComponentModel;
|
|
using System.Windows.Input;
|
|
|
|
namespace mbts.spark {
|
|
class AlgoParamRowVM : DisposableViewModelBase, DIYGrid.IRowVM, IEditableObject, ITemplateSelectorKey, IEquatable<AlgoParamRowVM> {
|
|
bool _is_enabled;
|
|
string _template_key;
|
|
ICommand _set_value_cmd;
|
|
|
|
public AlgoParamRowVM(IAlgoVM algo, IAlgoParamVM param, AlgoParamRowVM parent)
|
|
: this(algo, param, parent, (param as ITemplateSelectorKey)?.key) { }
|
|
|
|
public AlgoParamRowVM(IAlgoVM algo, IAlgoParamVM param, AlgoParamRowVM parent, string template_key = null) {
|
|
this.param = param;
|
|
this.algo = algo;
|
|
this.parent = parent;
|
|
_template_key = template_key;
|
|
param.permissions_changed += on_permissions_changed;
|
|
_dispose = Disposable.Create(() => param.permissions_changed -= on_permissions_changed);
|
|
void on_permissions_changed(AlgoParamPermissions oldval, AlgoParamPermissions newval) => RaisePropertyChanged(nameof(permissions));
|
|
}
|
|
|
|
public event Action invalidated;
|
|
|
|
public IAlgoVM algo { get; set; }
|
|
public IAlgoParamVM param { get; }
|
|
public AlgoParamPermissions permissions => param.permissions;
|
|
public AlgoParamRowVM parent { get; }
|
|
public int level => parent == null ? 0 : parent.level + 1;
|
|
public string path_disambiguation {
|
|
get {
|
|
if (parent == null)
|
|
return string.Empty;
|
|
string pd = parent.path_disambiguation;
|
|
if (parent.algo.order_type == bts.api.orderman.CustomOrderDefinition.CustomOneofCase.Cover) {
|
|
if (pd.Length > 0)
|
|
pd += ".";
|
|
pd += parent.param.display_name;
|
|
}
|
|
return pd;
|
|
}
|
|
}
|
|
public bool is_enabled { get => _is_enabled; set => Set(ref _is_enabled, value); }
|
|
public ICommand set_value_cmd {
|
|
get {
|
|
if (_set_value_cmd == null) {
|
|
var cmd = new DelegateCommand(v => {
|
|
var orig_val = param.value;
|
|
try {
|
|
param.value = v;
|
|
} catch (InvalidCastException) {
|
|
param.value = orig_val;
|
|
}
|
|
}, _ => param.permissions == AlgoParamPermissions.ReadWrite);
|
|
concated_disposable(param.subscribe_permissions().Subscribe(_ => cmd.RaiseCanExecuteChanged()));
|
|
_set_value_cmd = cmd;
|
|
}
|
|
return _set_value_cmd;
|
|
}
|
|
}
|
|
string ITemplateSelectorKey.key => _template_key;
|
|
|
|
public void raise_invalidated() => invalidated?.Invoke();
|
|
|
|
public bool Equals(AlgoParamRowVM other) =>
|
|
other != null &&
|
|
param.id == other.param.id &&
|
|
parent.safe_equals(other.parent);
|
|
|
|
public override bool Equals(object obj) =>
|
|
Equals(obj as AlgoParamRowVM);
|
|
|
|
public override int GetHashCode() =>
|
|
EqualityComparer.aggregate_hash(param.id.GetHashCode(), parent?.GetHashCode() ?? 0);
|
|
|
|
#region IEditableObject
|
|
object _committed_value;
|
|
void IEditableObject.BeginEdit() => _committed_value = param.value;
|
|
void IEditableObject.EndEdit() => _committed_value = null;
|
|
void IEditableObject.CancelEdit() {
|
|
if (_committed_value != null)
|
|
param.value = _committed_value;
|
|
_committed_value = null;
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
class AlgoSelectorParamRowVM : AlgoParamRowVM {
|
|
readonly IAlgoSelector _selector;
|
|
|
|
public AlgoSelectorParamRowVM(IAlgoVM algo_vm, IAlgoSelector selector, AlgoParamRowVM parent) : base(algo_vm, selector, parent) =>
|
|
_selector = selector;
|
|
|
|
public ObservableKeyedCollection<IAlgoVM, AlgoKey> algos => _selector.algos;
|
|
}
|
|
} |