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 The 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#include <string_utils.h>
30#include <wx/button.h>
31#include <wx/textctrl.h>
32#include <wx/checkbox.h>
33#include <wx/gbsizer.h>
34#include <wx/stattext.h>
35
36
38 std::vector<ENTRY> aEntries ) :
39 DIALOG_SHIM( aParent, wxID_ANY, aCaption ),
40 m_entries( std::move( aEntries ) )
41{
42 SetSizeHints( wxDefaultSize, wxDefaultSize );
43
44 wxBoxSizer* bSizerMain = new wxBoxSizer( wxVERTICAL );
45
46 wxGridBagSizer* bSizerContent;
47
48 bSizerContent = new wxGridBagSizer( 0, 0 );
49 bSizerContent->SetFlexibleDirection( wxHORIZONTAL );
50 bSizerContent->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_ALL );
51
52 bSizerMain->Add( bSizerContent, 1, wxEXPAND | wxRIGHT | wxLEFT, 5 );
53
54 int gbRow = 0;
55
56 for( const ENTRY& entry : m_entries )
57 {
58 std::visit(
59 [&]( const auto& aValue )
60 {
61 using EntryType = std::decay_t<decltype( aValue )>;
62
63 if constexpr( std::is_same_v<EntryType, UNIT_BOUND> )
64 {
65 // Label / Entry / Unit
66 // and a binder
67 wxStaticText* label = new wxStaticText( this, wxID_ANY, entry.m_label );
68 label->Wrap( -1 );
69 bSizerContent->Add( label, wxGBPosition( gbRow, 0 ), wxGBSpan( 1, 1 ),
70 wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxLEFT, 5 );
71
72 wxTextCtrl* textCtrl = new wxTextCtrl( this, wxID_ANY );
73 bSizerContent->Add( textCtrl, wxGBPosition( gbRow, 1 ), wxGBSpan( 1, 1 ),
74 wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
75
76 wxStaticText* units = new wxStaticText( this, wxID_ANY, _( "unit" ) );
77 units->Wrap( -1 );
78 bSizerContent->Add( units, wxGBPosition( gbRow, 2 ), wxGBSpan( 1, 1 ),
79 wxTOP | wxBOTTOM | wxRIGHT | wxALIGN_CENTER_VERTICAL, 5 );
80
81 if( !entry.m_tooltip.IsEmpty() )
82 textCtrl->SetToolTip( entry.m_tooltip );
83
84 m_controls.push_back( textCtrl );
85 m_unit_binders.push_back( std::make_unique<UNIT_BINDER>( aParent, label, textCtrl, units ) );
86
87 m_unit_binders.back()->SetValue( aValue.m_default );
88 }
89 else if constexpr( std::is_same_v<EntryType, CHECKBOX> )
90 {
91 // Checkbox across all 3 cols
92 wxCheckBox* checkBox = new wxCheckBox( this, wxID_ANY, entry.m_label );
93 checkBox->SetValue( aValue.m_default );
94 bSizerContent->Add( checkBox, wxGBPosition( gbRow, 0 ), wxGBSpan( 1, 3 ),
95 wxALIGN_CENTER_VERTICAL | wxALL, 5 );
96
97 if( !entry.m_tooltip.IsEmpty() )
98 checkBox->SetToolTip( entry.m_tooltip );
99
100 m_controls.push_back( checkBox );
101 m_unit_binders.push_back( nullptr );
102 }
103 else
104 {
105 static_assert( always_false<EntryType>::value, "non-exhaustive visitor" );
106 }
107 },
108 entry.m_value );
109
110 gbRow++;
111 }
112
113 // Grow the value column (now it knows it has a col 1)
114 bSizerContent->AddGrowableCol( 1 );
115
116 wxStdDialogButtonSizer* sdbSizer1 = new wxStdDialogButtonSizer();
117 wxButton* sdbSizer1OK = new wxButton( this, wxID_OK );
118 sdbSizer1->AddButton( sdbSizer1OK );
119 wxButton* sdbSizer1Cancel = new wxButton( this, wxID_CANCEL );
120 sdbSizer1->AddButton( sdbSizer1Cancel );
121 sdbSizer1->Realize();
122
123 bSizerMain->Add( sdbSizer1, 0, wxALL | wxEXPAND, 5 );
124
125 // DIALOG_SHIM needs a title--specific hash_key so we don't save/restore state between
126 // usage cases.
127 m_hash_key = TO_UTF8( GetTitle() );
128
129 SetSizer( bSizerMain );
131 Layout();
132
133 // Now all widgets have the size fixed, call FinishDialogSettings
135}
136
137
138std::vector<WX_MULTI_ENTRY_DIALOG::RESULT> WX_MULTI_ENTRY_DIALOG::GetValues() const
139{
140 std::vector<RESULT> results;
141
142 for( size_t ii = 0; ii < m_entries.size(); ++ii )
143 {
144 wxWindow* const control = m_controls[ii];
145
146 // Visit the value definitons to look up the right control type
147 std::visit(
148 [&]( const auto& aValueDef )
149 {
150 using ArgType = std::decay_t<decltype( aValueDef )>;
151 if constexpr( std::is_same_v<ArgType, UNIT_BOUND> )
152 {
153 UNIT_BINDER* binder = m_unit_binders[ii].get();
154 wxASSERT( binder );
155 results.push_back( binder ? binder->GetValue() : 0 );
156 }
157 else if constexpr( std::is_same_v<ArgType, CHECKBOX> )
158 {
159 wxCheckBox* checkBox = static_cast<wxCheckBox*>( control );
160 results.push_back( checkBox->GetValue() );
161 }
162 else
163 {
164 static_assert( always_false<ArgType>::value, "non-exhaustive visitor" );
165 }
166 },
167 m_entries[ii].m_value );
168 }
169
170 return results;
171}
void SetupStandardButtons(std::map< int, wxString > aLabels={})
std::string m_hash_key
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
The base class for create windows for drawing purpose.
virtual long long int GetValue() const
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.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
A type that is always false.