KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_config_equfiles.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <pgm_base.h>
26#include <confirm.h>
27#include <gestfich.h>
29#include <widgets/wx_grid.h>
30#include <bitmaps.h>
31#include <project.h> // For PROJECT_VAR_NAME definition
32#include <fp_lib_table.h> // For KICAD7_FOOTPRINT_DIR definition
33
38
39#include <wx/filedlg.h>
40#include <wx/msgdlg.h>
41
42
45{
46 SetTitle( wxString::Format( _( "Project file: '%s'" ), Prj().GetProjectFullName() ) );
47
48 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
49 m_bpEdit->SetBitmap( KiBitmapBundle( BITMAPS::small_edit ) );
50 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
51 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
52 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
53
55
56 if( !project.m_EquivalenceFiles.empty() )
57 {
58 wxArrayString arr;
59
60 for( const auto& entry : project.m_EquivalenceFiles )
61 arr.Add( entry );
62
63 m_filesListBox->InsertItems( arr, 0 );
64 }
65
67 m_gridEnvVars->AppendRows( 2 );
68 m_gridEnvVars->SetCellValue( 0, 0, PROJECT_VAR_NAME );
70
71 for( int row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
72 {
73 wxString evValue;
74
75 if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
76 m_gridEnvVars->SetCellValue( row, 1, evValue );
77 }
78
79 m_gridEnvVars->AutoSizeColumns();
80
82
83 GetSizer()->SetSizeHints( this );
84 Center();
85}
86
87
88void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
89{
90 wxString editorname = Pgm().GetTextEditor();
91
92 if( editorname.IsEmpty() )
93 {
94 wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
95 return;
96 }
97
98 wxArrayInt selections;
99 m_filesListBox->GetSelections( selections );
100
101 for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
102 ExecuteFile( editorname, wxExpandEnvVars( m_filesListBox->GetString( selections[ii] ) ) );
103}
104
105
106void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
107{
109
110 // Recreate equ list
111 project.m_EquivalenceFiles.clear();
112
113 for( unsigned ii = 0; ii < m_filesListBox->GetCount(); ii++ )
114 project.m_EquivalenceFiles.emplace_back( m_filesListBox->GetString( ii ) );
115
117
118 EndModal( wxID_OK );
119}
120
121
122void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
123{
124 EndModal( wxID_CANCEL );
125}
126
127
128void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
129{
130 wxArrayInt selections;
131 m_filesListBox->GetSelections( selections );
132
133 if ( selections.GetCount() <= 0 ) // No selection.
134 return;
135
136 if( selections[0] == 0 ) // The first lib is selected. cannot move up it
137 return;
138
139 wxArrayString libnames = m_filesListBox->GetStrings();
140
141 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
142 {
143 int jj = selections[ii];
144 std::swap( libnames[jj], libnames[jj-1] );
145 }
146
147 m_filesListBox->Set( libnames );
148
149 // Reselect previously selected names
150 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
151 {
152 int jj = selections[ii];
153 m_filesListBox->SetSelection( jj-1 );
154 }
155}
156
157
158void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
159{
160 wxArrayInt selections;
161 m_filesListBox->GetSelections( selections );
162
163 if ( selections.GetCount() <= 0 ) // No selection.
164 return;
165
166 // The last lib is selected. cannot move down it
167 if( selections.Last() == int( m_filesListBox->GetCount()-1 ) )
168 return;
169
170 wxArrayString libnames = m_filesListBox->GetStrings();
171
172 for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
173 {
174 int jj = selections[ii];
175 std::swap( libnames[jj], libnames[jj+1] );
176 }
177
178 m_filesListBox->Set( libnames );
179
180 // Reselect previously selected names
181 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
182 {
183 int jj = selections[ii];
184 m_filesListBox->SetSelection( jj+1 );
185 }
186}
187
188
189/* Remove a library to the library list.
190 * The real list (g_LibName_List) is not changed, so the change can be canceled
191 */
192void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
193{
194 wxArrayInt selections;
195 m_filesListBox->GetSelections( selections );
196
197 std::sort( selections.begin(), selections.end() );
198
199 for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
200 m_filesListBox->Delete( selections[ii] );
201}
202
203
204void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
205{
206 // Get a default path to open the file dialog:
207 wxArrayInt selectedRows = m_gridEnvVars->GetSelectedRows();
208 int row = selectedRows.GetCount() ? selectedRows[0] : m_gridEnvVars->GetGridCursorRow();
209 wxString libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
210
211 wxFileDialog dlg( this, _( "Footprint Association File" ), libpath, wxEmptyString,
212 FILEEXT::EquFileWildcard(), wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
213
214 if( dlg.ShowModal() != wxID_OK )
215 return;
216
217 wxArrayString filenames;
218 dlg.GetPaths( filenames );
219
220 for( unsigned jj = 0; jj < filenames.GetCount(); jj++ )
221 {
222 wxFileName fn = filenames[jj];
223 wxString filepath;
224
225 for( row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
226 {
227 libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
228
229 if( fn.MakeRelativeTo( libpath ) )
230 {
231 filepath.Printf( wxT( "${%s}%c%s" ),
232 m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ),
233 wxFileName::GetPathSeparator(),
234 fn.GetFullPath() );
235 break;
236 }
237 }
238
239 if( filepath.IsEmpty() )
240 filepath = filenames[jj];
241
242 // Add or insert new library name, if not already in list
243 if( m_filesListBox->FindString( filepath, wxFileName::IsCaseSensitive() ) == wxNOT_FOUND )
244 {
245 filepath.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
246 m_filesListBox->Append( filepath );
247 }
248 else
249 {
250 DisplayError( this, wxString::Format( _( "File '%s' already exists in list." ),
251 filepath.GetData() ) );
252 }
253 }
254}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Class DIALOG_CONFIG_EQUFILES_BASE.
DIALOG_CONFIG_EQUFILES(wxWindow *parent)
void OnCloseWindow(wxCloseEvent &event) override
void OnOkClick(wxCommandEvent &event) override
void OnButtonMoveDown(wxCommandEvent &event) override
void OnAddFiles(wxCommandEvent &event) override
void OnEditEquFile(wxCommandEvent &event) override
void OnRemoveFiles(wxCommandEvent &event) override
void OnButtonMoveUp(wxCommandEvent &event) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
static const wxString GlobalPathEnvVariableName()
Return the name of the environment variable used to hold the directory of locally installed "KiCad sp...
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition: pgm_base.cpp:195
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Saves a loaded project.
void SetBitmap(const wxBitmapBundle &aBmp)
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:165
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:161
This file is part of the common library.
#define _(s)
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:139
static wxString EquFileWildcard()
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1059
see class PGM_BASE
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:39
Definition of file extensions used in Kicad.