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.Convert == aSymbol.Convert;
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{
72 SetModal( true );
73
74 m_messagePanel->Hide();
75
76 wxBoxSizer* frameSizer = new wxBoxSizer( wxVERTICAL );
77
78 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
79 m_chooserPanel = new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */,
81 dummyAlreadyPlaced, false, false, aCancelled,
82 // Accept handler
83 [this]()
84 {
85 wxCommandEvent dummy;
86 OnOK( dummy );
87 },
88 // Escape handler
89 [this]()
90 {
91 DismissModal( false );
92 } );
93
94
95 frameSizer->Add( m_chooserPanel, 1, wxEXPAND );
96
97 wxPanel* bottomPanel = new wxPanel( this );
98 wxBoxSizer* bottomSizer = new wxBoxSizer( wxVERTICAL );
99
100 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
101 wxButton* okButton = new wxButton( bottomPanel, wxID_OK );
102 wxButton* cancelButton = new wxButton( bottomPanel, wxID_CANCEL );
103 sdbSizer->AddButton( okButton );
104 sdbSizer->AddButton( cancelButton );
105 sdbSizer->Realize();
106
107 bottomSizer->Add( sdbSizer, 1, wxEXPAND | wxALL, 5 );
108
109 bottomPanel->SetSizer( bottomSizer );
110 frameSizer->Add( bottomPanel, 0, wxEXPAND );
111
112 SetSizer( frameSizer );
113 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ), m_chooserPanel->GetItemCount() ) );
114 Layout();
115 m_chooserPanel->FinishSetup();
116
117 Bind( wxEVT_CHAR_HOOK, &PANEL_SYMBOL_CHOOSER::OnChar, m_chooserPanel );
118}
119
120
125
126
127bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
128{
129 if( aSymbol && !aSymbol->IsEmpty() )
130 {
131 LIB_ID libid;
132
133 libid.Parse( *aSymbol, true );
134
135 if( libid.IsValid() )
136 m_chooserPanel->SetPreselect( libid );
137 }
138
139 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
140}
141
142
144{
145 m_chooserPanel->ShutdownCanvases();
146
147 // Only dismiss a modal frame once, so that the return values set by
148 // the prior DismissModal() are not bashed for ShowModal().
149 if( !IsDismissed() )
150 DismissModal( false );
151
152 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
153}
154
155
156void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
157{
159 {
161 KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
162
163 m_firstPaintEvent = false;
164 }
165
166 aEvent.Skip();
167}
168
169
170void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
171{
172 LIB_ID libId = m_chooserPanel->GetSelectedLibId();
173
174 if( libId.IsValid() )
175 {
176 PICKED_SYMBOL symbol;
177 symbol.LibId = libId;
178
179 AddSymbolToHistory( symbol );
180 DismissModal( true, libId.Format() );
181 }
182 else
183 {
184 DismissModal( false );
185 }
186}
187
188
190{
191 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg ) )
192 return &cfg->m_LibViewPanel.window;
193
194 wxFAIL_MSG( wxT( "SYMBOL_CHOOSER not running with EESCHEMA_SETTINGS" ) );
195 return &aCfg->m_Window; // non-null fail-safe
196}
197
198
199void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
200{
201 Close( false );
202}
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:311
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:48
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
UTF8 Format() const
Definition lib_id.cpp:115
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:193
void ForceFocus(wxWindow *aWindow)
Pass the current focus to the window.
Definition wxgtk/ui.cpp:126
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