KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_multichannel_repeat_layout.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) 2014 John Beard, [email protected]
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22#include <widgets/wx_grid.h>
23#include <bitmaps.h>
24#include <grid_tricks.h>
25#include <pcb_edit_frame.h>
28#include <zone.h>
29#include <board.h>
30#include <footprint.h>
31#include <utility>
32#include <algorithm>
33
34
36 MULTICHANNEL_TOOL *aParentTool ) :
38 m_parentTool( aParentTool )
39{
41
42 m_raGrid->PushEventHandler( new GRID_TRICKS( static_cast<WX_GRID*>( m_raGrid ) ) );
43 m_raGrid->ClearGrid();
44 m_raGrid->EnableEditing( false );
45 m_raGrid->AutoSizeColumn( 1 );
46 m_raGrid->SetupColumnAutosizer( 1 );
47
48 m_raGrid->Bind( wxEVT_GRID_CELL_LEFT_CLICK, &DIALOG_MULTICHANNEL_REPEAT_LAYOUT::OnGridCellLeftClick, this );
49
50 // The parts of the row under the cursor are marked on the board next to the reference parts
51 m_raGrid->Bind( wxEVT_GRID_SELECT_CELL,
52 [this]( wxGridEvent& aEvent )
53 {
54 int row = aEvent.GetRow();
55
56 if( row >= 0 && row < static_cast<int>( m_targetRAs.size() ) )
57 m_parentTool->HighlightRepeatAreas( m_targetRAs[row].m_targetRA );
58
59 aEvent.Skip();
60 } );
61
62 // Ticks and the anchor reshape the channels; the other options only change the preview
63 m_raGrid->Bind( wxEVT_GRID_CELL_CHANGED, [this]( wxGridEvent& ) { onOptionChanged(); } );
64 m_refAnchorFp->Bind( wxEVT_CHOICE, [this]( wxCommandEvent& ) { onOptionChanged(); } );
65
66 Bind( wxEVT_CHECKBOX,
67 [this]( wxCommandEvent& )
68 {
69 readWindow();
70 m_parentTool->UpdateRepeatPreview();
71 } );
72
73 m_cbLooseMatching->Bind( wxEVT_CHECKBOX,
74 [this]( wxCommandEvent& )
75 {
76 m_parentTool->DiscoverRepeatCandidates( m_cbLooseMatching->GetValue() );
77 m_parentTool->UpdateRepeatPreview();
79 } );
80
81 // Not modal, so Cancel has to close the dialog itself
82 Bind( wxEVT_BUTTON, [this]( wxCommandEvent& ) { Close(); }, wxID_CANCEL );
83
84 Layout();
87}
88
94
95
97{
98 RULE_AREAS_DATA* data = m_parentTool->GetData();
99
100 for( size_t i = 0; i < m_targetRAs.size(); i++ )
101 data->m_compatMap[m_targetRAs[i].m_targetRA].m_doCopy = m_raGrid->GetCellValue( i, 0 ) == wxT( "1" );
102
103 data->m_options.m_copyPlacement = m_cbCopyPlacement->GetValue();
104 data->m_options.m_copyRouting = m_cbCopyRouting->GetValue();
107 data->m_options.m_groupItems = m_cbGroupItems->GetValue();
109 data->m_linkAreas = m_cbLinkAreas->GetValue();
110 data->m_options.m_anchor = niluuid;
111
112 // A duplicate reference designator outside the area must not become the anchor
113 for( FOOTPRINT* fp : data->m_refRA->m_components )
114 {
115 if( fp->GetReference() == m_refAnchorFp->GetStringSelection() )
116 data->m_options.m_anchor = fp->m_Uuid;
117 }
118}
119
120
122{
123 readWindow();
124
125 // The refresh pumps events while it shows progress, so a second one queued behind it would run inside it
126 if( std::exchange( m_refreshPending, true ) )
127 return;
128
129 // Rebuilding the grid from inside one of its own events is not safe
130 CallAfter( [this]()
131 {
132 readWindow();
133 m_parentTool->RefreshRepeat();
135 m_refreshPending = false;
136 } );
137}
138
139
141{
142 readWindow();
143 m_parentTool->ApplyRepeat();
144
145 // This dialog is not modal, so it closes itself
146 Close();
147 return true;
148}
149
150
152{
153 RULE_AREAS_DATA* data = m_parentTool->GetData();
154 const KIID& linkSet = data->m_refRA->m_zone->GetLinkSet();
155
156 m_targetRAs.clear();
157
158 for( const auto& [ruleArea, ruleAreaData] : data->m_compatMap )
159 {
160 TABLE_ENTRY ent;
161 bool linked = linkSet != niluuid && ruleArea->m_zone->GetLinkSet() == linkSet;
162
163 ent.m_doCopy = ruleAreaData.m_doCopy;
164 ent.m_errMsg = linked && ruleAreaData.m_isOk ? _( "OK (linked)" ) : ruleAreaData.m_errorMsg;
165 ent.m_isOK = ruleAreaData.m_isOk;
166 ent.m_raName = ruleArea->m_ruleName;
167 ent.m_targetRA = ruleArea;
168 ent.m_mismatchReasons = ruleAreaData.m_mismatchReasons;
169
170 // A discovered channel is named after its parts until it is created
171 if( ent.m_raName.IsEmpty() )
172 ent.m_raName = MULTICHANNEL_TOOL::JoinReferences( ruleArea->m_components );
173
174 m_targetRAs.push_back( ent );
175 }
176
177 std::sort( m_targetRAs.begin(), m_targetRAs.end(),
178 [] ( const TABLE_ENTRY&a ,const TABLE_ENTRY& b ) -> int
179 {
180 if ( !a.m_isOK && b.m_isOK )
181 return 0;
182 else if ( a.m_isOK && !b.m_isOK )
183 return 1;
184 else
185 return a.m_raName < b.m_raName;
186 } );
187
188 m_raGrid->ClearRows();
189 m_raGrid->AppendRows( m_targetRAs.size() );
190
191 int i = 0;
192
193 for( TABLE_ENTRY& entry : m_targetRAs )
194 {
195 m_raGrid->SetCellValue( i, 1, entry.m_raName );
196 m_raGrid->SetCellValue( i, 2, entry.m_errMsg );
197 m_raGrid->SetCellValue( i, 3, wxString() );
198 m_raGrid->SetCellRenderer( i, 0, new wxGridCellBoolRenderer);
199 m_raGrid->SetCellEditor( i, 0, new wxGridCellBoolEditor);
200 m_raGrid->SetCellValue( i, 0, entry.m_doCopy ? wxT( "1" ) : wxT( "" ) );
201
202 if( !entry.m_isOK && !entry.m_mismatchReasons.empty() )
203 {
204 wxGridCellAttr* attr = new wxGridCellAttr;
205 attr->SetRenderer( new GRID_CELL_ICON_RENDERER( m_detailsIcon ) );
206 attr->SetReadOnly();
207 m_raGrid->SetAttr( i, 3, attr );
208 m_raGrid->SetCellValue( i, 3, wxString() );
209 }
210
211 i++;
212 }
213
214 m_refRAName->SetLabelText( data->FromSelection()
215 ? wxString::Format( _( "%d selected footprints" ),
216 (int) data->m_selectedFootprints.size() )
217 : data->m_refRA->m_zone->GetZoneName() );
218
219 wxArrayString refFpNames;
220 refFpNames.push_back( "" );
221
222 for( FOOTPRINT* fp : data->m_refRA->m_components )
223 refFpNames.push_back( fp->GetReference() );
224
225 refFpNames.Sort();
226 m_refAnchorFp->Set( refFpNames );
227 m_refAnchorFp->SetSelection( 0 );
228
229 for( FOOTPRINT* fp : data->m_refRA->m_components )
230 {
231 if( fp->m_Uuid == data->m_options.m_anchor )
232 m_refAnchorFp->SetStringSelection( fp->GetReference() );
233 }
234
236 m_cbCopyRouting->SetValue( data->m_options.m_copyRouting );
238 m_cbGroupItems->SetValue( data->m_options.m_groupItems );
241 m_cbLinkAreas->SetValue( data->m_linkAreas );
242
243 // Channels made from a selection are always groups; loose matching only applies to discovery
244 m_cbGroupItems->Enable( !data->FromSelection() );
245 m_cbLooseMatching->Enable( data->FromSelection() );
246
247 return true;
248}
249
250
252{
253 int row = aEvent.GetRow();
254 int col = aEvent.GetCol();
255
256 if( col == 3 && row >= 0 && row < static_cast<int>( m_targetRAs.size() ) )
257 {
258 const TABLE_ENTRY& entry = m_targetRAs[row];
259
260 if( !entry.m_isOK && !entry.m_mismatchReasons.empty() && m_parentTool )
261 {
262 wxString summary = wxString::Format( _( "Rule area topologies do not match: %s" ), entry.m_errMsg );
263
264 m_parentTool->ShowMismatchDetails( this, summary, entry.m_mismatchReasons );
265 }
266 }
267
268 aEvent.Skip();
269}
wxBitmapBundle KiBitmapBundleDef(BITMAPS aBitmap, int aDefHeight)
Construct and return a bitmap bundle for the given icon ID, with the default bitmap size being aDefHe...
Definition bitmap.cpp:112
DIALOG_MULTICHANNEL_REPEAT_LAYOUT_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Repeat Multichannel Layout"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
DIALOG_MULTICHANNEL_REPEAT_LAYOUT(PCB_BASE_FRAME *aFrame, MULTICHANNEL_TOOL *aParentTool)
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...
const KIID m_Uuid
Definition eda_item.h:599
const wxString & GetReference() const
Definition footprint.h:793
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
Definition kiid.h:46
static wxString JoinReferences(const std::set< FOOTPRINT * > &aFootprints)
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
const wxString & GetZoneName() const
Definition zone.h:160
const KIID & GetLinkSet() const
The set of placement rule areas that share one layout, or niluuid when this one is not linked.
Definition zone.h:843
#define _(s)
KIID niluuid(0)
wxString m_errMsg
bool m_isOK
wxString m_raName
RULE_AREA * m_targetRA
std::vector< wxString > m_mismatchReasons
bool m_doCopy
KIID m_anchor
This area's own anchor footprint, or niluuid for the area center.
std::unordered_map< RULE_AREA *, RULE_AREA_COMPAT_DATA > m_compatMap
REPEAT_LAYOUT_OPTIONS m_options
bool m_linkAreas
Link the ticked targets to the reference.
bool FromSelection() const
A plain selection is the reference, and its channels are created only when applied.
std::set< FOOTPRINT * > m_selectedFootprints
std::set< FOOTPRINT * > m_components