From 2a32c706ec3d7a4cfcb93472a4025f46dac95030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Mus=C3=ADlek?= Date: Wed, 22 Jan 2025 17:20:23 +0100 Subject: [PATCH 1/2] insert multiple items if separated by newline when pasting --- .../GeometryWatchControl.xaml.cs | 8 ++++++- .../GraphicalWatchControl.xaml.cs | 8 ++++++- .../PlotWatchControl.xaml.cs | 10 +++++++- solution/GraphicalDebugging/Util.cs | 24 +++++++++++++++---- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/solution/GraphicalDebugging/GeometryWatchControl.xaml.cs b/solution/GraphicalDebugging/GeometryWatchControl.xaml.cs index 7c5a441..246d287 100644 --- a/solution/GraphicalDebugging/GeometryWatchControl.xaml.cs +++ b/solution/GraphicalDebugging/GeometryWatchControl.xaml.cs @@ -162,7 +162,13 @@ private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEven } else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control)) { - Util.PasteDataGridItemFromClipboard(dataGrid, Geometries); + Util.PasteDataGridItemFromClipboard(dataGrid, Geometries, (name) => + { + var newItem = new GeometryItem() { Name = name }; + newItem.PropertyChanged += GeometryItem_PropertyChanged; + return newItem; + }); + UpdateItems(false); } } diff --git a/solution/GraphicalDebugging/GraphicalWatchControl.xaml.cs b/solution/GraphicalDebugging/GraphicalWatchControl.xaml.cs index 69726cb..3a754e5 100644 --- a/solution/GraphicalDebugging/GraphicalWatchControl.xaml.cs +++ b/solution/GraphicalDebugging/GraphicalWatchControl.xaml.cs @@ -88,7 +88,13 @@ private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEven } else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control)) { - Util.PasteDataGridItemFromClipboard(dataGrid, Variables); + Util.PasteDataGridItemFromClipboard(dataGrid, Variables, (name) => + { + var newItem = new GraphicalItem() { Name = name }; + newItem.PropertyChanged += GraphicalItem_PropertyChanged; + return newItem; + }); + UpdateItems(false); } } diff --git a/solution/GraphicalDebugging/PlotWatchControl.xaml.cs b/solution/GraphicalDebugging/PlotWatchControl.xaml.cs index d1dd2f7..0b8b88f 100644 --- a/solution/GraphicalDebugging/PlotWatchControl.xaml.cs +++ b/solution/GraphicalDebugging/PlotWatchControl.xaml.cs @@ -8,10 +8,12 @@ namespace GraphicalDebugging { using System; using System.Collections.ObjectModel; + using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows; using System.Windows.Controls; + using System.Windows.Media; using System.Windows.Media.Imaging; /// @@ -163,7 +165,13 @@ private void dataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEven } else if (e.Key == System.Windows.Input.Key.V && e.KeyboardDevice.Modifiers.HasFlag(System.Windows.Input.ModifierKeys.Control)) { - Util.PasteDataGridItemFromClipboard(dataGrid, Plots); + Util.PasteDataGridItemFromClipboard(dataGrid, Plots, (name) => + { + var newItem = new PlotItem() { Name = name }; + newItem.PropertyChanged += PlotItem_PropertyChanged; + return newItem; + }); + UpdateItems(false); } } diff --git a/solution/GraphicalDebugging/Util.cs b/solution/GraphicalDebugging/Util.cs index 4ddee34..4d3d3fe 100644 --- a/solution/GraphicalDebugging/Util.cs +++ b/solution/GraphicalDebugging/Util.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; @@ -509,7 +510,8 @@ public static void EnableDataGridItems(System.Windows.Controls.DataGrid da } public static void PasteDataGridItemFromClipboard(System.Windows.Controls.DataGrid dataGrid, - System.Collections.ObjectModel.ObservableCollection itemsCollection) + System.Collections.ObjectModel.ObservableCollection itemsCollection, + Func newItemCallback) where Item : VariableItem { string text = Clipboard.GetText(); @@ -522,9 +524,23 @@ public static void PasteDataGridItemFromClipboard(System.Windows.Controls. int index = dataGrid.Items.IndexOf(dataGrid.SelectedItems[0]); if (index < 0 || index >= dataGrid.Items.Count) return; - - Item item = itemsCollection[index]; - item.Name = text; + + if (text.Contains(Environment.NewLine)) + { + // insert new items + var lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < lines.Length; i++) + { + var newItem = newItemCallback(lines[i]); + itemsCollection.Insert(index + i, (Item)newItem); + } + } + else + { + // replace existing item name + Item item = itemsCollection[index]; + item.Name = text; + } } // https://softwaremechanik.wordpress.com/2013/10/02/how-to-make-all-wpf-datagrid-cells-have-a-single-click-to-edit/ From a21951ff8e10f99ba6938f9dbf018612c4dde53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Mus=C3=ADlek?= Date: Wed, 22 Jan 2025 17:24:38 +0100 Subject: [PATCH 2/2] remove unused usings --- solution/GraphicalDebugging/PlotWatchControl.xaml.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/solution/GraphicalDebugging/PlotWatchControl.xaml.cs b/solution/GraphicalDebugging/PlotWatchControl.xaml.cs index 0b8b88f..bccb9fe 100644 --- a/solution/GraphicalDebugging/PlotWatchControl.xaml.cs +++ b/solution/GraphicalDebugging/PlotWatchControl.xaml.cs @@ -8,12 +8,10 @@ namespace GraphicalDebugging { using System; using System.Collections.ObjectModel; - using System.ComponentModel; using System.Drawing; using System.Reflection; using System.Windows; using System.Windows.Controls; - using System.Windows.Media; using System.Windows.Media.Imaging; ///