KiCad PCB EDA Suite
panel_eeschema_template_fieldnames.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) 2009 Wayne Stambaugh <stambaughw@verizon.net>
5  * Copyright (C) 1992-2020 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 
25 #include <fctsys.h>
26 #include <widgets/wx_grid.h>
27 #include <template_fieldnames.h>
28 #include <grid_tricks.h>
29 #include <sch_edit_frame.h>
30 #include <bitmaps.h>
31 
33 
35  wxWindow* aWindow,
36  bool aGlobal ) :
38  m_frame( aFrame ),
39  m_global( aGlobal )
40 {
41  wxString msg;
42 
43  msg.Printf( m_title->GetLabel(), aGlobal ? _( "Global" ) : _( "Project" ) );
44  m_title->SetLabel( msg );
45 
46  m_addFieldButton->SetBitmap( KiBitmap( small_plus_xpm ) );
47  m_deleteFieldButton->SetBitmap( KiBitmap( trash_xpm ) );
48 
49  m_checkboxColWidth = m_grid->GetColSize( 1 );
50 
51  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
52 }
53 
54 
56 {
57  // Delete the GRID_TRICKS.
58  m_grid->PopEventHandler( true );
59 }
60 
61 
63 {
65  return TransferDataToGrid();
66 }
67 
68 
70 {
72  return;
73 
74  int row = m_grid->GetNumberRows();
76 
77  TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
78  newFieldname.m_Visible = false;
79  m_fields.insert( m_fields.end(), newFieldname );
81 
82  // wx documentation is wrong, SetGridCursor does not make visible.
83  m_grid->MakeCellVisible( row, 0 );
84  m_grid->SetGridCursor( row, 0 );
85 }
86 
87 
89 {
91  return;
92 
93  int curRow = m_grid->GetGridCursorRow();
94 
95  if( curRow >= 0 && curRow < (int)m_fields.size() )
96  {
97  m_fields.erase( m_fields.begin() + curRow );
98  m_grid->DeleteRows( curRow );
99  }
100 
101  m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
102  m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
103 }
104 
105 
107 {
108  m_grid->Freeze();
109 
110  if( m_grid->GetNumberRows() )
111  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
112 
113  m_grid->AppendRows( m_fields.size() );
114 
115  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
116  {
117  m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
118  m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
119  m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
120 
121  // Set cell properties
122  m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
123 
124  // Render the Visible and URL columns as check boxes
125  m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
126  m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
127  m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
128 
129  m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
130  m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
131  m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
132  }
133 
134  m_grid->Thaw();
135 
136  return true;
137 }
138 
139 
141 {
142  if( !m_grid->CommitPendingChanges() )
143  return false;
144 
145  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
146  {
147  m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
148  m_fields[row].m_Visible = ( m_grid->GetCellValue( row, 1 ) != wxEmptyString );
149  m_fields[row].m_URL = ( m_grid->GetCellValue( row, 2 ) != wxEmptyString );
150  }
151 
152  return true;
153 }
154 
155 
157 {
158  if( !TransferDataFromGrid() )
159  return false;
160 
162 
163  for( const TEMPLATE_FIELDNAME& field : m_fields )
165 
166  return true;
167 }
168 
169 
171 {
172  if( aWidth <= 0 )
173  return;
174 
175  // Account for scroll bars
176  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
177 
178  m_grid->SetColSize( 0, aWidth - 2 * m_checkboxColWidth );
179  m_grid->SetColSize( 1, m_checkboxColWidth );
180  m_grid->SetColSize( 2, m_checkboxColWidth );
181 }
182 
183 
185 {
186  AdjustGridColumns( event.GetSize().GetX() );
187 
188  event.Skip();
189 }
190 
191 
193 {
194  m_fields = templateMgr->GetTemplateFieldNames( m_global );
196 }
197 
198 
const BITMAP_OPAQUE trash_xpm[1]
Definition: trash.cpp:46
GRID_TRICKS is used to add mouse and command handling (such as cut, copy, and paste) to a WX_GRID ins...
Definition: grid_tricks.h:51
Schematic editor (Eeschema) main window.
void DeleteAllTemplateFieldNames(bool aGlobal)
Remove all global or project fieldname templates.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:80
void OnAddButtonClick(wxCommandEvent &event) override
Function OnAddButtonClick Process the wxWidgets event produced when the user presses the Add buton fo...
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES(SCH_EDIT_FRAME *aFrame, wxWindow *aWindow, bool aGlobal)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:172
void OnDeleteButtonClick(wxCommandEvent &event) override
Function OnDeleteButtonClick Process the wxWidgets event produced when the user presses the Delete bu...
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
#define _(s)
Definition: 3d_actions.cpp:33
Struct TEMPLATE_FIELDNAME holds a name of a component's field, field value, and default visibility.
Class PANEL_EESCHEMA_TEMPLATE_FIELDNAMES_BASE.
const BITMAP_OPAQUE small_plus_xpm[1]
Definition: small_plus.cpp:20
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Function GetTemplateFieldName returns a template fieldnames list for read only access.
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field names list for read only access.