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 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
75
76 std::vector<PICKED_SYMBOL> dummyAlreadyPlaced;
78 new PANEL_SYMBOL_CHOOSER( this, this, nullptr /* no filter */, s_SymbolHistoryList,
79 dummyAlreadyPlaced, false, false,
80 [this]()
81 {
82 wxCommandEvent dummy;
83 OnOK( dummy );
84 } );
85
86 sizer->Add( m_chooserPanel, 1, wxEXPAND, 5 );
87
88 wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
89 wxButton* okButton = new wxButton( this, wxID_OK );
90 wxButton* cancelButton = new wxButton( this, wxID_CANCEL );
91 sdbSizer->AddButton( okButton );
92 sdbSizer->AddButton( cancelButton );
93 sdbSizer->Realize();
94
95 sizer->Add( sdbSizer, 0, wxEXPAND | wxALL, 5 );
96 SetSizer( sizer );
97
98 SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
100
101 Layout();
103}
104
105
106bool SYMBOL_CHOOSER_FRAME::ShowModal( wxString* aSymbol, wxWindow* aParent )
107{
108 if( aSymbol && !aSymbol->IsEmpty() )
109 {
110 LIB_ID libid;
111
112 libid.Parse( *aSymbol, true );
113
114 if( libid.IsValid() )
116 }
117
118 return KIWAY_PLAYER::ShowModal( aSymbol, aParent );
119}
120
121
123{
124 // Only dismiss a modal frame once, so that the return values set by
125 // the prior DismissModal() are not bashed for ShowModal().
126 if( !IsDismissed() )
127 DismissModal( false );
128
129 // window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
130}
131
132
133void SYMBOL_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
134{
136 {
139
140 m_firstPaintEvent = false;
141 }
142
143 aEvent.Skip();
144}
145
146
147void SYMBOL_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
148{
150
151 if( libId.IsValid() )
152 {
153 PICKED_SYMBOL symbol;
154 symbol.LibId = libId;
155
156 AddSymbolToHistory( symbol );
157 DismissModal( true, libId.Format() );
158 }
159 else
160 {
161 DismissModal( false );
162 }
163}
164
165
167{
168 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( aCfg );
169 wxASSERT( cfg );
170 return &cfg->m_LibViewPanel.window;
171}
172
173
174void SYMBOL_CHOOSER_FRAME::CloseSymbolChooser( wxCommandEvent& event )
175{
176 Close( false );
177}
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
Definition: app_settings.h:92
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:157
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