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 along
17 * with this program. If not, see <http://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 =
130 std::make_unique<LIBRARY_TABLE>( file, LIBRARY_TABLE_SCOPE::GLOBAL );
131
132 if( table.IsReadOnly() )
133 {
134 // Apply stored overrides to the source table so we compare against the
135 // "loaded with overrides" state, not the raw on-disk state.
136 Pgm().GetLibraryManager().ApplyLibOverrides( *sourceTable );
137
138 if( sourceTable->Rows().size() != table.Rows().size() )
139 return false;
140
141 for( size_t i = 0; i < table.Rows().size(); ++i )
142 {
143 const LIBRARY_TABLE_ROW& current = table.Rows()[i];
144 const LIBRARY_TABLE_ROW& source = sourceTable->Rows()[i];
145
146 if( current.Disabled() != source.Disabled() || current.Hidden() != source.Hidden() )
147 return true;
148 }
149
150 return false;
151 }
152
153 return table != *sourceTable;
154}
155
156
158{
159 int row = aEvent.GetRow();
160 int col = aEvent.GetCol();
161
162 if( col == COL_URI || col == COL_TYPE || col == COL_OPTIONS )
163 {
164 WX_GRID* grid = GetGrid();
165 wxString editValue = grid->GetCellValue( row, col );
166
167 if( wxGridCellEditor* cellEditor = grid->GetCellEditor( row, col ) )
168 {
169 if( cellEditor->IsCreated() && cellEditor->GetWindow()->IsShown() )
170 editValue = cellEditor->GetValue();
171
172 cellEditor->DecRef();
173 }
174
175 grid->GetTable()->SetValue( row, col, editValue );
176
177 if( GetModel()->Adapter() )
178 {
179 LIBRARY_TABLE_ROW& tableRow = GetModel()->At( row );
180 GetModel()->Adapter()->CheckTableRow( tableRow );
181 }
182
183 grid->RefreshBlock( row, COL_STATUS, row, COL_STATUS );
184 }
185}
186
187
189{
191
192 if( table.IsReadOnly() )
193 {
194 bool retVal = SaveOverrides();
195
196 if( retVal )
197 ClearDirty();
198
199 return retVal;
200 }
201
202 bool retVal = true;
203
204 table.Save().map_error(
205 [&]( const LIBRARY_ERROR& aError )
206 {
207 wxMessageBox( _( "Error saving nested library table:\n\n" ) + aError.message,
208 _( "File Save Error" ), wxOK | wxICON_ERROR );
209
210 retVal = false;
211 } );
212
213 if( retVal )
214 ClearDirty();
215
216 return retVal;
217}
218
219
221{
223 wxString tablePath = table.Path();
224
226
227 for( const LIBRARY_TABLE_ROW& row : table.Rows() )
228 {
229 if( row.Disabled() || row.Hidden() )
230 libMgr.SetLibOverride( tablePath, row.Nickname(), row.Disabled(), row.Hidden() );
231 else
232 libMgr.ClearLibOverride( tablePath, row.Nickname() );
233 }
234
235 return true;
236}
237
238
240{
241 if( !GetGrid()->CommitPendingChanges() )
242 return false;
243
244 if( TableModified() )
245 {
246 if( !HandleUnsavedChanges( this, _( "Save changes to nested library table?" ),
247 [&]() -> bool
248 {
249 return SaveTable();
250 } ) )
251 {
252 return false;
253 }
254 }
255
256 return true;
257}
258
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:132
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:150
This file is part of the common library.
#define _(s)
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString message
std::vector< std::vector< std::string > > table