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 The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <pgm_base.h>
22#include <confirm.h>
23#include <gestfich.h>
25#include <widgets/wx_grid.h>
26#include <bitmaps.h>
27#include <project.h> // For PROJECT_VAR_NAME definition
28#include <footprint_library_adapter.h> // For KICAD7_FOOTPRINT_DIR definition
29
34
35#include <wx/filedlg.h>
36#include <wx/msgdlg.h>
37#include <kiplatform/ui.h>
38
39
42{
43 SetTitle( wxString::Format( _( "Project file: '%s'" ), Prj().GetProjectFullName() ) );
44
50
52
53 if( !project.m_EquivalenceFiles.empty() )
54 {
55 wxArrayString arr;
56
57 for( const auto& entry : project.m_EquivalenceFiles )
58 arr.Add( entry );
59
60 m_filesListBox->InsertItems( arr, 0 );
61 }
62
63 m_gridEnvVars->ClearRows();
64 m_gridEnvVars->AppendRows( 2 );
65 m_gridEnvVars->SetCellValue( 0, 0, PROJECT_VAR_NAME );
67
68 for( int row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
69 {
70 wxString evValue;
71
72 if( wxGetEnv( m_gridEnvVars->GetCellValue( row, 0 ), &evValue ) )
73 m_gridEnvVars->SetCellValue( row, 1, evValue );
74 }
75
76 m_gridEnvVars->AutoSizeColumns();
77
79
80 GetSizer()->SetSizeHints( this );
81 Center();
82}
83
84
85void DIALOG_CONFIG_EQUFILES::OnEditEquFile( wxCommandEvent& event )
86{
87 wxString editorname = Pgm().GetTextEditor();
88
89 if( editorname.IsEmpty() )
90 {
91 wxMessageBox( _( "No text editor selected in KiCad. Please choose one." ) );
92 return;
93 }
94
95 wxArrayInt selections;
96 m_filesListBox->GetSelections( selections );
97
98 for( unsigned ii = 0; ii < selections.GetCount(); ii++ )
99 ExecuteFile( editorname, wxExpandEnvVars( m_filesListBox->GetString( selections[ii] ) ) );
100}
101
102
103void DIALOG_CONFIG_EQUFILES::OnOkClick( wxCommandEvent& event )
104{
106
107 // Recreate equ list
108 project.m_EquivalenceFiles.clear();
109
110 for( unsigned ii = 0; ii < m_filesListBox->GetCount(); ii++ )
111 project.m_EquivalenceFiles.emplace_back( m_filesListBox->GetString( ii ) );
112
114
115 EndModal( wxID_OK );
116}
117
118
119void DIALOG_CONFIG_EQUFILES::OnCloseWindow( wxCloseEvent& event )
120{
121 EndModal( wxID_CANCEL );
122}
123
124
125void DIALOG_CONFIG_EQUFILES::OnButtonMoveUp( wxCommandEvent& event )
126{
127 wxArrayInt selections;
128 m_filesListBox->GetSelections( selections );
129
130 if ( selections.GetCount() <= 0 ) // No selection.
131 return;
132
133 if( selections[0] == 0 ) // The first lib is selected. cannot move up it
134 return;
135
136 wxArrayString libnames = m_filesListBox->GetStrings();
137
138 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
139 {
140 int jj = selections[ii];
141 std::swap( libnames[jj], libnames[jj-1] );
142 }
143
144 m_filesListBox->Set( libnames );
145
146 // Reselect previously selected names
147 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
148 {
149 int jj = selections[ii];
150 m_filesListBox->SetSelection( jj-1 );
151 }
152}
153
154
155void DIALOG_CONFIG_EQUFILES::OnButtonMoveDown( wxCommandEvent& event )
156{
157 wxArrayInt selections;
158 m_filesListBox->GetSelections( selections );
159
160 if ( selections.GetCount() <= 0 ) // No selection.
161 return;
162
163 // The last lib is selected. cannot move down it
164 if( selections.Last() == int( m_filesListBox->GetCount()-1 ) )
165 return;
166
167 wxArrayString libnames = m_filesListBox->GetStrings();
168
169 for( int ii = selections.GetCount()-1; ii >= 0; ii-- )
170 {
171 int jj = selections[ii];
172 std::swap( libnames[jj], libnames[jj+1] );
173 }
174
175 m_filesListBox->Set( libnames );
176
177 // Reselect previously selected names
178 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
179 {
180 int jj = selections[ii];
181 m_filesListBox->SetSelection( jj+1 );
182 }
183}
184
185
186/* Remove a library to the library list.
187 * The real list (g_LibName_List) is not changed, so the change can be canceled
188 */
189void DIALOG_CONFIG_EQUFILES::OnRemoveFiles( wxCommandEvent& event )
190{
191 wxArrayInt selections;
192 m_filesListBox->GetSelections( selections );
193
194 if( !selections.empty() )
195 {
196 std::sort( selections.begin(), selections.end() );
197
198 for( int ii = (int) selections.GetCount() - 1; ii >= 0; ii-- )
199 m_filesListBox->Delete( selections[ii] );
200 }
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
215
216 if( dlg.ShowModal() != wxID_OK )
217 return;
218
219 wxArrayString filenames;
220 dlg.GetPaths( filenames );
221
222 for( unsigned jj = 0; jj < filenames.GetCount(); jj++ )
223 {
224 wxFileName fn = filenames[jj];
225 wxString filepath;
226
227 for( row = 0; row < m_gridEnvVars->GetTable()->GetRowsCount(); row++ )
228 {
229 libpath = m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 1 ) );
230
231 if( fn.MakeRelativeTo( libpath ) )
232 {
233 filepath.Printf( wxT( "${%s}%c%s" ),
234 m_gridEnvVars->GetCellValue( wxGridCellCoords( row, 0 ) ),
235 wxFileName::GetPathSeparator(),
236 fn.GetFullPath() );
237 break;
238 }
239 }
240
241 if( filepath.IsEmpty() )
242 filepath = filenames[jj];
243
244 // Add or insert new library name, if not already in list
245 if( m_filesListBox->FindString( filepath, wxFileName::IsCaseSensitive() ) == wxNOT_FOUND )
246 {
247 filepath.Replace( wxT( "\\" ), wxT( "/" ) ); // Use unix separators only.
248 m_filesListBox->Append( filepath );
249 }
250 else
251 {
252 DisplayErrorMessage( this, wxString::Format( _( "File '%s' already exists in list." ),
253 filepath.GetData() ) );
254 }
255 }
256}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
DIALOG_CONFIG_EQUFILES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
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={})
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:218
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
The backing store for a PROJECT, in JSON format.
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:200
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
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:160
static wxString EquFileWildcard()
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:448
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition project.h:37
Definition of file extensions used in Kicad.