Rewrite export of data in Contrast.cpp

This commit is contained in:
Paul Licameli 2019-12-26 20:08:12 -05:00
parent 700c3deba9
commit cf5c18b9b3
1 changed files with 70 additions and 50 deletions

View File

@ -36,6 +36,8 @@
#include <wx/filedlg.h>
#include <wx/valtext.h>
#include <wx/log.h>
#include <wx/wfstream.h>
#include <wx/txtstrm.h>
#include "../PlatformCompatibility.h"
@ -408,28 +410,29 @@ namespace {
// Should these variations in formats be collapsed further?
// Pass nullptr when value is not yet defined
wxString FormatRMSMessage( float *pValue )
TranslatableString FormatRMSMessage( float *pValue )
{
/* i18n-hint: RMS abbreviates root mean square, a certain averaging method */
wxString format0{ _("RMS = %s.") };
auto format0 = XO("RMS = %s.");
/* i18n-hint: dB abbreviates decibels */
wxString format1{ _("%s dB") };
auto format1 = XO("%s dB");
wxString value;
TranslatableString value;
if ( pValue )
if ( pValue ) {
if( fabs( *pValue ) != std::numeric_limits<float>::infinity() ) {
auto number = wxString::Format( _("%.2f"), *pValue );
value = wxString::Format( format1, number );
auto number = wxString::Format( wxT("%.2f"), *pValue );
value = format1.Format( number );
}
else
value = _("zero");
value = XO("zero");
}
else
value = wxString::Format( format1, "" );
value = format1.Format( "" );
return wxString::Format( format0, value );
return format0.Format( value );
}
wxString FormatDifference( float diffdB )
@ -447,19 +450,19 @@ namespace {
}
}
wxString FormatDifferenceForExport( float diffdB )
TranslatableString FormatDifferenceForExport( float diffdB )
{
if( diffdB != diffdB ) //test for NaN, reliant on IEEE implementation
return _("Difference is indeterminate.");
return XO("Difference is indeterminate.");
else
if( fabs(diffdB) != std::numeric_limits<float>::infinity() )
/* i18n-hint: dB abbreviates decibels */
/* i18n-hint: RMS abbreviates root mean square, a certain averaging method */
return wxString::Format(_("Difference = %.2f RMS dB."), diffdB );
return XO("Difference = %.2f RMS dB.").Format( diffdB );
else
/* i18n-hint: dB abbreviates decibels */
/* i18n-hint: RMS abbreviates root mean square, a certain averaging method */
return _("Difference = infinite RMS dB.");
return XO("Difference = infinite RMS dB.");
}
}
@ -540,58 +543,76 @@ void ContrastDialog::OnExport(wxCommandEvent & WXUNUSED(event))
if (fName.empty())
return;
wxTextFile f(fName);
f.Create();
f.Open();
if (!f.IsOpened()) {
wxFFileOutputStream ffStream{ fName };
if (!ffStream.IsOk()) {
AudacityMessageBox( XO("Couldn't write to file: %s").Format( fName ) );
return;
}
f.AddLine(wxT("==================================="));
wxTextOutputStream ss(ffStream);
ss
<< wxT("===================================") << '\n'
/* i18n-hint: WCAG abbreviates Web Content Accessibility Guidelines */
f.AddLine(_("WCAG 2.0 Success Criteria 1.4.7 Contrast Results"));
f.AddLine(wxT(""));
f.AddLine(wxString::Format(_("Filename = %s."), project->GetFileName() ));
f.AddLine(wxT(""));
f.AddLine(_("Foreground"));
<< XO("WCAG 2.0 Success Criteria 1.4.7 Contrast Results") << '\n'
<< '\n'
<< XO("Filename = %s.").Format( project->GetFileName() ) << '\n'
<< '\n'
<< XO("Foreground") << '\n';
float t = (float)mForegroundStartT->GetValue();
int h = (int)(t/3600); // there must be a standard function for this!
int m = (int)((t - h*3600)/60);
float s = t - h*3600.0 - m*60.0;
f.AddLine(wxString::Format(_("Time started = %2d hour(s), %2d minute(s), %.2f seconds."), h, m, s ));
ss
<< XO("Time started = %2d hour(s), %2d minute(s), %.2f seconds.")
.Format( h, m, s ) << '\n';
t = (float)mForegroundEndT->GetValue();
h = (int)(t/3600);
m = (int)((t - h*3600)/60);
s = t - h*3600.0 - m*60.0;
f.AddLine(wxString::Format(_("Time ended = %2d hour(s), %2d minute(s), %.2f seconds."), h, m, s ));
f.AddLine( FormatRMSMessage( mForegroundIsDefined ? &foregrounddB : nullptr ) );
f.AddLine(wxT(""));
f.AddLine(_("Background"));
ss
<< XO("Time ended = %2d hour(s), %2d minute(s), %.2f seconds.")
.Format( h, m, s ) << '\n'
<< FormatRMSMessage( mForegroundIsDefined ? &foregrounddB : nullptr ) << '\n'
<< '\n'
<< XO("Background") << '\n';
t = (float)mBackgroundStartT->GetValue();
h = (int)(t/3600);
m = (int)((t - h*3600)/60);
s = t - h*3600.0 - m*60.0;
f.AddLine(wxString::Format(_("Time started = %2d hour(s), %2d minute(s), %.2f seconds."), h, m, s ));
ss
<< XO("Time started = %2d hour(s), %2d minute(s), %.2f seconds.")
.Format( h, m, s ) << '\n';
t = (float)mBackgroundEndT->GetValue();
h = (int)(t/3600);
m = (int)((t - h*3600)/60);
s = t - h*3600.0 - m*60.0;
f.AddLine(wxString::Format(_("Time ended = %2d hour(s), %2d minute(s), %.2f seconds."), h, m, s ));
f.AddLine( FormatRMSMessage( mBackgroundIsDefined ? &backgrounddB : nullptr ) );
f.AddLine(wxT(""));
f.AddLine(_("Results"));
ss
<< XO("Time ended = %2d hour(s), %2d minute(s), %.2f seconds.")
.Format( h, m, s ) << '\n'
<< FormatRMSMessage( mBackgroundIsDefined ? &backgrounddB : nullptr ) << '\n'
<< '\n'
<< XO("Results") << '\n';
float diffdB = foregrounddB - backgrounddB;
f.AddLine( FormatDifferenceForExport( diffdB ) );
if( diffdB > 20. )
f.AddLine(_("Success Criteria 1.4.7 of WCAG 2.0: Pass"));
else
f.AddLine(_("Success Criteria 1.4.7 of WCAG 2.0: Fail"));
ss
<< FormatDifferenceForExport( diffdB ) << '\n'
<< (( diffdB > 20. )
? XO("Success Criteria 1.4.7 of WCAG 2.0: Pass")
: XO("Success Criteria 1.4.7 of WCAG 2.0: Fail")) << '\n'
<< '\n'
<< XO("Data gathered") << '\n';
f.AddLine(wxT(""));
f.AddLine(_("Data gathered"));
wxString sNow;
wxDateTime now = wxDateTime::Now();
int year = now.GetYear();
wxDateTime::Month month = now.GetMonth();
@ -600,15 +621,14 @@ void ContrastDialog::OnExport(wxCommandEvent & WXUNUSED(event))
int hour = now.GetHour();
int minute = now.GetMinute();
int second = now.GetSecond();
sNow = wxString::Format(wxT("%d %s %02d %02dh %02dm %02ds"),
dom, monthName, year, hour, minute, second);
f.AddLine(sNow);
/* i18n-hint: day of month, month, year, hour, minute, second */
auto sNow = XO("%d %s %02d %02dh %02dm %02ds")
.Format( dom, monthName, year, hour, minute, second );
f.AddLine(wxT("==================================="));
f.AddLine(wxT(""));
f.Write();
f.Close();
ss <<
sNow << '\n'
<< wxT("===================================") << '\n'
<< '\n';
}
void ContrastDialog::OnReset(wxCommandEvent & /*event*/)