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 <[email protected]>
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_base.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 
46 
47  m_checkboxColWidth = m_grid->GetColSize( 1 );
48 
49  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
50 }
51 
52 
54 {
55  // Delete the GRID_TRICKS.
56  m_grid->PopEventHandler( true );
57 }
58 
59 
61 {
62  SCHEMATIC& schematic = m_frame->Schematic();
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  m_grid->ClearRows();
111  m_grid->AppendRows( m_fields.size() );
112 
113  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
114  {
115  m_grid->SetCellValue( row, 0, m_fields[row].m_Name );
116  m_grid->SetCellValue( row, 1, m_fields[row].m_Visible ? wxT( "1" ) : wxEmptyString );
117  m_grid->SetCellValue( row, 2, m_fields[row].m_URL ? wxT( "1" ) : wxEmptyString );
118 
119  // Set cell properties
120  m_grid->SetCellAlignment( row, 0, wxALIGN_LEFT, wxALIGN_CENTRE );
121 
122  // Render the Visible and URL columns as check boxes
123  m_grid->SetCellRenderer( row, 1, new wxGridCellBoolRenderer() );
124  m_grid->SetReadOnly( row, 1 ); // Not really; we delegate interactivity to GRID_TRICKS
125  m_grid->SetCellAlignment( row, 1, wxALIGN_CENTRE, wxALIGN_CENTRE );
126 
127  m_grid->SetCellRenderer( row, 2, new wxGridCellBoolRenderer() );
128  m_grid->SetReadOnly( row, 2 ); // Not really; we delegate interactivity to GRID_TRICKS
129  m_grid->SetCellAlignment( row, 2, wxALIGN_CENTRE, wxALIGN_CENTRE );
130  }
131 
132  m_grid->Thaw();
133 
134  return true;
135 }
136 
137 
139 {
140  if( !m_grid->CommitPendingChanges() )
141  return false;
142 
143  for( int row = 0; row < m_grid->GetNumberRows(); ++row )
144  {
145  m_fields[row].m_Name = m_grid->GetCellValue( row, 0 );
146  m_fields[row].m_Visible = m_grid->GetCellValue( row, 1 ) != wxEmptyString;
147  m_fields[row].m_URL = m_grid->GetCellValue( row, 2 ) != wxEmptyString;
148  }
149 
150  return true;
151 }
152 
153 
155 {
156  if( !TransferDataFromGrid() )
157  return false;
158 
159  SCHEMATIC& schematic = m_frame->Schematic();
160 
162 
163  for( const TEMPLATE_FIELDNAME& field : m_fields )
165 
166  if( m_global )
167  {
168  auto* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
169 
170  if( cfg )
171  {
172  // Save global fieldname templates
173  STRING_FORMATTER sf;
174  schematic.Settings().m_TemplateFieldNames.Format( &sf, 0, true );
175 
176  wxString record = FROM_UTF8( sf.GetString().c_str() );
177  record.Replace( wxT("\n"), wxT(""), true ); // strip all newlines
178  record.Replace( wxT(" "), wxT(" "), true ); // double space to single
179 
180  cfg->m_Drawing.field_names = record.ToStdString();
181  }
182  }
183 
184  return true;
185 }
186 
187 
189 {
190  if( aWidth <= 0 )
191  return;
192 
193  // Account for scroll bars
194  aWidth -= ( m_grid->GetSize().x - m_grid->GetClientSize().x );
195 
196  m_grid->SetColSize( 0, aWidth - 2 * m_checkboxColWidth );
197  m_grid->SetColSize( 1, m_checkboxColWidth );
198  m_grid->SetColSize( 2, m_checkboxColWidth );
199 }
200 
201 
203 {
204  AdjustGridColumns( event.GetSize().GetX() );
205 
206  event.Skip();
207 }
208 
209 
211 {
212  m_fields = templateMgr->GetTemplateFieldNames( m_global );
214 }
215 
216 
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
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
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:167
TEMPLATES m_TemplateFieldNames
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
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.
SCHEMATIC & Schematic() const
#define _(s)
const std::string & GetString()
Definition: richio.h:438
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:190
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:105
void OnDeleteButtonClick(wxCommandEvent &event) override
Deletes the selected template fieldname from the template fieldnames data.
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:106
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:414
const TEMPLATE_FIELDNAMES & GetTemplateFieldNames()
Return a template field name list for read only access.