KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_table_notebook_panel.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 <wx/sizer.h>
21#include <wx/aui/auibook.h>
22#include <wx/msgdlg.h>
23#include <confirm.h>
27#include <pgm_base.h>
28
29
31{
32 // Delete the GRID_TRICKS.
33 GetGrid()->PopEventHandler( true );
34
35 GetGrid()->Unbind( wxEVT_GRID_CELL_CHANGING, &LIB_TABLE_NOTEBOOK_PANEL::onGridCellChanging, this );
36}
37
38
39void LIB_TABLE_NOTEBOOK_PANEL::AddTable( wxAuiNotebook* aNotebook, const wxString& aTitle, bool aClosable )
40{
41 LIB_TABLE_NOTEBOOK_PANEL* panel = new LIB_TABLE_NOTEBOOK_PANEL( aNotebook, wxID_ANY );
42 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
43 WX_GRID* grid = new WX_GRID( panel, wxID_ANY );
44
45 // Grid
46 grid->CreateGrid( 1, 7 );
47 grid->EnableGridLines( true );
48 grid->SetMargins( 0, 0 );
49 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
50
51 // Columns
52 grid->SetColSize( 0, 30 );
53 grid->SetColSize( 1, 48 );
54 grid->SetColSize( 2, 48 );
55 grid->SetColSize( 3, 240 );
56 grid->SetColSize( 4, 100 );
57 grid->SetColSize( 5, 80 );
58 grid->SetColSize( 6, 240 );
59 grid->SetColLabelSize( 22 );
60 grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
61
62 // Rows
63 grid->EnableDragRowSize( false );
64 grid->SetRowLabelSize( 0 );
65 grid->SetRowLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
66
67 // Cell Defaults
68 grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_CENTER );
69
70 grid->DisableColResize( COL_STATUS );
71 grid->DisableColResize( COL_VISIBLE );
72 grid->DisableColResize( COL_ENABLED );
73
74 grid->AutoSizeColumn( COL_VISIBLE, true );
75 grid->AutoSizeColumn( COL_ENABLED, true );
76
77 sizer->Add( grid, 1, wxALL|wxEXPAND, 5 );
78
79 panel->SetSizer( sizer );
80 panel->SetClosable( aClosable );
81 panel->Layout();
82 sizer->Fit( panel );
83
84 grid->Bind( wxEVT_GRID_CELL_CHANGING, &LIB_TABLE_NOTEBOOK_PANEL::onGridCellChanging, panel );
85
86 panel->m_baseTitle = aTitle;
87 aNotebook->AddPage( panel, aTitle, false );
88}
89
90
92{
93 wxAuiNotebook* notebook = dynamic_cast<wxAuiNotebook*>( GetParent() );
94
95 if( !notebook )
96 return;
97
98 int page = notebook->GetPageIndex( this );
99
100 if( page == wxNOT_FOUND )
101 return;
102
103 if( !notebook->GetPageText( page ).EndsWith( wxT( " *" ) ) )
104 notebook->SetPageText( page, m_baseTitle + wxT( " *" ) );
105}
106
107
109{
110 wxAuiNotebook* notebook = dynamic_cast<wxAuiNotebook*>( GetParent() );
111
112 if( !notebook )
113 return;
114
115 int page = notebook->GetPageIndex( this );
116
117 if( page == wxNOT_FOUND )
118 return;
119
120 notebook->SetPageText( page, m_baseTitle );
121}
122
123
125{
127 wxFileName file( table.Path() );
128
129 std::unique_ptr<LIBRARY_TABLE> sourceTable = std::make_unique<LIBRARY_TABLE>( file, table.Scope() );
130
131 if( table.IsReadOnly() )
132 {
133 // Apply stored overrides to the source table so we compare against the
134 // "loaded with overrides" state, not the raw on-disk state.
135 Pgm().GetLibraryManager().ApplyLibOverrides( *sourceTable );
136
137 if( sourceTable->Rows().size() != table.Rows().size() )
138 return false;
139
140 for( size_t i = 0; i < table.Rows().size(); ++i )
141 {
142 const LIBRARY_TABLE_ROW& current = table.Rows()[i];
143 const LIBRARY_TABLE_ROW& source = sourceTable->Rows()[i];
144
145 if( current.Disabled() != source.Disabled() || current.Hidden() != source.Hidden() )
146 return true;
147 }
148
149 return false;
150 }
151
152 return table != *sourceTable;
153}
154
155
157{
158 int row = aEvent.GetRow();
159 int col = aEvent.GetCol();
160
161 if( col == COL_URI || col == COL_TYPE || col == COL_OPTIONS )
162 {
163 WX_GRID* grid = GetGrid();
164 wxString editValue = grid->GetCellValue( row, col );
165
166 if( wxGridCellEditor* cellEditor = grid->GetCellEditor( row, col ) )
167 {
168 if( cellEditor->IsCreated() && cellEditor->GetWindow()->IsShown() )
169 editValue = cellEditor->GetValue();
170
171 cellEditor->DecRef();
172 }
173
174 grid->GetTable()->SetValue( row, col, editValue );
175
176 if( GetModel()->Adapter() )
177 {
178 LIBRARY_TABLE_ROW& tableRow = GetModel()->At( row );
179 GetModel()->Adapter()->CheckTableRow( tableRow );
180 }
181
182 grid->RefreshBlock( row, COL_STATUS, row, COL_STATUS );
183 }
184}
185
186
188{
190
191 if( table.IsReadOnly() )
192 {
193 bool retVal = SaveOverrides();
194
195 if( retVal )
196 ClearDirty();
197
198 return retVal;
199 }
200
201 bool retVal = true;
202
203 table.Save().map_error(
204 [&]( const LIBRARY_ERROR& aError )
205 {
206 wxMessageBox( _( "Error saving nested library table:\n\n" ) + aError.message,
207 _( "File Save Error" ), wxOK | wxICON_ERROR );
208
209 retVal = false;
210 } );
211
212 if( retVal )
213 ClearDirty();
214
215 return retVal;
216}
217
218
220{
222 wxString tablePath = table.Path();
223
225
226 for( const LIBRARY_TABLE_ROW& row : table.Rows() )
227 {
228 if( row.Disabled() || row.Hidden() )
229 libMgr.SetLibOverride( tablePath, row.Nickname(), row.Disabled(), row.Hidden() );
230 else
231 libMgr.ClearLibOverride( tablePath, row.Nickname() );
232 }
233
234 return true;
235}
236
237
239{
240 if( !GetGrid()->CommitPendingChanges() )
241 return false;
242
243 if( TableModified() )
244 {
245 if( !HandleUnsavedChanges( this, _( "Save changes to nested library table?" ),
246 [&]() -> bool
247 {
248 return SaveTable();
249 } ) )
250 {
251 return false;
252 }
253 }
254
255 return true;
256}
257
void CheckTableRow(LIBRARY_TABLE_ROW &aRow)
void ClearLibOverride(const wxString &aTablePath, const wxString &aNickname)
Removes any override for a library that no longer needs one.
void ApplyLibOverrides(LIBRARY_TABLE &aTable)
Applies stored user overrides (disabled/hidden) to rows of a read-only table.
void SetLibOverride(const wxString &aTablePath, const wxString &aNickname, bool aDisabled, bool aHidden)
Set a user override for a library in a read-only nested table.
bool Disabled() const
bool Hidden() const
LIBRARY_MANAGER_ADAPTER * Adapter() const
LIBRARY_TABLE_ROW & At(size_t aIndex)
static void AddTable(wxAuiNotebook *aNotebook, const wxString &aTitle, bool aClosable)
void onGridCellChanging(wxGridEvent &aEvent)
bool SaveOverrides()
Save enable/visible overrides for a read-only nested table to user settings.
LIB_TABLE_NOTEBOOK_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY)
LIB_TABLE_GRID_DATA_MODEL * GetModel()
void SetClosable(bool aYes)
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:125
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition confirm.cpp:146
This file is part of the common library.
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString message