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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <eda_item.h>
25#include <confirm.h>
26#include <bitmaps.h>
28#include <embedded_files.h>
29#include <font/outline_font.h>
30#include <kidialog.h>
32#include <widgets/wx_grid.h>
33
34#include <wx/clipbrd.h>
35#include <wx/dirdlg.h>
36#include <wx/filedlg.h>
37#include <wx/filename.h>
38#include <wx/log.h>
39#include <wx/menu.h>
40#include <wx/wfstream.h>
41#include <wx/wupdlock.h>
42
43/* ---------- GRID_TRICKS for embedded files grid ---------- */
44
50
51
52void EMBEDDED_FILES_GRID_TRICKS::showPopupMenu( wxMenu& menu, wxGridEvent& aEvent )
53{
54 if( const int row = aEvent.GetRow(); row >= 0 && row < m_grid->GetNumberRows() )
55 {
56 m_curRow = row;
57 menu.Append( EMBEDDED_FILES_GRID_TRICKS_COPY_FILENAME, _( "Copy Embedded Reference" ),
58 _( "Copy the reference for this embedded file" ) );
59 menu.AppendSeparator();
60 GRID_TRICKS::showPopupMenu( menu, aEvent );
61 }
62 else
63 {
64 m_curRow = -1;
65 }
66}
67
68
70{
71 if( event.GetId() == EMBEDDED_FILES_GRID_TRICKS_COPY_FILENAME )
72 {
73 if( m_curRow >= 0 )
74 {
75 const wxString cellValue = m_grid->GetCellValue( m_curRow, 1 );
76
77 if( wxTheClipboard->Open() )
78 {
79 wxTheClipboard->SetData( new wxTextDataObject( cellValue ) );
80 wxTheClipboard->Close();
81 }
82 }
83 }
84 else
85 {
87 }
88}
89
90
91/* ---------- End of GRID_TRICKS for embedded files grid ---------- */
92
93
94PANEL_EMBEDDED_FILES::PANEL_EMBEDDED_FILES( wxWindow* aParent, EMBEDDED_FILES* aFiles, int aFlags ) :
96 m_files( aFiles ),
98{
99 m_files_grid->SetUseNativeColLabels();
100
101 for( auto& [name, file] : m_files->EmbeddedFileMap() )
102 {
103 EMBEDDED_FILES::EMBEDDED_FILE* newFile = new EMBEDDED_FILES::EMBEDDED_FILE( *file );
104 m_localFiles->AddFile( newFile );
105 }
106
107 if( aFlags & NO_MARGINS )
108 {
109 m_filesGridSizer->Detach( m_files_grid );
110 m_filesGridSizer->Add( m_files_grid, 5, wxEXPAND, 5 );
111
112 m_buttonsSizer->Detach( m_browse_button );
113 m_buttonsSizer->Prepend( m_browse_button, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
114
115 m_buttonsSizer->Detach( m_export );
116 m_buttonsSizer->Add( m_export, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5 );
117 }
118
119 // Set up the standard buttons
122 m_files_grid->SetMargins( 0 - wxSYS_VSCROLL_X, 0 );
123 m_files_grid->EnableAlternateRowColors();
124
125 m_files_grid->PushEventHandler( new EMBEDDED_FILES_GRID_TRICKS( m_files_grid ) );
126 m_files_grid->SetupColumnAutosizer( 1 );
127
128 m_localFiles->SetFileAddedCallback(
130 {
131 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
132 {
133 if( m_files_grid->GetCellValue( ii, 1 ) == file->GetLink() )
134 {
135 m_files_grid->DeleteRows( ii );
136 break;
137 }
138 }
139
140 m_files_grid->AppendRows( 1 );
141 int ii = m_files_grid->GetNumberRows() - 1;
142 m_files_grid->SetCellValue( ii, 0, file->name );
143 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
144
145 } );
146}
147
148
150{
151 // Remove the GRID_TRICKS handler
152 m_files_grid->PopEventHandler( true );
153}
154
155
157{
158 m_files_grid->ClearGrid();
159 m_files_grid->ClearRows();
160
161 int ii = 0;
162
163 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
164 {
165 while( m_files_grid->GetNumberRows() < ii + 1 )
166 m_files_grid->AppendRows( 1 );
167
168 m_files_grid->SetCellValue( ii, 0, name );
169 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
170
171 ii++;
172 }
173
174 m_cbEmbedFonts->SetValue( m_files->GetAreFontsEmbedded() );
175 return true;
176}
177
178
180{
181 std::optional<bool> deleteReferences;
182
183 auto confirmDelete =
184 [&]() -> bool
185 {
186 if( EDA_ITEM* parent = dynamic_cast<EDA_ITEM*>( m_files ) )
187 {
188 if( parent->Type() == PCB_T )
189 {
190 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some footprints.\n"
191 "Delete from footprints as well?" ) );
192 }
193 else if( parent->Type() == SCHEMATIC_T )
194 {
195 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some symbols.\n"
196 "Delete from symbols as well?" ) );
197 }
198 }
199
200 wxFAIL_MSG( wxT( "Unexpected embedded files owner" ) );
201 return false;
202 };
203
204 for( const auto& [name, file] : m_files->EmbeddedFileMap() )
205 {
206 if( !m_localFiles->HasFile( name ) )
207 {
208 m_files->RunOnNestedEmbeddedFiles(
209 [&]( EMBEDDED_FILES* nested_files )
210 {
211 if( nested_files->HasFile( name ) )
212 {
213 if( !deleteReferences.has_value() )
214 deleteReferences = confirmDelete();
215
216 if( deleteReferences.value() )
217 nested_files->RemoveFile( name, true );
218 }
219 } );
220 }
221
222 if( deleteReferences.has_value() && deleteReferences.value() == false )
223 break;
224 }
225
226 m_files->ClearEmbeddedFiles();
227
228 std::vector<EMBEDDED_FILES::EMBEDDED_FILE*> files;
229
230 for( const auto& [name, file] : m_localFiles->EmbeddedFileMap() )
231 files.push_back( file );
232
233 for( EMBEDDED_FILES::EMBEDDED_FILE* file : files )
234 {
235 m_files->AddFile( file );
236 m_localFiles->RemoveFile( file->name, false );
237 }
238
239 m_files->SetAreFontsEmbedded( m_cbEmbedFonts->IsChecked() );
240
241 return true;
242}
243
244
245void PANEL_EMBEDDED_FILES::onFontEmbedClick( wxCommandEvent& event )
246{
247 wxWindowUpdateLocker updateLock( this );
248
249 int row_pos = m_files_grid->GetGridCursorRow();
250 int col_pos = m_files_grid->GetGridCursorCol();
251 wxString row_name;
252
253 if( row_pos >= 0 )
254 row_name = m_files_grid->GetCellValue( row_pos, 0 );
255
256 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
257 {
258 wxString name = m_files_grid->GetCellValue( ii, 0 );
259
260 EMBEDDED_FILES::EMBEDDED_FILE* file = m_localFiles->GetEmbeddedFile( name );
261
263 {
264 m_files_grid->DeleteRows( ii );
265 ii--;
266 m_localFiles->RemoveFile( name );
267 }
268 }
269
270 if( m_cbEmbedFonts->IsChecked() )
271 {
272 std::set<KIFONT::OUTLINE_FONT*> fonts = m_files->GetFonts();
273
274 for( KIFONT::OUTLINE_FONT* font : fonts )
275 {
276 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( font->GetFileName(), true );
277
278 if( !result )
279 {
280 wxLogTrace( wxT( "KICAD_EMBED" ), wxString::Format( "Could not embed font %s",
281 font->GetFileName() ) );
282 continue;
283 }
284 }
285 }
286
287 if( row_pos >= 0 )
288 {
289 col_pos = std::max( std::min( col_pos, m_files_grid->GetNumberCols() - 1 ), 0 );
290 row_pos = std::max( std::min( row_pos, m_files_grid->GetNumberRows() - 1 ), 0 );
291 m_files_grid->SetGridCursor( row_pos, col_pos );
292
293 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ++ii )
294 {
295 if( m_files_grid->GetCellValue( ii, 0 ) == row_name )
296 {
297 m_files_grid->SetGridCursor( ii, col_pos );
298 break;
299 }
300 }
301 }
302}
303
304
306{
307 wxFileName fileName( aFile );
308 wxString name = fileName.GetFullName();
309
310 if( m_localFiles->HasFile( name ) )
311 {
312 wxString msg = wxString::Format( _( "File '%s' already exists." ), name );
313
314 KIDIALOG errorDlg( m_parent, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
315 errorDlg.SetOKLabel( _( "Overwrite" ) );
316
317 if( errorDlg.ShowModal() != wxID_OK )
318 return nullptr;
319
320 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
321 {
322 if( m_files_grid->GetCellValue( ii, 0 ) == name )
323 {
324 m_files_grid->DeleteRows( ii );
325 break;
326 }
327 }
328 }
329
330 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( fileName, true );
331
332 if( !result )
333 {
334 wxString msg = wxString::Format( _( "Failed to add file '%s'." ), name );
335
336 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
337 errorDlg.ShowModal();
338 return nullptr;
339 }
340
341 return result;
342}
343
344
345void PANEL_EMBEDDED_FILES::onAddEmbeddedFiles( wxCommandEvent& event )
346{
347 // TODO: Update strings to reflect that multiple files can be selected.
348 wxFileDialog fileDialog( this, _( "Select a file to embed" ), wxEmptyString, wxEmptyString,
349 _( "All Files" ) + wxT( " (*.*)|*.*" ),
350 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
351
352 if( fileDialog.ShowModal() == wxID_OK )
353 {
354 wxArrayString paths;
355 fileDialog.GetPaths( paths );
356
357 for( const wxString& path : paths )
359 }
360}
361
362
363bool PANEL_EMBEDDED_FILES::RemoveEmbeddedFile( const wxString& aFileName )
364{
365 wxString name = aFileName;
366
367 if( name.StartsWith( FILEEXT::KiCadUriPrefix ) )
368 name = name.Mid( FILEEXT::KiCadUriPrefix.size() + 3 );
369
370 int row = std::max( 0, m_files_grid->GetGridCursorRow() );
371
372 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
373 {
374 if( m_files_grid->GetCellValue( ii, 0 ) == name )
375 {
376 m_files_grid->DeleteRows( ii );
377 m_localFiles->RemoveFile( name );
378
379 if( row < m_files_grid->GetNumberRows() )
380 m_files_grid->SetGridCursor( row, 0 );
381 else if( m_files_grid->GetNumberRows() > 0 )
382 m_files_grid->SetGridCursor( m_files_grid->GetNumberRows() - 1, 0 );
383
384 return true;
385 }
386 }
387
388 return false;
389}
390
391
393{
394 m_files_grid->OnDeleteRows(
395 [&]( int row )
396 {
397 wxString name = m_files_grid->GetCellValue( row, 0 );
398
399 m_localFiles->RemoveFile( name );
400 m_files_grid->DeleteRows( row );
401 } );
402}
403
404
405void PANEL_EMBEDDED_FILES::onExportFiles( wxCommandEvent& event )
406{
407 wxDirDialog dirDialog( this, _( "Select a directory to export files" ) );
408
409 if( dirDialog.ShowModal() != wxID_OK )
410 return;
411
412 wxString path = dirDialog.GetPath();
413
414 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
415 {
416 wxFileName fileName( path, name );
417
418 if( fileName.FileExists() )
419 {
420 wxString msg = wxString::Format( _( "File '%s' already exists." ), fileName.GetFullName() );
421
422 KIDIALOG errorDlg( m_parent, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
423 errorDlg.SetOKCancelLabels( _( "Overwrite" ), _( "Skip" ) );
424 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
425
426 if( errorDlg.ShowModal() != wxID_OK )
427 continue;
428 }
429
430 bool skip_file = false;
431
432 while( 1 )
433 {
434 if( !fileName.IsDirWritable() )
435 {
436#ifndef __WXMAC__
437 wxString msg = wxString::Format( _( "Directory '%s' is not writable." ), fileName.GetFullName() );
438#else
439 wxString msg = wxString::Format( _( "Folder '%s' is not writable." ), fileName.GetPath() );
440#endif
441 // Don't set a 'do not show again' checkbox for this dialog
442 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxYES_NO | wxCANCEL | wxICON_ERROR );
443 errorDlg.SetYesNoCancelLabels( _( "Retry" ), _( "Skip" ), _( "Cancel" ) );
444
445 int result = errorDlg.ShowModal();
446
447 if( result == wxID_CANCEL )
448 {
449 return;
450 }
451 else if( result == wxID_NO )
452 {
453 skip_file = true;
454 break;
455 }
456 }
457 else
458 {
459 break;
460 }
461 }
462
463 if( skip_file )
464 continue;
465
466
467 wxFFileOutputStream out( fileName.GetFullPath() );
468
469 if( !out.IsOk() )
470 {
471 wxString msg = wxString::Format( _( "Failed to open file '%s'." ), fileName.GetFullName() );
472
473 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
474 errorDlg.ShowModal();
475 continue;
476 }
477
478 out.Write( file->decompressedData.data(), file->decompressedData.size() );
479
480 if( !out.IsOk() || ( out.LastWrite() != file->decompressedData.size() ) )
481 {
482 wxString msg = wxString::Format( _( "Failed to write file '%s'." ), fileName.GetFullName() );
483
484 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
485
486 errorDlg.ShowModal();
487 }
488 }
489}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
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.
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:42
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox.
Definition kidialog.cpp:55
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Definition kidialog.h:52
int ShowModal() override
Definition kidialog.cpp:93
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)
bool RemoveEmbeddedFile(const wxString &aFileName)
void onFontEmbedClick(wxCommandEvent &event) override
void onAddEmbeddedFiles(wxCommandEvent &event) override
PANEL_EMBEDDED_FILES(wxWindow *aParent, EMBEDDED_FILES *aFiles, int aFlags=0)
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:251
This file is part of the common library.
#define _(s)
static const std::string KiCadUriPrefix
#define NO_MARGINS
wxString result
Test unit parsing edge cases and error handling.
@ PCB_T
Definition typeinfo.h:82
@ SCHEMATIC_T
Definition typeinfo.h:208