Пример #1
0
        private void Initialize()
        {
            base.PropertyChanged += PhotoCalibratedBoreIntervalVM_PropertyChanged;

            canMoveUp = new Predicate <object>(obj1 =>
            {
                if (obj1 != null)
                {
                    int order = (int)obj1;
                    return(order > 1);
                }
                else
                {
                    return(false);
                }
            });

            canMoveDown = new Predicate <object>(obj1 =>
            {
                if (obj1 != null)
                {
                    int order = (int)obj1;
                    return(order < imageRegions.SelectMany(i => i).Count());
                }
                else
                {
                    return(false);
                }
            });

            delComUp    = new DelegateCommand(obj1 => MoveRegionOrder((int)obj1, OrderMoveDirection.Up), canMoveUp);
            delComDown  = new DelegateCommand(obj1 => MoveRegionOrder((int)obj1, OrderMoveDirection.Down), canMoveDown);
            focusToNext = new DelegateCommand((obj1) =>
            {
                if (FocusedRegion != null)
                {
                    var array = CalibratedRegions.ToArray();
                    int idx   = -1;
                    for (int i = 0; i < array.Length; i++)
                    {
                        if (array[i] == FocusedRegion)
                        {
                            idx = i;
                            break;
                        }
                    }
                    if (idx != -1)
                    {
                        idx           = (idx + 1) % array.Length;
                        FocusedRegion = array[idx];
                    }
                }
            });

            addNewImageCommand = new DelegateCommand(() =>
            {
                Guid id = imageStorage.AddNewImage();
                string fileCandidate = imageStorage.GetFilePath(id);
                string name          = imageStorage.GetImageName(id);
                if (System.IO.File.Exists(fileCandidate))
                {
                    imageIDs.Add(id);
                    imageTransforms.Add(Transform.Identity);
                    imageRegions.Add(new List <CalibratedRegionVM>());
                    imageNames.Add(name);

                    CurImageIdx = imageIDs.Count - 1;
                    RaisePropertyChanged(nameof(ImagesCount));
                    if (imageIDs.Count == 1)
                    {
                        RaisePropertyChanged(nameof(ImageName));
                        RaisePropertyChanged(nameof(ImagePath));
                        RaisePropertyChanged(nameof(ImageSource));
                        RaisePropertyChanged(nameof(ImageTransform));
                        RaisePropertyChanged(nameof(CalibratedRegions));
                    }
                }
            });

            NextImageCommand = new DelegateCommand(() =>
            {
                if (imageIDs.Count > 0)
                {
                    CurImageIdx = (curImageIdx + 1) % imageIDs.Count;

                    FocusedRegion = null;
                }
            });

            RotateCurrentImageCommand = new DelegateCommand(() =>
            {
                BitmapImage bi = new BitmapImage(new Uri(ImagePath), new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore));

                Transform prevTransform = ImageTransform;

                Point imageCentre = new Point(bi.Width * 0.5, bi.Height * 0.5);

                Point movedCentre = prevTransform.Transform(imageCentre);

                Matrix orig       = ImageTransform.Value;
                Matrix additional = new RotateTransform(90.0, movedCentre.X, movedCentre.Y).Value;
                ImageTransform    = new MatrixTransform(orig * additional);
            });

            RemoveCurrentImageCommand = new DelegateCommand(() =>
            {
                var mbResult = MessageBox.Show("Удалить текущую фотографию из проекта? Все отмеченные на ней интервалы будут потеряны.", "Подтверждение удаления фотографии", MessageBoxButton.YesNo, MessageBoxImage.Question);
                switch (mbResult)
                {
                case MessageBoxResult.Yes:
                    int idxToRemove = curImageIdx;

                    var relatedRegions = imageRegions[idxToRemove];
                    foreach (var region in relatedRegions)
                    {
                        RemoveRegionByIndex(region.Order);
                    }

                    imageStorage.RemoveImage(imageIDs[idxToRemove]);

                    imageIDs.RemoveAt(idxToRemove);
                    imageNames.RemoveAt(idxToRemove);
                    imageTransforms.RemoveAt(idxToRemove);
                    imageRegions.RemoveAt(idxToRemove);

                    if (imageIDs.Count > 0)
                    {
                        CurImageIdx = (curImageIdx) % imageIDs.Count;
                    }

                    RaisePropertyChanged(nameof(ImagePath));
                    RaisePropertyChanged(nameof(ImageSource));
                    RaisePropertyChanged(nameof(ImageTransform));
                    RaisePropertyChanged(nameof(CalibratedRegions));

                    RaisePropertyChanged(nameof(ImagesCount));
                    break;

                case MessageBoxResult.No:
                    break;

                default: throw new NotImplementedException();
                }
            });
            RecalculateRegionProperties();
        }