KiCad PCB EDA Suite
Loading...
Searching...
No Matches
symbol_chooser_frame.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 <pgm_base.h>
25#include <kiplatform/ui.h>
26#include <wx/button.h>
27#include <sch_base_frame.h>
28#include <eeschema_settings.h>
31
32
33static std::vector<PICKED_SYMBOL> s_SymbolHistoryList;
34static unsigned s_SymbolHistoryMaxCount = 8;
35
36static void AddSymbolToHistory( const PICKED_SYMBOL& aSymbol )
37{
38 // Remove duplicates
40 [&]( const PICKED_SYMBOL& candidate ) -> bool
41 {
42 return candidate.LibId == aSymbol.LibId
43 && candidate.Unit == aSymbol.Unit
44 && candidate.Convert == aSymbol.Convert;
45 } );
46
47 // Add the new name at the beginning of the history list
48 s_SymbolHistoryList.insert( s_SymbolHistoryList.begin(), aSymbol );
49
50 // Remove extra names
53}
54
55
56BEGIN_EVENT_TABLE( SYMBOL_CHOOSER_FRAME, SCH_BASE_FRAME )
57 // Menu (and/or hotkey) events
58 EVT_MENU( wxID_CLOSE, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
59 EVT_BUTTON( wxID_OK, SYMBOL_CHOOSER_FRAME::OnOK )
60 EVT_BUTTON( wxID_CANCEL, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
62END_EVENT_TABLE()
63
64
65#define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
66 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP )
67
69 SCH_BASE_FRAME( aKiway, aParent, FRAME_SYMBOL_CHOOSER, _( "Symbol Chooser" ),
70 wxDefaultPosition, wxDefaultSize, MODAL_FRAME, SYMBOL_CHOOSER_FRAME_NAME )
71{
72 SetModal( true );
73
74 m_messagePanel->Hide();
75
76 wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
77
78 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
80 new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */, s_SymbolHistoryList,
81 dummyAlreadyPlaced, false, false,
82 [this]()
83 {
84 wxCommandEvent dummy;
85 OnOK( dummy );
86 },
87 [this]()
88 {
89 DismissModal( false );
90 } );
91
92
93 frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
94
95 wxPanel* bottomPanel = new wxPanel( this );
96 wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
97
98 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
99 wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
100 wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
101 sdbSizer->AddButton( okButton );
102 sdbSizer->AddButton( cancelButton );
103 sdbSizer->Realize();
104
105 bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
106
107 bottomPanel->SetSizer( bottomSizer );
108 frameSizer->Add( bottomPanel, 0, wxEXPAND );
109
110 SetSizer( frameSizer );
111
112 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
114
115 Layout();
117}
118
119
120bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
121{
122 if( aSymbol && !aSymbol->IsEmpty() )
123 {
124 LIB_ID libid;
125
126 libid.Parse( *aSymbol, true );
127
128 if( libid.IsValid() )
130 }
131
132 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
133}
134
135
137{
138 // Only dismiss a modal frame once, so that the return values set by
139 // the prior DismissModal() are not bashed for ShowModal().
140 if( !IsDismissed() )
141 DismissModal( false );
142
143 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
144}
145
146
147void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
148{
150 {
153
154 m_firstPaintEvent = false;
155 }
156
157 aEvent.Skip();
158}
159
160
161void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
162{
164
165 if( libId.IsValid() )
166 {
167 PICKED_SYMBOL symbol;
168 symbol.LibId = libId;
169
170 AddSymbolToHistory( symbol );
171 DismissModal( true, libId.Format() );
172 }
173 else
174 {
175 DismissModal( false );
176 }
177}
178
179
181{
182 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
183 wxASSERT( cfg );
184 return &cfg->m_LibViewPanel.window;
185}
186
187
188void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
189{
190 Close( false );
191}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
EDA_MSG_PANEL * m_messagePanel
PANEL_LIB_VIEW m_LibViewPanel
virtual bool ShowModal(wxString *aResult=nullptr, wxWindow *aResultantFocusWindow=nullptr)
Show this wxFrame as if it were a modal dialog, with all other instantiated wxFrames disabled until t...
bool IsDismissed()
void SetModal(bool aIsModal)
Definition: kiway_player.h:155
void DismissModal(bool aRetVal, const wxString &aResult=wxEmptyString)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:51
bool IsValid() const
Check if this LID_ID is valid.
Definition: lib_id.h:172
UTF8 Format() const
Definition: lib_id.cpp:118
wxWindow * GetFocusTarget() const
void SetPreselect(const LIB_ID &aPreselect)
LIB_ID GetSelectedLibId(int *aUnit=nullptr) const
To be called after this dialog returns from ShowModal().
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Symbol library viewer main window.
void OnOK(wxCommandEvent &aEvent)
void OnPaint(wxPaintEvent &aEvent)
WINDOW_SETTINGS * GetWindowSettings(APP_SETTINGS_BASE *aCfg) override
Return a pointer to the window settings for this frame.
bool ShowModal(wxString *aSymbol, wxWindow *aParent) override
Runs the symbol viewer as a modal dialog.
PANEL_SYMBOL_CHOOSER * m_chooserPanel
SYMBOL_CHOOSER_FRAME(KIWAY *aKiway, wxWindow *aParent)
void CloseSymbolChooser(wxCommandEvent &aEvent)
#define _(s)
#define SYMBOL_CHOOSER_FRAME_NAME
@ FRAME_SYMBOL_CHOOSER
Definition: frame_type.h:37
void FixupCancelButtonCmdKeyCollision(wxWindow *aWindow)
Definition: gtk/ui.cpp:94
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition: gtk/ui.cpp:67
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:174
see class PGM_BASE
std::vector< FAB_LAYER_COLOR > dummy
LIB_ID LibId
Definition: sch_screen.h:80
Stores the common settings that are saved and loaded for each window / frame.
Definition: app_settings.h:74
static void AddSymbolToHistory(const PICKED_SYMBOL &aSymbol)
static std::vector< PICKED_SYMBOL > s_SymbolHistoryList
static unsigned s_SymbolHistoryMaxCount
#define MODAL_FRAME