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
28
30{
31 // Delete the GRID_TRICKS.
32 GetGrid()->PopEventHandler( true );
33
34 GetGrid()->Unbind( wxEVT_GRID_CELL_CHANGING, &LIB_TABLE_NOTEBOOK_PANEL::onGridCellChanging, this );
35}
36
37
38void LIB_TABLE_NOTEBOOK_PANEL::AddTable( wxAuiNotebook* aNotebook, const wxString& aTitle, bool aClosable )
39{
40 LIB_TABLE_NOTEBOOK_PANEL* panel = new LIB_TABLE_NOTEBOOK_PANEL( aNotebook, wxID_ANY );
41 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
42 WX_GRID* grid = new WX_GRID( panel, wxID_ANY );
43
44 // Grid
45 grid->CreateGrid( 1, 7 );
46 grid->EnableGridLines( true );
47 grid->SetMargins( 0, 0 );
48 grid->SetSelectionMode( wxGrid::wxGridSelectRows );
49
50 // Columns
51 grid->SetColSize( 0, 30 );
52 grid->SetColSize( 1, 48 );
53 grid->SetColSize( 2, 48 );
54 grid->SetColSize( 3, 240 );
55 grid->SetColSize( 4, 100 );
56 grid->SetColSize( 5, 80 );
57 grid->SetColSize( 6, 240 );
58 grid->SetColLabelSize( 22 );
59 grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
60
61 // Rows
62 grid->EnableDragRowSize( false );
63 grid->SetRowLabelSize( 0 );
64 grid->SetRowLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
65
66 // Cell Defaults
67 grid->SetDefaultCellAlignment( wxALIGN_LEFT, wxALIGN_CENTER );
68
69 grid->DisableColResize( COL_STATUS );
70 grid->DisableColResize( COL_VISIBLE );
71 grid->DisableColResize( COL_ENABLED );
72
73 grid->AutoSizeColumn( COL_VISIBLE, true );
74 grid->AutoSizeColumn( COL_ENABLED, true );
75
76 sizer->Add( grid, 1, wxALL|wxEXPAND, 5 );
77
78 panel->SetSizer( sizer );
79 panel->SetClosable( aClosable );
80 panel->Layout();
81 sizer->Fit( panel );
82
83 grid->Bind( wxEVT_GRID_CELL_CHANGING, &LIB_TABLE_NOTEBOOK_PANEL::onGridCellChanging, panel );
84
85 aNotebook->AddPage( panel, aTitle, false );
86}
87
88
90{
91 wxFileName file( GetModel()->Table().Path() );
92 std::unique_ptr<LIBRARY_TABLE> sourceTable = std::make_unique<LIBRARY_TABLE>( file, LIBRARY_TABLE_SCOPE::GLOBAL );
93
94 return GetModel()->Table() != *sourceTable;
95}
96
97
99{
100 int row = aEvent.GetRow();
101 int col = aEvent.GetCol();
102
103 if( col == COL_URI || col == COL_TYPE || col == COL_OPTIONS )
104 {
105 WX_GRID* grid = GetGrid();
106 wxString editValue = grid->GetCellValue( row, col );
107
108 if( wxGridCellEditor* cellEditor = grid->GetCellEditor( row, col ) )
109 {
110 if( cellEditor->IsCreated() && cellEditor->GetWindow()->IsShown() )
111 editValue = cellEditor->GetValue();
112
113 cellEditor->DecRef();
114 }
115
116 grid->GetTable()->SetValue( row, col, editValue );
117
118 if( GetModel()->Adapter() )
119 {
120 LIBRARY_TABLE_ROW& tableRow = GetModel()->At( row );
121 GetModel()->Adapter()->CheckTableRow( tableRow );
122 }
123
124 grid->RefreshBlock( row, COL_STATUS, row, COL_STATUS );
125 }
126}
127
128
130{
131 bool retVal = true;
132
133 GetModel()->Table().Save().map_error(
134 [&]( const LIBRARY_ERROR& aError )
135 {
136 wxMessageBox( _( "Error saving nested library table:\n\n" ) + aError.message,
137 _( "File Save Error" ), wxOK | wxICON_ERROR );
138
139 retVal = false;
140 } );
141
142 return retVal;
143}
144
145
147{
148 if( !GetGrid()->CommitPendingChanges() )
149 return false;
150
151 if( TableModified() )
152 {
153 if( !HandleUnsavedChanges( this, _( "Save changes to nested library table?" ),
154 [&]() -> bool
155 {
156 return SaveTable();
157 } ) )
158 {
159 return false;
160 }
161 }
162
163 return true;
164}
165
void CheckTableRow(LIBRARY_TABLE_ROW &aRow)
LIBRARY_RESULT< void > Save()
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)
LIB_TABLE_NOTEBOOK_PANEL(wxWindow *parent, wxWindowID id=wxID_ANY)
LIB_TABLE_GRID_DATA_MODEL * GetModel()
void SetClosable(bool aYes)
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:131
This file is part of the common library.
#define _(s)
wxString message