public LoadObjectiveFunctionsViewModel(List <Models.Roi> rois, List <Models.PlanLabel> planLabels, string defaultDirectoryPath)
        {
            DefaultDirectoryPath = defaultDirectoryPath;

            PlanLabels.Clear();
            foreach (var p in planLabels)
            {
                if (p.Label == PlanLabelCombinedDose)
                {
                    DoesUseCombinedDose = true;
                    continue;
                }
                p.LabelInObjectiveFunction = PlanLabelNone;
                PlanLabels.Add(p);
            }
            PlanLabelsInObjectiveFuntions.Add(PlanLabelNone);

            Rois = new ObservableCollection <Models.Roi>(rois);
            foreach (var r in Rois)
            {
                r.NameInObjectiveFunction = RoiNameNone;
            }
            RoiNamesInObjectiveFunctions.Add(RoiNameNone);

            OkCommand         = new DelegateCommand(() => { CanExecute = true; SetObjectiveFunctionArguments(); });
            CancelCommand     = new DelegateCommand(() => { CanExecute = false; });
            ChooseFileCommand = new DelegateCommand(ChooseFile);
        }
        public LoadObjectiveFunctionsViewModel()
        {
            List <Models.PlanLabel> planLabels0 = new List <Models.PlanLabel>();

            planLabels0.Add(new Models.PlanLabel("1-1-1", 4600));
            planLabels0[0].LabelInObjectiveFunction = PlanLabelNone;
            planLabels0.Add(new Models.PlanLabel("1-1-2", 2600));
            planLabels0[1].LabelInObjectiveFunction = PlanLabelNone;
            PlanLabels = new ObservableCollection <Models.PlanLabel>(planLabels0);

            List <Models.Roi> rois0 = new List <Models.Roi>();

            rois0.Add(new Models.Roi("PTV1"));
            rois0.Add(new Models.Roi("CTV"));
            rois0.Add(new Models.Roi("Rectum"));
            rois0.Add(new Models.Roi("Bladder"));

            Rois = new ObservableCollection <Models.Roi>(rois0);
            foreach (var r in Rois)
            {
                r.NameInObjectiveFunction = RoiNameNone;
            }
            RoiNamesInObjectiveFunctions.Add(RoiNameNone);
            PlanLabelsInObjectiveFuntions.Add(PlanLabelNone);

            Prescriptions.Add(new Models.Prescription {
                PlanLabel = "test", PrescribedDose = 300, PrescribedDoseInObjectiveFunction = 400
            });

            OkCommand         = new DelegateCommand(() => { CanExecute = true; SetObjectiveFunctionArguments(); });
            CancelCommand     = new DelegateCommand(() => { CanExecute = false; });
            ChooseFileCommand = new DelegateCommand(ChooseFile);
        }
        private void ChooseFile()
        {
            var dialog = new CommonOpenFileDialog("Choose File");

            if (Directory.Exists(DefaultDirectoryPath))
            {
                dialog.InitialDirectory = DefaultDirectoryPath;
            }

            dialog.IsFolderPicker = false;

            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                FilePath = dialog.FileName;

                using (var sr = new StreamReader(FilePath))
                    using (JsonTextReader reader = new JsonTextReader(sr))
                    {
                        ObjectiveFunctionsJObject = (JObject)JToken.ReadFrom(reader);
                    }

                var prescriptionsJArray = (JArray)ObjectiveFunctionsJObject["Prescriptions"];
                Prescriptions = prescriptionsJArray.ToObject <ObservableCollection <Models.Prescription> >();

                var descriptionJObject = ObjectiveFunctionsJObject["Description"];
                if (descriptionJObject != null)
                {
                    Description = descriptionJObject.ToObject <string>();
                }
                else
                {
                    Description = string.Empty;
                }

                var argumentsJArray = (JArray)ObjectiveFunctionsJObject["Arguments"];
                ObjectiveFunctions.Clear();
                foreach (var a in argumentsJArray)
                {
                    var jObject = JObject.Parse(a.ToString());
                    ObjectiveFunctions.Add(new Models.ObjectiveFunction(jObject));
                    string roiName = jObject["RoiName"].ToObject <string>();
                    if (!RoiNamesInObjectiveFunctions.Contains(roiName))
                    {
                        RoiNamesInObjectiveFunctions.Add(roiName);
                    }

                    string planLabel = jObject["PlanLabel"].ToObject <string>();
                    if (!(planLabel == PlanLabelCombinedDose) && !PlanLabelsInObjectiveFuntions.Contains(planLabel))
                    {
                        PlanLabelsInObjectiveFuntions.Add(planLabel);
                    }
                }
                RoiNamesInObjectiveFunctions.OrderBy(r => r);

                foreach (var r in Rois)
                {
                    if (RoiNamesInObjectiveFunctions.Contains(r.Name))
                    {
                        r.InUse = true;
                        r.NameInObjectiveFunction = r.Name;
                    }
                }

                foreach (var p in PlanLabels)
                {
                    if (p.InUse && PlanLabelsInObjectiveFuntions.Contains(p.Label))
                    {
                        p.LabelInObjectiveFunction = p.Label;
                    }
                }

                var planSumDose = PlanLabels.Select(p => p.PrescribedDose).Sum();
                foreach (var p in Prescriptions)
                {
                    var query = PlanLabels.Where(pl => pl.Label == p.PlanLabel);
                    if (query.Count() > 0)
                    {
                        p.PrescribedDose = query.First().PrescribedDose;
                        continue;
                    }

                    if (p.PlanLabel == PlanLabelCombinedDose)
                    {
                        p.PrescribedDose = planSumDose;
                    }
                }
            }
            else
            {
                Message = "\"Choose File\" is canceled";
            }
        }