Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LT-21863: Add comment field to Parser Test Reports #149

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Src/LexText/ParserCore/ParserReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class ParserReport: IEquatable<ParserReport>
/// </summary>
public long DiffTimestamp { get; set; }

/// <summary>
/// User-specified comment.
/// </summary>
public string Comment { get; set; }

/// <summary>
/// Number of words parsed
/// </summary>
Expand Down
69 changes: 61 additions & 8 deletions Src/LexText/ParserUI/ParserListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class ParserListener : IxCoreColleague, IDisposable, IVwNotifyChange
private string m_sourceText = null;
private ObservableCollection<ParserReportViewModel> m_parserReports = null;
private ParserReportsDialog m_parserReportsDialog = null;
private string m_defaultComment = null;

public void Init(Mediator mediator, PropertyTable propertyTable, XmlNode configurationParameters)
{
Expand Down Expand Up @@ -617,7 +618,7 @@ private void UpdateWordforms(IEnumerable<IWfiWordform> wordforms, ParserPriority
{
ReadParserReports();
// Write an empty parser report.
var parserReport = WriteParserReport();
var parserReport = CreateParserReport();
AddParserReport(parserReport);
ShowParserReport(parserReport, m_mediator, m_cache);
}
Expand Down Expand Up @@ -668,18 +669,17 @@ private void WordformUpdatedEventHandler(object sender, WordformUpdatedEventArgs
// Read parser reports before writing and adding a parser report to avoid duplicates.
ReadParserReports();
// Convert parse results into ParserReport.
var parserReport = WriteParserReport();
var parserReport = CreateParserReport();
AddParserReport(parserReport);
ShowParserReport(parserReport, m_mediator, m_cache);
}
}

/// <summary>
/// Write the parse results as a parser report in the standard place.
/// Create a parser report from the parse results.
/// </summary>
ParserReport WriteParserReport()
ParserReport CreateParserReport()
{
// Create a parser report from the parse results.
var parserReport = new ParserReport(m_cache)
{
SourceText = m_sourceText
Expand All @@ -696,13 +696,62 @@ ParserReport WriteParserReport()
parserReport.AddParseReport(form.Text, parseReport);
}
}
// Write the parser report to the default place.
parserReport.WriteJsonFile(m_cache);
// Clear the data we wrote.
m_checkParserResults = null;
return parserReport;
}

public static void SaveParserReport(ParserReport report, LcmCache cache, string defaultComment)
{
Form inputBox = CreateInputBox(ParserUIStrings.ksEnterComment, ref defaultComment);
DialogResult result = inputBox.ShowDialog();
if (result == DialogResult.OK)
{
Control textBox = inputBox.Controls["input"];
report.Comment = textBox.Text;
report.WriteJsonFile(cache);
}
}

private static Form CreateInputBox(string title, ref string input)
{
System.Drawing.Size size = new System.Drawing.Size(400, 70);
Form inputBox = new Form();

inputBox.FormBorderStyle = FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = title;
inputBox.StartPosition = FormStartPosition.CenterScreen;

TextBox textBox = new TextBox();
textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
textBox.Location = new System.Drawing.Point(5, 5);
textBox.Text = input;
textBox.Name = "input";
inputBox.Controls.Add(textBox);

Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
inputBox.Controls.Add(okButton);

Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new System.Drawing.Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
inputBox.Controls.Add(cancelButton);

inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;

return inputBox;
}

/// <summary>
/// Suppress this parse result if it is an uppercase wordform whose analyses all came from its lowercase version.
/// This only happens in projects that were parsed before we decided that the case of wordforms in analyses
Expand Down Expand Up @@ -765,7 +814,7 @@ public void ShowParserReports()
{
ReadParserReports();
// Create parser reports window.
m_parserReportsDialog = new ParserReportsDialog(m_parserReports, m_mediator, m_cache);
m_parserReportsDialog = new ParserReportsDialog(m_parserReports, m_mediator, m_cache, m_defaultComment);
m_parserReportsDialog.Closed += ParserReportsDialog_Closed;
}
m_parserReportsDialog.Show(); // Show the dialog but do not block other app access
Expand All @@ -774,6 +823,10 @@ public void ShowParserReports()

private void ParserReportsDialog_Closed(object sender, EventArgs e)
{
ParserReportsDialog dialog = (ParserReportsDialog)sender;
// Preserve the default comment for the next call to ShowParserReports.
if (dialog != null)
m_defaultComment = dialog.DefaultComment;
m_parserReportsDialog = null;
}

Expand Down
22 changes: 21 additions & 1 deletion Src/LexText/ParserUI/ParserReportsDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</Grid.RowDefinitions>

<ScrollViewer VerticalScrollBarVisibility="Auto" PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" SelectionChanged="DataGrid_SelectionChanged"
<DataGrid Name="DataGrid" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" SelectionChanged="DataGrid_SelectionChanged"
MouseDoubleClick="DataGrid_MouseDoubleClick" ItemsSource="{Binding ParserReports, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Expand All @@ -39,6 +39,22 @@
<Label Content="{x:Static local:ParserUIStrings.ksText}" ToolTip="{x:Static local:ParserUIStrings.ksTextToolTip}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ParserReport.Comment, Mode=TwoWay}" Width="100">
<DataGridTextColumn.Header>
<Label Content="{x:Static local:ParserUIStrings.ksComment}" ToolTip="{x:Static local:ParserUIStrings.ksCommentToolTip}"/>
</DataGridTextColumn.Header>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource {x:Type TextBlock}}">
<Setter Property="TextWrapping"
Value="NoWrap" />
<Setter Property="TextTrimming"
Value="CharacterEllipsis" />
<Setter Property="ToolTip"
Value="{Binding ParserReport.Comment}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Timestamp, StringFormat=\{0:dd MMM yyyy hh:mm tt\}}">
<DataGridTextColumn.Header>
<Label Content="{x:Static local:ParserUIStrings.ksTimestamp}" ToolTip="{x:Static local:ParserUIStrings.ksTimestampToolTip}"/>
Expand Down Expand Up @@ -98,6 +114,10 @@
ToolTip="{x:Static local:ParserUIStrings.ksShowReportToolTip}"
IsEnabled="{Binding CanShowReport}"
Click="ShowParserReport" Width="100" Margin="5"/>
<Button Content="{x:Static local:ParserUIStrings.ksSaveReport}"
ToolTip="{x:Static local:ParserUIStrings.ksSaveReportToolTip}"
IsEnabled="{Binding CanSaveReport}"
Click="SaveParserReport" Width="100" Margin="5"/>
<Button Content="{x:Static local:ParserUIStrings.ksDiffButton}"
ToolTip="{x:Static local:ParserUIStrings.ksDiffButtonToolTip}"
IsEnabled="{Binding CanDiffReports}"
Expand Down
20 changes: 19 additions & 1 deletion Src/LexText/ParserUI/ParserReportsDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ public partial class ParserReportsDialog : Window

public LcmCache Cache { get; set; }

public string DefaultComment = null;

public ParserReportsDialog()
{
InitializeComponent();
}

public ParserReportsDialog(ObservableCollection<ParserReportViewModel> parserReports, Mediator mediator, LcmCache cache)
public ParserReportsDialog(ObservableCollection<ParserReportViewModel> parserReports, Mediator mediator, LcmCache cache, string defaultComment)
{
InitializeComponent();
parserReports.Sort((x, y) => y.Timestamp.CompareTo(x.Timestamp));
ParserReports = parserReports;
Mediator = mediator;
Cache = cache;
DataContext = new ParserReportsViewModel { ParserReports = parserReports };
DefaultComment = defaultComment;
}

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
Expand All @@ -63,6 +66,21 @@ public void ShowParserReport(object sender, RoutedEventArgs e)
}
}

public void SaveParserReport(object sender, RoutedEventArgs e)
{
foreach (var report in ParserReports)
{
if (report.IsSelected)
{
ParserListener.SaveParserReport(report.ParserReport, Cache, DefaultComment);
}
}
// The comment may have been updated.
DataGrid.Items.Refresh();
((ParserReportsViewModel)DataContext).UpdateButtonStates();

}

public void DeleteParserReport(object sender, RoutedEventArgs e)
{
foreach (var report in ParserReports.ToArray()) // ToArray to avoid modifying the collection while iterating
Expand Down
7 changes: 6 additions & 1 deletion Src/LexText/ParserUI/ParserReportsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace SIL.FieldWorks.LexText.Controls
public class ParserReportsViewModel : INotifyPropertyChanged
{
private ObservableCollection<ParserReportViewModel> _parserReports;

public ObservableCollection<ParserReportViewModel> ParserReports
{
get => _parserReports;
Expand All @@ -36,13 +37,15 @@ public ObservableCollection<ParserReportViewModel> ParserReports
}
}

OnPropertyChanged(nameof(ParserReports));
// Don't call OnPropertyChanged here! It suppresses the default behavior.
UpdateButtonStates(); // Update button states when the collection changes
}
}
public bool CanShowReport => ParserReports.Count(report => report.IsSelected) == 1;
public bool CanDiffReports => ParserReports.Count(report => report.IsSelected) == 2;
public bool CanDeleteReports => ParserReports.Any(report => report.IsSelected);
public bool CanSaveReport => ParserReports.Count(report => report.IsSelected) == 1 &&
ParserReports.Count(report => report.IsSelected && report.ParserReport.Filename == null) == 1;

public string DiffButtonContent => string.Format(ParserUIStrings.ksDelete,
ParserReports.Count(report => report.IsSelected));
Expand Down Expand Up @@ -86,6 +89,7 @@ private void OnReportPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// Notify changes to button state properties
OnPropertyChanged(nameof(CanShowReport));
OnPropertyChanged(nameof(CanSaveReport));
OnPropertyChanged(nameof(CanDiffReports));
OnPropertyChanged(nameof(CanDeleteReports));
OnPropertyChanged(nameof(DiffButtonContent));
Expand All @@ -101,6 +105,7 @@ public void UpdateButtonStates()
{
OnPropertyChanged(nameof(CanShowReport));
OnPropertyChanged(nameof(CanDiffReports));
OnPropertyChanged(nameof(CanSaveReport));
OnPropertyChanged(nameof(CanDeleteReports));
}
}
Expand Down
45 changes: 45 additions & 0 deletions Src/LexText/ParserUI/ParserUIStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Src/LexText/ParserUI/ParserUIStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,19 @@
<data name="ksXGenre" xml:space="preserve">
<value>{0} genre</value>
</data>
<data name="ksComment" xml:space="preserve">
<value>Comment</value>
</data>
<data name="ksCommentToolTip" xml:space="preserve">
<value>The comment provided by the user when the report was saved</value>
</data>
<data name="ksSaveReport" xml:space="preserve">
<value>Save Report</value>
</data>
<data name="ksSaveReportToolTip" xml:space="preserve">
<value>Save the report in the project</value>
</data>
<data name="ksEnterComment" xml:space="preserve">
<value>Please enter a comment for the parser report</value>
</data>
</root>
Loading