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
41
44{
45 SetTitle( wxString::Format( _( "Project file: '%s'" ), Prj().GetProjectFullName() ) );
46
47 m_bpAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
48 m_bpEdit->SetBitmap( KiBitmapBundle( BITMAPS::small_edit ) );
49 m_bpMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
50 m_bpMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
51 m_bpDelete->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
52
54
55 if( !project.m_EquivalenceFiles.empty() )
56 {
57 wxArrayString arr;
58
59 for( const auto& entry : project.m_EquivalenceFiles )
60 arr.Add( entry );
61
62 m_filesListBox->InsertItems( arr, 0 );
63 }
64
66 m_gridEnvVars->AppendRows( 2 );
67 m_gridEnvVars->SetCellValue( 0, 0, PROJECT_VAR_NAME );
69
70 for( int row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
71 {
72 wxString evValue;
73
74 if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
75 m_gridEnvVars->SetCellValue( row, 1, evValue );
76 }
77
78 m_gridEnvVars->AutoSizeColumns();
79
81
82 GetSizer()->SetSizeHints( this );
83 Center();
84}
85
86
87void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
88{
89 wxString editorname = Pgm().GetTextEditor();
90
91 if( editorname.IsEmpty() )
92 {
93 wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
94 return;
95 }
96
97 wxArrayInt selections;
98 m_filesListBox->GetSelections( selections );
99
100 for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
101 ExecuteFile( editorname, wxExpandEnvVars( m_filesListBox->GetString( selections[ii] ) ) );
102}
103
104
105void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
106{
108
109 // Recreate equ list
110 project.m_EquivalenceFiles.clear();
111
112 for( unsigned ii = 0; ii < m_filesListBox->GetCount(); ii++ )
113 project.m_EquivalenceFiles.emplace_back( m_filesListBox->GetString( ii ) );
114
115 Pgm().GetSettingsManager().SaveProject();
116
117 EndModal( wxID_OK );
118}
119
120
121void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
122{
123 EndModal( wxID_CANCEL );
124}
125
126
127void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
128{
129 wxArrayInt selections;
130 m_filesListBox->GetSelections( selections );
131
132 if ( selections.GetCount() <= 0 ) // No selection.
133 return;
134
135 if( selections[0] == 0 ) // The first lib is selected. cannot move up it
136 return;
137
138 wxArrayString libnames = m_filesListBox->GetStrings();
139
140 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
141 {
142 int jj = selections[ii];
143 std::swap( libnames[jj], libnames[jj-1] );
144 }
145
146 m_filesListBox->Set( libnames );
147
148 // Reselect previously selected names
149 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
150 {
151 int jj = selections[ii];
152 m_filesListBox->SetSelection( jj-1 );
153 }
154}
155
156
157void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
158{
159 wxArrayInt selections;
160 m_filesListBox->GetSelections( selections );
161
162 if ( selections.GetCount() <= 0 ) // No selection.
163 return;
164
165 // The last lib is selected. cannot move down it
166 if( selections.Last() == int( m_filesListBox->GetCount()-1 ) )
167 return;
168
169 wxArrayString libnames = m_filesListBox->GetStrings();
170
171 for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
172 {
173 int jj = selections[ii];
174 std::swap( libnames[jj], libnames[jj+1] );
175 }
176
177 m_filesListBox->Set( libnames );
178
179 // Reselect previously selected names
180 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
181 {
182 int jj = selections[ii];
183 m_filesListBox->SetSelection( jj+1 );
184 }
185}
186
187
188/* Remove a library to the library list.
189 * The real list (g_LibName_List) is not changed, so the change can be canceled
190 */
191void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
192{
193 wxArrayInt selections;
194 m_filesListBox->GetSelections( selections );
195
196 std::sort( selections.begin(), selections.end() );
197
198 for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
199 m_filesListBox->Delete( selections[ii] );
200}
201
202
203void DIALOG_CONFIG_EQUFILES::OnAddFiles( wxCommandEvent& event )
204{
205 // Get a default path to open the file dialog:
206 wxArrayInt selectedRows = m_gridEnvVars->GetSelectedRows();
207 int row = selectedRows.GetCount() ? selectedRows[0] : m_gridEnvVars->GetGridCursorRow();
208 wxString libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
209
210 wxFileDialog dlg( this, _( "Footprint Association File" ), libpath, wxEmptyString,
211 FILEEXT::EquFileWildcard(), wxFD_DEFAULT_STYLE | wxFD_MULTIPLE );
212
213 if( dlg.ShowModal() != wxID_OK )
214 return;
215
216 wxArrayString filenames;
217 dlg.GetPaths( filenames );
218
219 for( unsigned jj = 0; jj < filenames.GetCount(); jj++ )
220 {
221 wxFileName fn = filenames[jj];
222 wxString filepath;
223
224 for( row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
225 {
226 libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
227
228 if( fn.MakeRelativeTo( libpath ) )
229 {
230 filepath.Printf( wxT( "${%s}%c%s" ),
231 m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ),
232 wxFileName::GetPathSeparator(),
233 fn.GetFullPath() );
234 break;
235 }
236 }
237
238 if( filepath.IsEmpty() )
239 filepath = filenames[jj];
240
241 // Add or insert new library name, if not already in list
242 if( m_filesListBox->FindString( filepath, wxFileName::IsCaseSensitive() ) == wxNOT_FOUND )
243 {
244 filepath.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
245 m_filesListBox->Append( filepath );
246 }
247 else
248 {
249 DisplayError( this, wxString::Format( _( "File '%s' already exists in list." ),
250 filepath.GetData() ) );
251 }
252 }
253}
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.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:70
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:166
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:147
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
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()
see class PGM_BASE
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:39
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
Definition of file extensions used in Kicad.