KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_embedded_files.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 3
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 <eda_item.h>
21#include <confirm.h>
22#include <bitmaps.h>
24#include <embedded_files.h>
25#include <font/outline_font.h>
26#include <kidialog.h>
28#include <widgets/wx_grid.h>
29
30#include <wx/clipbrd.h>
31#include <wx/dirdlg.h>
32#include <wx/filedlg.h>
33#include <wx/filename.h>
34#include <wx/log.h>
35#include <wx/menu.h>
36#include <wx/wfstream.h>
37#include <wx/wupdlock.h>
38#include <kiplatform/ui.h>
39
40/* ---------- GRID_TRICKS for embedded files grid ---------- */
41
47
48
49void EMBEDDED_FILES_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
50{
51 if( const int row = aEvent.GetRow(); row >= 0 && row < m_grid->GetNumberRows() )
52 {
53 m_curRow = row;
54 menu.Append( EMBEDDED_FILES_GRID_TRICKS_COPY_FILENAME, _( "Copy Embedded Reference" ),
55 _( "Copy the reference for this embedded file" ) );
56 menu.AppendSeparator();
57 GRID_TRICKS::showPopupMenu( menu, aEvent );
58 }
59 else
60 {
61 m_curRow = -1;
62 }
63}
64
65
67{
68 if( event.GetId() == EMBEDDED_FILES_GRID_TRICKS_COPY_FILENAME )
69 {
70 if( m_curRow >= 0 )
71 {
72 const wxString cellValue = m_grid->GetCellValue( m_curRow, 1 );
73
74 if( wxTheClipboard->Open() )
75 {
76 wxTheClipboard->SetData( new wxTextDataObject( cellValue ) );
77 wxTheClipboard->Close();
78 }
79 }
80 }
81 else
82 {
84 }
85}
86
87
88/* ---------- End of GRID_TRICKS for embedded files grid ---------- */
89
90
91PANEL_EMBEDDED_FILES::PANEL_EMBEDDED_FILES( wxWindow* aParent, EMBEDDED_FILES* aFiles, int aFlags,
92 std::vector<const EMBEDDED_FILES*> aInheritedFiles ) :
94 m_files( aFiles ),
96 m_inheritedFiles( std::move( aInheritedFiles ) )
97{
98 m_files_grid->SetUseNativeColLabels();
99
100 for( auto& [name, file] : m_files->EmbeddedFileMap() )
101 {
102 EMBEDDED_FILES::EMBEDDED_FILE* newFile = new EMBEDDED_FILES::EMBEDDED_FILE( *file );
103 m_localFiles->AddFile( newFile );
104 }
105
106 for( const EMBEDDED_FILES* inheritedFiles : m_inheritedFiles )
107 {
108 for( auto& [name, file] : inheritedFiles->EmbeddedFileMap() )
109 {
110 if( m_localFiles->HasFile( name ) )
111 continue;
112
113 EMBEDDED_FILES::EMBEDDED_FILE* newFile = new EMBEDDED_FILES::EMBEDDED_FILE( *file );
114 m_localFiles->AddFile( newFile );
115 m_inheritedFileNames.insert( name );
116 }
117 }
118
119 if( aFlags & NO_MARGINS )
120 {
121 m_filesGridSizer->Detach( m_files_grid );
122 m_filesGridSizer->Add( m_files_grid, 5, wxEXPAND, 5 );
123
124 m_buttonsSizer->Detach( m_browse_button );
125 m_buttonsSizer->Prepend( m_browse_button, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
126
127 m_buttonsSizer->Detach( m_export );
128 m_buttonsSizer->Add( m_export, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
129 }
130
131 // Set up the standard buttons
132 m_delete_button->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
133 m_browse_button->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
134 m_files_grid->SetMargins( 0 - wxSYS_VSCROLL_X, 0 );
135 m_files_grid->EnableAlternateRowColors();
136
137 m_files_grid->PushEventHandler( new EMBEDDED_FILES_GRID_TRICKS( m_files_grid ) );
138 m_files_grid->SetupColumnAutosizer( 1 );
139
140 m_localFiles->SetFileAddedCallback(
142 {
143 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
144 {
145 if( m_files_grid->GetCellValue( ii, 1 ) == file->GetLink() )
146 {
147 m_files_grid->DeleteRows( ii );
148 break;
149 }
150 }
151
152 m_files_grid->AppendRows( 1 );
153 int ii = m_files_grid->GetNumberRows() - 1;
154 m_files_grid->SetCellValue( ii, 0, file->name );
155 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
156
157 } );
158}
159
160
162{
163 // Remove the GRID_TRICKS handler
164 m_files_grid->PopEventHandler( true );
165 delete m_localFiles;
166}
167
168
170{
171 m_files_grid->ClearGrid();
172 m_files_grid->ClearRows();
173
174 int ii = 0;
175
176 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
177 {
178 while( m_files_grid->GetNumberRows() < ii + 1 )
179 m_files_grid->AppendRows( 1 );
180
181 m_files_grid->SetCellValue( ii, 0, name );
182 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
183
184 ii++;
185 }
186
187 m_cbEmbedFonts->SetValue( m_files->GetAreFontsEmbedded() );
188 return true;
189}
190
191
193{
194 std::optional<bool> deleteReferences;
195
196 auto confirmDelete =
197 [&]() -> bool
198 {
199 if( EDA_ITEM* parent = dynamic_cast<EDA_ITEM*>( m_files ) )
200 {
201 if( parent->Type() == PCB_T )
202 {
203 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some footprints.\n"
204 "Delete from footprints as well?" ) );
205 }
206 else if( parent->Type() == SCHEMATIC_T )
207 {
208 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some symbols.\n"
209 "Delete from symbols as well?" ) );
210 }
211 }
212
213 wxFAIL_MSG( wxT( "Unexpected embedded files owner" ) );
214 return false;
215 };
216
217 for( const auto& [name, file] : m_files->EmbeddedFileMap() )
218 {
219 if( !m_localFiles->HasFile( name ) )
220 {
221 m_files->RunOnNestedEmbeddedFiles(
222 [&]( EMBEDDED_FILES* nested_files )
223 {
224 if( nested_files->HasFile( name ) )
225 {
226 if( !deleteReferences.has_value() )
227 deleteReferences = confirmDelete();
228
229 if( deleteReferences.value() )
230 nested_files->RemoveFile( name, true );
231 }
232 } );
233 }
234
235 if( deleteReferences.has_value() && deleteReferences.value() == false )
236 break;
237 }
238
239 m_files->ClearEmbeddedFiles();
240
241 std::vector<EMBEDDED_FILES::EMBEDDED_FILE*> files;
242
243 for( const auto& [name, file] : m_localFiles->EmbeddedFileMap() )
244 files.push_back( file );
245
246 for( EMBEDDED_FILES::EMBEDDED_FILE* file : files )
247 {
248 if( m_inheritedFileNames.count( file->name ) )
249 continue;
250
251 m_files->AddFile( file );
252 m_localFiles->RemoveFile( file->name, false );
253 }
254
255 m_files->SetAreFontsEmbedded( m_cbEmbedFonts->IsChecked() );
256
257 return true;
258}
259
260
261void PANEL_EMBEDDED_FILES::onFontEmbedClick( wxCommandEvent& event )
262{
263 wxWindowUpdateLocker updateLock( this );
264
265 int row_pos = m_files_grid->GetGridCursorRow();
266 int col_pos = m_files_grid->GetGridCursorCol();
267 wxString row_name;
268
269 if( row_pos >= 0 )
270 row_name = m_files_grid->GetCellValue( row_pos, 0 );
271
272 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
273 {
274 wxString name = m_files_grid->GetCellValue( ii, 0 );
275
276 EMBEDDED_FILES::EMBEDDED_FILE* file = m_localFiles->GetEmbeddedFile( name );
277
279 {
280 m_files_grid->DeleteRows( ii );
281 ii--;
282 m_localFiles->RemoveFile( name );
283 }
284 }
285
286 if( m_cbEmbedFonts->IsChecked() )
287 {
288 std::set<KIFONT::OUTLINE_FONT*> fonts = m_files->GetFonts();
289
290 for( KIFONT::OUTLINE_FONT* font : fonts )
291 {
292 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( font->GetFileName(), true );
293
294 if( !result )
295 {
296 wxLogTrace( wxT( "KICAD_EMBED" ), wxString::Format( "Could not embed font %s",
297 font->GetFileName() ) );
298 continue;
299 }
300 }
301 }
302
303 if( row_pos >= 0 )
304 {
305 col_pos = std::max( std::min( col_pos, m_files_grid->GetNumberCols() - 1 ), 0 );
306 row_pos = std::max( std::min( row_pos, m_files_grid->GetNumberRows() - 1 ), 0 );
307 m_files_grid->SetGridCursor( row_pos, col_pos );
308
309 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ++ii )
310 {
311 if( m_files_grid->GetCellValue( ii, 0 ) == row_name )
312 {
313 m_files_grid->SetGridCursor( ii, col_pos );
314 break;
315 }
316 }
317 }
318}
319
320
322{
323 wxFileName fileName( aFile );
324 wxString name = fileName.GetFullName();
325
326 if( m_localFiles->HasFile( name ) )
327 {
328 EMBEDDED_FILES::EMBEDDED_FILE* existingFile = m_localFiles->GetEmbeddedFile( name );
329 std::string newFileHash;
330
332 {
333 wxString msg = wxString::Format( _( "Failed to read file '%s'." ), name );
334 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
335 errorDlg.ShowModal();
336 return nullptr;
337 }
338
339 if( existingFile && existingFile->data_hash == newFileHash )
340 {
341 return existingFile;
342 }
343
344 wxString msg = wxString::Format(
345 _( "A file named '%s' is already embedded, but the file on disk has different "
346 "content.\n\nDo you want to replace the embedded file with the new version?" ),
347 name );
348
349 KIDIALOG dlg( m_parent, msg, _( "Embedded File Conflict" ),
350 wxYES_NO | wxCANCEL | wxICON_WARNING );
351 dlg.SetYesNoLabels( _( "Replace" ), _( "Reuse Existing" ) );
352
353 int result = dlg.ShowModal();
354
355 if( result == wxID_CANCEL )
356 return nullptr;
357
358 if( result == wxID_NO )
359 {
360 return existingFile;
361 }
362
363 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
364 {
365 if( m_files_grid->GetCellValue( ii, 0 ) == name )
366 {
367 m_files_grid->DeleteRows( ii );
368 break;
369 }
370 }
371 }
372
373 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( fileName, true );
374
375 if( !result )
376 {
377 wxString msg = wxString::Format( _( "Failed to add file '%s'." ), name );
378
379 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
380 errorDlg.ShowModal();
381 return nullptr;
382 }
383
384 return result;
385}
386
387
388void PANEL_EMBEDDED_FILES::onAddEmbeddedFiles( wxCommandEvent& event )
389{
390 // TODO: Update strings to reflect that multiple files can be selected.
391 wxFileDialog fileDialog( this, _( "Select a file to embed" ), wxEmptyString, wxEmptyString,
392 _( "All Files" ) + wxT( " (*.*)|*.*" ),
393 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
394
396
397 if( fileDialog.ShowModal() == wxID_OK )
398 {
399 wxArrayString paths;
400 fileDialog.GetPaths( paths );
401
402 for( const wxString& path : paths )
404 }
405}
406
407
408bool PANEL_EMBEDDED_FILES::RemoveEmbeddedFile( const wxString& aFileName )
409{
410 wxString name = aFileName;
411
412 if( name.StartsWith( FILEEXT::KiCadUriPrefix ) )
413 name = name.Mid( FILEEXT::KiCadUriPrefix.size() + 3 );
414
415 if( m_inheritedFileNames.count( name ) )
416 {
417 wxString msg = _( "Embedded files inherited from a parent symbol cannot be removed." );
418
419 DisplayErrorMessage( this, msg );
420 return false;
421 }
422
423 int row = std::max( 0, m_files_grid->GetGridCursorRow() );
424
425 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
426 {
427 if( m_files_grid->GetCellValue( ii, 0 ) == name )
428 {
429 m_files_grid->DeleteRows( ii );
430 m_localFiles->RemoveFile( name );
431
432 if( row < m_files_grid->GetNumberRows() )
433 m_files_grid->SetGridCursor( row, 0 );
434 else if( m_files_grid->GetNumberRows() > 0 )
435 m_files_grid->SetGridCursor( m_files_grid->GetNumberRows() - 1, 0 );
436
437 return true;
438 }
439 }
440
441 return false;
442}
443
444
446{
447 m_files_grid->OnDeleteRows(
448 [&]( int row )
449 {
450 wxString name = m_files_grid->GetCellValue( row, 0 );
451
453 } );
454}
455
456
457void PANEL_EMBEDDED_FILES::onExportFiles( wxCommandEvent& event )
458{
459 wxDirDialog dirDialog( this, _( "Select a directory to export files" ) );
460
461 if( dirDialog.ShowModal() != wxID_OK )
462 return;
463
464 wxString path = dirDialog.GetPath();
465
466 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
467 {
468 wxFileName fileName( path, name );
469
470 if( fileName.FileExists() )
471 {
472 wxString msg = wxString::Format( _( "File '%s' already exists." ), fileName.GetFullName() );
473
474 KIDIALOG errorDlg( m_parent, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
475 errorDlg.SetOKCancelLabels( _( "Overwrite" ), _( "Skip" ) );
476 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
477
478 if( errorDlg.ShowModal() != wxID_OK )
479 continue;
480 }
481
482 bool skip_file = false;
483
484 while( 1 )
485 {
486 if( !fileName.IsDirWritable() )
487 {
488#ifndef __WXMAC__
489 wxString msg = wxString::Format( _( "Directory '%s' is not writable." ), fileName.GetFullName() );
490#else
491 wxString msg = wxString::Format( _( "Folder '%s' is not writable." ), fileName.GetPath() );
492#endif
493 // Don't set a 'do not show again' checkbox for this dialog
494 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxYES_NO | wxCANCEL | wxICON_ERROR );
495 errorDlg.SetYesNoCancelLabels( _( "Retry" ), _( "Skip" ), _( "Cancel" ) );
496
497 int result = errorDlg.ShowModal();
498
499 if( result == wxID_CANCEL )
500 {
501 return;
502 }
503 else if( result == wxID_NO )
504 {
505 skip_file = true;
506 break;
507 }
508 }
509 else
510 {
511 break;
512 }
513 }
514
515 if( skip_file )
516 continue;
517
518
519 wxFFileOutputStream out( fileName.GetFullPath() );
520
521 if( !out.IsOk() )
522 {
523 wxString msg = wxString::Format( _( "Failed to open file '%s'." ), fileName.GetFullName() );
524
525 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
526 errorDlg.ShowModal();
527 continue;
528 }
529
530 out.Write( file->decompressedData.data(), file->decompressedData.size() );
531
532 if( !out.IsOk() || ( out.LastWrite() != file->decompressedData.size() ) )
533 {
534 wxString msg = wxString::Format( _( "Failed to write file '%s'." ), fileName.GetFullName() );
535
536 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
537
538 errorDlg.ShowModal();
539 }
540 }
541}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
void doPopupSelection(wxCommandEvent &event) override
void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent) override
void RemoveFile(const wxString &name, bool aErase=true)
Remove a file from the collection and frees the memory.
static RETURN_CODE ComputeFileHash(const wxFileName &aFileName, std::string &aHash)
Compute the hash of a file on disk without fully embedding it.
bool HasFile(const wxString &name) const
GRID_TRICKS(WX_GRID *aGrid)
virtual void doPopupSelection(wxCommandEvent &event)
virtual void showPopupMenu(wxMenu &menu, wxGridEvent &aEvent)
WX_GRID * m_grid
I don't own the grid, but he owns me.
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:38
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:51
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Definition kidialog.h:48
int ShowModal() override
Definition kidialog.cpp:89
Class OUTLINE_FONT implements outline font drawing.
PANEL_EMBEDDED_FILES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
PANEL_EMBEDDED_FILES(wxWindow *aParent, EMBEDDED_FILES *aFiles, int aFlags=0, std::vector< const EMBEDDED_FILES * > aInheritedFiles={})
bool RemoveEmbeddedFile(const wxString &aFileName)
std::set< wxString > m_inheritedFileNames
void onFontEmbedClick(wxCommandEvent &event) override
void onAddEmbeddedFiles(wxCommandEvent &event) override
std::vector< const EMBEDDED_FILES * > m_inheritedFiles
void onDeleteEmbeddedFile(wxCommandEvent &event) override
bool TransferDataToWindow() override
EMBEDDED_FILES * m_localFiles
void onExportFiles(wxCommandEvent &event) override
EMBEDDED_FILES::EMBEDDED_FILE * AddEmbeddedFile(const wxString &aFileName)
bool TransferDataFromWindow() override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
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)
static const std::string KiCadUriPrefix
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:448
STL namespace.
#define NO_MARGINS
std::vector< char > decompressedData
std::string path
wxString result
Test unit parsing edge cases and error handling.
@ PCB_T
Definition typeinfo.h:75
@ SCHEMATIC_T
Definition typeinfo.h:201