Skip to content

Commit

Permalink
add save svg to wpf test app
Browse files Browse the repository at this point in the history
  • Loading branch information
levnach committed Jul 22, 2024
1 parent 29b730c commit b199357
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions GraphLayout/Test/TestWpfViewer/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ internal class App : Application {

public static readonly RoutedUICommand OpenFileCommand = new RoutedUICommand("Open File...", "OpenFileCommand",
typeof (App));
public static readonly RoutedUICommand SaveSvgCommand = new RoutedUICommand("Save SVG", "SaveSvgCommand", typeof(App));


public static readonly RoutedUICommand CancelLayoutCommand = new RoutedUICommand("Cancel Layout...", "CancelLayoutCommand",
Expand Down Expand Up @@ -605,6 +606,8 @@ void UpdateThumbOnDrag(Slider slider, double del, Thumb draggedThumb) {


void SetCommands() {
appWindow.CommandBindings.Add(new CommandBinding(SaveSvgCommand, SaveSvg));

appWindow.CommandBindings.Add(new CommandBinding(SaveImageCommand, SaveImage));
appWindow.CommandBindings.Add(new CommandBinding(SaveMsaglCommand, SaveMsagl));
appWindow.CommandBindings.Add(new CommandBinding(ExitCommand, ExitHandler));
Expand All @@ -629,6 +632,29 @@ void SetCommands() {

}

private void SaveSvg(object sender, ExecutedRoutedEventArgs e) {
if (graphViewer.Graph==null) {
MessageBox.Show("Graph is not loaded", "No Graph", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = lastFileName + ".svg"; // Default file name
dlg.DefaultExt = ".svg "; // Default file extension
dlg.Filter = "svg files (.svg )|*.svg "; // Filter files by extension

Nullable<bool> result = dlg.ShowDialog();

if (result != true) {
return;
}

using (StreamWriter writer = new StreamWriter(dlg.FileName)) {
var svgWriter = new SvgGraphWriter(writer.BaseStream, graphViewer.Graph);
svgWriter.Write();
}

}

void SaveImage(object sender, ExecutedRoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
Expand All @@ -641,6 +667,7 @@ void SaveImage(object sender, ExecutedRoutedEventArgs e)
if (result == true)
{
graphViewer.DrawImage(dlg.FileName);

}
}

Expand Down Expand Up @@ -693,7 +720,9 @@ void SetViewMenu(Menu mainMenu) {
void SetFileMenu(Menu mainMenu) {
var fileMenu = new MenuItem {Header = "_File"};
var openFileMenuItem = new MenuItem {Header = "_Open", Command = OpenFileCommand};
fileMenu.Items.Add(openFileMenuItem);
fileMenu.Items.Add(openFileMenuItem);
fileMenu.Items.Add(new MenuItem { Header = "Save _SVG", Command = SaveSvgCommand });

var reloadFileMenuItem = new MenuItem {Header = "_Reload", Command = ReloadCommand};
fileMenu.Items.Add(reloadFileMenuItem);

Expand All @@ -702,7 +731,7 @@ void SetFileMenu(Menu mainMenu) {
mainMenu.Items.Add(fileMenu);
toolBar.Items.Add(mainMenu);

var saveImageMenuItem = new MenuItem { Header = "_Save Image", Command = SaveImageCommand };
var saveImageMenuItem = new MenuItem { Header = "Save _Image", Command = SaveImageCommand };
fileMenu.Items.Add(saveImageMenuItem);

var saveMsaglMenuItem = new MenuItem { Header = "_Save MSAGL", Command = SaveMsaglCommand };
Expand Down

0 comments on commit b199357

Please sign in to comment.