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 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <pgm_base.h>
21#include <kiplatform/ui.h>
22#include <wx/button.h>
23#include <sch_base_frame.h>
24#include <eeschema_settings.h>
27#include <algorithm>
28
29
30static std::vector<PICKED_SYMBOL> s_SymbolHistoryList;
31static unsigned s_SymbolHistoryMaxCount = 8;
32
33static void AddSymbolToHistory( const PICKED_SYMBOL& aSymbol )
34{
35 // Remove duplicates
36 std::erase_if( s_SymbolHistoryList,
37 [&]( const PICKED_SYMBOL& candidate ) -> bool
38 {
39 return candidate.LibId == aSymbol.LibId
40 && candidate.Unit == aSymbol.Unit
41 && candidate.BodyStyle == aSymbol.BodyStyle;
42 } );
43
44 // Add the new name at the beginning of the history list
45 s_SymbolHistoryList.insert( s_SymbolHistoryList.begin(), aSymbol );
46
47 // Remove extra names
50}
51
52
53BEGIN_EVENT_TABLE( SYMBOL_CHOOSER_FRAME, SCH_BASE_FRAME )
54 // Menu (and/or hotkey) events
56 EVT_BUTTON( wxID_OK, SYMBOL_CHOOSER_FRAME::OnOK )
57 EVT_BUTTON( wxID_CANCEL, SYMBOL_CHOOSER_FRAME::CloseSymbolChooser )
59END_EVENT_TABLE()
60
61
62#define PARENT_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
63 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT )
64#define MODAL_STYLE ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
65 | wxWANTS_CHARS | wxFRAME_NO_TASKBAR )
66
67SYMBOL_CHOOSER_FRAME::SYMBOL_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent, bool& aCancelled ) :
68 SCH_BASE_FRAME( aKiway, aParent, FRAME_SYMBOL_CHOOSER, _( "Symbol Chooser" ),
69 wxDefaultPosition, wxDefaultSize, aParent ? PARENT_STYLE : MODAL_STYLE,
71 m_firstPaintEvent( true )
72{
73 SetModal( true );
74
75 m_messagePanel->Hide();
76
77 wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
78
79 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
80 m_chooserPanel = new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */,
82 dummyAlreadyPlaced, false, false, aCancelled,
83 // Accept handler
84 [this]()
85 {
86 wxCommandEvent dummy;
87 OnOK( dummy );
88 },
89 // Escape handler
90 [this]()
91 {
92 DismissModal( false );
93 } );
94
95
96 frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
97
98 wxPanel* bottomPanel = new wxPanel( this );
99 wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
100
101 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
102 wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
103 wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
104 sdbSizer->AddButton( okButton );
105 sdbSizer->AddButton( cancelButton );
106 sdbSizer->Realize();
107
108 bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
109
110 bottomPanel->SetSizer( bottomSizer );
111 frameSizer->Add( bottomPanel, 0, wxEXPAND );
112
113 SetSizer( frameSizer );
114 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ), m_chooserPanel->GetItemCount() ) );
115 Layout();
116 m_chooserPanel->FinishSetup();
117
118 Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
119}
120
121
126
127
128bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
129{
130 if( aSymbol && !aSymbol->IsEmpty() )
131 {
132 LIB_ID libid;
133
134 libid.Parse( *aSymbol, true );
135
136 if( libid.IsValid() )
137 m_chooserPanel->SetPreselect( libid );
138 }
139
140 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
141}
142
143
145{
146 m_chooserPanel->ShutdownCanvases();
147
148 // Only dismiss a modal frame once, so that the return values set by
149 // the prior DismissModal() are not bashed for ShowModal().
150 if( !IsDismissed() )
151 DismissModal( false );
152
153 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
154}
155
156
157void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
158{
160 {
162 KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
163
164 m_firstPaintEvent = false;
165 }
166
167 aEvent.Skip();
168}
169
170
171void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
172{
173 LIB_ID libId = m_chooserPanel->GetSelectedLibId();
174
175 if( libId.IsValid() )
176 {
177 PICKED_SYMBOL symbol;
178 symbol.LibId = libId;
179
180 AddSymbolToHistory( symbol );
181 DismissModal( true, libId.Format() );
182 }
183 else
184 {
185 DismissModal( false );
186 }
187}
188
189
191{
192 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg ) )
193 return &cfg->m_LibViewPanel.window;
194
195 wxFAIL_MSG( wxT( "SYMBOL_CHOOSER not running with EESCHEMA_SETTINGS" ) );
196 return &aCfg->m_Window; // non-null fail-safe
197}
198
199
200void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
201{
202 Close( false );
203}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
WINDOW_SETTINGS m_Window
EDA_MSG_PANEL * m_messagePanel
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...
void SetModal(bool aIsModal)
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:348
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
UTF8 Format() const
Definition lib_id.cpp:132
void OnChar(wxKeyEvent &aEvent)
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
SCH_BASE_FRAME(KIWAY *aKiway, wxWindow *aParent, FRAME_T aWindowType, const wxString &aTitle, const wxPoint &aPosition, const wxSize &aSize, long aStyle, const wxString &aFrameName)
Symbol library viewer main window.
void OnOK(wxCommandEvent &aEvent)
SYMBOL_CHOOSER_FRAME(KIWAY *aKiway, wxWindow *aParent, bool &aCancelled)
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
void CloseSymbolChooser(wxCommandEvent &aEvent)
#define _(s)
#define SYMBOL_CHOOSER_FRAME_NAME
@ FRAME_SYMBOL_CHOOSER
Definition frame_type.h:33
EVT_MENU(ID_COMPARE_PROJECT_BRANCHES, KICAD_MANAGER_FRAME::OnCompareProjectBranches) KICAD_MANAGER_FRAME
void FixupCancelButtonCmdKeyCollision(wxWindow *aWindow)
Definition wxgtk/ui.cpp:266
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition wxgtk/ui.cpp:127
see class PGM_BASE
std::vector< FAB_LAYER_COLOR > dummy
Store the common settings that are saved and loaded for each window / frame.
#define PARENT_STYLE
static void AddSymbolToHistory(const PICKED_SYMBOL &aSymbol)
#define MODAL_STYLE
static std::vector< PICKED_SYMBOL > s_SymbolHistoryList
static unsigned s_SymbolHistoryMaxCount