void MainWindow_PreviewMouseMove(object sender, MouseEventArgs e) { FrameworkElement host; lock (_dragLock) { if (_isDragging) { return; } var position = e.GetPosition(null); if (Math.Abs(position.X - _startPoint.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(position.Y - _startPoint.Y) <= SystemParameters.MinimumVerticalDragDistance) { return; } host = AppSettings.Default.View == ViewMode.TreeFull ? FlatDocView.FindAncestor <TreeViewItem>((DependencyObject)e.OriginalSource) : (FrameworkElement)FlatDocView.FindAncestor <Border>((DependencyObject)e.OriginalSource); if (host == null || host.DataContext == null || host.DataContext is PackageViewModel || !(host.DataContext is IItemViewModel)) { return; } if (AppSettings.Default.View == ViewMode.Flat && (host.DataContext is RoundViewModel || host.DataContext is ThemeViewModel)) { return; } if (DataContext == null) { return; } _isDragging = true; } var active = (QDocument)DataContext; var item = ((IItemViewModel)host.DataContext).GetModel(); InfoOwnerData itemData = null; try { itemData = new InfoOwnerData(item); FlatDocView.DoDrag(host, active, item, itemData); } catch (OutOfMemoryException) { MessageBox.Show("Ошибка копирования данных: слишком большой объём", App.ProductName, MessageBoxButton.OK, MessageBoxImage.Exclamation); } catch (InvalidOperationException) { } catch (Exception exc) { MessageBox.Show(string.Format("Ошибка копирования данных: {0}", exc.Message), App.ProductName, MessageBoxButton.OK, MessageBoxImage.Exclamation); } finally { lock (_dragLock) { _isDragging = false; } e.Handled = true; PreviewMouseMove -= MainWindow_PreviewMouseMove; PreviewMouseLeftButtonUp -= DocumentView_PreviewMouseLeftButtonUp; itemData?.Dispose(); } }
private async void Main_Drop(object sender, DragEventArgs e) { e.Handled = true; if (e.Data.GetDataPresent("FileName")) { var files = e.Data.GetData("FileName") as string[]; foreach (var file in files) { var longPath = new StringBuilder(255); GetLongPathName(file, longPath, longPath.Capacity); var longPathString = longPath.ToString(); if (Path.GetExtension(longPathString) == ".txt") { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(longPathString); } else { ApplicationCommands.Open.Execute(longPathString, this); } } e.Effects = e.AllowedEffects; return; } if (e.Data.GetDataPresent("FileContents")) { using (var contentStream = e.Data.GetData("FileContents") as MemoryStream) { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(contentStream); } e.Effects = e.AllowedEffects; return; } var treeViewItem = FlatDocView.FindAncestor <TreeViewItem>((DependencyObject)e.OriginalSource); if (treeViewItem == null) { e.Effects = DragDropEffects.None; return; } var format = FlatDocView.GetDragFormat(e); InfoOwnerData dragData = null; try { dragData = (InfoOwnerData)e.Data.GetData(typeof(InfoOwnerData)); } catch (SerializationException) { return; } var dataContext = ((IItemViewModel)treeViewItem.DataContext).GetModel(); if (dataContext == null || dataContext.GetType().ToString() == format && Equals(dataContext, dragData.ItemData)) { e.Effects = DragDropEffects.None; return; } var document = ((QDocument)DataContext).Document; e.Effects = DragDropEffects.Move; try { if (format == "siqround") { Round round = null; if (dragData != null) { round = (Round)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { round = new Round(); round.ReadXml(reader); } } if (treeViewItem.DataContext is PackageViewModel) { var package = treeViewItem.DataContext as PackageViewModel; package.Rounds.Add(new RoundViewModel(round)); await dragData.ApplyData(document); } else if (treeViewItem.DataContext is RoundViewModel) { var docRound = treeViewItem.DataContext as RoundViewModel; docRound.OwnerPackage.Rounds.Insert(docRound.OwnerPackage.Rounds.IndexOf(docRound), new RoundViewModel(round)); await dragData.ApplyData(document); } } else if (format == "siqtheme") { Theme theme = null; if (dragData != null) { theme = (Theme)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { theme = new Theme(); theme.ReadXml(reader); } } if (treeViewItem.DataContext is RoundViewModel) { var docRound = treeViewItem.DataContext as RoundViewModel; docRound.Themes.Add(new ThemeViewModel(theme)); await dragData.ApplyData(document); } else if (treeViewItem.DataContext is ThemeViewModel) { var docTheme = treeViewItem.DataContext as ThemeViewModel; docTheme.OwnerRound.Themes.Insert(docTheme.OwnerRound.Themes.IndexOf(docTheme), new ThemeViewModel(theme)); await dragData.ApplyData(document); } } else if (format == "siqquestion") { Question question = null; if (dragData != null) { question = (Question)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { question = new Question(); question.ReadXml(reader); } } if (treeViewItem.DataContext is ThemeViewModel) { var docTheme = treeViewItem.DataContext as ThemeViewModel; if (docTheme.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } docTheme.Questions.Add(new QuestionViewModel(question)); if (AppSettings.Default.ChangePriceOnMove) { FlatDocView.RecountPrices(docTheme); } await dragData.ApplyData(document); } else if (treeViewItem.DataContext is QuestionViewModel) { var docQuestion = treeViewItem.DataContext as QuestionViewModel; if (docQuestion.OwnerTheme.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } int pos = docQuestion.OwnerTheme.Questions.IndexOf(docQuestion); docQuestion.OwnerTheme.Questions.Insert(pos, new QuestionViewModel(question)); if (AppSettings.Default.ChangePriceOnMove) { FlatDocView.RecountPrices(docQuestion.OwnerTheme); } await dragData.ApplyData(document); } } else { e.Effects = DragDropEffects.None; } } finally { if (dragData != null) { dragData.Dispose(); } } }
internal static void DoDrag(FrameworkElement host, QDocument active, InfoOwner item, InfoOwnerData itemData, Action beforeDrag = null, Action afterDrag = null) { if (host == null) { throw new ArgumentNullException(nameof(host)); } if (active == null) { throw new ArgumentNullException(nameof(active)); } if (item == null) { throw new ArgumentNullException(nameof(item)); } if (itemData == null) { throw new ArgumentNullException(nameof(itemData)); } itemData.GetFullData(active.Document, item); var dataObject = new DataObject(itemData); if (host.DataContext is RoundViewModel roundViewModel) { var packageViewModel = roundViewModel.OwnerPackage; if (packageViewModel == null) { throw new ArgumentException(nameof(packageViewModel)); } int index = packageViewModel.Rounds.IndexOf(roundViewModel); active.BeginChange(); try { var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb)) { roundViewModel.Model.WriteXml(writer); } dataObject.SetData("siqround", "1"); dataObject.SetData(DataFormats.Serializable, sb); var result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move); if (result == DragDropEffects.Move) { if (packageViewModel.Rounds[index] != roundViewModel) { index++; } packageViewModel.Rounds.RemoveAt(index); active.CommitChange(); } else { active.RollbackChange(); } } catch (Exception exc) { active.RollbackChange(); throw exc; } } else { if (host.DataContext is ThemeViewModel themeViewModel) { roundViewModel = themeViewModel.OwnerRound; if (roundViewModel == null) { throw new ArgumentException(nameof(roundViewModel)); } int index = roundViewModel.Themes.IndexOf(themeViewModel); active.BeginChange(); try { var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb)) { themeViewModel.Model.WriteXml(writer); } dataObject.SetData("siqtheme", "1"); dataObject.SetData(DataFormats.Serializable, sb); var result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move); if (result == DragDropEffects.Move) { if (roundViewModel.Themes[index] != themeViewModel) { index++; } roundViewModel.Themes.RemoveAt(index); } active.CommitChange(); } catch (Exception exc) { active.RollbackChange(); throw exc; } } else { var questionViewModel = host.DataContext as QuestionViewModel; themeViewModel = questionViewModel.OwnerTheme; if (themeViewModel == null) { throw new ArgumentException(nameof(themeViewModel)); } var index = themeViewModel.Questions.IndexOf(questionViewModel); active.BeginChange(); try { var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb)) { questionViewModel.Model.WriteXml(writer); } dataObject.SetData("siqquestion", "1"); dataObject.SetData(DataFormats.Serializable, sb); beforeDrag?.Invoke(); DragDropEffects result; try { result = DragDrop.DoDragDrop(host, dataObject, DragDropEffects.Move); } catch (InvalidOperationException) { result = DragDropEffects.None; } finally { host.Opacity = 1.0; afterDrag?.Invoke(); } if (result == DragDropEffects.Move) { if (themeViewModel.Questions[index] != questionViewModel) { index++; } themeViewModel.Questions.RemoveAt(index); if (AppSettings.Default.ChangePriceOnMove) { RecountPrices(themeViewModel); } } active.CommitChange(); } catch (Exception exc) { active.RollbackChange(); throw exc; } } } }
private async void Main_Drop(object sender, DragEventArgs e) { e.Handled = true; line.Visibility = Visibility.Hidden; if (e.Data.GetDataPresent("FileName")) { var files = e.Data.GetData("FileName") as string[]; foreach (var file in files) { var longPath = new StringBuilder(255); GetLongPathName(file, longPath, longPath.Capacity); var longPathString = longPath.ToString(); if (Path.GetExtension(longPathString) == ".txt") { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(longPathString); } else { ApplicationCommands.Open.Execute(longPathString, this); } } e.Effects = e.AllowedEffects; return; } if (e.Data.GetDataPresent("FileContents")) { using (var contentStream = e.Data.GetData("FileContents") as MemoryStream) { ((MainViewModel)Application.Current.MainWindow.DataContext).ImportTxt.Execute(contentStream); } e.Effects = e.AllowedEffects; return; } var format = GetDragFormat(e); InfoOwnerData dragData = null; try { dragData = (InfoOwnerData)e.Data.GetData(typeof(InfoOwnerData)); } catch (SerializationException) { return; } e.Effects = DragDropEffects.Move; try { if (format == "siqquestion" && _insertionPosition != null) { Question question = null; if (dragData != null) { question = (Question)dragData.GetItem(); } else { var value = e.Data.GetData(DataFormats.Serializable).ToString(); using (var stringReader = new StringReader(value)) using (var reader = XmlReader.Create(stringReader)) { question = new Question(); question.ReadXml(reader); } } var themeViewModel = _insertionPosition.Item1; var index = _insertionPosition.Item2; if (themeViewModel.Questions.Any(questionViewModel => questionViewModel.Model == question)) { question = question.Clone(); } var questionViewModelNew = new QuestionViewModel(question); themeViewModel.Questions.Insert(index, questionViewModelNew); if (AppSettings.Default.ChangePriceOnMove) { RecountPrices(themeViewModel); } var document = (QDocument)DataContext; await dragData.ApplyData(document.Document); document.Navigate.Execute(questionViewModelNew); } else { e.Effects = DragDropEffects.None; } } finally { if (dragData != null) { dragData.Dispose(); } } }
void MainWindow_PreviewMouseMove(object sender, MouseEventArgs e) { FrameworkElement host; if (AppSettings.Default.FlatScale != FlatScale.Theme) { return; } lock (_dragLock) { if (_isDragging || _startPoint.X == -1) { return; } var position = e.GetPosition(null); if (Math.Abs(position.X - _startPoint.X) <= 5 * SystemParameters.MinimumHorizontalDragDistance && Math.Abs(position.Y - _startPoint.Y) <= 5 * SystemParameters.MinimumVerticalDragDistance) { return; } if (AppSettings.Default.View == ViewMode.TreeFull) { host = FindAncestor <TreeViewItem>((DependencyObject)e.OriginalSource); } else { host = FindAncestor <Border>((DependencyObject)e.OriginalSource); } if (host == null || host.DataContext == null || host.DataContext is PackageViewModel || !(host.DataContext is IItemViewModel)) { return; } if (AppSettings.Default.View == ViewMode.Flat && (host.DataContext is RoundViewModel || host.DataContext is ThemeViewModel)) { return; } if (DataContext == null) { return; } _isDragging = true; } var active = (QDocument)DataContext; var item = ((IItemViewModel)host.DataContext).GetModel(); InfoOwnerData itemData = null; try { itemData = new InfoOwnerData(item); DoDrag(host, active, item, itemData, () => { var rtb = new RenderTargetBitmap((int)host.ActualWidth, (int)host.ActualHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(host); host.Opacity = 0.5; dragImage.Width = host.ActualWidth; dragImage.Height = host.ActualHeight; dragImage.Source = rtb; dragImage.Visibility = Visibility.Visible; }, () => { dragImage.Source = null; }); } catch (OutOfMemoryException) { MessageBox.Show("Ошибка копирования данных: слишком большой объём", App.ProductName, MessageBoxButton.OK, MessageBoxImage.Exclamation); } catch (InvalidOperationException) { } finally { lock (_dragLock) { _isDragging = false; } e.Handled = true; PreviewMouseMove -= MainWindow_PreviewMouseMove; if (itemData != null) { itemData.Dispose(); } } }