예제 #1
0
        private static IDisposable InitControl <T, TContrl, TCtrlProp, TDataProp>(
            IPropertyManagerPageGroup @group,
            TContrl control,
            Func <TContrl, IDisposable> controlConfig,
            Expression <Func <TContrl, TCtrlProp> > ctrlPropSelector,
            IObservable <TCtrlProp> ctrlPropChangeObservable,
            T propParent,
            Expression <Func <T, TDataProp> > propSelector,
            Func <TCtrlProp, TDataProp> controlToDataConversion,
            Func <TDataProp, TCtrlProp> dataToControlConversion)
        {
            var proxy     = propSelector.GetProxy(propParent);
            var ctrlProxy = ctrlPropSelector.GetProxy(control);

            var d5 = controlConfig?.Invoke(control);

            var d2 = propParent
                     .WhenAnyValue(propSelector)
                     .Select(dataToControlConversion)
                     .Subscribe(v => ctrlProxy.Value = v);

            var d1 = ctrlPropChangeObservable
                     .Select(controlToDataConversion)
                     .Subscribe(v => proxy.Value = v);

            return(ControlHolder.Create(@group, control, d1, d2, d5));
        }
예제 #2
0
        public void CreateOption(
            string caption,
            TOption match)
        {
            var id  = _Page.NextId();
            var box = _PageGroup.CreateOption(id, caption, caption);

            _Options.Add(box);
            if (_Options.Count == 1)
            {
                box.Style = (int)swPropMgrPageOptionStyle_e.swPropMgrPageOptionStyle_FirstInGroup;
            }

            var proxy = _Selector.GetProxy(_Source);

            var d2 = _Source
                     .WhenAnyValue(_Selector)
                     .Subscribe(v1 => box.Checked = v1.Equals(match));

            var d1 = _Page.OptionCheckedObservable(id).Subscribe(v2 => proxy.Value = match);

            var d = ControlHolder.Create(_PageGroup, box, d1, d2);

            _Disposable.Add(d);
        }
        protected IDisposable CreateSelectionBox <TModel>(
            IPropertyManagerPageGroup @group,
            string tip,
            string caption,
            swSelectType_e[] selectType,
            TModel model,
            Expression <Func <TModel, SelectionData> > propertyExpr,
            Action <IPropertyManagerPageSelectionbox> config,
            Action onFocus)
        {
            var id  = NextId();
            var box = @group.CreateSelectionBox(id, caption, tip);

            config(box);
            box.SetSelectionFilters(selectType);
            var d0 = SelectionBoxFocusChangedObservable(id).Subscribe(_ => onFocus());

            var d1 = TwoWayBind(
                sourceObservable: model.WhenAnyValue(propertyExpr),
                onSourceChanged: s => ModelDoc.AddSelection(s),
                targetObservable: SelectionChangedObservable(id),
                onTargetChanged: _ => SetSelection(box, selectType, model, propertyExpr)
                );

            return(ControlHolder.Create(@group, box, d0, d1));
        }
예제 #4
0
        public static IDisposable CreateControl(PropertyManagerPageBase pmp, IPropertyManagerPageGroup @group, CompositeSourceList <SwEq> list, int index)
        {
            var equation = list.Source[index];

            var caption = equation.Id.CamelCaseToHumanReadable();
            var label   = pmp.CreateLabel(@group, caption, caption);
            var id      = pmp.NextId();
            var box     = @group.CreateNumberBox(id, caption, caption);

            if (equation.UnitsType == UnitsEnum.Angle)
            {
                box.SetRange2((int)swNumberboxUnitType_e.swNumberBox_Angle, -10, 10, true, 0.005, 0.010, 0.001);
                box.DisplayedUnit = (int)swAngleUnit_e.swDEGREES;
            }
            else
            {
                const double increment = 1e-2;
                box.SetRange2((int)swNumberboxUnitType_e.swNumberBox_Length, -10, 10, true, increment, increment * 10, increment / 10);
                box.DisplayedUnit = (int)swLengthUnit_e.swMM;
            }
            var obs = pmp.NumberBoxChangedObservable(id);
            var d2  = obs.Subscribe(value => list.ReplaceAt(index, equation.WithValue(value)));

            var d3 = list.ChangesObservable()
                     .Ignore()
                     .StartWith(Unit.Default)
                     // Don't set value when we selected another model with less equations. We will recreate the controls anyway.
                     .Where(_ => list.Source.Count > index)
                     .Subscribe(v => box.Value = list.Source[index].Val);

            return(ControlHolder.Create(@group, box, d2, label, d3));
        }
예제 #5
0
        protected IDisposable CreateButton(IPropertyManagerPageGroup @group, string tip, string caption, Action onClick, Func <IPropertyManagerPageButton, IDisposable> config = null)
        {
            var id  = NextId();
            var box = PropertyManagerGroupExtensions.CreateButton(@group, id, caption, tip);
            var d0  = ButtonPressedObservable(id).Subscribe(_ => onClick());

            return(ControlHolder.Create(@group, box, d0, config?.Invoke(box) ?? Disposable.Empty));
        }
        protected IDisposable CreateButton(IPropertyManagerPageGroup @group, string tip, string caption, Action onClick)
        {
            var id  = NextId();
            var box = PropertyManagerGroupExtensions.CreateButton(@group, id, caption, tip);
            var d0  = ButtonPressedObservable(id).Subscribe(_ => onClick());

            return(ControlHolder.Create(@group, box, d0));
        }
예제 #7
0
        private IDisposable CreateLabel(IPropertyManagerPageGroup @group, string tip, IObservable <string> captionObs, Func <IPropertyManagerPageLabel, IDisposable> config)
        {
            var id  = NextId();
            var box = @group.CreateLabel(id, string.Empty, tip);
            var d1  = config?.Invoke(box) ?? Disposable.Empty;
            var d0  = captionObs.Subscribe(caption => box.Caption = caption);

            return(ControlHolder.Create(@group, box, d0, d1));
        }
예제 #8
0
        protected IDisposable CreateTextBox(IPropertyManagerPageGroup @group, string caption, string tip, Func <string> get, Action <string> set)
        {
            var id   = NextId();
            var text = PropertyManagerGroupExtensions.CreateTextBox(@group, id, caption, tip);

            text.Text = get();
            var d = TextBoxChangedObservable(id).Subscribe(set);

            return(ControlHolder.Create(@group, text, d));
        }
예제 #9
0
        protected IDisposable CreateListBox(IPropertyManagerPageGroup @group, string caption, string tip, Func <int> get, Action <int> set, Action <IPropertyManagerPageListbox> config)
        {
            var id   = NextId();
            var list = PropertyManagerGroupExtensions.CreateListBox(@group, id, caption, tip);

            config(list);
            list.CurrentSelection = (short)get();
            var d = ListBoxSelectionObservable(id).Subscribe(set);

            return(ControlHolder.Create(@group, list, d));
        }
예제 #10
0
        protected IDisposable CreateSelectionBox <TModel>(
            IPropertyManagerPageGroup @group,
            string tip,
            string caption,
            swSelectType_e[] selectType,
            TModel model,
            Expression <Func <TModel, SelectionData> > propertyExpr,
            Action <IPropertyManagerPageSelectionbox> config,
            Action onFocus)
        {
            var d = new CompositeDisposable();

            var id  = NextId();
            var box = @group.CreateSelectionBox(id, caption, tip);

            config(box);
            box.SetSelectionFilters(selectType);

            SelectionBoxFocusChangedObservable(id)
            .Subscribe(_ => onFocus())
            .DisposeWith(d);

            var canSetSelectionSubject          = new BehaviorSubject <bool>(true).DisposeWith(d);
            Func <IDisposable> startTransaction = () =>
            {
                canSetSelectionSubject.OnNext(false);
                return(Disposable.Create(() => canSetSelectionSubject.OnNext(true)));
            };

            BindFromSource(model, propertyExpr, startTransaction)
            .DisposeWith(d);

            var inTransactionObservable = canSetSelectionSubject.AsObservable();

            BindFromTarget(selectType, model, propertyExpr, id, inTransactionObservable, box)
            .DisposeWith(d);

            return(ControlHolder.Create(@group, box, d));
        }
예제 #11
0
        protected IDisposable CreateComboBox <T, TModel>(
            IPropertyManagerPageGroup @group,
            string caption,
            string tip,
            ICompositeList <T> items,
            IEqualityComparer <T> itemEqualityComparer,
            Func <T, string> itemToStringFn,
            TModel model,
            Expression <Func <TModel, T> > selectedItemExpression,
            Action <IPropertyManagerPageCombobox> config = null)
        {
            var id       = NextId();
            var comboBox = @group.CreateComboBox(id, caption, tip);

            config?.Invoke(comboBox);

            // Sync source collection with SW ComboBox collection
            Action <short, T> insert = (index, element) =>
            {
                var insertIndex = comboBox.InsertItem(index, itemToStringFn(element));
                Debug.Assert(insertIndex != -1, "Item couldn't be inserted");
            };

            Action <short> delete = index =>
            {
                var deleteIndex = comboBox.DeleteItem(index);
                Debug.Assert(deleteIndex != -1, "Item couldn't be deleted");
            };

            var swComboBoxUpdated = new Subject <Unit>();
            var d0 = AfterActivationObs
                     .Select(_ => items.ChangesObservable(itemEqualityComparer))
                     .Switch()
                     .Subscribe(changes =>
            {
                short index = 0;
                foreach (var change in changes)
                {
                    switch (change.Operation)
                    {
                    case DiffOperation.Match:
                        break;

                    case DiffOperation.Insert:
                        insert(index++, change.ElementFromCollection2.Value);
                        break;

                    case DiffOperation.Delete:
                        delete(index);
                        break;

                    case DiffOperation.Replace:
                        delete(index);
                        insert(index++, change.ElementFromCollection2.Value);
                        break;

                    case DiffOperation.Modify:
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                swComboBoxUpdated.OnNext(Unit.Default);
            });

            // Sync source to SW selection
            var d1 = swComboBoxUpdated
                     .Select(_ => model.WhenAnyValue(selectedItemExpression))
                     .Switch()
                     .Select(selectedItem => items
                             .Items
                             .Select(list => (short)list.IndexOf(selectedItem))
                             )
                     .Switch()
                     .AssignTo(comboBox, box => box.CurrentSelection);

            // Sync SW to source selection
            var selectedItemProxy = selectedItemExpression.GetProxy(model);
            var d2 = ComboBoxSelectionObservable(id)
                     .Select(index => items
                             .Items
                             .Select(list => list[index])
                             )
                     .Switch()
                     .AssignTo(selectedItemProxy, p => p.Value);

            return(ControlHolder.Create(@group, comboBox, d0, d1, d2, swComboBoxUpdated));
        }