Properties: rework color editor

Fixes https://gitlab.com/kicad/code/kicad/-/issues/15145
This commit is contained in:
Jon Evans 2023-07-16 17:37:26 -04:00
parent c3c7f731df
commit 4e3fd816cb
4 changed files with 77 additions and 34 deletions

View File

@ -22,6 +22,7 @@
#include <properties/eda_angle_variant.h>
#include <properties/pg_editors.h>
#include <properties/pg_properties.h>
#include <widgets/color_swatch.h>
#include <widgets/unit_binder.h>
#include <wx/log.h>
@ -225,38 +226,69 @@ wxPGWindowList PG_CHECKBOX_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGPr
}
bool PG_COLOR_EDITOR::OnEvent( wxPropertyGrid* aGrid, wxPGProperty* aProperty, wxWindow* aWindow,
wxEvent& aEvent ) const
{
return false;
}
wxPGWindowList PG_COLOR_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGProperty* aProperty,
const wxPoint& aPos, const wxSize& aSize ) const
{
wxVariant val = aProperty->GetValue();
auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( aProperty );
if( !colorProp )
return nullptr;
KIGFX::COLOR4D color = colorFromProperty( aProperty );
KIGFX::COLOR4D defColor = colorFromVariant( colorProp->GetDefaultValue() );
COLOR_SWATCH* editor = new COLOR_SWATCH( aGrid->GetPanel(), color, wxID_ANY,
colorProp->GetBackgroundColor(), defColor,
SWATCH_LARGE, true );
editor->SetPosition( aPos );
editor->SetSize( aSize );
editor->Bind( COLOR_SWATCH_CHANGED,
[=]( wxCommandEvent& aEvt )
{
wxVariant val;
auto data = new COLOR4D_VARIANT_DATA( editor->GetSwatchColor() );
val.SetData( data );
aGrid->ChangePropertyValue( colorProp, val );
} );
if( aGrid->GetInternalFlags() & wxPG_FL_ACTIVATION_BY_CLICK )
aGrid->CallAfter( [=]() { editor->GetNewSwatchColor(); } );
return editor;
}
void PG_COLOR_EDITOR::UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const
{
if( auto swatch = dynamic_cast<COLOR_SWATCH*>( aCtrl ) )
swatch->SetSwatchColor( colorFromProperty( aProperty ), false );
}
KIGFX::COLOR4D PG_COLOR_EDITOR::colorFromVariant( const wxVariant& aVariant ) const
{
KIGFX::COLOR4D color = KIGFX::COLOR4D::UNSPECIFIED;
COLOR4D_VARIANT_DATA* data = nullptr;
if( val.IsType( wxS( "COLOR4D" ) ) )
if( aVariant.IsType( wxS( "COLOR4D" ) ) )
{
data = static_cast<COLOR4D_VARIANT_DATA*>( val.GetData() );
data = static_cast<COLOR4D_VARIANT_DATA*>( aVariant.GetData() );
color = data->Color();
}
DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( aGrid ), color, true,
nullptr, KIGFX::COLOR4D::UNSPECIFIED );
int res = dialog.ShowModal();
if( res == wxID_OK )
{
data = new COLOR4D_VARIANT_DATA();
data->SetColor( dialog.GetColor() );
val.SetData( data );
aGrid->ChangePropertyValue( aProperty, val );
}
// Deselect property so that this gets called again on next click
aGrid->CallAfter( [=]()
{
aGrid->RemoveFromSelection( aProperty );
} );
return nullptr;
return color;
}
KIGFX::COLOR4D PG_COLOR_EDITOR::colorFromProperty( wxPGProperty* aProperty ) const
{
return colorFromVariant( aProperty->GetValue() );
}

View File

@ -123,7 +123,7 @@ void COLOR_SWATCH::RenderToDC( wxDC* aDC, const KIGFX::COLOR4D& aColor,
COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID,
const COLOR4D& aBackground, const COLOR4D& aDefault,
SWATCH_SIZE aSwatchSize ) :
SWATCH_SIZE aSwatchSize, bool aTriggerWithSingleClick ) :
wxPanel( aParent, aID ),
m_color( aColor ),
m_background( aBackground ),
@ -164,7 +164,7 @@ COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, const COLOR4D& aColor, int aID,
sizer->Add( m_swatch, 0, 0 );
setupEvents();
setupEvents( aTriggerWithSingleClick );
}
@ -194,11 +194,11 @@ COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, wxWindowID aID, const wxPoint& aP
sizer->Add( m_swatch, 0, 0 );
setupEvents();
setupEvents( false );
}
void COLOR_SWATCH::setupEvents()
void COLOR_SWATCH::setupEvents( bool aTriggerWithSingleClick )
{
wxWindow* topLevelParent = GetParent();
@ -224,6 +224,15 @@ void COLOR_SWATCH::setupEvents()
{
GetNewSwatchColor();
} );
if( aTriggerWithSingleClick )
{
m_swatch->Bind( wxEVT_LEFT_UP,
[this] ( wxMouseEvent& aEvt )
{
GetNewSwatchColor();
} );
}
}
m_swatch->Bind( wxEVT_MIDDLE_DOWN,

View File

@ -99,13 +99,15 @@ public:
wxPGWindowList CreateControls( wxPropertyGrid* aGrid, wxPGProperty* aProperty,
const wxPoint& aPos, const wxSize& aSize ) const override;
void UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const override {}
void UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const override;
bool OnEvent( wxPropertyGrid* aGrid, wxPGProperty* aProperty, wxWindow* aWindow,
wxEvent& aEvent ) const override
{
return false;
}
wxEvent& aEvent ) const override;
private:
KIGFX::COLOR4D colorFromVariant( const wxVariant& aVariant ) const;
KIGFX::COLOR4D colorFromProperty( wxPGProperty* aProperty ) const;
};
#endif //KICAD_PG_EDITORS_H

View File

@ -66,7 +66,7 @@ public:
*/
COLOR_SWATCH( wxWindow* aParent, const KIGFX::COLOR4D& aColor, int aID,
const KIGFX::COLOR4D& aBackground, const KIGFX::COLOR4D& aDefault,
SWATCH_SIZE aSwatchType );
SWATCH_SIZE aSwatchType, bool aTriggerWithSingleClick = false );
/**
* constructor for wxFormBuilder
@ -137,7 +137,7 @@ public:
const KIGFX::COLOR4D& aCheckerboardBackground );
private:
void setupEvents();
void setupEvents( bool aTriggerWithSingleClick );
/**
* Pass unwanted events on to listeners of this object