KiCad PCB EDA Suite
PANEL_TEXT_VARIABLES Class Reference

#include <panel_text_variables.h>

Inheritance diagram for PANEL_TEXT_VARIABLES:
PANEL_TEXT_VARIABLES_BASE

Public Member Functions

 PANEL_TEXT_VARIABLES (wxWindow *aParent, PROJECT *aProject)
 
 ~PANEL_TEXT_VARIABLES () override
 
bool TransferDataToWindow () override
 
bool TransferDataFromWindow () override
 

Protected Member Functions

void OnGridCellChange (wxGridEvent &event) override
 
void OnGridSize (wxSizeEvent &event) override
 
void OnUpdateUI (wxUpdateUIEvent &event) override
 
void OnGridCellChanging (wxGridEvent &event)
 
void OnAddTextVar (wxCommandEvent &event) override
 
void OnRemoveTextVar (wxCommandEvent &event) override
 
void AppendTextVar (const wxString &aName, const wxString &aValue)
 

Protected Attributes

WX_GRIDm_TextVars
 
wxBitmapButton * m_btnAddTextVar
 
wxBitmapButton * m_btnDeleteTextVar
 

Private Attributes

PROJECTm_project
 
wxString m_errorMsg
 
int m_errorRow
 
int m_errorCol
 
wxTextValidator m_nameValidator
 
bool m_gridWidthsDirty
 

Detailed Description

Definition at line 33 of file panel_text_variables.h.

Constructor & Destructor Documentation

◆ PANEL_TEXT_VARIABLES()

PANEL_TEXT_VARIABLES::PANEL_TEXT_VARIABLES ( wxWindow *  aParent,
PROJECT aProject 
)

Definition at line 42 of file panel_text_variables.cpp.

42  :
43  PANEL_TEXT_VARIABLES_BASE( aParent ),
44  m_project( aProject ),
45  m_errorRow( -1 ), m_errorCol( -1 ),
46  m_gridWidthsDirty( true )
47 {
50 
52 
53  // prohibit these characters in the alias names: []{}()%~<>"='`;:.,&?/\|$
54  m_nameValidator.SetStyle( wxFILTER_EXCLUDE_CHAR_LIST );
55  m_nameValidator.SetCharExcludes( wxT( "{}[]()%~<>\"='`;:.,&?/\\|$" ) );
56 
57  m_TextVars->PushEventHandler( new GRID_TRICKS( m_TextVars ) );
58  m_TextVars->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
59 
60  // wxFormBuilder doesn't include this event...
61  m_TextVars->Connect( wxEVT_GRID_CELL_CHANGING,
62  wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
63  nullptr, this );
64 }
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
PANEL_TEXT_VARIABLES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
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
wxTextValidator m_nameValidator
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
void OnGridCellChanging(wxGridEvent &event)

References WX_GRID::ClearRows(), KiBitmap(), PANEL_TEXT_VARIABLES_BASE::m_btnAddTextVar, PANEL_TEXT_VARIABLES_BASE::m_btnDeleteTextVar, m_nameValidator, PANEL_TEXT_VARIABLES_BASE::m_TextVars, OnGridCellChanging(), small_plus, and small_trash.

◆ ~PANEL_TEXT_VARIABLES()

PANEL_TEXT_VARIABLES::~PANEL_TEXT_VARIABLES ( )
override

Definition at line 67 of file panel_text_variables.cpp.

68 {
69  // Delete the GRID_TRICKS.
70  m_TextVars->PopEventHandler( true );
71 
72  m_TextVars->Disconnect( wxEVT_GRID_CELL_CHANGING,
73  wxGridEventHandler( PANEL_TEXT_VARIABLES::OnGridCellChanging ),
74  nullptr, this );
75 }
void OnGridCellChanging(wxGridEvent &event)

References PANEL_TEXT_VARIABLES_BASE::m_TextVars, and OnGridCellChanging().

Member Function Documentation

◆ AppendTextVar()

void PANEL_TEXT_VARIABLES::AppendTextVar ( const wxString &  aName,
const wxString &  aValue 
)
protected

Definition at line 89 of file panel_text_variables.cpp.

90 {
91  int i = m_TextVars->GetNumberRows();
92 
93  m_TextVars->AppendRows( 1 );
94 
95  m_TextVars->SetCellValue( i, TV_NAME_COL, aName );
96 
97  wxGridCellAttr* nameCellAttr = m_TextVars->GetOrCreateCellAttr( i, TV_NAME_COL );
98  wxGridCellTextEditor* nameTextEditor = new GRID_CELL_TEXT_EDITOR();
99  nameTextEditor->SetValidator( m_nameValidator );
100  nameCellAttr->SetEditor( nameTextEditor );
101  nameCellAttr->DecRef();
102 
103  m_TextVars->SetCellValue( i, TV_VALUE_COL, aValue );
104 }
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Definition: validators.h:44
wxTextValidator m_nameValidator

References m_nameValidator, PANEL_TEXT_VARIABLES_BASE::m_TextVars, TV_NAME_COL, and TV_VALUE_COL.

Referenced by OnAddTextVar(), and TransferDataToWindow().

◆ OnAddTextVar()

void PANEL_TEXT_VARIABLES::OnAddTextVar ( wxCommandEvent &  event)
overrideprotectedvirtual

Reimplemented from PANEL_TEXT_VARIABLES_BASE.

Definition at line 155 of file panel_text_variables.cpp.

156 {
158  return;
159 
160  AppendTextVar( wxEmptyString, wxEmptyString );
161 
162  m_TextVars->MakeCellVisible( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
163  m_TextVars->SetGridCursor( m_TextVars->GetNumberRows() - 1, TV_NAME_COL );
164 
165  m_TextVars->EnableCellEditControl( true );
166  m_TextVars->ShowCellEditControl();
167 }
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
void AppendTextVar(const wxString &aName, const wxString &aValue)

References AppendTextVar(), WX_GRID::CommitPendingChanges(), PANEL_TEXT_VARIABLES_BASE::m_TextVars, and TV_NAME_COL.

◆ OnGridCellChange()

void PANEL_TEXT_VARIABLES::OnGridCellChange ( wxGridEvent &  event)
overrideprotectedvirtual

Reimplemented from PANEL_TEXT_VARIABLES_BASE.

Definition at line 185 of file panel_text_variables.cpp.

186 {
187  m_gridWidthsDirty = true;
188 
189  aEvent.Skip();
190 }

References m_gridWidthsDirty.

◆ OnGridCellChanging()

void PANEL_TEXT_VARIABLES::OnGridCellChanging ( wxGridEvent &  event)
protected

Definition at line 138 of file panel_text_variables.cpp.

139 {
140  int row = event.GetRow();
141  int col = event.GetCol();
142  wxString text = event.GetString();
143 
144  if( text.IsEmpty() && col == TV_NAME_COL )
145  {
146  m_errorMsg = _( "Variable name cannot be empty." );
147  m_errorRow = row;
148  m_errorCol = col;
149 
150  event.Veto();
151  }
152 }
#define _(s)

References _, m_errorCol, m_errorMsg, m_errorRow, text, and TV_NAME_COL.

Referenced by PANEL_TEXT_VARIABLES(), and ~PANEL_TEXT_VARIABLES().

◆ OnGridSize()

void PANEL_TEXT_VARIABLES::OnGridSize ( wxSizeEvent &  event)
overrideprotectedvirtual

Reimplemented from PANEL_TEXT_VARIABLES_BASE.

Definition at line 230 of file panel_text_variables.cpp.

231 {
232  m_gridWidthsDirty = true;
233 
234  event.Skip();
235 }

References m_gridWidthsDirty.

◆ OnRemoveTextVar()

void PANEL_TEXT_VARIABLES::OnRemoveTextVar ( wxCommandEvent &  event)
overrideprotectedvirtual

Reimplemented from PANEL_TEXT_VARIABLES_BASE.

Definition at line 170 of file panel_text_variables.cpp.

171 {
172  int curRow = m_TextVars->GetGridCursorRow();
173 
174  if( curRow < 0 || m_TextVars->GetNumberRows() <= curRow )
175  return;
176 
177  m_TextVars->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
178  m_TextVars->DeleteRows( curRow, 1 );
179 
180  m_TextVars->MakeCellVisible( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
181  m_TextVars->SetGridCursor( std::max( 0, curRow-1 ), m_TextVars->GetGridCursorCol() );
182 }
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190

References WX_GRID::CommitPendingChanges(), and PANEL_TEXT_VARIABLES_BASE::m_TextVars.

◆ OnUpdateUI()

void PANEL_TEXT_VARIABLES::OnUpdateUI ( wxUpdateUIEvent &  event)
overrideprotectedvirtual

Reimplemented from PANEL_TEXT_VARIABLES_BASE.

Definition at line 193 of file panel_text_variables.cpp.

194 {
195  if( m_gridWidthsDirty )
196  {
197  int width = m_TextVars->GetClientRect().GetWidth();
198 
199  m_TextVars->AutoSizeColumn( TV_NAME_COL );
200  m_TextVars->SetColSize( TV_NAME_COL, std::max( m_TextVars->GetColSize( TV_NAME_COL ),
201  120 ) );
202 
203  m_TextVars->SetColSize( TV_VALUE_COL, width - m_TextVars->GetColSize( TV_NAME_COL ) );
204  m_gridWidthsDirty = false;
205  }
206 
207  // Handle a grid error. This is delayed to OnUpdateUI so that we can change focus
208  // even when the original validation was triggered from a killFocus event (and for
209  // dialog with notebooks, so that the corresponding notebook page can be shown in
210  // the background when triggered from an OK).
211  if( !m_errorMsg.IsEmpty() )
212  {
213  // We will re-enter this routine when the error dialog is displayed, so make
214  // sure we don't keep putting up more dialogs.
215  wxString errorMsg = m_errorMsg;
216  m_errorMsg = wxEmptyString;
217 
218  DisplayErrorMessage( this, errorMsg );
219 
220  m_TextVars->SetFocus();
221  m_TextVars->MakeCellVisible( m_errorRow, m_errorCol );
222  m_TextVars->SetGridCursor( m_errorRow, m_errorCol );
223 
224  m_TextVars->EnableCellEditControl( true );
225  m_TextVars->ShowCellEditControl();
226  }
227 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:284

References DisplayErrorMessage(), m_errorCol, m_errorMsg, m_errorRow, m_gridWidthsDirty, PANEL_TEXT_VARIABLES_BASE::m_TextVars, TV_NAME_COL, and TV_VALUE_COL.

◆ TransferDataFromWindow()

bool PANEL_TEXT_VARIABLES::TransferDataFromWindow ( )
override

Definition at line 107 of file panel_text_variables.cpp.

108 {
110  return false;
111 
112  for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
113  {
114  if( m_TextVars->GetCellValue( row, TV_NAME_COL ).IsEmpty() )
115  {
116  m_errorRow = row;
118  m_errorMsg = _( "Variable name cannot be empty." );
119  return false;
120  }
121  }
122 
123  std::map<wxString, wxString>& variables = m_project->GetTextVars();
124 
125  variables.clear();
126 
127  for( int row = 0; row < m_TextVars->GetNumberRows(); ++row )
128  {
129  wxString name = m_TextVars->GetCellValue( row, TV_NAME_COL );
130  wxString value = m_TextVars->GetCellValue( row, TV_VALUE_COL );
131  variables[ name ] = value;
132  }
133 
134  return true;
135 }
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:78
#define _(s)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:190
const char * name
Definition: DXF_plotter.cpp:56

References _, WX_GRID::CommitPendingChanges(), PROJECT::GetTextVars(), m_errorCol, m_errorMsg, m_errorRow, m_project, PANEL_TEXT_VARIABLES_BASE::m_TextVars, name, TV_NAME_COL, and TV_VALUE_COL.

◆ TransferDataToWindow()

bool PANEL_TEXT_VARIABLES::TransferDataToWindow ( )
override

Definition at line 78 of file panel_text_variables.cpp.

79 {
80  std::map<wxString, wxString>& variables = m_project->GetTextVars();
81 
82  for( const auto& var : variables )
83  AppendTextVar( var.first, var.second );
84 
85  return true;
86 }
virtual std::map< wxString, wxString > & GetTextVars() const
Definition: project.cpp:78
void AppendTextVar(const wxString &aName, const wxString &aValue)

References AppendTextVar(), PROJECT::GetTextVars(), and m_project.

Member Data Documentation

◆ m_btnAddTextVar

wxBitmapButton* PANEL_TEXT_VARIABLES_BASE::m_btnAddTextVar
protectedinherited

◆ m_btnDeleteTextVar

wxBitmapButton* PANEL_TEXT_VARIABLES_BASE::m_btnDeleteTextVar
protectedinherited

◆ m_errorCol

int PANEL_TEXT_VARIABLES::m_errorCol
private

Definition at line 58 of file panel_text_variables.h.

Referenced by OnGridCellChanging(), OnUpdateUI(), and TransferDataFromWindow().

◆ m_errorMsg

wxString PANEL_TEXT_VARIABLES::m_errorMsg
private

Definition at line 56 of file panel_text_variables.h.

Referenced by OnGridCellChanging(), OnUpdateUI(), and TransferDataFromWindow().

◆ m_errorRow

int PANEL_TEXT_VARIABLES::m_errorRow
private

Definition at line 57 of file panel_text_variables.h.

Referenced by OnGridCellChanging(), OnUpdateUI(), and TransferDataFromWindow().

◆ m_gridWidthsDirty

bool PANEL_TEXT_VARIABLES::m_gridWidthsDirty
private

Definition at line 62 of file panel_text_variables.h.

Referenced by OnGridCellChange(), OnGridSize(), and OnUpdateUI().

◆ m_nameValidator

wxTextValidator PANEL_TEXT_VARIABLES::m_nameValidator
private

Definition at line 60 of file panel_text_variables.h.

Referenced by AppendTextVar(), and PANEL_TEXT_VARIABLES().

◆ m_project

PROJECT* PANEL_TEXT_VARIABLES::m_project
private

Definition at line 54 of file panel_text_variables.h.

Referenced by TransferDataFromWindow(), and TransferDataToWindow().

◆ m_TextVars


The documentation for this class was generated from the following files: