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 <widgets/wx_grid.h>
26 #include <template_fieldnames.h>
27 #include <grid_tricks.h>
28 #include <sch_edit_frame.h>
29 #include <bitmaps.h>
30 #include <schematic.h>
32 #include <kiface_i.h>
33 
35  wxWindow* aWindow,
36  bool aGlobal ) :
38  m_frame( aFrame ),
39  m_global( aGlobal )
40 {
41  m_title->SetLabel( aGlobal ? _( "Global field name templates:" )
42  : _( "Project field name templates:" ) );
43 
44  // Yep, it's a hack. I haven't a clue why the global and project versions don't lay
45  // out the same.
46  if( !aGlobal )
47  {
48  m_leftMargin->PrependSpacer( 10 );
49  m_topMargin->PrependSpacer( 10 );
50  }
51 
54 
55  m_checkboxColWidth = m_grid->GetColSize( 1 );
56 
57  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
58 }
59 
60 
62 {
63  // Delete the GRID_TRICKS.
64  m_grid->PopEventHandler( true );
65 }
66 
67 
69 {
70  SCHEMATIC& schematic = m_frame->Schematic();
71 
73  return TransferDataToGrid();
74 }
75 
76 
78 {
80  return;
81 
82  int row = m_grid->GetNumberRows();
84 
85  TEMPLATE_FIELDNAME newFieldname = TEMPLATE_FIELDNAME( _( "Untitled Field" ) );
86  newFieldname.m_Visible = false;
87  m_fields.insert( m_fields.end(), newFieldname );
89 
90  // wx documentation is wrong, SetGridCursor does not make visible.
91  m_grid->MakeCellVisible( row, 0 );
92  m_grid->SetGridCursor( row, 0 );
93 }
94 
95 
97 {
99  return;
100 
101  int curRow = m_grid->GetGridCursorRow();
102 
103  if( curRow >= 0 && curRow < (int)m_fields.size() )
104  {
105  m_fields.erase( m_fields.begin() + curRow );
106  m_grid->DeleteRows( curRow );
107  }
108 
109  m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
110  m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
111 }
112 
113 
115 {
116  m_grid->Freeze();
117 
118  if( m_grid->GetNumberRows() )
119  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
120 
121  m_grid->AppendRows( m_fields.size() );
122 
123  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
124  {
125  m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
126  m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
127  m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
128 
129  // Set cell properties
130  m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
131 
132  // Render the Visible and URL columns as check boxes
133  m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
134  m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
135  m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
136 
137  m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
138  m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
139  m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
140  }
141 
142  m_grid->Thaw();
143 
144  return true;
145 }
146 
147 
149 {
150  if( !m_grid->CommitPendingChanges() )
151  return false;
152 
153  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
154  {
155  m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
156  m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) != wxEmptyString;
157  m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) != wxEmptyString;
158  }
159 
160  return true;
161 }
162 
163 
165 {
166  if( !TransferDataFromGrid() )
167  return false;
168 
169  SCHEMATIC& schematic = m_frame->Schematic();
170 
172 
173  for( const TEMPLATE_FIELDNAME& field : m_fields )
175 
176  if( m_global )
177  {
178  auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
179 
180  if( cfg )
181  {
182  // Save global fieldname templates
183  STRING_FORMATTER sf;
184  schematic.Settings().m_TemplateFieldNames.Format( &sf, 0, true );
185 
186  wxString record = FROM_UTF8( sf.GetString().c_str() );
187  record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
188  record.Replace( wxT(" "), wxT(" "), true ); // double space to single
189 
190  cfg->m_Drawing.field_names = record.ToStdString();
191  }
192  }
193 
194  return true;
195 }
196 
197 
199 {
200  if( aWidth <= 0 )
201  return;
202 
203  // Account for scroll bars
204  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
205 
206  m_grid->SetColSize( 0, aWidth - 2 * m_checkboxColWidth );
207  m_grid->SetColSize( 1, m_checkboxColWidth );
208  m_grid->SetColSize( 2, m_checkboxColWidth );
209 }
210 
211 
213 {
214  AdjustGridColumns( event.GetSize().GetX() );
215 
216  event.Skip();
217 }
218 
219 
221 {
222  m_fields = templateMgr->GetTemplateFieldNames( m_global );
224 }
225 
226 
void Format(OUTPUTFORMATTER *out, int nestLevel, bool aGlobal) const
Serialize this object out as text into the given OUTPUTFORMATTER.
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
Holds all the data relating to one schematic.
Definition: schematic.h:59
void AddTemplateFieldName(const TEMPLATE_FIELDNAME &aFieldName, bool aGlobal)
Insert or append a wanted symbol field name into the field names template.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:127
TEMPLATES m_TemplateFieldNames
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:52
Schematic editor (Eeschema) main window.
void DeleteAllFieldNameTemplates(bool aGlobal)
Delete the entire contents.
void OnAddButtonClick(wxCommandEvent &event) override
Adds a new template fieldname (with default values) to the template fieldnames data.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
SCHEMATIC & Schematic() const
const std::string & GetString()
Definition: richio.h:435
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:182
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104
void OnDeleteButtonClick(wxCommandEvent &event) override
Deletes the selected template fieldname from the template fieldnames data.
#define _(s)
Definition: 3d_actions.cpp:33
Hold a name of a symbol's field, field value, and default visibility.
Class PANEL_EESCHEMA_TEMPLATE_FIELDNAMES_BASE.
Implement an OUTPUTFORMATTER to a memory buffer.
Definition: richio.h:411
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.