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) 2023 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 attr->SetEditor( new GRID_CELL_STC_EDITOR( true,
90 [this]( wxStyledTextEvent& aEvent, SCINTILLA_TRICKS* aScintillaTricks )
91 {
92 onScintillaCharAdded( aEvent, aScintillaTricks );
93 } ) );
94
95 m_grid->SetAttr( row, 0, attr );
96}
97
98
99void DIALOG_USER_DEFINED_SIGNALS::onAddSignal( wxCommandEvent& event )
100{
102 return;
103
104 long newId = 0;
105
106 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
107 {
108 long usedId;
109 m_grid->GetCellValue( ii, 1 ).ToLong( &usedId );
110
111 if( usedId >= newId )
112 newId = usedId + 1;
113 }
114
115 addGridRow( wxEmptyString, (int) newId );
116
117 m_grid->MakeCellVisible( m_grid->GetNumberRows() - 1, 0 );
118 m_grid->SetGridCursor( m_grid->GetNumberRows() - 1, 0 );
119
120 m_grid->EnableCellEditControl( true );
121 m_grid->ShowCellEditControl();
122}
123
124
126{
127 int curRow = m_grid->GetGridCursorRow();
128
129 if( curRow < 0 || m_grid->GetNumberRows() <= curRow )
130 return;
131
132 m_grid->CommitPendingChanges( true /* silent mode; we don't care if it's valid */ );
133 m_grid->DeleteRows( curRow, 1 );
134
135 m_grid->MakeCellVisible( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
136 m_grid->SetGridCursor( std::max( 0, curRow-1 ), m_grid->GetGridCursorCol() );
137}
138
139
141 SCINTILLA_TRICKS* aTricks )
142{
143 wxStyledTextCtrl* textCtrl = aTricks->Scintilla();
144 wxArrayString tokens;
145
146 for( const wxString& signal : m_frame->SimPlotVectors() )
147 tokens.push_back( signal );
148
149 tokens.push_back( wxS( "sqrt(x)" ) );
150 tokens.push_back( wxS( "sin(x)" ) );
151 tokens.push_back( wxS( "cos(x)" ) );
152 tokens.push_back( wxS( "tan(x)" ) );
153 tokens.push_back( wxS( "sinh(x)" ) );
154 tokens.push_back( wxS( "cosh(x)" ) );
155 tokens.push_back( wxS( "tanh(x)" ) );
156 tokens.push_back( wxS( "asin(x)" ) );
157 tokens.push_back( wxS( "acos(x)" ) );
158 tokens.push_back( wxS( "atan(x)" ) );
159 tokens.push_back( wxS( "asinh(x)" ) );
160 tokens.push_back( wxS( "acosh(x)" ) );
161 tokens.push_back( wxS( "atanh(x)" ) );
162 tokens.push_back( wxS( "arctan(x)" ) );
163 tokens.push_back( wxS( "exp(x)" ) );
164 tokens.push_back( wxS( "ln(x)" ) );
165 tokens.push_back( wxS( "log(x)" ) );
166 tokens.push_back( wxS( "abs(x)" ) );
167 tokens.push_back( wxS( "nint(x)" ) );
168 tokens.push_back( wxS( "int(x)" ) );
169 tokens.push_back( wxS( "floor(x)" ) );
170 tokens.push_back( wxS( "ceil(x)" ) );
171 tokens.push_back( wxS( "pow(x,y)" ) );
172 tokens.push_back( wxS( "pwr(x,y)" ) );
173 tokens.push_back( wxS( "min(x,y)" ) );
174 tokens.push_back( wxS( "max(x,y)" ) );
175 tokens.push_back( wxS( "sgn(x)" ) );
176 tokens.push_back( wxS( "ternary_fcn(x,y,z)" ) );
177 tokens.push_back( wxS( "gauss(nom,rvar,sigma)" ) );
178 tokens.push_back( wxS( "agauss(nom,avar,sigma)" ) );
179 tokens.push_back( wxS( "unif(nom,rvar)" ) );
180 tokens.push_back( wxS( "aunif(nom,avar)" ) );
181 tokens.push_back( wxS( "limit(nom,avar)" ) );
182
183 int text_pos = textCtrl->GetCurrentPos();
184 int start = textCtrl->WordStartPosition( text_pos, true );
185 int parenCount = 0;
186
187 for( start = text_pos - 1; start > 0; start-- )
188 {
189 wxUniChar c = textCtrl->GetCharAt( start );
190
191 if( c == '(' )
192 {
193 if( parenCount )
194 {
195 start += 1;
196 break;
197 }
198 else
199 {
200 parenCount++;
201 }
202 }
203 else if( wxIsalpha( c ) && parenCount )
204 {
205 break;
206 }
207 else if( !wxIsalnum( c ) && c != '/' )
208 {
209 start += 1;
210 break;
211 }
212 }
213
214 wxString partial = textCtrl->GetRange( start, text_pos );
215
216 aTricks->DoAutocomplete( partial, tokens );
217 textCtrl->SetFocus();
218}
219
220
222{
223 if( !wxDialog::TransferDataFromWindow() )
224 return false;
225
227 return false;
228
229 m_signals->clear();
230
231 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
232 {
233 wxString signal = m_grid->GetCellValue( ii, 0 );
234
235 if( !signal.IsEmpty() )
236 {
237 long id;
238 m_grid->GetCellValue( ii, 1 ).ToLong( &id );
239 (*m_signals)[ (int) id ] = signal;
240 }
241 }
242
243 return true;
244}
245
246
247void DIALOG_USER_DEFINED_SIGNALS::OnFormattingHelp( wxHyperlinkEvent& aEvent )
248{
249 wxString msg =
250#include "../sim/user_defined_signals_help_md.h"
251 ;
252
253 m_helpWindow = new HTML_MESSAGE_BOX( nullptr, _( "Syntax Help" ) );
254
255 wxSize sz( 320, 320 );
256 m_helpWindow->SetMinSize( m_helpWindow->ConvertDialogToPixels( sz ) );
257 m_helpWindow->SetDialogSizeInDU( sz.x, sz.y );
258
259 wxString html_txt;
260 ConvertMarkdown2Html( wxGetTranslation( msg ), html_txt );
261 m_helpWindow->AddHTML_Text( html_txt );
263}
264
265
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
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:594
#define _(s)
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)