KiCad PCB EDA Suite
Loading...
Searching...
No Matches
layer_pairs.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
24#include "layer_pairs.h"
25
26#include <wx/translation.h>
27
28wxDEFINE_EVENT( PCB_LAYER_PAIR_PRESETS_CHANGED, wxCommandEvent );
29wxDEFINE_EVENT( PCB_CURRENT_LAYER_PAIR_CHANGED, wxCommandEvent );
30
31
32static bool IsAnEnabledPreset( const LAYER_PAIR& aPair, const LAYER_PAIR_SETTINGS& aSettings )
33{
34 const auto presets = aSettings.GetLayerPairs();
35 const auto matcher = [&aPair]( const LAYER_PAIR_INFO& aPreset )
36 {
37 return aPreset.GetLayerPair().HasSameLayers( aPair ) && aPreset.IsEnabled();
38 };
39
40 return std::any_of( presets.begin(), presets.end(), matcher );
41}
42
43
45{
46 m_pairs = aOther.m_pairs;
48}
49
50
52{
53 const LAYER_PAIR& newPair = aPairInfo.GetLayerPair();
54 const auto pairMatcher = [&]( const LAYER_PAIR_INFO& aExistingPair )
55 {
56 return newPair.HasSameLayers( aExistingPair.GetLayerPair() );
57 };
58
59 const bool alreadyExists = std::any_of( m_pairs.begin(), m_pairs.end(), pairMatcher );
60
61 if( alreadyExists )
62 {
63 return false;
64 }
65
66 // If we're adding a pair that matches the last manual pair
67 // we no longer need the manual one
68 if( m_lastManualPair && m_lastManualPair->HasSameLayers( newPair ) )
69 {
70 m_lastManualPair.reset();
71 }
72
73 m_pairs.push_back( std::move( aPairInfo ) );
74 return true;
75}
76
77
79{
80 bool ret = addLayerPairInternal( std::move( aPair ) );
81
82 wxCommandEvent* evt = new wxCommandEvent( PCB_LAYER_PAIR_PRESETS_CHANGED, wxID_ANY );
83 QueueEvent( evt );
84 return ret;
85}
86
87
89{
90 const auto pairMatcher = [&aPair]( const LAYER_PAIR_INFO& aPairInfo )
91 {
92 return aPairInfo.GetLayerPair().HasSameLayers( aPair );
93 };
94
95 const auto pairToRemoveIt = std::find_if( m_pairs.begin(), m_pairs.end(), pairMatcher );
96
97 if( pairToRemoveIt == m_pairs.end() )
98 {
99 return false;
100 }
101
102 m_pairs.erase( pairToRemoveIt );
103 return true;
104}
105
106
108{
109 bool ret = removeLayerPairInternal( aPair );
110
111 wxCommandEvent* evt = new wxCommandEvent( PCB_LAYER_PAIR_PRESETS_CHANGED, wxID_ANY );
112 QueueEvent( evt );
113 return ret;
114}
115
116
117std::span<const LAYER_PAIR_INFO> LAYER_PAIR_SETTINGS::GetLayerPairs() const
118{
119 return m_pairs;
120}
121
122std::span<LAYER_PAIR_INFO> LAYER_PAIR_SETTINGS::GetLayerPairs()
123{
124 return m_pairs;
125}
126
127std::vector<LAYER_PAIR_INFO> LAYER_PAIR_SETTINGS::GetEnabledLayerPairs( int& aCurrent ) const
128{
129 std::vector<LAYER_PAIR_INFO> enabledPairs;
130
131 aCurrent = -1;
132
133 if( m_lastManualPair )
134 {
135 enabledPairs.emplace_back( LAYER_PAIR_INFO{
137 true,
138 _( "Manual" ),
139 } );
140
142 {
143 aCurrent = 0;
144 }
145 }
146
147 for( const LAYER_PAIR_INFO& pair : m_pairs )
148 {
149 if( pair.IsEnabled() )
150 {
151 enabledPairs.push_back( pair );
152
153 if( m_currentPair.HasSameLayers( pair.GetLayerPair() ) )
154 {
155 aCurrent = enabledPairs.size() - 1;
156 }
157 }
158 }
159
160 return enabledPairs;
161}
162
163void LAYER_PAIR_SETTINGS::SetLayerPairs( std::span<const LAYER_PAIR_INFO> aPairs )
164{
165 // Replace all pairs with the given list
166 m_pairs.clear();
167 for( const LAYER_PAIR_INFO& pair : aPairs )
168 {
169 // Skip dupes and other
170 addLayerPairInternal( pair );
171 }
172
173 {
174 wxCommandEvent* evt = new wxCommandEvent( PCB_LAYER_PAIR_PRESETS_CHANGED, wxID_ANY );
175 QueueEvent( evt );
176 }
177}
178
180{
181 m_currentPair = aPair;
182
183 if( !IsAnEnabledPreset( aPair, *this ) )
184 {
185 m_lastManualPair = aPair;
186 }
187
188 wxCommandEvent* evt = new wxCommandEvent( PCB_CURRENT_LAYER_PAIR_CHANGED, wxID_ANY );
189 QueueEvent( evt );
190}
All information about a layer pair as stored in the layer pair store.
const LAYER_PAIR & GetLayerPair() const
Management class for layer pairs in a PCB.
Definition: layer_pairs.h:47
std::vector< LAYER_PAIR_INFO > m_pairs
Definition: layer_pairs.h:99
std::vector< LAYER_PAIR_INFO > GetEnabledLayerPairs(int &aCurrentIndex) const
Get a vector of all enabled layer pairs, in order.
bool removeLayerPairInternal(const LAYER_PAIR &aPair)
Definition: layer_pairs.cpp:88
bool AddLayerPair(LAYER_PAIR_INFO aPair)
Definition: layer_pairs.cpp:78
std::span< const LAYER_PAIR_INFO > GetLayerPairs() const
Returns a span of all stored layer pairs.
std::optional< LAYER_PAIR > m_lastManualPair
Definition: layer_pairs.h:103
void SetCurrentLayerPair(const LAYER_PAIR &aPair)
Set the "active" layer pair.
void SetLayerPairs(std::span< const LAYER_PAIR_INFO > aPairs)
Replace the stored layer pairs with the given list.
LAYER_PAIR m_currentPair
Definition: layer_pairs.h:105
bool RemoveLayerPair(const LAYER_PAIR &aPair)
Remove the matching layer pair from the store, if present.
bool addLayerPairInternal(LAYER_PAIR_INFO aPair)
Definition: layer_pairs.cpp:51
LAYER_PAIR_SETTINGS()
Construct a new layer pair manager.
Definition: layer_pairs.h:52
bool HasSameLayers(const LAYER_PAIR &aOther) const
#define _(s)
static bool IsAnEnabledPreset(const LAYER_PAIR &aPair, const LAYER_PAIR_SETTINGS &aSettings)
Definition: layer_pairs.cpp:32
wxDEFINE_EVENT(PCB_LAYER_PAIR_PRESETS_CHANGED, wxCommandEvent)