30#include <wx/clipbrd.h>
32#include <wx/filedlg.h>
33#include <wx/filename.h>
36#include <wx/wfstream.h>
37#include <wx/wupdlock.h>
51 if(
const int row = aEvent.GetRow(); row >= 0 && row < m_grid->GetNumberRows() )
55 _(
"Copy the reference for this embedded file" ) );
56 menu.AppendSeparator();
74 if( wxTheClipboard->Open() )
76 wxTheClipboard->SetData(
new wxTextDataObject( cellValue ) );
77 wxTheClipboard->Close();
92 std::vector<const EMBEDDED_FILES*> aInheritedFiles ) :
102 for(
auto& [
name, file] :
m_files->EmbeddedFileMap() )
107 for( auto& [name, file] : inheritedFiles->EmbeddedFileMap() )
109 if( m_localFiles->HasFile( name ) )
112 m_localFiles->AddFile( new EMBEDDED_FILES::EMBEDDED_FILE( *file ) );
113 m_inheritedFileNames.insert( name );
119 m_filesGridSizer->Detach( m_files_grid );
120 m_filesGridSizer->Add( m_files_grid, 5, wxEXPAND, 5 );
122 m_buttonsSizer->Detach( m_browse_button );
123 m_buttonsSizer->Prepend( m_browse_button, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
125 m_buttonsSizer->Detach( m_export );
126 m_buttonsSizer->Add( m_export, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
132 m_files_grid->SetMargins( 0 - wxSYS_VSCROLL_X, 0 );
133 m_files_grid->EnableAlternateRowColors();
136 m_files_grid->SetupColumnAutosizer( 1 );
138 m_localFiles->SetFileAddedCallback(
141 for(
int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
143 if( m_files_grid->GetCellValue( ii, 1 ) == file->
GetLink() )
145 m_files_grid->DeleteRows( ii );
150 m_files_grid->AppendRows( 1 );
151 int ii = m_files_grid->GetNumberRows() - 1;
152 m_files_grid->SetCellValue( ii, 0, file->
name );
153 m_files_grid->SetCellValue( ii, 1, file->
GetLink() );
192 std::optional<bool> deleteReferences;
199 if( parent->Type() ==
PCB_T )
201 return IsOK( m_parent,
_(
"Deleted embedded files are also referenced in some footprints.\n"
202 "Delete from footprints as well?" ) );
206 return IsOK( m_parent,
_(
"Deleted embedded files are also referenced in some symbols.\n"
207 "Delete from symbols as well?" ) );
211 wxFAIL_MSG( wxT(
"Unexpected embedded files owner" ) );
215 for(
const auto& [
name, file] :
m_files->EmbeddedFileMap() )
219 m_files->RunOnNestedEmbeddedFiles(
224 if( !deleteReferences.has_value() )
225 deleteReferences = confirmDelete();
227 if( deleteReferences.value() )
233 if( deleteReferences.has_value() && deleteReferences.value() ==
false )
239 std::vector<std::shared_ptr<EMBEDDED_FILES::EMBEDDED_FILE>> files;
242 files.push_back( file );
244 for( std::shared_ptr<EMBEDDED_FILES::EMBEDDED_FILE>& file : files )
250 m_files->AddFile( std::move( file ) );
262 wxWindowUpdateLocker updateLock(
this );
271 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
287 std::set<KIFONT::OUTLINE_FONT*>
fonts =
m_files->GetFonts();
295 wxLogTrace( wxT(
"KICAD_EMBED" ), wxString::Format(
"Could not embed font %s",
296 font->GetFileName() ) );
304 col_pos = std::max( std::min( col_pos,
m_files_grid->GetNumberCols() - 1 ), 0 );
305 row_pos = std::max( std::min( row_pos,
m_files_grid->GetNumberRows() - 1 ), 0 );
308 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ++ii )
322 wxFileName fileName( aFile );
323 wxString
name = fileName.GetFullName();
328 std::string newFileHash;
332 wxString msg = wxString::Format(
_(
"Failed to read file '%s'." ),
name );
333 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
338 if( existingFile && existingFile->
data_hash == newFileHash )
343 wxString msg = wxString::Format(
344 _(
"A file named '%s' is already embedded, but the file on disk has different "
345 "content.\n\nDo you want to replace the embedded file with the new version?" ),
348 KIDIALOG dlg( m_parent, msg,
_(
"Embedded File Conflict" ),
349 wxYES_NO | wxCANCEL | wxICON_WARNING );
350 dlg.SetYesNoLabels(
_(
"Replace" ),
_(
"Reuse Existing" ) );
354 if(
result == wxID_CANCEL )
362 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
376 wxString msg = wxString::Format(
_(
"Failed to add file '%s'." ),
name );
378 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
390 wxFileDialog fileDialog(
this,
_(
"Select a file to embed" ), wxEmptyString, wxEmptyString,
391 _(
"All Files" ) + wxT(
" (*.*)|*.*" ),
392 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
396 if( fileDialog.ShowModal() == wxID_OK )
399 fileDialog.GetPaths( paths );
401 for(
const wxString&
path : paths )
409 wxString
name = aFileName;
416 wxString msg =
_(
"Embedded files inherited from a parent symbol cannot be removed." );
422 int row = std::max( 0,
m_files_grid->GetGridCursorRow() );
424 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
431 if( row < m_files_grid->GetNumberRows() )
458 wxDirDialog dirDialog(
this,
_(
"Select a directory to export files" ) );
460 if( dirDialog.ShowModal() != wxID_OK )
463 wxString
path = dirDialog.GetPath();
469 if( fileName.FileExists() )
471 wxString msg = wxString::Format(
_(
"File '%s' already exists." ), fileName.GetFullName() );
473 KIDIALOG errorDlg( m_parent, msg,
_(
"Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
481 bool skip_file =
false;
485 if( !fileName.IsDirWritable() )
488 wxString msg = wxString::Format(
_(
"Directory '%s' is not writable." ), fileName.GetFullName() );
490 wxString msg = wxString::Format(
_(
"Folder '%s' is not writable." ), fileName.GetPath() );
493 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxYES_NO | wxCANCEL | wxICON_ERROR );
494 errorDlg.SetYesNoCancelLabels(
_(
"Retry" ),
_(
"Skip" ),
_(
"Cancel" ) );
498 if(
result == wxID_CANCEL )
502 else if(
result == wxID_NO )
518 wxFFileOutputStream out( fileName.GetFullPath() );
522 wxString msg = wxString::Format(
_(
"Failed to open file '%s'." ), fileName.GetFullName() );
524 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
533 wxString msg = wxString::Format(
_(
"Failed to write file '%s'." ), fileName.GetFullName() );
535 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
A base class for most all the KiCad significant classes used in schematics and boards.
@ EMBEDDED_FILES_GRID_TRICKS_COPY_FILENAME
EMBEDDED_FILES_GRID_TRICKS(WX_GRID *aGrid)
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.
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Class OUTLINE_FONT implements outline font drawing.
wxCheckBox * m_cbEmbedFonts
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
~PANEL_EMBEDDED_FILES() 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.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
static const std::string KiCadUriPrefix
std::vector< char > decompressedData
wxString result
Test unit parsing edge cases and error handling.