2022-11-03 02:50:49 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-24 22:15:07 +00:00
|
|
|
#include <eda_draw_frame.h>
|
2022-12-03 02:25:42 +00:00
|
|
|
#include <properties/eda_angle_variant.h>
|
2022-11-03 02:50:49 +00:00
|
|
|
#include <properties/pg_editors.h>
|
|
|
|
#include <properties/pg_properties.h>
|
|
|
|
#include <widgets/unit_binder.h>
|
|
|
|
|
|
|
|
#include <wx/log.h>
|
|
|
|
|
2022-11-30 17:09:57 +00:00
|
|
|
const wxString PG_UNIT_EDITOR::EDITOR_NAME = wxS( "KiCadUnitEditor" );
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
|
|
|
|
PG_UNIT_EDITOR::PG_UNIT_EDITOR( EDA_DRAW_FRAME* aFrame ) :
|
|
|
|
wxPGTextCtrlEditor(),
|
|
|
|
m_frame( aFrame )
|
|
|
|
{
|
|
|
|
m_unitBinder = std::make_unique<PROPERTY_EDITOR_UNIT_BINDER>( m_frame );
|
2022-11-24 22:15:07 +00:00
|
|
|
m_unitBinder->SetUnits( m_frame->GetUserUnits() );
|
2022-11-03 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PG_UNIT_EDITOR::~PG_UNIT_EDITOR()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-29 16:31:00 +00:00
|
|
|
void PG_UNIT_EDITOR::UpdateFrame( EDA_DRAW_FRAME* aFrame )
|
|
|
|
{
|
|
|
|
m_frame = aFrame;
|
|
|
|
|
|
|
|
if( aFrame )
|
|
|
|
{
|
|
|
|
m_unitBinder = std::make_unique<PROPERTY_EDITOR_UNIT_BINDER>( m_frame );
|
|
|
|
m_unitBinder->SetUnits( m_frame->GetUserUnits() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_unitBinder = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
wxPGWindowList PG_UNIT_EDITOR::CreateControls( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty,
|
|
|
|
const wxPoint& aPos, const wxSize& aSize ) const
|
|
|
|
{
|
2022-11-29 16:31:00 +00:00
|
|
|
wxASSERT( m_unitBinder );
|
|
|
|
|
2022-12-05 21:32:35 +00:00
|
|
|
wxString text = aProperty->GetValueAsString( wxPG_EDITABLE_VALUE );
|
|
|
|
wxWindow* win = aPropGrid->GenerateEditorTextCtrl(aPos, aSize, text, nullptr, 0, aProperty->GetMaxLength() );
|
|
|
|
wxPGWindowList ret( win, nullptr );
|
2022-11-03 02:50:49 +00:00
|
|
|
|
2022-12-05 21:32:35 +00:00
|
|
|
m_unitBinder->SetControl( win );
|
2022-11-03 02:50:49 +00:00
|
|
|
m_unitBinder->RequireEval();
|
2022-12-06 04:46:01 +00:00
|
|
|
m_unitBinder->SetUnits( m_frame->GetUserUnits() );
|
2022-11-03 02:50:49 +00:00
|
|
|
|
|
|
|
if( PGPROPERTY_DISTANCE* prop = dynamic_cast<PGPROPERTY_DISTANCE*>( aProperty ) )
|
|
|
|
m_unitBinder->SetCoordType( prop->CoordType() );
|
2022-12-09 03:24:13 +00:00
|
|
|
else if( dynamic_cast<PGPROPERTY_ANGLE*>( aProperty ) != nullptr )
|
2022-12-03 02:25:42 +00:00
|
|
|
m_unitBinder->SetUnits( EDA_UNITS::DEGREES );
|
2022-11-03 02:50:49 +00:00
|
|
|
|
2022-12-06 13:55:48 +00:00
|
|
|
UpdateControl( aProperty, win );
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-03 00:37:44 +00:00
|
|
|
void PG_UNIT_EDITOR::UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const
|
|
|
|
{
|
2022-12-06 13:55:48 +00:00
|
|
|
wxVariant var = aProperty->GetValue();
|
|
|
|
|
|
|
|
if( var.GetType() == wxPG_VARIANT_TYPE_LONG )
|
|
|
|
{
|
|
|
|
m_unitBinder->ChangeValue( var.GetLong() );
|
|
|
|
}
|
|
|
|
else if( var.GetType() == wxPG_VARIANT_TYPE_DOUBLE )
|
|
|
|
{
|
|
|
|
m_unitBinder->ChangeValue( var.GetDouble() );
|
|
|
|
}
|
2022-12-09 03:24:13 +00:00
|
|
|
else if( var.GetType() == wxT( "EDA_ANGLE" ) )
|
|
|
|
{
|
|
|
|
EDA_ANGLE_VARIANT_DATA* angleData = static_cast<EDA_ANGLE_VARIANT_DATA*>( var.GetData() );
|
|
|
|
m_unitBinder->ChangeAngleValue( angleData->Angle() );
|
|
|
|
}
|
2022-12-18 16:06:26 +00:00
|
|
|
else if( !aProperty->IsValueUnspecified() )
|
2022-12-06 13:55:48 +00:00
|
|
|
{
|
|
|
|
wxFAIL_MSG( wxT( "PG_UNIT_EDITOR should only be used with numeric properties!" ) );
|
2022-12-18 16:06:26 +00:00
|
|
|
}
|
2022-12-03 00:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PG_UNIT_EDITOR::OnEvent( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty,
|
|
|
|
wxWindow* aCtrl, wxEvent& aEvent ) const
|
|
|
|
{
|
|
|
|
if( aEvent.GetEventType() == wxEVT_LEFT_UP )
|
|
|
|
{
|
|
|
|
if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl ) )
|
|
|
|
{
|
2022-12-03 02:25:42 +00:00
|
|
|
if( !textCtrl->HasFocus() )
|
|
|
|
{
|
|
|
|
textCtrl->SelectAll();
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-03 00:37:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxPGTextCtrlEditor::OnEvent( aPropGrid, aProperty, aCtrl, aEvent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
bool PG_UNIT_EDITOR::GetValueFromControl( wxVariant& aVariant, wxPGProperty* aProperty,
|
|
|
|
wxWindow* aCtrl ) const
|
|
|
|
{
|
2022-12-03 00:37:44 +00:00
|
|
|
if( !m_unitBinder )
|
|
|
|
return false;
|
2022-11-29 16:31:00 +00:00
|
|
|
|
2022-11-03 02:50:49 +00:00
|
|
|
wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl );
|
|
|
|
wxCHECK_MSG( textCtrl, false, "PG_UNIT_EDITOR requires a text control!" );
|
|
|
|
wxString textVal = textCtrl->GetValue();
|
|
|
|
|
|
|
|
if( aProperty->UsesAutoUnspecified() && textVal.empty() )
|
|
|
|
{
|
|
|
|
aVariant.MakeNull();
|
|
|
|
return true;
|
|
|
|
}
|
2022-12-09 03:24:13 +00:00
|
|
|
bool changed;
|
2022-11-03 02:50:49 +00:00
|
|
|
|
2022-12-09 03:24:13 +00:00
|
|
|
if( dynamic_cast<PGPROPERTY_ANGLE*>( aProperty ) != nullptr )
|
2022-12-03 02:25:42 +00:00
|
|
|
{
|
2022-12-09 03:24:13 +00:00
|
|
|
EDA_ANGLE angle = m_unitBinder->GetAngleValue();
|
2022-11-03 02:50:49 +00:00
|
|
|
|
2022-12-09 03:24:13 +00:00
|
|
|
if( aVariant.GetType() == wxT( "EDA_ANGLE" ) )
|
2022-12-03 02:25:42 +00:00
|
|
|
{
|
2022-12-09 03:24:13 +00:00
|
|
|
EDA_ANGLE_VARIANT_DATA* ad = static_cast<EDA_ANGLE_VARIANT_DATA*>( aVariant.GetData() );
|
|
|
|
changed = ( aVariant.IsNull() || angle != ad->Angle() );
|
|
|
|
|
|
|
|
if( changed )
|
|
|
|
{
|
|
|
|
ad->SetAngle( angle );
|
|
|
|
m_unitBinder->SetAngleValue( angle );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
changed = ( aVariant.IsNull() || angle.AsDegrees() != aVariant.GetDouble() );
|
|
|
|
|
|
|
|
if( changed )
|
|
|
|
{
|
|
|
|
aVariant = angle.AsDegrees();
|
|
|
|
m_unitBinder->SetValue( angle.AsDegrees() );
|
|
|
|
}
|
2022-12-03 02:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-11-03 02:50:49 +00:00
|
|
|
{
|
2022-12-03 02:25:42 +00:00
|
|
|
long result = m_unitBinder->GetValue();
|
|
|
|
changed = ( aVariant.IsNull() || result != aVariant.GetLong() );
|
|
|
|
|
|
|
|
if( changed )
|
|
|
|
{
|
|
|
|
aVariant = result;
|
|
|
|
m_unitBinder->SetValue( result );
|
|
|
|
}
|
2022-11-03 02:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Changing unspecified always causes event (returning
|
|
|
|
// true here should be enough to trigger it).
|
|
|
|
if( !changed && aVariant.IsNull() )
|
|
|
|
changed = true;
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|