KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sheet_synchronization_model.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) 2023 Ethan Chien <[email protected]>
5 * Copyright (C) 2023 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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
29
30#include <sch_label.h>
31#include <sch_sheet_pin.h>
32#include <wx/colour.h>
33#include <wx/variant.h>
34
35
36// sch_label.cpp
37extern wxString getElectricalTypeLabel( LABEL_FLAG_SHAPE aType );
38
39
41 SCH_SHEET* aSheet,
42 SCH_SHEET_PATH& aPath ) :
43 m_selectedIndex( std::optional<unsigned>() ),
44 m_agent( aAgent ),
45 m_sheet( aSheet ),
46 m_path( std::move( aPath ) )
47{
48}
49
50
52
53
54void SHEET_SYNCHRONIZATION_MODEL::GetValueByRow( wxVariant& aVariant, unsigned row,
55 unsigned col ) const
56{
57 const std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM>& item = m_items[row];
58
59 switch( col )
60 {
61 case NAME:
62 aVariant << wxDataViewIconText( item->GetName(), item->GetBitmap() );
63 break;
64 case SHAPE:
65 aVariant = getElectricalTypeLabel( static_cast<LABEL_FLAG_SHAPE>( item->GetShape() ) );
66 break;
67 }
68}
69
70
71bool SHEET_SYNCHRONIZATION_MODEL::SetValueByRow( const wxVariant& aVariant, unsigned row,
72 unsigned col )
73{
74 WXUNUSED( aVariant )
75 WXUNUSED( row )
76 WXUNUSED( col )
77
78 return {};
79}
80
81
82bool SHEET_SYNCHRONIZATION_MODEL::GetAttrByRow( unsigned row, unsigned int col,
83 wxDataViewItemAttr& attr ) const
84{
85 if( m_selectedIndex.has_value() && row == m_selectedIndex )
86 {
87 attr.SetBold( true );
88 return true;
89 }
90
91 return false;
92}
93
94
95void SHEET_SYNCHRONIZATION_MODEL::RemoveItems( wxDataViewItemArray const& aItems )
96{
97 if( aItems.empty() )
98 return;
99
100 for( const auto& item : TakeItems( aItems ) )
101 m_agent.RemoveItem( *item, m_sheet, m_path );
102
103 DoNotify();
104}
105
106
107bool SHEET_SYNCHRONIZATION_MODEL::AppendNewItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem )
108{
109 m_items.push_back( std::move( aItem ) );
110 RowAppended();
111 DoNotify();
112 return true;
113}
114
115
116bool SHEET_SYNCHRONIZATION_MODEL::AppendItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem )
117{
118 m_items.push_back( std::move( aItem ) );
119 RowAppended();
120 return true;
121}
122
123
125SHEET_SYNCHRONIZATION_MODEL::TakeItems( wxDataViewItemArray const& aItems )
126{
127 if( aItems.size() == 1 )
128 return { TakeItem( aItems[0] ) };
129
130 std::set<unsigned> rowsToBeRemove;
133
134 for( const auto& item : aItems )
135 {
136 if( item.IsOk() )
137 {
138 unsigned int idx = GetRow( item );
139 rowsToBeRemove.insert( idx );
140 }
141 }
142
143 for( unsigned i = 0; i < m_items.size(); i++ )
144 {
145 if( rowsToBeRemove.find( i ) == rowsToBeRemove.end() )
146 {
147 items_remain.push_back( m_items[i] );
148 }
149 else
150 {
151 items_token.push_back( m_items[i] );
152 }
153 }
154
155 UpdateItems( std::move( items_remain ) );
156 OnRowSelected( {} );
157 return items_token;
158}
159
160
162{
163 const unsigned int row = GetRow( aItem );
164
165 if( row + 1 > m_items.size() )
166 return {};
167
168 std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> item = m_items[row];
169 m_items.erase( m_items.begin() + row );
170 OnRowSelected( {} );
171 RowDeleted( row );
172 return item;
173}
174
175
178{
179 if( aIndex < m_items.size() )
180 return m_items[aIndex];
181
182 return {};
183}
184
185
186void SHEET_SYNCHRONIZATION_MODEL::OnRowSelected( std::optional<unsigned> aRow )
187{
188 m_selectedIndex = aRow;
189
190 if( aRow.has_value() && m_items.size() > *aRow )
191 {
192 if( wxDataViewItem item = GetItem( *aRow ); item.IsOk() )
193 ItemChanged( item );
194 }
195}
196
197
199{
200 m_items = std::move( aItems );
201 Reset( m_items.size() );
202}
203
204
206 std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER> aNotifier )
207{
208 m_notifiers.push_back( std::move( aNotifier ) );
209}
210
211
213{
214 for( const auto& notifier : m_notifiers )
215 notifier->Notify();
216}
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
Agent for all the modifications while syncing the sheet pin and hierarchical label.
void RemoveItem(SHEET_SYNCHRONIZATION_ITEM &aItem, SCH_SHEET *aSheet, SCH_SHEET_PATH const &aPath)
~SHEET_SYNCHRONIZATION_MODEL() override
std::list< std::shared_ptr< SHEET_SYNCHRONIZATION_NOTIFIER > > m_notifiers
std::optional< unsigned > m_selectedIndex
bool GetAttrByRow(unsigned row, unsigned int col, wxDataViewItemAttr &attr) const override
bool SetValueByRow(const wxVariant &variant, unsigned row, unsigned col) override
SHEET_SYNCHRONIZATION_ITE_PTR TakeItem(wxDataViewItem const &aItem)
SHEET_SYNCHRONIZATION_ITEM_LIST TakeItems(wxDataViewItemArray const &aItems)
SHEET_SYNCHRONIZATION_ITE_PTR GetSynchronizationItem(unsigned aIndex) const
void AddNotifier(std::shared_ptr< SHEET_SYNCHRONIZATION_NOTIFIER > aNotifier)
void GetValueByRow(wxVariant &variant, unsigned row, unsigned col) const override
bool AppendItem(std::shared_ptr< SHEET_SYNCHRONIZATION_ITEM > aItem)
Just append item to the list, the notifiers are not notified.
void UpdateItems(SHEET_SYNCHRONIZATION_ITEM_LIST aItems)
SHEET_SYNCHRONIZATION_AGENT & m_agent
SHEET_SYNCHRONIZATION_ITEM_LIST m_items
void RemoveItems(wxDataViewItemArray const &aItems)
bool AppendNewItem(std::shared_ptr< SHEET_SYNCHRONIZATION_ITEM > aItem)
Add a new item, the notifiers are notified.
void OnRowSelected(std::optional< unsigned > aRow)
SHEET_SYNCHRONIZATION_MODEL(SHEET_SYNCHRONIZATION_AGENT &aAgent, SCH_SHEET *aSheet, SCH_SHEET_PATH &aPath)
void Reset() override
STL namespace.
wxString getElectricalTypeLabel(LABEL_FLAG_SHAPE aType)
Definition: sch_label.cpp:143
LABEL_FLAG_SHAPE
Definition: sch_label.h:91
wxString getElectricalTypeLabel(LABEL_FLAG_SHAPE aType)
Definition: sch_label.cpp:143
std::vector< SHEET_SYNCHRONIZATION_ITE_PTR > SHEET_SYNCHRONIZATION_ITEM_LIST
std::shared_ptr< SHEET_SYNCHRONIZATION_ITEM > SHEET_SYNCHRONIZATION_ITE_PTR