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-2018 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 <base_screen.h>
27 #include <widgets/wx_grid.h>
28 #include <template_fieldnames.h>
29 #include <grid_tricks.h>
30 #include <sch_edit_frame.h>
31 #include <bitmaps.h>
32 
34 
36  wxWindow* aWindow ) :
38  m_frame( aFrame )
39 {
40  m_addFieldButton->SetBitmap( KiBitmap( small_plus_xpm ) );
41  m_deleteFieldButton->SetBitmap( KiBitmap( trash_xpm ) );
42 
43  m_checkboxColWidth = m_grid->GetColSize( 1 );
44 
45  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
46 }
47 
48 
50 {
51  // Delete the GRID_TRICKS.
52  m_grid->PopEventHandler( true );
53 }
54 
55 
57 {
59  return TransferDataToGrid();
60 }
61 
62 
64 {
66  return;
67 
68  int row = m_grid->GetNumberRows();
70 
71  TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( "Fieldname" );
72  newFieldname.m_Visible = false;
73  m_fields.insert( m_fields.end(), newFieldname );
75 
76  // wx documentation is wrong, SetGridCursor does not make visible.
77  m_grid->MakeCellVisible( row, 0 );
78  m_grid->SetGridCursor( row, 0 );
79 }
80 
81 
83 {
85  return;
86 
87  int curRow = m_grid->GetGridCursorRow();
88 
89  if( curRow >= 0 )
90  {
91  m_fields.erase( m_fields.begin() + curRow );
92  m_grid->DeleteRows( curRow );
93  }
94 
95  m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
96  m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
97 }
98 
99 
101 {
102  m_grid->Freeze();
103 
104  if( m_grid->GetNumberRows() )
105  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
106 
107  m_grid->AppendRows( m_fields.size() );
108 
109  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
110  {
111  m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
112  m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
113  m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
114 
115  // Set cell properties
116  m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
117 
118  // Render the Visible and URL columns as check boxes
119  m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
120  m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
121  m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
122 
123  m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
124  m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
125  m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
126  }
127 
128  m_grid->Thaw();
129 
130  return true;
131 }
132 
133 
135 {
136  if( !m_grid->CommitPendingChanges() )
137  return false;
138 
139  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
140  {
141  m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
142  m_fields[row].m_Visible = ( m_grid->GetCellValue( row, 1 ) != wxEmptyString );
143  m_fields[row].m_URL = ( m_grid->GetCellValue( row, 2 ) != wxEmptyString );
144  }
145 
146  return true;
147 }
148 
149 
151 {
152  if( !TransferDataFromGrid() )
153  return false;
154 
156 
157  for( const TEMPLATE_FIELDNAME& field : m_fields )
158  m_frame->AddTemplateFieldName( field );
159 
160  return true;
161 }
162 
163 
165 {
166  if( aWidth <= 0 )
167  return;
168 
169  // Account for scroll bars
170  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
171 
172  m_grid->SetColSize( 0, aWidth - 2 * m_checkboxColWidth );
173  m_grid->SetColSize( 1, m_checkboxColWidth );
174  m_grid->SetColSize( 2, m_checkboxColWidth );
175 }
176 
177 
179 {
180  AdjustGridColumns( event.GetSize().GetX() );
181 
182  event.Skip();
183 }
bool m_Visible
If first appearance of the field&#39;s editor has as visible.
PANEL_EESCHEMA_TEMPLATE_FIELDNAMES(SCH_EDIT_FRAME *aFrame, wxWindow *aWindow)
int AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName)
Insert or append a wanted symbol field name into the field names template.
Class GRID_TRICKS is used to add cut, copy, and paste to an otherwise unmodied wxGrid instance...
Definition: grid_tricks.h:51
Schematic editor (Eeschema) main window.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
void DeleteAllTemplateFieldNames()
Remove all template field names.
void OnAddButtonClick(wxCommandEvent &event) override
Function OnAddButtonClick Process the wxWidgets event produced when the user presses the Add buton fo...
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames() const
Return a template field names list for read only access.
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:156
void OnDeleteButtonClick(wxCommandEvent &event) override
Function OnDeleteButtonClick Process the wxWidgets event produced when the user presses the Delete bu...
#define max(a, b)
Definition: auxiliary.h:86
Struct TEMPLATE_FIELDNAME holds a name of a component&#39;s field, field value, and default visibility...
Class PANEL_EESCHEMA_TEMPLATE_FIELDNAMES_BASE.
BASE_SCREEN class implementation.