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