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 The 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
71void DIALOG_USER_DEFINED_SIGNALS::addGridRow( const wxString& aText, int aId )
72{
73 int row = m_grid->GetNumberRows();
74
75 m_grid->AppendRows();
76 m_grid->SetCellValue( row, 0, aText );
77 m_grid->SetCellValue( row, 1, wxString::Format( wxS( "%d" ), aId ) );
78
79 wxGridCellAttr* attr = new wxGridCellAttr;
80 attr->SetEditor( new GRID_CELL_STC_EDITOR( true, true,
81 [this]( wxStyledTextEvent& aEvent, SCINTILLA_TRICKS* aScintillaTricks )
82 {
83 onScintillaCharAdded( aEvent, aScintillaTricks );
84 } ) );
85 m_grid->SetAttr( row, 0, attr );
86}
87
88
89void DIALOG_USER_DEFINED_SIGNALS::onAddSignal( wxCommandEvent& event )
90{
92 [&]() -> std::pair<int, int>
93 {
94 long newId = 0;
95
96 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
97 {
98 long usedId;
99 m_grid->GetCellValue( ii, 1 ).ToLong( &usedId );
100
101 if( usedId >= newId )
102 newId = usedId + 1;
103 }
104
105 addGridRow( wxEmptyString, (int) newId );
106 return { m_grid->GetNumberRows() - 1, 0 };
107 } );
108}
109
110
112{
114 [&]( int row )
115 {
116 m_grid->DeleteRows( row, 1 );
117 } );
118}
119
120
122{
123 wxStyledTextCtrl* textCtrl = aTricks->Scintilla();
124 wxArrayString tokens;
125
126 for( const wxString& signal : m_frame->SimPlotVectors() )
127 tokens.push_back( signal );
128
129 tokens.push_back( wxS( "sqrt(x)" ) );
130 tokens.push_back( wxS( "sin(x)" ) );
131 tokens.push_back( wxS( "cos(x)" ) );
132 tokens.push_back( wxS( "tan(x)" ) );
133 tokens.push_back( wxS( "sinh(x)" ) );
134 tokens.push_back( wxS( "cosh(x)" ) );
135 tokens.push_back( wxS( "tanh(x)" ) );
136 tokens.push_back( wxS( "asin(x)" ) );
137 tokens.push_back( wxS( "acos(x)" ) );
138 tokens.push_back( wxS( "atan(x)" ) );
139 tokens.push_back( wxS( "asinh(x)" ) );
140 tokens.push_back( wxS( "acosh(x)" ) );
141 tokens.push_back( wxS( "atanh(x)" ) );
142 tokens.push_back( wxS( "arctan(x)" ) );
143 tokens.push_back( wxS( "exp(x)" ) );
144 tokens.push_back( wxS( "ln(x)" ) );
145 tokens.push_back( wxS( "log(x)" ) );
146 tokens.push_back( wxS( "abs(x)" ) );
147 tokens.push_back( wxS( "nint(x)" ) );
148 tokens.push_back( wxS( "int(x)" ) );
149 tokens.push_back( wxS( "floor(x)" ) );
150 tokens.push_back( wxS( "ceil(x)" ) );
151 tokens.push_back( wxS( "pow(x,y)" ) );
152 tokens.push_back( wxS( "pwr(x,y)" ) );
153 tokens.push_back( wxS( "min(x,y)" ) );
154 tokens.push_back( wxS( "max(x,y)" ) );
155 tokens.push_back( wxS( "sgn(x)" ) );
156 tokens.push_back( wxS( "ternary_fcn(x,y,z)" ) );
157 tokens.push_back( wxS( "gauss(nom,rvar,sigma)" ) );
158 tokens.push_back( wxS( "agauss(nom,avar,sigma)" ) );
159 tokens.push_back( wxS( "unif(nom,rvar)" ) );
160 tokens.push_back( wxS( "aunif(nom,avar)" ) );
161 tokens.push_back( wxS( "limit(nom,avar)" ) );
162
163 int text_pos = textCtrl->GetCurrentPos();
164 int start = textCtrl->WordStartPosition( text_pos, true );
165 int parenCount = 0;
166
167 for( start = text_pos - 1; start > 0; start-- )
168 {
169 wxUniChar c = textCtrl->GetCharAt( start );
170
171 if( c == '(' )
172 {
173 if( parenCount )
174 {
175 start += 1;
176 break;
177 }
178 else
179 {
180 parenCount++;
181 }
182 }
183 else if( wxIsalpha( c ) && parenCount )
184 {
185 break;
186 }
187 else if( !wxIsalnum( c ) && c != '/' )
188 {
189 start += 1;
190 break;
191 }
192 }
193
194 if( start >= 0 ) // i.e. at least one char entered
195 {
196 wxString partial = textCtrl->GetRange( start, text_pos );
197 aTricks->DoAutocomplete( partial, tokens );
198 }
199
200 textCtrl->SetFocus();
201}
202
203
205{
206 if( !wxDialog::TransferDataFromWindow() )
207 return false;
208
210 return false;
211
212 m_signals->clear();
213
214 for( int ii = 0; ii < m_grid->GetNumberRows(); ++ii )
215 {
216 wxString signal = m_grid->GetCellValue( ii, 0 );
217
218 if( !signal.IsEmpty() )
219 {
220 long id;
221 m_grid->GetCellValue( ii, 1 ).ToLong( &id );
222 (*m_signals)[ (int) id ] = signal;
223 }
224 }
225
226 return true;
227}
228
229
230void DIALOG_USER_DEFINED_SIGNALS::OnFormattingHelp( wxHyperlinkEvent& aEvent )
231{
232 wxString msg =
233#include "../sim/user_defined_signals_help_md.h"
234 ;
235
236 m_helpWindow = new HTML_MESSAGE_BOX( nullptr, _( "Syntax Help" ) );
237
238 wxSize sz( 320, 320 );
239 m_helpWindow->SetMinSize( m_helpWindow->ConvertDialogToPixels( sz ) );
240 m_helpWindow->SetDialogSizeInDU( sz.x, sz.y );
241
242 wxString html_txt;
243 ConvertMarkdown2Html( wxGetTranslation( msg ), html_txt );
244 m_helpWindow->AddHTML_Text( html_txt );
246}
247
248
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
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)
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
#define _(s)
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)