public SwitchViewModel(int id, DiagramViewModel parent, double left, double top, bool isOn) : base(id,parent, left,top)
 {
     Init();
     ViewModelParent = parent;
     double resistance = isOn ? DiagramViewModel.ZERO_RESISTANCE : DiagramViewModel.INFINITE_RESISTANCE;
     Component = new Component(new Node(), new Node(), resistance, 0);
     Value = isOn;
 }
        public MainWindowViewModel()
        {
            _databaseAccessService = ApplicationServicesProvider.Instance.Provider.DatabaseAccessService;
            _savedDiagrams =  _databaseAccessService.ListAllDiagramIds();

            ToolBoxViewModel = new ToolBoxViewModel();
            DiagramViewModel = new DiagramViewModel();
            DeleteSelectedItemsCommand = new SimpleCommand(ExecuteDeleteSelectedItemsCommand);
            CreateNewDiagramCommand = new SimpleCommand(ExecuteCreateNewDiagramCommand);
            SaveDiagramCommand = new SimpleCommand(ExecuteSaveDiagramCommand);
            LoadDiagramCommand = new SimpleCommand(ExecuteLoadDiagramCommand);
            DeleteDiagramCommand = new SimpleCommand(ExecuteDeleteDiagramCommand);

            ConnectorViewModel.PathFinder = new OrthogonalPathFinder();
        }
 public LightBulbViewModel(int id, DiagramViewModel parent, double left, double top, double resistance) : base(id,parent, left,top)
 {
     Init();
     ViewModelParent = parent;
     Component = new Component(new Node(), new Node(), resistance, 0);
 }
 public void SetDvParent(DiagramViewModel newModel)
 {
     ViewModelParent = newModel;
 }
        private DesignerItemViewModelBase GetConnectorDataItem(DiagramViewModel diagramViewModel, int conectorDataItemId, Type connectorDataItemType)
        {
            DesignerItemViewModelBase dataItem = null;

            if (connectorDataItemType == typeof(BatteryDesignerItem))
                dataItem = diagramViewModel.Items.OfType<BatteryViewModel>().Single(x => x.Id == conectorDataItemId);
            else if (connectorDataItemType == typeof(ResistorDesignerItem))
                dataItem = diagramViewModel.Items.OfType<ResistorViewModel>().Single(x => x.Id == conectorDataItemId);
            else if(connectorDataItemType == typeof(LightBulbDesignerItem))
                dataItem = diagramViewModel.Items.OfType<LightBulbViewModel>().Single(x => x.Id == conectorDataItemId);
            else if(connectorDataItemType == typeof(SwitchDesignerItem))
                dataItem = diagramViewModel.Items.OfType<SwitchViewModel>().Single(x => x.Id == conectorDataItemId);
            else if(connectorDataItemType == typeof(VoltmeterDesignerItem))
                dataItem = diagramViewModel.Items.OfType<VoltmeterViewModel>().Single(x => x.Id == conectorDataItemId);
            else if(connectorDataItemType == typeof(AmmeterDesignerItem))
                dataItem = diagramViewModel.Items.OfType<AmmeterViewModel>().Single(x => x.Id == conectorDataItemId);

            return dataItem;
        }
        private void ExecuteLoadDiagramCommand(object parameter)
        {
            IsBusy = true;
            DiagramItem wholeDiagramToLoad = null;
            if (SavedDiagramId == null)
            {
                MessageBox.Show("No circuit selected to load!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                IsBusy = false;
                return;
            }

            Task<DiagramViewModel> task = Task.Factory.StartNew(() =>
            {
                _itemsToRemove = new List<SelectableDesignerItemViewModelBase>();           
                DiagramViewModel diagramViewModel = new DiagramViewModel();

                wholeDiagramToLoad = _databaseAccessService.FetchDiagram(SavedDiagramId.Value);

                foreach (DiagramItemData diagramItemData in wholeDiagramToLoad.DesignerItems)
                {
                    if (diagramItemData.ItemType == typeof (BatteryDesignerItem))
                    {
                        BatteryDesignerItem item = _databaseAccessService.FetchDesignerItem<BatteryDesignerItem>(diagramItemData.ItemId);
                        BatteryViewModel viewModel = new BatteryViewModel(item.Id, diagramViewModel, item.Left, item.Top, item.Voltage);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                    else if (diagramItemData.ItemType == typeof (ResistorDesignerItem))
                    {
                        ResistorDesignerItem item = _databaseAccessService.FetchDesignerItem<ResistorDesignerItem>(diagramItemData.ItemId);
                        ResistorViewModel viewModel = new ResistorViewModel(item.Id, diagramViewModel, item.Left, item.Top, item.Resistance);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                    else if (diagramItemData.ItemType == typeof (LightBulbDesignerItem))
                    {
                        LightBulbDesignerItem item = _databaseAccessService.FetchDesignerItem<LightBulbDesignerItem>(diagramItemData.ItemId);
                        LightBulbViewModel viewModel = new LightBulbViewModel(item.Id, diagramViewModel, item.Left, item.Top, item.Resistance);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                    else if (diagramItemData.ItemType == typeof (SwitchDesignerItem))
                    {
                        SwitchDesignerItem item = _databaseAccessService.FetchDesignerItem<SwitchDesignerItem>(diagramItemData.ItemId);
                        SwitchViewModel viewModel = new SwitchViewModel(item.Id, diagramViewModel, item.Left, item.Top, item.IsOn);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                    else if (diagramItemData.ItemType == typeof (VoltmeterDesignerItem))
                    {
                        VoltmeterDesignerItem item = _databaseAccessService.FetchDesignerItem<VoltmeterDesignerItem>(diagramItemData.ItemId);
                        VoltmeterViewModel viewModel = new VoltmeterViewModel(item.Id, diagramViewModel, item.Left, item.Top);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                    else if (diagramItemData.ItemType == typeof (AmmeterDesignerItem))
                    {
                        AmmeterDesignerItem item = _databaseAccessService.FetchDesignerItem<AmmeterDesignerItem>(diagramItemData.ItemId);
                        AmmeterViewModel viewModel = new AmmeterViewModel(item.Id, diagramViewModel, item.Left, item.Top);
                        diagramViewModel.Items.Add(viewModel);
                        diagramViewModel.Circuit.AddComponent(viewModel.Component);
                    }
                }

                foreach (int connectionId in wholeDiagramToLoad.ConnectionIds) // Load connection items
                {
                    Connection connection = _databaseAccessService.FetchConnection(connectionId);

                    DesignerItemViewModelBase sourceItem = GetConnectorDataItem(diagramViewModel, connection.SourceId, connection.SourceType);
                    ConnectorOrientation sourceConnectorOrientation = GetOrientationForConnector(connection.SourceOrientation);
                    FullyCreatedConnectorInfo sourceConnectorInfo = GetFullConnectorInfo(connection.Id, sourceItem, sourceConnectorOrientation);

                    DesignerItemViewModelBase sinkItem = GetConnectorDataItem(diagramViewModel, connection.SinkId, connection.SinkType);
                    ConnectorOrientation sinkConnectorOrientation = GetOrientationForConnector(connection.SinkOrientation);
                    FullyCreatedConnectorInfo sinkConnectorInfo = GetFullConnectorInfo(connection.Id, sinkItem, sinkConnectorOrientation);

                    Node left = sourceConnectorOrientation == ConnectorOrientation.Left ? sourceItem.Component.Nodes[0] : sourceItem.Component.Nodes[1];
                    Node right = sinkConnectorOrientation == ConnectorOrientation.Left ? sinkItem.Component.Nodes[0] : sinkItem.Component.Nodes[1];

                    ConnectorViewModel model = new ConnectorViewModel(connection.Id, diagramViewModel, sourceConnectorInfo, sinkConnectorInfo, left, right);
                    diagramViewModel.Items.Add(model);
                    diagramViewModel.Circuit.AddComponent(model.Component);
                }

                return diagramViewModel;
            });
            task.ContinueWith(ant =>
            {
                DiagramViewModel = ant.Result; 
                IsBusy = false;
                MessageBox.Show("Circuit \"" + wholeDiagramToLoad.Id + "\" loaded successfully.", "Load", MessageBoxButton.OK, MessageBoxImage.Information);
                DiagramViewModel.UpdateCircuit(false);
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }
 public AmmeterViewModel(int id, DiagramViewModel parent, double left, double top) : base(id,parent, left,top)
 {
     Init();
     ViewModelParent = parent;
     Component = new Component(new Node(), new Node(), DiagramViewModel.ZERO_RESISTANCE, 0);
 }
 public BatteryViewModel(int id, DiagramViewModel parent, double left, double top, double voltage) : base(id,parent, left,top)
 {
     Init();
     ViewModelParent = parent;
     Component = new Component(new Node(), new Node(), DiagramViewModel.ZERO_RESISTANCE, voltage);
 }