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{
45}
46
47
49{
50 if( m_errorIcon )
51 {
52 m_errorIcon->Destroy();
53 m_errorIcon = nullptr;
54 }
55
56 if( m_label )
57 {
58 m_label->Destroy();
59 m_label = nullptr;
60 }
61}
62
63
65{
66 if( m_errorIcon )
67 return;
68
69 m_errorIcon = new wxStaticBitmap( m_parent, wxID_ANY, KiBitmapBundle( BITMAPS::small_warning ) );
70
71 // Position the icon to the right of the control with a small gap
72 wxPoint controlPos = m_control->GetPosition();
73 wxSize controlSize = m_control->GetSize();
74 wxSize iconSize = m_errorIcon->GetSize();
75
76 int iconX = controlPos.x + controlSize.GetWidth() + 4;
77 int iconY = controlPos.y + ( controlSize.GetHeight() - iconSize.GetHeight() ) / 2;
78
79 m_errorIcon->SetPosition( wxPoint( iconX, iconY ) );
80 m_errorIcon->Hide();
81}
82
83
85{
86 if( aShow && !m_errorIcon )
88
89 if( m_errorIcon )
90 m_errorIcon->Show( aShow );
91
92 m_showingError = aShow;
93}
94
95
97{
98 if( !m_getter )
99 return true;
100
101 try
102 {
103 double value = m_getter();
104
105 if( m_unitBinder )
106 {
107 m_unitBinder->SetDoubleValue( value );
108 }
109
110 return true;
111 }
112 catch( ... )
113 {
114 return false;
115 }
116}
117
118
120{
121 if( !m_setter )
122 return true;
123
124 try
125 {
126 if( m_unitBinder )
127 {
128 double value = m_unitBinder->GetDoubleValue();
129 m_setter( value );
130 }
131
132 return true;
133 }
134 catch( ... )
135 {
136 return false;
137 }
138}
139
140
142{
143 if( m_label )
144 return;
145
146 if( m_position.labelText.IsEmpty() || m_position.labelPosition == LABEL_POSITION::NONE )
147 return;
148
149 m_label = new wxStaticText( m_parent, wxID_ANY, m_position.labelText );
150}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
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.