KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_overlay_field.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <bitmaps.h>
23#include <widgets/unit_binder.h>
24
25#include <wx/control.h>
26#include <wx/statbmp.h>
27#include <wx/stattext.h>
28#include <wx/window.h>
29
30
31DRC_RE_OVERLAY_FIELD::DRC_RE_OVERLAY_FIELD( wxWindow* aParent, const wxString& aFieldId,
32 wxControl* aControl,
33 const DRC_RE_FIELD_POSITION& aPosition ) :
34 m_parent( aParent ),
35 m_fieldId( aFieldId ),
36 m_control( aControl ),
37 m_position( aPosition ),
38 m_getter( nullptr ),
39 m_setter( nullptr ),
40 m_unitBinder( nullptr ),
41 m_errorIcon( nullptr ),
42 m_showingError( false ),
43 m_label( nullptr ),
44 m_prefixLabel( nullptr )
45{
46}
47
48
50{
51 if( m_errorIcon )
52 {
53 m_errorIcon->Destroy();
54 m_errorIcon = nullptr;
55 }
56
57 if( m_label )
58 {
59 m_label->Destroy();
60 m_label = nullptr;
61 }
62}
63
64
66{
67 if( m_errorIcon )
68 return;
69
70 m_errorIcon = new wxStaticBitmap( m_parent, wxID_ANY, KiBitmapBundle( BITMAPS::small_warning ) );
71
72 // Position the icon to the right of the control with a small gap
73 wxPoint controlPos = m_control->GetPosition();
74 wxSize controlSize = m_control->GetSize();
75 wxSize iconSize = m_errorIcon->GetSize();
76
77 int iconX = controlPos.x + controlSize.GetWidth() + 4;
78 int iconY = controlPos.y + ( controlSize.GetHeight() - iconSize.GetHeight() ) / 2;
79
80 m_errorIcon->SetPosition( wxPoint( iconX, iconY ) );
81 m_errorIcon->Hide();
82}
83
84
86{
87 if( aShow && !m_errorIcon )
89
90 if( m_errorIcon )
91 m_errorIcon->Show( aShow );
92
93 m_showingError = aShow;
94}
95
96
98{
99 if( !m_getter )
100 return true;
101
102 try
103 {
104 double value = m_getter();
105
106 if( m_unitBinder )
107 {
108 m_unitBinder->SetDoubleValue( value );
109 }
110
111 return true;
112 }
113 catch( ... )
114 {
115 return false;
116 }
117}
118
119
121{
122 if( !m_setter )
123 return true;
124
125 try
126 {
127 if( m_unitBinder )
128 {
129 double value = m_unitBinder->GetDoubleValue();
130 m_setter( value );
131 }
132
133 return true;
134 }
135 catch( ... )
136 {
137 return false;
138 }
139}
140
141
143{
144 if( !m_label && !m_position.labelText.IsEmpty() && m_position.labelPosition != LABEL_POSITION::NONE
145 && GetControl()->GetLabel() != m_position.labelText )
146 {
147 m_label = new wxStaticText( m_parent, wxID_ANY, m_position.labelText );
148 }
149
150 if( !m_prefixLabel && !m_position.prefixText.IsEmpty() )
151 {
152 m_prefixLabel = new wxStaticText( m_parent, wxID_ANY, m_position.prefixText );
153 }
154}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
wxStaticText * GetLabel() const
void CreateLabels()
Create and associate labels with this field.
bool TransferFromWindow()
Transfer data from the control to the model.
DRC_RE_OVERLAY_FIELD(wxWindow *aParent, const wxString &aFieldId, wxControl *aControl, const DRC_RE_FIELD_POSITION &aPosition)
Construct an overlay field wrapping an existing control.
bool TransferToWindow()
Transfer data from the model to the control.
void ShowError(bool aShow)
Show or hide the error indicator icon adjacent to this field.
DRC_RE_FIELD_POSITION m_position
wxControl * GetControl() const
Specifies the position and size of a field overlaid on a constraint bitmap.