1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-01 16:19:43 +02:00

Structured output for CommandContext

This commit is contained in:
James Crook 2018-02-08 10:09:16 +00:00 committed by Paul Licameli
parent b83b862b3f
commit c0c5cc6337
5 changed files with 186 additions and 19 deletions

View File

@ -98,3 +98,42 @@ AudacityApp * CommandContext::GetApp() const
AudacityProject *CommandContext::GetProject() const
{ return GetActiveProject();}
void CommandContext::StartArray() const
{
if( pOutput )
pOutput->StartArray();
}
void CommandContext::EndArray() const
{
if( pOutput )
pOutput->EndArray();
}
void CommandContext::StartStruct() const
{
if( pOutput )
pOutput->StartStruct();
}
void CommandContext::EndStruct() const
{
if( pOutput )
pOutput->EndStruct();
}
void CommandContext::AddItem(const wxString &value , const wxString &name ) const
{
if( pOutput )
pOutput->AddItem( value, name );
}
void CommandContext::AddBool(const bool value , const wxString &name ) const
{
if( pOutput )
pOutput->AddItem( value, name );
}
void CommandContext::AddItem(const double value , const wxString &name ) const
{
if( pOutput )
pOutput->AddItem( value, name );
}

View File

@ -38,6 +38,16 @@ public:
virtual void Status( const wxString & WXUNUSED(message) ) const;
virtual void Error( const wxString & WXUNUSED(message) ) const;
virtual void Progress( double WXUNUSED(d) ) const;
// Output formatting...
void StartArray() const;
void EndArray() const;
void StartStruct() const;
void EndStruct() const;
void AddItem(const wxString &value , const wxString &name="" ) const;
void AddBool(const bool value , const wxString &name="" ) const;
void AddItem(const double value , const wxString &name="" ) const;
AudacityProject &project;
std::unique_ptr<CommandOutputTarget> pOutput;
const wxEvent *pEvt;

View File

@ -23,6 +23,46 @@ dialog, if necessary.
#include "../ShuttleGui.h"
#include "../Project.h"
void CommandMessageTarget::StartArray()
{
wxString Padding;
Padding.Pad( mCounts.GetCount() *2 -2);
Update( wxString::Format( "%s%s[ ", ( mCounts.Last() > 0 ) ? ",\n" : "\n", Padding ));
mCounts.Last() += 1;
mCounts.push_back( 0 );
}
void CommandMessageTarget::EndArray(){
if( mCounts.GetCount() > 1 ){
mCounts.pop_back();
}
Update( " ]" );
}
void CommandMessageTarget::StartStruct(){
wxString Padding;
Padding.Pad( mCounts.GetCount() *2 -2);
Update( wxString::Format( "%s%s{ ", ( mCounts.Last() > 0 ) ? ",\n" : "\n", Padding ));
mCounts.Last() += 1;
mCounts.push_back( 0 );
}
void CommandMessageTarget::EndStruct(){
if( mCounts.GetCount() > 1 ){
mCounts.pop_back();
}
Update( " }" );
}
void CommandMessageTarget::AddItem(const wxString &value, const wxString &name){
Update( wxString::Format( "%s%s%s\"%s\"", (mCounts.Last()>0)?", ":"", name, !name.IsEmpty()?":":"",value));
mCounts.Last() += 1;
}
void CommandMessageTarget::AddItem(const bool value, const wxString &name){
Update( wxString::Format( "%s%s%s%s", (mCounts.Last()>0)?", ":"", name, !name.IsEmpty()?":":"",value?"True":"False"));
mCounts.Last() += 1;
}
void CommandMessageTarget::AddItem(const double value, const wxString &name){
Update( wxString::Format( "%s%s%s%g", (mCounts.Last()>0)?", ":"", name, !name.IsEmpty()?":":"",value));
mCounts.Last() += 1;
}
/// Dialog for long messages.
@ -114,7 +154,7 @@ void LongMessageDialog::AcceptText( const wxString & Text )
pDlg->Init();
pDlg->Show();
}
pDlg->mText->SetValue( pDlg->mText->GetValue( ) + "\n" + Text );
pDlg->mText->SetValue( pDlg->mText->GetValue( ) + Text );
}

View File

@ -38,6 +38,25 @@ public:
virtual void Update(double completed) = 0;
};
/// Interface for objects that can receive (string) messages from a command
class CommandMessageTarget /* not final */
{
public:
CommandMessageTarget() {mCounts.push_back(0);}
virtual ~CommandMessageTarget() {}
virtual void Update(const wxString &message) = 0;
virtual void StartArray();
virtual void EndArray();
virtual void StartStruct();
virtual void EndStruct();
virtual void AddItem(const wxString &value , const wxString &name="" );
virtual void AddItem(const bool value , const wxString &name="" );
virtual void AddItem(const double value , const wxString &name="" );
wxArrayInt mCounts;
};
/// Used to ignore a command's progress updates
class NullProgressTarget final : public CommandProgressTarget
{
@ -62,13 +81,6 @@ public:
}
};
/// Interface for objects that can receive (string) messages from a command
class CommandMessageTarget /* not final */
{
public:
virtual ~CommandMessageTarget() {}
virtual void Update(const wxString &message) = 0;
};
///
class ProgressToMessageTarget final : public CommandProgressTarget
@ -198,6 +210,7 @@ public:
~CommandOutputTarget()
{
}
// Lots of forwarding...
void Progress(double completed)
{
if (mProgressTarget)
@ -213,6 +226,44 @@ public:
if (mErrorTarget)
mErrorTarget->Update(message);
}
void StartArray()
{
if (mStatusTarget)
mStatusTarget->StartArray();
}
void EndArray()
{
if (mStatusTarget)
mStatusTarget->EndArray();
}
void StartStruct()
{
if (mStatusTarget)
mStatusTarget->StartStruct();
}
void EndStruct()
{
if (mStatusTarget)
mStatusTarget->EndStruct();
}
void AddItem(const wxString &value , const wxString &name="" )
{
if (mStatusTarget)
mStatusTarget->AddItem( value, name );
}
void AddItem(const bool value , const wxString &name="" )
{
if (mStatusTarget)
mStatusTarget->AddItem( value, name );
}
void AddItem(const double value , const wxString &name="" )
{
if (mStatusTarget)
mStatusTarget->AddItem( value, name );
}
};
class InteractiveOutputTarget : public CommandOutputTarget

View File

@ -256,8 +256,14 @@ void GetInfoCommand::ExploreAdornments( const CommandContext &context,
wxSize s = pWin->GetWindowBorderSize();
wxRect R( 2,32, R1.GetWidth() - s.GetWidth() * 2 -16, 22 );
context.Status( wxString::Format(" [ %2i, %3i, %3i, %3i, %3i, \"%s\" ],",
depth, R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom(), "MenuBar" ));
context.StartArray();
context.AddItem( depth );
context.AddItem( R.GetLeft() );
context.AddItem( R.GetTop() );
context.AddItem( R.GetRight() );
context.AddItem( R.GetBottom() );
context.AddItem( "MenuBar" );
context.EndArray();
}
void GetInfoCommand::ExploreTrackPanel( const CommandContext &context,
@ -331,8 +337,14 @@ void GetInfoCommand::ExploreTrackPanel( const CommandContext &context,
R.height -= (kTopMargin + kBottomMargin);
R.SetPosition( R.GetPosition() + P );
context.Status( wxString::Format(" [ %2i, %3i, %3i, %3i, %3i, \"%s\" ],",
depth, R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom(), "VRuler" ));
context.StartArray();
context.AddItem( depth );
context.AddItem( R.GetLeft() );
context.AddItem( R.GetTop() );
context.AddItem( R.GetRight() );
context.AddItem( R.GetBottom() );
context.AddItem( "VRuler" );
context.EndArray();
}
}
}
@ -367,8 +379,16 @@ void GetInfoCommand::ExploreWindows( const CommandContext &context,
continue;
if( Name.IsEmpty() )
Name = wxString("*") + item->GetToolTipText();
context.Status( wxString::Format(" [ %2i, %3i, %3i, %3i, %3i, \"%s\" ],",
depth, R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom(), Name ));
context.StartArray();
context.AddItem( depth );
context.AddItem( R.GetLeft() );
context.AddItem( R.GetTop() );
context.AddItem( R.GetRight() );
context.AddItem( R.GetBottom() );
context.AddItem( Name );
context.EndArray();
ExploreWindows( context, P, item, item->GetId(), depth+1 );
}
}
@ -376,20 +396,27 @@ void GetInfoCommand::ExploreWindows( const CommandContext &context,
bool GetInfoCommand::SendBoxesAsJson(const CommandContext &context)
{
context.Status("Boxes");
//context.Status("Boxes");
wxWindow * pWin = context.GetProject();
context.Status( "[" );
context.StartArray();
wxRect R = pWin->GetScreenRect();
//R.SetPosition( wxPoint(0,0) );
//wxString Name = pWin->GetName();
context.Status( wxString::Format(" [ %2i, %3i, %3i, %3i, %3i, \"%s\" ],",
0, R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom(), "Audacity Window" ));
context.StartArray();
context.AddItem( 0 );
context.AddItem( R.GetLeft() );
context.AddItem( R.GetTop() );
context.AddItem( R.GetRight() );
context.AddItem( R.GetBottom() );
context.AddItem( "Audacity Window" );
context.EndArray( );
ExploreAdornments( context, pWin->GetPosition()+wxSize( 6,-1), pWin, pWin->GetId(), 1 );
ExploreWindows( context, pWin->GetPosition()+wxSize( 6,-1), pWin, pWin->GetId(), 1 );
context.Status( "]" );
context.EndArray( );
return true;
}