Inheritance: INotifyPropertyChanged
示例#1
0
        public static ElementVM CreateElementVMByElement(Element element)
        {
            ElementVM toReturn;

            switch (element.Type)
            {
            case ElementType.Element:
                toReturn = new ElementVM(element);
                break;

            case ElementType.Port:
                toReturn = new PortVM(element);
                break;

            case ElementType.ResistorInSeries:
                toReturn = new ResistorInSeriesVM(element);
                break;

            case ElementType.ResistorInParallel:
                toReturn = new ResistorInParallelVM(element);
                break;

            case ElementType.InductorInSeries:
                toReturn = new InductorInSeriesVM(element);
                break;

            case ElementType.InductorInParallel:
                toReturn = new InductorInParallelVM(element);
                break;

            case ElementType.CapacitorInSeries:
                toReturn = new CapacitorInSeriesVM(element);
                break;

            case ElementType.CapacitorInParallel:
                toReturn = new CapacitorInParallelVM(element);
                break;

            case ElementType.OpenCircuit:
                toReturn = new OpenCircuitVM(element);
                break;

            case ElementType.ShortCircuit:
                toReturn = new ShortCircuitVM(element);
                break;

            case ElementType.TransmissionLine:
                toReturn = new TransmissionLineVM(element);
                break;

            case ElementType.ImportedComponent:
                toReturn = new ImportedComponentVM(element);
                break;

            default:
                toReturn = new ElementVM(element);
                break;
            }
            return(toReturn);
        }
示例#2
0
        private void DragTop(double scale, ElementVM elementVm, SelectionService selectionService)
        {
            double delta = elementVm.Height * (scale - 1);

            elementVm.Top    = elementVm.Top - delta;
            elementVm.Height = elementVm.Height * scale;
        }
示例#3
0
        private void DragLeft(double scale, ElementVM elementVm, SelectionService selectionService)
        {
            double delta = elementVm.Width * (scale - 1);

            elementVm.Left  = elementVm.Left - delta;
            elementVm.Width = elementVm.Width * scale;
        }
示例#4
0
        public void CreateFolder_ReturnsFolderIfItIsNew()
        {
            // arrange
            string   userId = "1234";
            string   name   = "newFolder";
            FolderVM folder = new FolderVM {
                Id = "1", Path = _rootPath, OwnerId = userId, ParentFolderId = null, IsShared = false
            };
            var mock = new Mock <IUnitOfWork>();

            mock.Setup(a => a.GetRepository <Folder>().Create(It.IsAny <Folder>()));
            FileSystemService service  = new FileSystemService(mock.Object, _mapper);
            ElementVM         expected = new ElementVM
            {
                ElemName     = name,
                IsFolder     = true,
                IsShared     = false,
                LastModified = DateTime.Now.ToShortDateString(),
                Size         = null,
                Type         = ElementVM.FileType.Folder
            };

            // act
            ElementVM result = service.CreateFolder(folder, name, userId);

            expected.Id = result.Id;

            // assert
            Assert.AreEqual(expected, result);
        }
示例#5
0
 public IActionResult AddElements([FromBody] ElementVM Elements)
 {
     try
     {
         _ElementsService.AddElement(Elements);
         return(Ok());
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
示例#6
0
        public void AddElement(ElementVM element)
        {
            var _element = new Element()
            {
                Symbol       = element.Symbol,
                NumberAtomic = element.NumberAtomic,
                Name         = element.Name
            };

            _context.Elements.Add(_element);
            _context.SaveChanges();
        }
示例#7
0
        private void Edit(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel.Project.Selected.Count != 1)
            {
                return;
            }

            ElementVM element = this.ViewModel.Project.Selected[0];

            this.ViewModel.Edit((new ImagePoint(element.X, element.Y) + new ImagePoint(element.Width / 2, element.Height / 2)).ToWindow(this.Zoom) + new WindowPoint(160, 20));

            this.EditWindow.Focus();
        }
示例#8
0
 private void LoadInfo(Element x)
 {
     if (x is Folder folder)
     {
         SelectedElement = new FolderInfoVM(folder);
     }
     else if (x is CustomImage customImage)
     {
         SelectedElement = new CustomImageInfoVM(customImage);
     }
     else if (x is CustomFile customFile)
     {
         SelectedElement = new CustomFileInfoVM(customFile);
     }
 }
示例#9
0
        public IActionResult AddFolder(string folderId, string newFolderName)
        {
            if (!_fileSystemService.CheckEditAccess(_fileSystemService.GetFolderById(folderId), User.FindFirstValue(ClaimTypes.NameIdentifier)))
            {
                throw new StatusCodeException($"You don't have access to folder with ID = {folderId}.", StatusCodes.Status403Forbidden);
            }

            ElementVM newFolder = _fileSystemService.CreateFolder(_fileSystemService.GetFolderById(folderId),
                                                                  newFolderName, User.FindFirstValue(ClaimTypes.NameIdentifier));

            if (newFolder == null)
            {
                return(BadRequest("This folder already exists."));
            }
            return(new ObjectResult(newFolder));
        }
示例#10
0
        public Copy(IList <ElementVM> source, ImagePoint point)
        {
            this.Clones = new List <ElementVM>();

            (int x_min, int y_min, int x_max, int y_max) = (int.MaxValue, int.MaxValue, int.MinValue, int.MinValue);

            for (int i = 0; i < source.Count; i++)
            {
                if (source[i].X < x_min)
                {
                    x_min = source[i].X;
                }

                if (source[i].Y < y_min)
                {
                    y_min = source[i].Y;
                }

                if (source[i].X + source[i].Width > x_max)
                {
                    x_max = source[i].X + source[i].Width;
                }

                if (source[i].Y + source[i].Height > y_max)
                {
                    y_max = source[i].Y + source[i].Height;
                }
            }

            int mid_x = x_min + ((x_max - x_min) / 2);
            int mid_y = y_min + ((y_max - y_min) / 2);

            for (int i = 0; i < source.Count; i++)
            {
                ElementVM clone = source[i].Clone();

                clone.X        = source[i].X - mid_x + point.X;
                clone.Y        = source[i].Y - mid_y + point.Y;
                clone.Optional = source[i].Optional;
                clone.IsCopy   = true;

                this.Clones.Add(clone);
            }
        }
示例#11
0
        public void CreateFolder_ReturnsNullIfFolderAlreadyExists()
        {
            // arrange
            string   userId = "1234";
            string   name   = "ExistsFolder";
            FolderVM folder = new FolderVM {
                Id = "1", Path = _rootPath, OwnerId = userId, ParentFolderId = null, IsShared = false
            };
            var mock = new Mock <IUnitOfWork>();

            mock.Setup(a => a.GetRepository <Folder>().Create(It.IsAny <Folder>()));
            FileSystemService service = new FileSystemService(mock.Object, _mapper);

            // act
            ElementVM result = service.CreateFolder(folder, name, userId);

            // assert
            Assert.IsNull(result);
        }
示例#12
0
        // this method upload files on the hard drive and saves them in the database
        public async Task <ICollection <ElementVM> > UploadFiles(IFormFileCollection files, FolderVM parentFolder)
        {
            List <ElementVM> elements = new List <ElementVM>();

            foreach (var uploadedFile in files)
            {
                string path = parentFolder.Path + '\\' + uploadedFile.FileName;

                if (!System.IO.File.Exists(path))
                {
                    DAL.Entities.File file = new DAL.Entities.File
                    {
                        Id       = Guid.NewGuid().ToString(),
                        Path     = path,
                        IsShared = parentFolder.IsShared,
                        FolderId = parentFolder.Id
                    };
                    _database.GetRepository <DAL.Entities.File>().Create(file);

                    ElementVM elem = new ElementVM
                    {
                        Id           = file.Id,
                        IsFolder     = false,
                        ElemName     = uploadedFile.FileName,
                        LastModified = DateTime.Now.ToShortDateString(),
                        Type         = ElementHelperClass.DefineFileType(ElementHelperClass.DefineFileExtension(uploadedFile.FileName)),
                        Size         = ElementHelperClass.Recount(new Tuple <double, ElementVM.Unit>(uploadedFile.Length, ElementVM.Unit.B)),
                        IsShared     = file.IsShared
                    };
                    elements.Add(elem);
                }

                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await uploadedFile.CopyToAsync(fileStream);
                }
            }
            _database.Save();
            return(elements);
        }
示例#13
0
        // this method creates a new folder on the hard drive, saves it in the database and return it
        public ElementVM CreateFolder(FolderVM parentFolder, string name, string ownerId)
        {
            if (parentFolder == null)
            {
                throw new StatusCodeException("Unable to create new folder.", StatusCodes.Status500InternalServerError);
            }

            string newFolderPath = parentFolder.Path + '\\' + name;

            if (!Directory.Exists(newFolderPath))
            {
                Directory.CreateDirectory(newFolderPath);
                Folder newFolder = new Folder
                {
                    Id             = Guid.NewGuid().ToString(),
                    Path           = newFolderPath,
                    IsShared       = parentFolder.IsShared,
                    OwnerId        = ownerId,
                    ParentFolderId = parentFolder.Id
                };
                _database.GetRepository <Folder>().Create(newFolder);
                _database.Save();

                ElementVM model = new ElementVM
                {
                    Id           = newFolder.Id,
                    IsFolder     = true,
                    Type         = ElementVM.FileType.Folder,
                    ElemName     = name,
                    LastModified = DateTime.Now.ToShortDateString(),
                    Size         = null
                };
                return(model);
            }
            return(null);
        }
示例#14
0
 public Rotate(ElementVM element, bool clockwise)
 {
     this.element   = element;
     this.clockwise = clockwise;
 }
示例#15
0
        // this method returns all folders and files that are contained in the needed folder
        public ICollection <ElementVM> GetElementsFromFolder(FolderVM folderVM, string userId)
        {
            if (folderVM == null)
            {
                throw new StatusCodeException("The needed folder doesn't exists.", StatusCodes.Status404NotFound);
            }

            DirectoryInfo dir = new DirectoryInfo(folderVM.Path);

            FileInfo[]      files;
            DirectoryInfo[] dirs;

            files = dir.GetFiles();
            dirs  = dir.GetDirectories();

            List <ElementVM> models = new List <ElementVM>();

            List <Folder>            childFolders = _database.GetRepository <Folder>().Find(f => f.ParentFolderId == folderVM.Id).ToList();
            List <DAL.Entities.File> childFiles   = _database.GetRepository <DAL.Entities.File>().Find(f => f.FolderId == folderVM.Id).ToList();

            foreach (var elem in dirs)
            {
                Folder folder = childFolders.Find(f => f.Path == elem.FullName);
                if (folder != null)
                {
                    if (CheckAccessToView(new FolderVM {
                        OwnerId = folder.OwnerId, IsShared = folder.IsShared
                    }, userId))
                    {
                        ElementVM model = new ElementVM
                        {
                            Id           = folder.Id,
                            IsFolder     = true,
                            Type         = ElementVM.FileType.Folder,
                            ElemName     = elem.Name,
                            LastModified = elem.LastWriteTime.ToShortDateString(),
                            Size         = null,
                            IsShared     = folder.IsShared
                        };

                        models.Add(model);
                    }
                }
            }

            foreach (var elem in files)
            {
                DAL.Entities.File file = childFiles.Find(f => f.Path == elem.FullName);
                if (file != null)
                {
                    FileVM vm = new FileVM {
                        IsShared = file.IsShared, OwnerId = _database.GetRepository <Folder>().Get(file.FolderId).OwnerId
                    };
                    if (CheckAccessToView(vm, userId))
                    {
                        // for a more convenient display of file size, call the conversion function
                        Tuple <double, ElementVM.Unit> size = new Tuple <double, ElementVM.Unit>(elem.Length, ElementVM.Unit.B);
                        size = ElementHelperClass.Recount(size);
                        ElementVM.FileType type = ElementHelperClass.DefineFileType(elem.Extension);

                        ElementVM model = new ElementVM
                        {
                            Id           = childFiles.Find(f => f.Path == elem.FullName).Id,
                            IsFolder     = false,
                            Type         = type,
                            ElemName     = elem.Name,
                            LastModified = elem.LastWriteTime.ToShortDateString(),
                            Size         = size,
                            IsShared     = file.IsShared
                        };
                        models.Add(model);
                    }
                }
            }

            return(models);
        }
示例#16
0
 private void DragBottom(double scale, ElementVM elementVm, SelectionService selectionService)
 {
     elementVm.Height = elementVm.Height * scale;
 }
示例#17
0
 private void DragRight(double scale, ElementVM elementVm, SelectionService selectionService)
 {
     elementVm.Width = elementVm.Width * scale;
 }
示例#18
0
        private void DrawElement(Graphics g, ElementVM e, Point offset)
        {
            Point p = new Point(e.X + offset.X, e.Y + offset.Y);

            if (e is AreaElementVM a)
            {
                Brush pen = new SolidBrush(a.Color);

                g.FillRectangle(pen, new Rectangle(p, new Size(a.Width, a.Height)));
            }
            else
            {
                Bitmap image = new Bitmap(e.Image.DrawingImage);

                // TODO: Make this work with any size of icon
                switch (e.Rotation)
                {
                case 90:
                case -270:
                    p = new Point(e.X + offset.X + 2, e.Y + offset.Y - 2);
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

                case 180:
                case -180:
                    image.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;

                case 270:
                case -90:
                    p = new Point(e.X + offset.X + 2, e.Y + offset.Y - 2);
                    image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;
                }

                g.DrawImage(image, p);
            }

            if (e.Optional)
            {
                Rectangle rect = new Rectangle(p, new Size(e.Width, e.Height));

                using SolidBrush brush = new SolidBrush(Color.FromArgb(255, 0xC0, 0xC0, 0xC0));
                using Pen pen          = new Pen(brush, 1);

                // TopLeft
                Point topLeft = rect.TopLeft();
                g.DrawLine(pen, topLeft.OffsetBy(-1, -1), topLeft.OffsetBy(2, -1));
                g.DrawLine(pen, topLeft.OffsetBy(-1, -1), topLeft.OffsetBy(-1, 2));

                // TopRight
                Point topRight = rect.TopRight();
                g.DrawLine(pen, topRight.OffsetBy(-3, -1), topRight.OffsetBy(0, -1));
                g.DrawLine(pen, topRight.OffsetBy(0, -1), topRight.OffsetBy(0, 2));

                // BottomLeft
                Point bottomLeft = rect.BottomLeft();
                g.DrawLine(pen, bottomLeft.OffsetBy(-1, 0), bottomLeft.OffsetBy(2, 0));
                g.DrawLine(pen, bottomLeft.OffsetBy(-1, 0), bottomLeft.OffsetBy(-1, -3));

                // BottomRight
                Point bottomRight = rect.BottomRight();
                g.DrawLine(pen, bottomRight.OffsetBy(-3, 0), bottomRight.OffsetBy(0, 0));
                g.DrawLine(pen, bottomRight.OffsetBy(-0, 0), bottomRight.OffsetBy(0, -3));
            }
        }
示例#19
0
 public Add(ElementVM added)
 {
     this.Added = added;
 }