/// <summary> /// Paste questions in certain position /// </summary> /// <param name="position">Position to paste questions</param> private void OnPasteQuestions(Point position) { this.ClearSelection(); // for the case when mouse position was out of UI change start position if (position.X < 0 || position.Y < 0) { position.X = position.Y = 30; } // calculate delta between topmost item and paste position BaseQuestionViewModel topmostItem = this.copiedQuestionsBuffer.OrderByDescending(x => x.Top).Last(); double deltaTop = topmostItem.Top - position.Y; double deltaLeft = topmostItem.Left - position.X; // buffer for undo/redo BaseQuestionViewModel[] copiedElements = new BaseQuestionViewModel[this.copiedQuestionsBuffer.Count]; for (int i = 0; i < this.copiedQuestionsBuffer.Count; i++) { // create new copy and update name and position BaseQuestionViewModel copy = this.copiedQuestionsBuffer[i].CreateCopy(); copy.Name = NamingManager.GetNextAvailableElementName(this.PageQuestions); copy.Top -= deltaTop; copy.Left -= deltaLeft; this.OnAddQuestion(copy); copy.IsSelected = true; copiedElements[i] = copy; } ActionTracker.TrackAction(new AddElementsAction(copiedElements, this.PageQuestions)); }
/// <summary> /// Creates copy of current question /// </summary> /// <returns>Question copy</returns> public override BaseQuestionViewModel CreateCopy() { GridViewModel elementCopy = new GridViewModel(this.Name, this.Top, this.Left, this.Width, this.Height, this.ParentTemplate, this.Orientation); elementCopy.SelectedMapping = this.SelectedMapping; foreach (ChoiceBoxViewModel choiceBox in this.ChoiceBoxes) { BaseQuestionViewModel choiceBoxCopy = choiceBox.CreateCopy(); elementCopy.ChoiceBoxes.Add((ChoiceBoxViewModel)choiceBoxCopy); } return(elementCopy); }
/// <summary> /// Removes selected elements /// </summary> private void OnRemoveElement() { BaseQuestionViewModel[] removedElements = new BaseQuestionViewModel[this.SelectedElements.Count]; int index = 0; foreach (BaseQuestionViewModel selectedItem in this.SelectedElements) { this.PageQuestions.Remove(selectedItem); removedElements[index++] = selectedItem; } this.SelectedElements.Clear(); ActionTracker.TrackAction(new RemoveElementsAction(removedElements, this.PageQuestions)); this.OnPropertyChanged(nameof(this.PropertiesContext)); }
/// <summary> /// Handles questions selection logic /// </summary> /// <param name="sender">Question with changed property</param> /// <param name="e">The event args</param> private void ElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "IsSelected") { BaseQuestionViewModel element = (BaseQuestionViewModel)sender; if (element.IsSelected) { this.SelectedElements.Add(element); } else { this.SelectedElements.Remove(element); } this.OnPropertyChanged(nameof(this.PropertiesContext)); } }
/// <summary> /// Apply selected question's style to all other questions /// </summary> private void OnApplyFormatting() { BaseQuestionViewModel ethalon = this.SelectedElements[0]; if (ethalon is ChoiceBoxViewModel) { foreach (var element in this.PageQuestions.Where(x => x is ChoiceBoxViewModel && x != ethalon)) { ((ChoiceBoxViewModel)element).ApplyStyle((ChoiceBoxViewModel)ethalon); } } else if (ethalon is GridViewModel) { foreach (var element in this.PageQuestions.Where(x => x is GridViewModel && x != ethalon)) { ((GridViewModel)element).ApplyStyle((GridViewModel)ethalon); } } }
/// <summary> /// Add element to page elements collection /// </summary> /// <param name="question">Element to add</param> private void OnAddQuestion(BaseQuestionViewModel question) { // subscribe to property changed to track selection changes question.PropertyChanged += this.ElementPropertyChanged; this.PageQuestions.Add(question); }