KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_re_bitmap_overlay_panel.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
20#include <algorithm>
21
25
26#include <bitmaps.h>
27
28#include <wx/bmpbndl.h>
29#include <wx/checkbox.h>
30#include <wx/dcbuffer.h>
31#include <wx/dcclient.h>
32#include <wx/stattext.h>
33#include <wx/textctrl.h>
34
35
38static constexpr int LABEL_GAP = 4;
39
40
42 wxPanel(),
45{
46 // A transparent child turns black on GTK once it repaints, so the paint handler fills it instead
47 SetBackgroundStyle( wxBG_STYLE_PAINT );
48 Create( aParent, aId );
49
50 Bind( wxEVT_PAINT, &DRC_RE_BITMAP_OVERLAY_PANEL::OnPaint, this );
51 Bind( wxEVT_DPI_CHANGED, &DRC_RE_BITMAP_OVERLAY_PANEL::OnDPIChanged, this );
52 Bind( wxEVT_SYS_COLOUR_CHANGED, &DRC_RE_BITMAP_OVERLAY_PANEL::OnThemeChange, this );
53}
54
55
59
60
61void DRC_RE_BITMAP_OVERLAY_PANEL::OnPaint( wxPaintEvent& aEvent )
62{
63 wxAutoBufferedPaintDC dc( this );
64
65 dc.SetBackground( wxBrush( pageBackground() ) );
66 dc.Clear();
67
68 if( m_bitmap.IsOk() )
69 dc.DrawBitmap( m_bitmap, 0, 0, true );
70}
71
72
73void DRC_RE_BITMAP_OVERLAY_PANEL::OnDPIChanged( wxDPIChangedEvent& aEvent )
74{
75 LoadBitmap();
77
78 // Positioning the fields changed the panel's minimum size, which only reaches the screen
79 // once the sizers above it run again.
81 editor->RelayoutHost();
82 else if( wxWindow* parent = GetParent() )
83 parent->Layout();
84
85 Refresh();
86 aEvent.Skip();
87}
88
89
91{
92 // A panel with no colour of its own shows the window behind it, and GTK reports a darker one
93 // for it, so take the first ancestor that paints. Read it when painting, once the parents exist
94 for( wxWindow* window = GetParent(); window; window = window->GetParent() )
95 {
96 if( window->UseBgCol() || window->IsTopLevel() )
97 return window->GetBackgroundColour();
98 }
99
100 return GetBackgroundColour();
101}
102
103
104void DRC_RE_BITMAP_OVERLAY_PANEL::OnThemeChange( wxSysColourChangedEvent& aEvent )
105{
106 LoadBitmap();
107 Refresh();
108 aEvent.Skip();
109}
110
111
113{
114 // Platform handles focus ring automatically.
115 // Could add custom highlighting here if needed.
116 aEvent.Skip();
117}
118
119
121{
122 // Platform handles focus ring automatically.
123 aEvent.Skip();
124}
125
126
128{
129 if( !aControl )
130 return;
131
132 // Bind focus events to track active field
133 aControl->Bind( wxEVT_SET_FOCUS, &DRC_RE_BITMAP_OVERLAY_PANEL::OnFieldFocus, this );
134 aControl->Bind( wxEVT_KILL_FOCUS, &DRC_RE_BITMAP_OVERLAY_PANEL::OnFieldBlur, this );
135}
136
137
139{
141 return;
142
143 wxBitmapBundle bundle = KiBitmapBundle( m_bitmapId );
144
145 m_logicalBitmapSize = FromDIP( bundle.GetDefaultSize() );
146 m_bitmap = bundle.GetBitmap( ToPhys( m_logicalBitmapSize ) );
147
148 Refresh();
149}
150
151
153{
154 m_bitmapId = aBitmap;
155 LoadBitmap();
156
157 if( m_bitmap.IsOk() )
158 SetMinSize( m_logicalBitmapSize );
159}
160
161
163{
164 for( const auto& field : m_fields )
165 {
166 const DRC_RE_FIELD_POSITION& pos = field->GetPosition();
167 wxControl* ctrl = field->GetControl();
168
169 if( !ctrl )
170 continue;
171
172 int heightLogical = ctrl->GetBestSize().GetHeight();
173
174 // The slots were drawn for millimetre values. Give a longer one the room its text
175 // needs, up to where the next field on the line begins.
176 int slotLogical = FromDIP( pos.xEnd - pos.xStart );
177 int widthLogical = slotLogical;
178
179 if( wxTextCtrl* text = dynamic_cast<wxTextCtrl*>( ctrl ) )
180 {
181 int textWidth = 0;
182 int textHeight = 0;
183
184 text->GetTextExtent( text->GetValue(), &textWidth, &textHeight );
185 widthLogical = std::max( widthLogical, textWidth + FromDIP( 24 ) );
186 }
187
188 // Where there is a neighbour the field stops short of it, and it never goes below the
189 // slot the drawing was made for, however little room the neighbour leaves.
190 if( std::optional<int> roomLogical = roomBeforeNextField( field.get() ) )
191 widthLogical = std::min( widthLogical, std::max( *roomLogical, slotLogical ) );
192
193 int xLogical = FromDIP( pos.xStart );
194 int yLogical = FromDIP( pos.yCenter ) - heightLogical / 2;
195
196 ctrl->SetPosition( wxPoint( xLogical, yLogical ) );
197 ctrl->SetSize( wxSize( widthLogical, heightLogical ) );
198
199 // Position labels if present
200 if( field->HasLabel() )
201 PositionLabel( field.get() );
202
203 if( field->HasPrefixLabel() )
204 PositionPrefixLabel( field.get() );
205 }
206
207 // A field, and the unit label after it, can reach past the right edge of the drawing, so
208 // ask for the room they use rather than the room the bitmap takes.
209 int used = m_logicalBitmapSize.x;
210
211 for( const auto& field : m_fields )
212 {
213 if( wxControl* ctrl = field->GetControl() )
214 used = std::max( used, ctrl->GetPosition().x + ctrl->GetSize().x );
215
216 if( field->HasLabel() )
217 used = std::max( used, field->GetLabel()->GetPosition().x + field->GetLabel()->GetSize().x );
218
219 if( field->HasPrefixLabel() )
220 used = std::max( used, field->GetPrefixLabel()->GetPosition().x + field->GetPrefixLabel()->GetSize().x );
221 }
222
223 int height = m_logicalBitmapSize.y;
224
225 if( m_minHeightDIP > 0 )
226 height = std::max( height, FromDIP( m_minHeightDIP ) );
227
228 SetMinSize( wxSize( used, height ) );
229}
230
231
233{
234 const DRC_RE_FIELD_POSITION& pos = aField->GetPosition();
235 std::optional<int> room;
236
237 // Leave room for what is drawn between the two fields: this field's unit label and the
238 // next field's prefix.
239 int reserved = FromDIP( LABEL_GAP );
240
241 if( aField->HasLabel() && pos.labelPosition == LABEL_POSITION::RIGHT )
242 reserved += aField->GetLabel()->GetBestSize().GetWidth() + FromDIP( LABEL_GAP );
243
244 for( const auto& other : m_fields )
245 {
246 const DRC_RE_FIELD_POSITION& otherPos = other->GetPosition();
247
248 if( other.get() == aField || otherPos.yCenter != pos.yCenter || otherPos.xStart <= pos.xStart )
249 continue;
250
251 int limit = FromDIP( otherPos.xStart - pos.xStart ) - reserved;
252
253 if( other->HasPrefixLabel() )
254 limit -= other->GetPrefixLabel()->GetBestSize().GetWidth() + FromDIP( LABEL_GAP );
255
256 if( !room || limit < *room )
257 room = limit;
258 }
259
260 return room;
261}
262
263
265{
266 wxStaticText* label = aField->GetLabel();
267 wxControl* ctrl = aField->GetControl();
268
269 if( !label || !ctrl )
270 return;
271
272 const DRC_RE_FIELD_POSITION& pos = aField->GetPosition();
273 wxPoint ctrlPos = ctrl->GetPosition();
274 wxSize ctrlSize = ctrl->GetSize();
275 wxSize labelSize = label->GetBestSize();
276
277 wxPoint labelPos;
278 int GAP = FromDIP( LABEL_GAP );
279
280 switch( pos.labelPosition )
281 {
283 labelPos.x = ctrlPos.x - labelSize.GetWidth() - GAP;
284 labelPos.y = ctrlPos.y + ( ctrlSize.GetHeight() - labelSize.GetHeight() ) / 2;
285 break;
286
288 labelPos.x = ctrlPos.x + ctrlSize.GetWidth() + GAP;
289 labelPos.y = ctrlPos.y + ( ctrlSize.GetHeight() - labelSize.GetHeight() ) / 2;
290 break;
291
293 labelPos.x = ctrlPos.x + ( ctrlSize.GetWidth() - labelSize.GetWidth() ) / 2;
294 labelPos.y = ctrlPos.y - labelSize.GetHeight() - GAP;
295 break;
296
298 labelPos.x = ctrlPos.x + ( ctrlSize.GetWidth() - labelSize.GetWidth() ) / 2;
299 labelPos.y = ctrlPos.y + ctrlSize.GetHeight() + GAP;
300 break;
301
303 default:
304 return;
305 }
306
307 label->SetPosition( labelPos );
308 label->SetSize( labelSize );
309}
310
311
313{
314 wxStaticText* prefix = aField->GetPrefixLabel();
315 wxControl* ctrl = aField->GetControl();
316
317 if( !prefix || !ctrl )
318 return;
319
320 wxPoint ctrlPos = ctrl->GetPosition();
321 wxSize ctrlSize = ctrl->GetSize();
322 wxSize prefixSize = prefix->GetBestSize();
323
324 int GAP = FromDIP( LABEL_GAP );
325
326 wxPoint prefixPos;
327 prefixPos.x = ctrlPos.x - prefixSize.GetWidth() - GAP;
328 prefixPos.y = ctrlPos.y + ( ctrlSize.GetHeight() - prefixSize.GetHeight() ) / 2;
329
330 prefix->SetPosition( prefixPos );
331 prefix->SetSize( prefixSize );
332}
333
334
336{
337 // Base implementation does nothing; derived classes override to populate fields
338 return true;
339}
340
341
343{
344 // Base implementation does nothing; derived classes override to read fields
345 return true;
346}
347
348
350{
351 for( const auto& field : m_fields )
352 field->ClearError();
353}
354
355
356void DRC_RE_BITMAP_OVERLAY_PANEL::ShowFieldError( const wxString& aFieldId, const wxString& aMessage )
357{
358 auto it = m_fieldIdMap.find( aFieldId );
359
360 if( it != m_fieldIdMap.end() )
361 it->second->ShowError( aMessage );
362}
363
364
366 const DRC_RE_FIELD_POSITION& aPosition )
367{
368 long style = wxALIGN_CENTER_VERTICAL;
369
370 if( aPosition.labelPosition == LABEL_POSITION::LEFT )
371 style |= wxALIGN_RIGHT;
372 else
373 style |= wxALIGN_LEFT;
374
375 wxCheckBox* checkbox =
376 new wxCheckBox( this, wxID_ANY, aPosition.labelText, wxDefaultPosition, wxDefaultSize, style );
377
378 return AddControl( aId, aPosition, checkbox );
379}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
BITMAPS
A list of all bitmap identifiers.
@ INVALID_BITMAP
DRC_RE_OVERLAY_FIELD * AddCheckbox(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition)
Create and position a checkbox control on the bitmap overlay.
void LoadBitmap()
Load the appropriate bitmap variant for the current theme and DPI.
void OnThemeChange(wxSysColourChangedEvent &aEvent)
wxBitmap m_bitmap
Current background bitmap.
DRC_RE_OVERLAY_FIELD * AddControl(const wxString &aId, const DRC_RE_FIELD_POSITION &aPosition, T *aControl)
Position a control on the bitmap overlay.
void OnDPIChanged(wxDPIChangedEvent &aEvent)
void PositionLabel(DRC_RE_OVERLAY_FIELD *aField)
Position a label relative to its field control based on the label position setting.
std::optional< int > roomBeforeNextField(const DRC_RE_OVERLAY_FIELD *aField) const
void SetupFieldStyling(wxControl *aControl)
Apply transparent styling to a field control.
std::map< wxString, DRC_RE_OVERLAY_FIELD * > m_fieldIdMap
Field ID to field lookup.
void ShowFieldError(const wxString &aFieldId, const wxString &aMessage)
Show an error indicator on the specified field.
int m_minHeightDIP
Height a subclass asks for, in DIP.
void SetBackgroundBitmap(BITMAPS aBitmap)
Set the background bitmap for this panel.
wxSize m_logicalBitmapSize
Bitmap size in logical pixels.
DRC_RE_BITMAP_OVERLAY_PANEL(wxWindow *aParent, wxWindowID aId=wxID_ANY)
void ClearFieldErrors()
Clear error indicators from all fields.
BITMAPS m_bitmapId
BITMAPS enum value.
void PositionFields()
Position all fields based on the current scale factor.
std::vector< std::unique_ptr< DRC_RE_OVERLAY_FIELD > > m_fields
All overlay fields.
void PositionPrefixLabel(DRC_RE_OVERLAY_FIELD *aField)
Position a prefix label to the left of its field control.
Wraps a wxControl positioned over a bitmap overlay panel.
wxStaticText * GetLabel() const
wxStaticText * GetPrefixLabel() const
const DRC_RE_FIELD_POSITION & GetPosition() const
wxControl * GetControl() const
static PANEL_DRC_RULE_EDITOR * FindEditor(wxWindow *aChild)
The rule editor that hosts aChild, or nullptr when it is not inside one.
static constexpr int LABEL_GAP
Space between a field and the label beside it, in DIP.
@ BOTTOM
Label below the field.
@ RIGHT
Label to the right of the field.
@ TOP
Label above the field.
@ LEFT
Label to the left of the field.
Specifies the position and size of a field overlaid on a constraint bitmap.
LABEL_POSITION labelPosition
Position of label relative to field.
int xEnd
Right edge X coordinate where the field ends.
int xStart
Left edge X coordinate where the field starts.
wxString labelText
Optional label text (empty for no label)
int yCenter
Vertical center Y coordinate of the field.
DATABASE_CONNECTION dc(odbcBackend(cs))