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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <bitmaps.h>
27#include <widgets/unit_binder.h>
28
29#include <wx/control.h>
30#include <wx/statbmp.h>
31#include <wx/stattext.h>
32#include <wx/window.h>
33
34
35DRC_RE_OVERLAY_FIELD::DRC_RE_OVERLAY_FIELD( wxWindow* aParent, const wxString& aFieldId,
36 wxControl* aControl,
37 const DRC_RE_FIELD_POSITION& aPosition ) :
38 m_parent( aParent ),
39 m_fieldId( aFieldId ),
40 m_control( aControl ),
41 m_position( aPosition ),
42 m_getter( nullptr ),
43 m_setter( nullptr ),
44 m_unitBinder( nullptr ),
45 m_errorIcon( nullptr ),
46 m_showingError( false ),
47 m_label( nullptr )
48{
49}
50
51
53{
54 if( m_errorIcon )
55 {
56 m_errorIcon->Destroy();
57 m_errorIcon = nullptr;
58 }
59
60 if( m_label )
61 {
62 m_label->Destroy();
63 m_label = nullptr;
64 }
65}
66
67
69{
70 if( m_errorIcon )
71 return;
72
73 m_errorIcon = new wxStaticBitmap( m_parent, wxID_ANY, KiBitmapBundle( BITMAPS::small_warning ) );
74
75 // Position the icon to the right of the control with a small gap
76 wxPoint controlPos = m_control->GetPosition();
77 wxSize controlSize = m_control->GetSize();
78 wxSize iconSize = m_errorIcon->GetSize();
79
80 int iconX = controlPos.x + controlSize.GetWidth() + 4;
81 int iconY = controlPos.y + ( controlSize.GetHeight() - iconSize.GetHeight() ) / 2;
82
83 m_errorIcon->SetPosition( wxPoint( iconX, iconY ) );
84 m_errorIcon->Hide();
85}
86
87
89{
90 if( aShow && !m_errorIcon )
92
93 if( m_errorIcon )
94 m_errorIcon->Show( aShow );
95
96 m_showingError = aShow;
97}
98
99
101{
102 if( !m_getter )
103 return true;
104
105 try
106 {
107 double value = m_getter();
108
109 if( m_unitBinder )
110 {
111 m_unitBinder->SetDoubleValue( value );
112 }
113
114 return true;
115 }
116 catch( ... )
117 {
118 return false;
119 }
120}
121
122
124{
125 if( !m_setter )
126 return true;
127
128 try
129 {
130 if( m_unitBinder )
131 {
132 double value = m_unitBinder->GetDoubleValue();
133 m_setter( value );
134 }
135
136 return true;
137 }
138 catch( ... )
139 {
140 return false;
141 }
142}
143
144
146{
147 if( m_label )
148 return;
149
150 if( m_position.labelText.IsEmpty() || m_position.labelPosition == LABEL_POSITION::NONE )
151 return;
152
153 m_label = new wxStaticText( m_parent, wxID_ANY, m_position.labelText );
154}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
bool TransferFromWindow()
Transfer data from the control to the model.
void CreateLabel()
Create and associate a label with this field.
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
Specifies the position and size of a field overlaid on a constraint bitmap.