KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pg_editors.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <eda_draw_frame.h>
24#include <widgets/unit_binder.h>
25
26#include <wx/log.h>
27
28const wxString PG_UNIT_EDITOR::EDITOR_NAME = wxS( "KiCadUnitEditor" );
29const wxString PG_CHECKBOX_EDITOR::EDITOR_NAME = wxS( "KiCadCheckboxEditor" );
30
31
33 wxPGTextCtrlEditor(),
34 m_frame( aFrame )
35{
36 m_unitBinder = std::make_unique<PROPERTY_EDITOR_UNIT_BINDER>( m_frame );
37 m_unitBinder->SetUnits( m_frame->GetUserUnits() );
38
40}
41
42
44{
45}
46
47
49{
50 return EDITOR_NAME + aFrame->GetName();
51}
52
53
55{
56 m_frame = aFrame;
57
58 if( aFrame )
59 {
60 m_unitBinder = std::make_unique<PROPERTY_EDITOR_UNIT_BINDER>( m_frame );
61 m_unitBinder->SetUnits( m_frame->GetUserUnits() );
62 }
63 else
64 {
65 m_unitBinder = nullptr;
66 }
67}
68
69
70wxPGWindowList PG_UNIT_EDITOR::CreateControls( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty,
71 const wxPoint& aPos, const wxSize& aSize ) const
72{
73 wxASSERT( m_unitBinder );
74
75 wxString text = aProperty->GetValueAsString( wxPG_EDITABLE_VALUE );
76 wxWindow* win = aPropGrid->GenerateEditorTextCtrl(aPos, aSize, text, nullptr, 0, aProperty->GetMaxLength() );
77 wxPGWindowList ret( win, nullptr );
78
79 m_unitBinder->SetControl( win );
80 m_unitBinder->RequireEval();
81 m_unitBinder->SetUnits( m_frame->GetUserUnits() );
82
83 if( PGPROPERTY_DISTANCE* prop = dynamic_cast<PGPROPERTY_DISTANCE*>( aProperty ) )
84 {
85 m_unitBinder->SetCoordType( prop->CoordType() );
86 }
87 else if( dynamic_cast<PGPROPERTY_ANGLE*>( aProperty ) != nullptr )
88 {
90 m_unitBinder->SetUnits( EDA_UNITS::DEGREES );
91 }
92
93 UpdateControl( aProperty, win );
94
95 return ret;
96}
97
98
99void PG_UNIT_EDITOR::UpdateControl( wxPGProperty* aProperty, wxWindow* aCtrl ) const
100{
101 wxVariant var = aProperty->GetValue();
102
103 if( var.GetType() == wxPG_VARIANT_TYPE_LONG )
104 {
105 m_unitBinder->ChangeValue( var.GetLong() );
106 }
107 else if( var.GetType() == wxPG_VARIANT_TYPE_DOUBLE )
108 {
109 m_unitBinder->ChangeValue( var.GetDouble() );
110 }
111 else if( var.GetType() == wxT( "EDA_ANGLE" ) )
112 {
113 EDA_ANGLE_VARIANT_DATA* angleData = static_cast<EDA_ANGLE_VARIANT_DATA*>( var.GetData() );
114 m_unitBinder->ChangeAngleValue( angleData->Angle() );
115 }
116 else if( !aProperty->IsValueUnspecified() )
117 {
118 wxFAIL_MSG( wxT( "PG_UNIT_EDITOR should only be used with numeric properties!" ) );
119 }
120}
121
122
123bool PG_UNIT_EDITOR::OnEvent( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty,
124 wxWindow* aCtrl, wxEvent& aEvent ) const
125{
126 if( aEvent.GetEventType() == wxEVT_LEFT_UP )
127 {
128 if( wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl ) )
129 {
130 if( !textCtrl->HasFocus() )
131 {
132 textCtrl->SelectAll();
133 return false;
134 }
135 }
136 }
137
138 return wxPGTextCtrlEditor::OnEvent( aPropGrid, aProperty, aCtrl, aEvent );
139}
140
141
142bool PG_UNIT_EDITOR::GetValueFromControl( wxVariant& aVariant, wxPGProperty* aProperty,
143 wxWindow* aCtrl ) const
144{
145 if( !m_unitBinder )
146 return false;
147
148 wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>( aCtrl );
149 wxCHECK_MSG( textCtrl, false, "PG_UNIT_EDITOR requires a text control!" );
150 wxString textVal = textCtrl->GetValue();
151
152 if( aProperty->UsesAutoUnspecified() && textVal.empty() )
153 {
154 aVariant.MakeNull();
155 return true;
156 }
157 bool changed;
158
159 if( dynamic_cast<PGPROPERTY_ANGLE*>( aProperty ) != nullptr )
160 {
161 EDA_ANGLE angle = m_unitBinder->GetAngleValue();
162
163 if( aVariant.GetType() == wxT( "EDA_ANGLE" ) )
164 {
165 EDA_ANGLE_VARIANT_DATA* ad = static_cast<EDA_ANGLE_VARIANT_DATA*>( aVariant.GetData() );
166 changed = ( aVariant.IsNull() || angle != ad->Angle() );
167
168 if( changed )
169 {
170 ad->SetAngle( angle );
171 m_unitBinder->SetAngleValue( angle );
172 }
173 }
174 else
175 {
176 changed = ( aVariant.IsNull() || angle.AsDegrees() != aVariant.GetDouble() );
177
178 if( changed )
179 {
180 aVariant = angle.AsDegrees();
181 m_unitBinder->SetValue( angle.AsDegrees() );
182 }
183 }
184 }
185 else
186 {
187 long result = m_unitBinder->GetValue();
188 changed = ( aVariant.IsNull() || result != aVariant.GetLong() );
189
190 if( changed )
191 {
192 aVariant = result;
193 m_unitBinder->SetValue( result );
194 }
195 }
196
197 // Changing unspecified always causes event (returning
198 // true here should be enough to trigger it).
199 if( !changed && aVariant.IsNull() )
200 changed = true;
201
202 return changed;
203}
204
205
207 wxPGCheckBoxEditor()
208{
209}
210
211
212wxPGWindowList PG_CHECKBOX_EDITOR::CreateControls( wxPropertyGrid* aGrid, wxPGProperty* aProperty,
213 const wxPoint& aPos, const wxSize& aSize ) const
214{
215 // Override wx behavior and toggle unspecified checkboxes to "true"
216 // CreateControls for a checkbox editor is only triggered when the user activates the checkbox
217 // Set the value to false here; the base class will then trigger an event setting it true.
218 if( aProperty->IsValueUnspecified() )
219 aProperty->SetValueFromInt( 0 );
220
221 return wxPGCheckBoxEditor::CreateControls( aGrid, aProperty, aPos, aSize );
222}
const EDA_ANGLE & Angle()
void SetAngle(const EDA_ANGLE &aAngle)
double AsDegrees() const
Definition: eda_angle.h:149
The base class for create windows for drawing purpose.
A wxEnumProperty that displays a color next to the enum value.
wxPGWindowList CreateControls(wxPropertyGrid *aGrid, wxPGProperty *aProperty, const wxPoint &aPos, const wxSize &aSize) const override
Definition: pg_editors.cpp:212
static const wxString EDITOR_NAME
Definition: pg_editors.h:75
std::unique_ptr< PROPERTY_EDITOR_UNIT_BINDER > m_unitBinder
Definition: pg_editors.h:66
wxPGWindowList CreateControls(wxPropertyGrid *aPropGrid, wxPGProperty *aProperty, const wxPoint &aPos, const wxSize &aSize) const override
Definition: pg_editors.cpp:70
static const wxString EDITOR_NAME
Definition: pg_editors.h:34
void UpdateControl(wxPGProperty *aProperty, wxWindow *aCtrl) const override
Definition: pg_editors.cpp:99
void UpdateFrame(EDA_DRAW_FRAME *aFrame)
When restarting an editor, the instance of PG_UNIT_EDITOR may be the same but the referenced frame is...
Definition: pg_editors.cpp:54
EDA_DRAW_FRAME * m_frame
Definition: pg_editors.h:64
PG_UNIT_EDITOR(EDA_DRAW_FRAME *aFrame)
Definition: pg_editors.cpp:32
bool GetValueFromControl(wxVariant &aVariant, wxPGProperty *aProperty, wxWindow *aCtrl) const override
Definition: pg_editors.cpp:142
wxString m_editorName
Definition: pg_editors.h:68
bool OnEvent(wxPropertyGrid *aPropGrid, wxPGProperty *aProperty, wxWindow *aCtrl, wxEvent &aEvent) const override
Definition: pg_editors.cpp:123
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
Definition: pg_editors.cpp:48
virtual ~PG_UNIT_EDITOR()
Definition: pg_editors.cpp:43
EDA_UNITS GetUserUnits() const