KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_multi_unit_entry.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 <eda_draw_frame.h>
27#include <widgets/unit_binder.h>
28#include <core/type_helpers.h>
29
30#include <wx/button.h>
31#include <wx/checkbox.h>
32#include <wx/gbsizer.h>
33#include <wx/stattext.h>
34
35
37 std::vector<ENTRY> aEntries ) :
38 DIALOG_SHIM( aParent, wxID_ANY, aCaption ), m_entries( std::move( aEntries ) )
39{
40 SetSizeHints( wxDefaultSize, wxDefaultSize );
41
42 wxBoxSizer* bSizerMain = new wxBoxSizer( wxVERTICAL );
43
44 wxGridBagSizer* bSizerContent;
45
46 bSizerContent = new wxGridBagSizer( 0, 0 );
47 bSizerContent->SetFlexibleDirection( wxHORIZONTAL );
48 bSizerContent->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_ALL );
49
50 bSizerMain->Add( bSizerContent, 1, wxEXPAND | wxRIGHT | wxLEFT, 5 );
51
52 int gbRow = 0;
53
54 for( const ENTRY& entry : m_entries )
55 {
56 std::visit(
57 [&]( const auto& aValue )
58 {
59 using EntryType = std::decay_t<decltype( aValue )>;
60 if constexpr( std::is_same_v<EntryType, UNIT_BOUND> )
61 {
62 // Label / Entry / Unit
63 // and a binder
64 wxStaticText* label =
65 new wxStaticText( this, wxID_ANY, entry.m_label, wxDefaultPosition,
66 wxDefaultSize, 0 );
67 label->Wrap( -1 );
68 bSizerContent->Add( label, wxGBPosition( gbRow, 0 ), wxGBSpan( 1, 1 ),
69 wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxLEFT,
70 5 );
71
72 wxTextCtrl* textCtrl =
73 new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
74 wxDefaultSize, 0 );
75 bSizerContent->Add( textCtrl, wxGBPosition( gbRow, 1 ), wxGBSpan( 1, 1 ),
76 wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
77
78 wxStaticText* unit_label = new wxStaticText(
79 this, wxID_ANY, _( "unit" ), wxDefaultPosition, wxDefaultSize, 0 );
80 unit_label->Wrap( -1 );
81 bSizerContent->Add( unit_label, wxGBPosition( gbRow, 2 ), wxGBSpan( 1, 1 ),
82 wxTOP | wxBOTTOM | wxRIGHT | wxALIGN_CENTER_VERTICAL,
83 5 );
84
85 if( !entry.m_tooltip.IsEmpty() )
86 textCtrl->SetToolTip( entry.m_tooltip );
87
88 m_controls.push_back( textCtrl );
89 m_unit_binders.push_back( std::make_unique<UNIT_BINDER>(
90 aParent, label, textCtrl, unit_label ) );
91
92 m_unit_binders.back()->SetValue( aValue.m_default );
93 }
94 else if constexpr( std::is_same_v<EntryType, CHECKBOX> )
95 {
96 // Checkbox across all 3 cols
97 wxCheckBox* checkBox =
98 new wxCheckBox( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
99 wxDefaultSize, 0 );
100 bSizerContent->Add( checkBox, wxGBPosition( gbRow, 0 ), wxGBSpan( 1, 3 ),
101 wxALIGN_CENTER_VERTICAL | wxALL, 5 );
102
103 checkBox->SetLabel( entry.m_label );
104 checkBox->SetValue( aValue.m_default );
105
106 if( !entry.m_tooltip.IsEmpty() )
107 checkBox->SetToolTip( entry.m_tooltip );
108
109 m_controls.push_back( checkBox );
110 m_unit_binders.push_back( nullptr );
111 }
112 else
113 {
114 static_assert( always_false<EntryType>::value, "non-exhaustive visitor" );
115 }
116 },
117 entry.m_value );
118
119 gbRow++;
120 }
121
122 // Grow the value column (now it knows it has a col 1)
123 bSizerContent->AddGrowableCol( 1 );
124
125 wxStdDialogButtonSizer* sdbSizer1 = new wxStdDialogButtonSizer();
126 wxButton* sdbSizer1OK = new wxButton( this, wxID_OK );
127 sdbSizer1->AddButton( sdbSizer1OK );
128 wxButton* sdbSizer1Cancel = new wxButton( this, wxID_CANCEL );
129 sdbSizer1->AddButton( sdbSizer1Cancel );
130 sdbSizer1->Realize();
131
132 bSizerMain->Add( sdbSizer1, 0, wxALL | wxEXPAND, 5 );
133
134 SetSizer( bSizerMain );
136 Layout();
137
138 // Now all widgets have the size fixed, call FinishDialogSettings
140}
141
142
143std::vector<WX_MULTI_ENTRY_DIALOG::RESULT> WX_MULTI_ENTRY_DIALOG::GetValues() const
144{
145 std::vector<RESULT> results;
146
147 for( size_t ii = 0; ii < m_entries.size(); ++ii )
148 {
149 wxWindow* const control = m_controls[ii];
150
151 // Visit the value definitons to look up the right control type
152 std::visit(
153 [&]( const auto& aValueDef )
154 {
155 using ArgType = std::decay_t<decltype( aValueDef )>;
156 if constexpr( std::is_same_v<ArgType, UNIT_BOUND> )
157 {
158 UNIT_BINDER* binder = m_unit_binders[ii].get();
159 wxASSERT( binder );
160 results.push_back( binder ? binder->GetValue() : 0 );
161 }
162 else if constexpr( std::is_same_v<ArgType, CHECKBOX> )
163 {
164 wxCheckBox* checkBox = static_cast<wxCheckBox*>( control );
165 results.push_back( checkBox->GetValue() );
166 }
167 else
168 {
169 static_assert( always_false<ArgType>::value, "non-exhaustive visitor" );
170 }
171 },
172 m_entries[ii].m_value );
173 }
174
175 return results;
176}
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:88
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
The base class for create windows for drawing purpose.
virtual long long int GetValue()
Return the current value in Internal Units.
std::vector< std::unique_ptr< UNIT_BINDER > > m_unit_binders
WX_MULTI_ENTRY_DIALOG(EDA_DRAW_FRAME *aParent, const wxString &aCaption, std::vector< ENTRY > aEntries)
Create a multi-entry dialog.
std::vector< RESULT > GetValues() const
Returns the values in the order they were added.
std::vector< ENTRY > m_entries
std::vector< wxWindow * > m_controls
#define _(s)
STL namespace.
A type that is always false.
Definition: type_helpers.h:46