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 )
250 wxWindowUpdateLocker updateLock(
this );
259 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
275 std::set<KIFONT::OUTLINE_FONT*>
fonts =
m_files->GetFonts();
283 wxLogTrace( wxT(
"KICAD_EMBED" ), wxString::Format(
"Could not embed font %s",
284 font->GetFileName() ) );
292 col_pos = std::max( std::min( col_pos,
m_files_grid->GetNumberCols() - 1 ), 0 );
293 row_pos = std::max( std::min( row_pos,
m_files_grid->GetNumberRows() - 1 ), 0 );
296 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ++ii )
310 wxFileName fileName( aFile );
311 wxString
name = fileName.GetFullName();
316 std::string newFileHash;
320 wxString msg = wxString::Format(
_(
"Failed to read file '%s'." ),
name );
321 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
326 if( existingFile && existingFile->
data_hash == newFileHash )
331 wxString msg = wxString::Format(
332 _(
"A file named '%s' is already embedded, but the file on disk has different "
333 "content.\n\nDo you want to replace the embedded file with the new version?" ),
336 KIDIALOG dlg( m_parent, msg,
_(
"Embedded File Conflict" ),
337 wxYES_NO | wxCANCEL | wxICON_WARNING );
338 dlg.SetYesNoLabels(
_(
"Replace" ),
_(
"Reuse Existing" ) );
342 if(
result == wxID_CANCEL )
350 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
364 wxString msg = wxString::Format(
_(
"Failed to add file '%s'." ),
name );
366 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
378 wxFileDialog fileDialog(
this,
_(
"Select a file to embed" ), wxEmptyString, wxEmptyString,
379 _(
"All Files" ) + wxT(
" (*.*)|*.*" ),
380 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
384 if( fileDialog.ShowModal() == wxID_OK )
387 fileDialog.GetPaths( paths );
389 for(
const wxString&
path : paths )
397 wxString
name = aFileName;
404 wxString msg =
_(
"Embedded files inherited from a parent symbol cannot be removed." );
410 int row = std::max( 0,
m_files_grid->GetGridCursorRow() );
412 for(
int ii = 0; ii <
m_files_grid->GetNumberRows(); ii++ )
419 if( row < m_files_grid->GetNumberRows() )
446 wxDirDialog dirDialog(
this,
_(
"Select a directory to export files" ) );
448 if( dirDialog.ShowModal() != wxID_OK )
451 wxString
path = dirDialog.GetPath();
457 if( fileName.FileExists() )
459 wxString msg = wxString::Format(
_(
"File '%s' already exists." ), fileName.GetFullName() );
461 KIDIALOG errorDlg( m_parent, msg,
_(
"Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
469 bool skip_file =
false;
473 if( !fileName.IsDirWritable() )
476 wxString msg = wxString::Format(
_(
"Directory '%s' is not writable." ), fileName.GetFullName() );
478 wxString msg = wxString::Format(
_(
"Folder '%s' is not writable." ), fileName.GetPath() );
481 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxYES_NO | wxCANCEL | wxICON_ERROR );
482 errorDlg.SetYesNoCancelLabels(
_(
"Retry" ),
_(
"Skip" ),
_(
"Cancel" ) );
486 if(
result == wxID_CANCEL )
490 else if(
result == wxID_NO )
506 wxFFileOutputStream out( fileName.GetFullPath() );
510 wxString msg = wxString::Format(
_(
"Failed to open file '%s'." ), fileName.GetFullName() );
512 KIDIALOG errorDlg( m_parent, msg,
_(
"Error" ), wxOK | wxICON_ERROR );
521 wxString msg = wxString::Format(
_(
"Failed to write file '%s'." ), fileName.GetFullName() );
523 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.