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 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
25
26#include <sch_label.h>
27#include <sch_sheet_pin.h>
28#include <wx/colour.h>
29#include <wx/variant.h>
30
31
32// sch_label.cpp
33extern wxString getElectricalTypeLabel( LABEL_FLAG_SHAPE aType );
34
35
37 SCH_SHEET* aSheet,
38 const SCH_SHEET_PATH& aPath ) :
39 m_selectedIndex( std::optional<unsigned>() ),
40 m_agent( aAgent ),
41 m_sheet( aSheet ),
42 m_path( aPath )
43{
44}
45
46
48
49
50void SHEET_SYNCHRONIZATION_MODEL::GetValueByRow( wxVariant& aVariant, unsigned row,
51 unsigned col ) const
52{
53 const std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM>& item = m_items[row];
54
55 switch( col )
56 {
57 case NAME:
58 aVariant << wxDataViewIconText( item->GetName(), item->GetBitmap() );
59 break;
60 case SHAPE:
61 aVariant = getElectricalTypeLabel( static_cast<LABEL_FLAG_SHAPE>( item->GetShape() ) );
62 break;
63 }
64}
65
66
67bool SHEET_SYNCHRONIZATION_MODEL::SetValueByRow( const wxVariant& aVariant, unsigned row,
68 unsigned col )
69{
70 WXUNUSED( aVariant )
71 WXUNUSED( row )
72 WXUNUSED( col )
73
74 return {};
75}
76
77
78bool SHEET_SYNCHRONIZATION_MODEL::GetAttrByRow( unsigned row, unsigned int col,
79 wxDataViewItemAttr& attr ) const
80{
81 if( m_selectedIndex.has_value() && row == m_selectedIndex )
82 {
83 attr.SetBold( true );
84 return true;
85 }
86
87 return false;
88}
89
90
91void SHEET_SYNCHRONIZATION_MODEL::RemoveItems( wxDataViewItemArray const& aItems )
92{
93 if( aItems.empty() )
94 return;
95
96 for( const auto& item : TakeItems( aItems ) )
97 m_agent.RemoveItem( *item, m_sheet, m_path );
98
99 DoNotify();
100}
101
102
103bool SHEET_SYNCHRONIZATION_MODEL::AppendNewItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem )
104{
105 m_items.push_back( std::move( aItem ) );
106 Reset( GetCount() );
107 DoNotify();
108 return true;
109}
110
111
112bool SHEET_SYNCHRONIZATION_MODEL::AppendItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem )
113{
114 m_items.push_back( std::move( aItem ) );
115 Reset( GetCount() );
116 return true;
117}
118
119
121SHEET_SYNCHRONIZATION_MODEL::TakeItems( wxDataViewItemArray const& aItems )
122{
123 if( aItems.size() == 1 )
124 return { TakeItem( aItems[0] ) };
125
126 std::set<unsigned> rowsToBeRemove;
129
130 for( const auto& item : aItems )
131 {
132 if( item.IsOk() )
133 {
134 unsigned int idx = GetRow( item );
135 rowsToBeRemove.insert( idx );
136 }
137 }
138
139 for( unsigned i = 0; i < m_items.size(); i++ )
140 {
141 if( rowsToBeRemove.find( i ) == rowsToBeRemove.end() )
142 {
143 items_remain.push_back( m_items[i] );
144 }
145 else
146 {
147 items_token.push_back( m_items[i] );
148 }
149 }
150
151 UpdateItems( std::move( items_remain ) );
152 OnRowSelected( {} );
153 return items_token;
154}
155
156
158{
159 const unsigned int row = GetRow( aItem );
160
161 if( row + 1 > m_items.size() )
162 return {};
163
164 std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> item = m_items[row];
165 m_items.erase( m_items.begin() + row );
166 OnRowSelected( {} );
167 Reset( GetCount() );
168 return item;
169}
170
171
174{
175 if( aIndex < m_items.size() )
176 return m_items[aIndex];
177
178 return {};
179}
180
181
183SHEET_SYNCHRONIZATION_MODEL::GetSynchronizationItem( wxDataViewItem const& aItem ) const
184{
185 return GetSynchronizationItem( GetRow( aItem ) );
186}
187
188
189void SHEET_SYNCHRONIZATION_MODEL::OnRowSelected( std::optional<unsigned> aRow )
190{
191 m_selectedIndex = aRow;
192
193 if( aRow.has_value() && m_items.size() > *aRow )
194 {
195 if( wxDataViewItem item = GetItem( *aRow ); item.IsOk() )
196 ItemChanged( item );
197 }
198}
199
200
202{
203 m_items = std::move( aItems );
204 Reset( GetCount() );
205}
206
207
209 std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER> aNotifier )
210{
211 m_notifiers.push_back( std::move( aNotifier ) );
212}
213
214
216{
217 for( const auto& notifier : m_notifiers )
218 notifier->Notify();
219}
220
222{
223 return m_items.size();
224}
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:44
Agent for all the modifications while syncing the sheet pin and hierarchical label.
~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)
unsigned int GetCount() const override
SHEET_SYNCHRONIZATION_ITEM_LIST TakeItems(wxDataViewItemArray const &aItems)
SHEET_SYNCHRONIZATION_MODEL(SHEET_SYNCHRONIZATION_AGENT &aAgent, SCH_SHEET *aSheet, const SCH_SHEET_PATH &aPath)
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)
void Reset() override
STL namespace.
wxString getElectricalTypeLabel(LABEL_FLAG_SHAPE aType)
Definition sch_label.cpp:95
LABEL_FLAG_SHAPE
Definition sch_label.h:97
wxString getElectricalTypeLabel(LABEL_FLAG_SHAPE aType)
Definition sch_label.cpp:95
std::vector< SHEET_SYNCHRONIZATION_ITE_PTR > SHEET_SYNCHRONIZATION_ITEM_LIST
std::shared_ptr< SHEET_SYNCHRONIZATION_ITEM > SHEET_SYNCHRONIZATION_ITE_PTR