Skip to content

Commit

Permalink
Merge pull request #37 from arthurits/SaveFileUpdate
Browse files Browse the repository at this point in the history
Save file update
  • Loading branch information
arthurits authored Oct 16, 2024
2 parents b7ebe2a + 79f02ff commit 7fd3300
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 157 deletions.
5 changes: 3 additions & 2 deletions ErgoCalc/IChildResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public interface IChildResults
/// <summary>
/// Saves the data shown in the child window into a file
/// </summary>
/// <param name="path">Path where the data should be saved</param>
void Save(string path);
/// <param name="directoryPath">Initial directory to be set in <see langword="SaveFileDialog"/></param>
/// <returns>Path selected by the user in <see langword="SaveFileDialog"/> to save the data</returns>
string Save(string directoryPath);

/// <summary>
/// Opens a document
Expand Down
5 changes: 4 additions & 1 deletion ErgoCalc/UserInteractionGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ private void Open_Click(object sender, EventArgs e)
private void Save_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
((IChildResults)this.ActiveMdiChild).Save(string.Empty);
{
string path = ((IChildResults)this.ActiveMdiChild).Save(_settings.RememberFileDialogPath ? _settings.UserSavePath : _settings.DefaultSavePath);
if (_settings.RememberFileDialogPath) _settings.UserSavePath = path;
}
}

private void Copy_Click(object sender, EventArgs e)
Expand Down
71 changes: 42 additions & 29 deletions ErgoCalc/frmResultsCLM.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using ErgoCalc.Models.CLM;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace ErgoCalc;

Expand Down Expand Up @@ -115,20 +116,23 @@ private void SerializeToJSON(Utf8JsonWriter writer)

public ModelType? Model { get; set; }

public void Save(string path)
public string Save(string directoryPath)
{
DialogResult result;
string userPath = string.Empty;

// Displays a SaveFileDialog so the user can save the Image
SaveFileDialog SaveDlg = new()
{
DefaultExt = "*.csv",
Filter = "ERGO file (*.ergo)|*.ergo|RTF file (*.rtf)|*.rtf|Text file (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 1,
Title = "Save CLM data",
FileName = "CLM results",
Title = "Save CLM results",
OverwritePrompt = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
InitialDirectory = string.IsNullOrWhiteSpace(directoryPath) ? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) : directoryPath
};

DialogResult result;
using (new CenterWinDialog(this))
{
result = SaveDlg.ShowDialog(this.Parent);
Expand All @@ -139,34 +143,43 @@ public void Save(string path)
{
using var fs = SaveDlg.OpenFile();

switch (SaveDlg.FilterIndex)
{
case 1:
if (fs != null)
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
//var jsonString = JsonSerializer.Serialize(_datos[0]._points[0], new JsonSerializerOptions { WriteIndented = true });
}
break;
case 2:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.RichText);
break;
case 3:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.PlainText);
break;
case 4:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.UnicodePlainText);
break;
}

FrmMain.SetFormTitle(this, StringResources.FormResultsCLM, SaveDlg.FileName);

using (new CenterWinDialog(this.MdiParent))
// Saves the text via a FileStream created by the OpenFile method.
if (fs != null)
{
MessageBox.Show(this, "The file was successfully saved", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Get the actual directory path selected by the user in order to store it later in the settings
userPath = Path.GetDirectoryName(SaveDlg.FileName) ?? string.Empty;

// Saves the text in the appropriate TextFormat based upon the File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (SaveDlg.FilterIndex)
{
case 1:
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
//var jsonString = JsonSerializer.Serialize(_datos[0]._points[0], new JsonSerializerOptions { WriteIndented = true });
}
break;
case 2:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.RichText);
break;
case 3:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.PlainText);
break;
case 4:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.UnicodePlainText);
break;
}

FrmMain.SetFormTitle(this, StringResources.FormResultsCLM, SaveDlg.FileName);

using (new CenterWinDialog(this.MdiParent))
{
MessageBox.Show(this, "The file was successfully saved", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
return userPath;
}

public bool OpenFile(JsonDocument document)
Expand Down
71 changes: 42 additions & 29 deletions ErgoCalc/frmResultsLiberty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,23 @@ private void SerializeToJSON(Utf8JsonWriter writer)

public bool[] GetToolbarEnabledState() => [true, true, true, false, true, true, true, true, true, false, false, true, true, true];

public void Save(string path)
public string Save(string directoryPath)
{
DialogResult result;
string userPath = string.Empty;

// Displays a SaveFileDialog so the user can save the Image
SaveFileDialog SaveDlg = new()
{
DefaultExt = "*.csv",
Filter = "ERGO file (*.ergo)|*.ergo|RTF file (*.rtf)|*.rtf|Text file (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 1,
Title = "Save LM-MMH data",
FileName = "LM-MMH results",
Title = "Save LM-MMH results",
OverwritePrompt = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
InitialDirectory = string.IsNullOrWhiteSpace(directoryPath) ? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) : directoryPath
};

DialogResult result;
using (new CenterWinDialog(this))
{
result = SaveDlg.ShowDialog(this.Parent);
Expand All @@ -290,34 +293,44 @@ public void Save(string path)
{
using var fs = SaveDlg.OpenFile();

switch (SaveDlg.FilterIndex)
{
case 1:
if (fs != null)
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
//var jsonString = JsonSerializer.Serialize(_datos[0]._points[0], new JsonSerializerOptions { WriteIndented = true });
}
break;
case 2:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.RichText);
break;
case 3:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.PlainText);
break;
case 4:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.UnicodePlainText);
break;
}

FrmMain.SetFormTitle(this, StringResources.FormResultsLiberty, SaveDlg.FileName);

using (new CenterWinDialog(this.MdiParent))
// Saves the text via a FileStream created by the OpenFile method.
if (fs is not null)
{
MessageBox.Show(this, "The file was successfully saved", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
// Get the actual directory path selected by the user in order to store it later in the settings
userPath = Path.GetDirectoryName(SaveDlg.FileName) ?? string.Empty;

// Saves the text in the appropriate TextFormat based upon the File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (SaveDlg.FilterIndex)
{
case 1:
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
//var jsonString = JsonSerializer.Serialize(_datos[0]._points[0], new JsonSerializerOptions { WriteIndented = true });
}
break;
case 2:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.RichText);
break;
case 3:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.PlainText);
break;
case 4:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.UnicodePlainText);
break;
}

FrmMain.SetFormTitle(this, StringResources.FormResultsLiberty, SaveDlg.FileName);

using (new CenterWinDialog(this.MdiParent))
{
MessageBox.Show(this, "The file was successfully saved", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

return userPath;
}

public bool OpenFile(JsonDocument document)
Expand Down
19 changes: 13 additions & 6 deletions ErgoCalc/frmResultsLifting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ public ToolStrip? ChildToolStrip
}
public ModelType? Model { get; set; }

public void Save(string path)
public string Save(string directoryPath)
{
DialogResult result;
string userPath = string.Empty;

// Displays a SaveFileDialog so the user can save the results. More information here: https://msdn.microsoft.com/en-us/library/ms160336(v=vs.110).aspx
SaveFileDialog SaveDlg = new()
{
Expand All @@ -197,10 +200,9 @@ public void Save(string path)
},
Title = "Save lifting model results",
OverwritePrompt = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
InitialDirectory = string.IsNullOrWhiteSpace(directoryPath) ? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) : directoryPath
};

DialogResult result;
using (new CenterWinDialog(this.MdiParent))
{
result = SaveDlg.ShowDialog(this);
Expand All @@ -212,15 +214,20 @@ public void Save(string path)
using var fs = SaveDlg.OpenFile();

// Saves the text via a FileStream created by the OpenFile method.
if (fs != null)
if (fs is not null)
{
// Get the actual directory path selected by the user in order to store it later in the settings
userPath = Path.GetDirectoryName(SaveDlg.FileName) ?? string.Empty;

// Saves the text in the appropriate TextFormat based upon the File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (SaveDlg.FilterIndex)
{
case 1:
using (var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true }))
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
}
break;
case 2:
rtbShowResult.SaveFile(fs, RichTextBoxStreamType.RichText);
Expand All @@ -242,7 +249,7 @@ public void Save(string path)
}
}

return;
return userPath;
}

public bool OpenFile(JsonDocument document)
Expand Down
4 changes: 2 additions & 2 deletions ErgoCalc/frmResultsMet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ private void ShowResults(bool compute = true)

public bool[] GetToolbarEnabledState() => [true, true, false, false, true, true, false, false, true, false, false, true, true, true];

public void Save(string path)
public string Save(string directoryPath)
{

return string.Empty;
}

public bool OpenFile(JsonDocument document)
Expand Down
25 changes: 19 additions & 6 deletions ErgoCalc/frmResultsOCRAcheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,41 @@ private void SerializeToJSON(Utf8JsonWriter writer)

public bool[] GetToolbarEnabledState() => [true, true, true, false, true, true, false, true, true, false, false, true, true, true];

public void Save(string path)
public string Save(string directoryPath)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog
DialogResult result;
string userPath = string.Empty;

SaveFileDialog SaveDlg = new()
{
DefaultExt = "*.rtf",
Filter = "ERGO file (*.ergo)|*.ergo|RTF file (*.rtf)|*.rtf|Text file (*.txt)|*.txt|All files (*.*)|*.*",
FilterIndex = 2,
FileName = "OCRA checklist results",
Title = "Save OCRA checklist results",
OverwritePrompt = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
InitialDirectory = string.IsNullOrWhiteSpace(directoryPath) ? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) : directoryPath
};

DialogResult result;
using (new CenterWinDialog(this))
{
result = saveFileDialog1.ShowDialog();
result = SaveDlg.ShowDialog();
}

// If the file name is not an empty string open it for saving.
if (result == DialogResult.OK && saveFileDialog1.FileName != "")
if (result == DialogResult.OK && SaveDlg.FileName != "")
{
using var fs = SaveDlg.OpenFile();

// Saves the text via a FileStream created by the OpenFile method.
if (fs is not null)
{
// Get the actual directory path selected by the user in order to store it later in the settings
userPath = Path.GetDirectoryName(SaveDlg.FileName) ?? string.Empty;
}
}

return userPath;
}

public bool OpenFile(JsonDocument document)
Expand Down
17 changes: 11 additions & 6 deletions ErgoCalc/frmResultsStrainIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,11 @@ public bool OpenFile(JsonDocument document)
return result;
}

public void Save(string path)
public string Save(string directoryPath)
{
DialogResult result;
string userPath = string.Empty;

// Displays a SaveFileDialog so the user can save the results. More information here: https://msdn.microsoft.com/en-us/library/ms160336(v=vs.110).aspx
SaveFileDialog SaveDlg = new()
{
Expand All @@ -437,10 +440,9 @@ public void Save(string path)
},
Title = "Save Strain Index results",
OverwritePrompt = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
InitialDirectory = string.IsNullOrWhiteSpace(directoryPath) ? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) : directoryPath
};

DialogResult result;
using (new CenterWinDialog(this.MdiParent))
{
result = SaveDlg.ShowDialog(this);
Expand All @@ -452,15 +454,18 @@ public void Save(string path)
using var fs = SaveDlg.OpenFile();

// Saves the text via a FileStream created by the OpenFile method.
if ( fs != null)
if ( fs is not null)
{
// Get the actual directory path selected by the user in order to store it later in the settings
userPath = Path.GetDirectoryName(SaveDlg.FileName) ?? string.Empty;

// Saves the text in the appropriate TextFormat based upon the File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (SaveDlg.FilterIndex)
{
case 1:
using (var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true }))
{
using var writer = new Utf8JsonWriter(fs, options: new JsonWriterOptions { Indented = true });
SerializeToJSON(writer);
}
break;
Expand All @@ -484,7 +489,7 @@ public void Save(string path)
}
}

return;
return userPath;
}

public void UpdateOutput(System.Globalization.CultureInfo culture)
Expand Down
Loading

0 comments on commit 7fd3300

Please sign in to comment.