KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_user_defined_signals.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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <scintilla_tricks.h>
27#include <grid_tricks.h>
29#include <../sim/simulator_frame.h>
31#include <string_utils.h>
32
33
35 std::map<int, wxString>* aSignals ) :
37 m_frame( aParent ),
38 m_signals( aSignals ),
39 m_helpWindow( nullptr )
40{
41 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
42
43 wxGridCellAttr* attr = new wxGridCellAttr;
44 attr->SetReadOnly();
45 m_grid->SetColAttr( 1, attr );
46
47 for( const auto& [ id, signal ] : *m_signals )
48 addGridRow( signal, id );
49
50 m_addButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
51 m_deleteButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
52
54 Layout();
55
56 // Now all widgets have the size fixed, call FinishDialogSettings
58}
59
60
62{
63 // Delete the GRID_TRICKS.
64 m_grid->PopEventHandler( true );
65
66 if( m_helpWindow )
67 m_helpWindow->Destroy();
68}
69
70
72{
73 if( !wxDialog::TransferDataToWindow() )
74 return false;
75
76 return true;
77}
78
79
80void DIALOG_USER_DEFINED_SIGNALS::addGridRow( const wxString& aText, int aId )
81{
82 int row = m_grid->GetNumberRows();
83
84 m_grid->AppendRows();
85 m_grid->SetCellValue( row, 0, aText );
86 m_grid->SetCellValue( row, 1, wxString::Format( wxS( "%d" ), aId ) );
87
88 wxGridCellAttr* attr = new wxGridCellAttr;
89#if defined( __WXMSW__ )
90 // For some obscure reason, a GRID_CELL_STC_EDITOR does not work on MSW:
91 // It has a strange behavior when entering data, making it unusable.
92 // (see https://gitlab.com/kicad/code/kicad/-/issues/15837)
93 // So use a GRID_CELL_TEXT_EDITOR.
94 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
95#else
96 attr->SetEditor( new GRID_CELL_STC_EDITOR( true,
97 [this]( wxStyledTextEvent& aEvent, SCINTILLA_TRICKS* aScintillaTricks )
98 {
99 onScintillaCharAdded( aEvent, aScintillaTricks );
100 } ) );
101#endif
102 m_grid->SetAttr( row, 0, attr );
103}
104
105
106void DIALOG_USER_DEFINED_SIGNALS::onAddSignal( wxCommandEvent& event )
107{
109 return;
110
111 long newId = 0;
112
113 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
114 {
115 long usedId;
116 m_grid->GetCellValue( ii, 1 ).ToLong( &usedId );
117
118 if( usedId >= newId )
119 newId = usedId + 1;
120 }
121
122 addGridRow( wxEmptyString, (int) newId );
123
124 m_grid->MakeCellVisible( m_grid->GetNumberRows() - 1, 0 );
125 m_grid->SetGridCursor( m_grid->GetNumberRows() - 1, 0 );
126
127 m_grid->EnableCellEditControl( true );
128 m_grid->ShowCellEditControl();
129}
130
131
133{
134 int curRow = m_grid->GetGridCursorRow();
135
136 if( curRow < 0 || m_grid->GetNumberRows() <= curRow )
137 return;
138
139 m_grid->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
140 m_grid->DeleteRows( curRow, 1 );
141
142 m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
143 m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
144}
145
146
148 SCINTILLA_TRICKS* aTricks )
149{
150 wxStyledTextCtrl* textCtrl = aTricks->Scintilla();
151 wxArrayString tokens;
152
153 for( const wxString& signal : m_frame->SimPlotVectors() )
154 tokens.push_back( signal );
155
156 tokens.push_back( wxS( "sqrt(x)" ) );
157 tokens.push_back( wxS( "sin(x)" ) );
158 tokens.push_back( wxS( "cos(x)" ) );
159 tokens.push_back( wxS( "tan(x)" ) );
160 tokens.push_back( wxS( "sinh(x)" ) );
161 tokens.push_back( wxS( "cosh(x)" ) );
162 tokens.push_back( wxS( "tanh(x)" ) );
163 tokens.push_back( wxS( "asin(x)" ) );
164 tokens.push_back( wxS( "acos(x)" ) );
165 tokens.push_back( wxS( "atan(x)" ) );
166 tokens.push_back( wxS( "asinh(x)" ) );
167 tokens.push_back( wxS( "acosh(x)" ) );
168 tokens.push_back( wxS( "atanh(x)" ) );
169 tokens.push_back( wxS( "arctan(x)" ) );
170 tokens.push_back( wxS( "exp(x)" ) );
171 tokens.push_back( wxS( "ln(x)" ) );
172 tokens.push_back( wxS( "log(x)" ) );
173 tokens.push_back( wxS( "abs(x)" ) );
174 tokens.push_back( wxS( "nint(x)" ) );
175 tokens.push_back( wxS( "int(x)" ) );
176 tokens.push_back( wxS( "floor(x)" ) );
177 tokens.push_back( wxS( "ceil(x)" ) );
178 tokens.push_back( wxS( "pow(x,y)" ) );
179 tokens.push_back( wxS( "pwr(x,y)" ) );
180 tokens.push_back( wxS( "min(x,y)" ) );
181 tokens.push_back( wxS( "max(x,y)" ) );
182 tokens.push_back( wxS( "sgn(x)" ) );
183 tokens.push_back( wxS( "ternary_fcn(x,y,z)" ) );
184 tokens.push_back( wxS( "gauss(nom,rvar,sigma)" ) );
185 tokens.push_back( wxS( "agauss(nom,avar,sigma)" ) );
186 tokens.push_back( wxS( "unif(nom,rvar)" ) );
187 tokens.push_back( wxS( "aunif(nom,avar)" ) );
188 tokens.push_back( wxS( "limit(nom,avar)" ) );
189
190 int text_pos = textCtrl->GetCurrentPos();
191 int start = textCtrl->WordStartPosition( text_pos, true );
192 int parenCount = 0;
193
194 for( start = text_pos - 1; start > 0; start-- )
195 {
196 wxUniChar c = textCtrl->GetCharAt( start );
197
198 if( c == '(' )
199 {
200 if( parenCount )
201 {
202 start += 1;
203 break;
204 }
205 else
206 {
207 parenCount++;
208 }
209 }
210 else if( wxIsalpha( c ) && parenCount )
211 {
212 break;
213 }
214 else if( !wxIsalnum( c ) && c != '/' )
215 {
216 start += 1;
217 break;
218 }
219 }
220
221 wxString partial = textCtrl->GetRange( start, text_pos );
222
223 aTricks->DoAutocomplete( partial, tokens );
224 textCtrl->SetFocus();
225}
226
227
229{
230 if( !wxDialog::TransferDataFromWindow() )
231 return false;
232
234 return false;
235
236 m_signals->clear();
237
238 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
239 {
240 wxString signal = m_grid->GetCellValue( ii, 0 );
241
242 if( !signal.IsEmpty() )
243 {
244 long id;
245 m_grid->GetCellValue( ii, 1 ).ToLong( &id );
246 (*m_signals)[ (int) id ] = signal;
247 }
248 }
249
250 return true;
251}
252
253
254void DIALOG_USER_DEFINED_SIGNALS::OnFormattingHelp( wxHyperlinkEvent& aEvent )
255{
256 wxString msg =
257#include "../sim/user_defined_signals_help_md.h"
258 ;
259
260 m_helpWindow = new HTML_MESSAGE_BOX( nullptr, _( "Syntax Help" ) );
261
262 wxSize sz( 320, 320 );
263 m_helpWindow->SetMinSize( m_helpWindow->ConvertDialogToPixels( sz ) );
264 m_helpWindow->SetDialogSizeInDU( sz.x, sz.y );
265
266 wxString html_txt;
267 ConvertMarkdown2Html( wxGetTranslation( msg ), html_txt );
268 m_helpWindow->AddHTML_Text( html_txt );
270}
271
272
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Class DIALOG_USER_DEFINED_SIGNALS_BASE.
void onAddSignal(wxCommandEvent &event) override
DIALOG_USER_DEFINED_SIGNALS(SIMULATOR_FRAME *parent, std::map< int, wxString > *aSignals)
void onDeleteSignal(wxCommandEvent &event) override
void addGridRow(const wxString &aValue, int aId)
void onScintillaCharAdded(wxStyledTextEvent &aEvent, SCINTILLA_TRICKS *aTricks)
std::map< int, wxString > * m_signals
void OnFormattingHelp(wxHyperlinkEvent &aEvent) override
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
void SetDialogSizeInDU(int aWidth, int aHeight)
Set the dialog size, using a "logical" value.
void AddHTML_Text(const wxString &message)
Add HTML text (without any change) to message list.
void ShowModeless()
Show a modeless version of the dialog (without an OK button).
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
wxStyledTextCtrl * Scintilla() const
void DoAutocomplete(const wxString &aPartial, const wxArrayString &aTokens)
The SIMULATOR_FRAME holds the main user-interface for running simulations.
const std::vector< wxString > SimPlotVectors()
void SetBitmap(const wxBitmapBundle &aBmp)
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:637
#define _(s)
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)