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
96 m_files( aFiles ),
98{
99 for( auto& [name, file] : m_files->EmbeddedFileMap() )
100 {
101 EMBEDDED_FILES::EMBEDDED_FILE* newFile = new EMBEDDED_FILES::EMBEDDED_FILE( *file );
102 m_localFiles->AddFile( newFile );
103 }
104
105 // Set up the standard buttons
108 m_files_grid->SetMargins( 0 - wxSYS_VSCROLL_X, 0 );
109 m_files_grid->EnableAlternateRowColors();
110
111 m_files_grid->PushEventHandler( new EMBEDDED_FILES_GRID_TRICKS( m_files_grid ) );
112
113 m_localFiles->SetFileAddedCallback(
115 {
116 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
117 {
118 if( m_files_grid->GetCellValue( ii, 1 ) == file->GetLink() )
119 {
120 m_files_grid->DeleteRows( ii );
121 break;
122 }
123 }
124
125 m_files_grid->AppendRows( 1 );
126 int ii = m_files_grid->GetNumberRows() - 1;
127 m_files_grid->SetCellValue( ii, 0, file->name );
128 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
129
130 } );
131}
132
133
135{
136 // Remove the GRID_TRICKS handler
137 m_files_grid->PopEventHandler( true );
138}
139
140
141void PANEL_EMBEDDED_FILES::onSize( wxSizeEvent& event )
142{
143 resizeGrid();
144}
145
146
148{
149 int panel_width = GetClientRect().GetWidth();
150 int first_width = m_files_grid->GetColSize( 0 );
151 int second_width = m_files_grid->GetColSize( 1 );
152
153 double ratio;
154
155 if( first_width + second_width > 0 )
156 ratio = (double)first_width / (double)( first_width + second_width );
157 else
158 ratio = 0.3;
159
160
161 m_files_grid->SetColSize( 0, panel_width * ratio );
162 m_files_grid->SetColSize( 1, panel_width * ( 1 - ratio ) );
163 Layout();
164}
165
166
168{
169 m_files_grid->ClearGrid();
170 m_files_grid->ClearRows();
171
172 int ii = 0;
173
174 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
175 {
176 while( m_files_grid->GetNumberRows() < ii + 1 )
177 m_files_grid->AppendRows( 1 );
178
179 m_files_grid->SetCellValue( ii, 0, name );
180 m_files_grid->SetCellValue( ii, 1, file->GetLink() );
181
182 ii++;
183 }
184
185 m_cbEmbedFonts->SetValue( m_files->GetAreFontsEmbedded() );
186
187 resizeGrid();
188
189 return true;
190}
191
192
194{
195 std::optional<bool> deleteReferences;
196
197 auto confirmDelete =
198 [&]() -> bool
199 {
200 if( EDA_ITEM* parent = dynamic_cast<EDA_ITEM*>( m_files ) )
201 {
202 if( parent->Type() == PCB_T )
203 {
204 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some footprints.\n"
205 "Delete from footprints as well?" ) );
206 }
207 else if( parent->Type() == SCHEMATIC_T )
208 {
209 return IsOK( m_parent, _( "Deleted embedded files are also referenced in some symbols.\n"
210 "Delete from symbols as well?" ) );
211 }
212 }
213
214 wxFAIL_MSG( wxT( "Unexpected embedded files owner" ) );
215 return false;
216 };
217
218 for( const auto& [name, file] : m_files->EmbeddedFileMap() )
219 {
220 if( !m_localFiles->HasFile( name ) )
221 {
222 m_files->RunOnNestedEmbeddedFiles(
223 [&]( EMBEDDED_FILES* nested_files )
224 {
225 if( nested_files->HasFile( name ) )
226 {
227 if( !deleteReferences.has_value() )
228 deleteReferences = confirmDelete();
229
230 if( deleteReferences.value() )
231 nested_files->RemoveFile( name, true );
232 }
233 } );
234 }
235
236 if( deleteReferences.has_value() && deleteReferences.value() == false )
237 break;
238 }
239
240 m_files->ClearEmbeddedFiles();
241
242 std::vector<EMBEDDED_FILES::EMBEDDED_FILE*> files;
243
244 for( const auto& [name, file] : m_localFiles->EmbeddedFileMap() )
245 files.push_back( file );
246
247 for( EMBEDDED_FILES::EMBEDDED_FILE* file : files )
248 {
249 m_files->AddFile( file );
250 m_localFiles->RemoveFile( file->name, false );
251 }
252
253 m_files->SetAreFontsEmbedded( m_cbEmbedFonts->IsChecked() );
254
255 return true;
256}
257
258
259void PANEL_EMBEDDED_FILES::onFontEmbedClick( wxCommandEvent& event )
260{
261 wxWindowUpdateLocker updateLock( this );
262
263 int row_pos = m_files_grid->GetGridCursorRow();
264 int col_pos = m_files_grid->GetGridCursorCol();
265 wxString row_name;
266
267 if( row_pos >= 0 )
268 row_name = m_files_grid->GetCellValue( row_pos, 0 );
269
270 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
271 {
272 wxString name = m_files_grid->GetCellValue( ii, 0 );
273
274 EMBEDDED_FILES::EMBEDDED_FILE* file = m_localFiles->GetEmbeddedFile( name );
275
277 {
278 m_files_grid->DeleteRows( ii );
279 ii--;
280 m_localFiles->RemoveFile( name );
281 }
282 }
283
284 if( m_cbEmbedFonts->IsChecked() )
285 {
286 std::set<KIFONT::OUTLINE_FONT*> fonts = m_files->GetFonts();
287
288 for( KIFONT::OUTLINE_FONT* font : fonts )
289 {
290 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( font->GetFileName(), true );
291
292 if( !result )
293 {
294 wxLogTrace( wxT( "KICAD_EMBED" ), wxString::Format( "Could not embed font %s",
295 font->GetFileName() ) );
296 continue;
297 }
298 }
299 }
300
301 if( row_pos >= 0 )
302 {
303 col_pos = std::max( std::min( col_pos, m_files_grid->GetNumberCols() - 1 ), 0 );
304 row_pos = std::max( std::min( row_pos, m_files_grid->GetNumberRows() - 1 ), 0 );
305 m_files_grid->SetGridCursor( row_pos, col_pos );
306
307 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ++ii )
308 {
309 if( m_files_grid->GetCellValue( ii, 0 ) == row_name )
310 {
311 m_files_grid->SetGridCursor( ii, col_pos );
312 break;
313 }
314 }
315 }
316}
317
318
320{
321 wxFileName fileName( aFile );
322 wxString name = fileName.GetFullName();
323
324 if( m_localFiles->HasFile( name ) )
325 {
326 wxString msg = wxString::Format( _( "File '%s' already exists." ), name );
327
328 KIDIALOG errorDlg( m_parent, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
329 errorDlg.SetOKLabel( _( "Overwrite" ) );
330
331 if( errorDlg.ShowModal() != wxID_OK )
332 return nullptr;
333
334 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
335 {
336 if( m_files_grid->GetCellValue( ii, 0 ) == name )
337 {
338 m_files_grid->DeleteRows( ii );
339 break;
340 }
341 }
342 }
343
344 EMBEDDED_FILES::EMBEDDED_FILE* result = m_localFiles->AddFile( fileName, true );
345
346 if( !result )
347 {
348 wxString msg = wxString::Format( _( "Failed to add file '%s'." ), name );
349
350 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
351 errorDlg.ShowModal();
352 return nullptr;
353 }
354
355 return result;
356}
357
358
359void PANEL_EMBEDDED_FILES::onAddEmbeddedFiles( wxCommandEvent& event )
360{
361 // TODO: Update strings to reflect that multiple files can be selected.
362 wxFileDialog fileDialog( this, _( "Select a file to embed" ), wxEmptyString, wxEmptyString,
363 _( "All Files" ) + wxT( " (*.*)|*.*" ),
364 wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_MULTIPLE );
365
366 if( fileDialog.ShowModal() == wxID_OK )
367 {
368 wxArrayString paths;
369 fileDialog.GetPaths( paths );
370
371 for( const wxString& path : paths )
373 }
374}
375
376
377bool PANEL_EMBEDDED_FILES::RemoveEmbeddedFile( const wxString& aFileName )
378{
379 wxString name = aFileName;
380
381 if( name.StartsWith( FILEEXT::KiCadUriPrefix ) )
382 name = name.Mid( FILEEXT::KiCadUriPrefix.size() + 3 );
383
384 int row = std::max( 0, m_files_grid->GetGridCursorRow() );
385
386 for( int ii = 0; ii < m_files_grid->GetNumberRows(); ii++ )
387 {
388 if( m_files_grid->GetCellValue( ii, 0 ) == name )
389 {
390 m_files_grid->DeleteRows( ii );
391 m_localFiles->RemoveFile( name );
392
393 if( row < m_files_grid->GetNumberRows() )
394 m_files_grid->SetGridCursor( row, 0 );
395 else if( m_files_grid->GetNumberRows() > 0 )
396 m_files_grid->SetGridCursor( m_files_grid->GetNumberRows() - 1, 0 );
397
398 return true;
399 }
400 }
401
402 return false;
403}
404
405
407{
408 m_files_grid->OnDeleteRows(
409 [&]( int row )
410 {
411 wxString name = m_files_grid->GetCellValue( row, 0 );
412
413 m_localFiles->RemoveFile( name );
414 m_files_grid->DeleteRows( row );
415 } );
416}
417
418
419void PANEL_EMBEDDED_FILES::onExportFiles( wxCommandEvent& event )
420{
421 wxDirDialog dirDialog( this, _( "Select a directory to export files" ) );
422
423 if( dirDialog.ShowModal() != wxID_OK )
424 return;
425
426 wxString path = dirDialog.GetPath();
427
428 for( auto& [name, file] : m_localFiles->EmbeddedFileMap() )
429 {
430 wxFileName fileName( path, name );
431
432 if( fileName.FileExists() )
433 {
434 wxString msg = wxString::Format( _( "File '%s' already exists." ), fileName.GetFullName() );
435
436 KIDIALOG errorDlg( m_parent, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
437 errorDlg.SetOKCancelLabels( _( "Overwrite" ), _( "Skip" ) );
438 errorDlg.DoNotShowCheckbox( __FILE__, __LINE__ );
439
440 if( errorDlg.ShowModal() != wxID_OK )
441 continue;
442 }
443
444 bool skip_file = false;
445
446 while( 1 )
447 {
448 if( !fileName.IsDirWritable() )
449 {
450#ifndef __WXMAC__
451 wxString msg = wxString::Format( _( "Directory '%s' is not writable." ), fileName.GetFullName() );
452#else
453 wxString msg = wxString::Format( _( "Folder '%s' is not writable." ), fileName.GetPath() );
454#endif
455 // Don't set a 'do not show again' checkbox for this dialog
456 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxYES_NO | wxCANCEL | wxICON_ERROR );
457 errorDlg.SetYesNoCancelLabels( _( "Retry" ), _( "Skip" ), _( "Cancel" ) );
458
459 int result = errorDlg.ShowModal();
460
461 if( result == wxID_CANCEL )
462 {
463 return;
464 }
465 else if( result == wxID_NO )
466 {
467 skip_file = true;
468 break;
469 }
470 }
471 else
472 {
473 break;
474 }
475 }
476
477 if( skip_file )
478 continue;
479
480
481 wxFFileOutputStream out( fileName.GetFullPath() );
482
483 if( !out.IsOk() )
484 {
485 wxString msg = wxString::Format( _( "Failed to open file '%s'." ), fileName.GetFullName() );
486
487 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
488 errorDlg.ShowModal();
489 continue;
490 }
491
492 out.Write( file->decompressedData.data(), file->decompressedData.size() );
493
494 if( !out.IsOk() || ( out.LastWrite() != file->decompressedData.size() ) )
495 {
496 wxString msg = wxString::Format( _( "Failed to write file '%s'." ), fileName.GetFullName() );
497
498 KIDIALOG errorDlg( m_parent, msg, _( "Error" ), wxOK | wxICON_ERROR );
499
500 errorDlg.ShowModal();
501 }
502 }
503}
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)
PANEL_EMBEDDED_FILES(wxWindow *parent, EMBEDDED_FILES *aFiles)
void onFontEmbedClick(wxCommandEvent &event) override
void onAddEmbeddedFiles(wxCommandEvent &event) override
void onDeleteEmbeddedFile(wxCommandEvent &event) override
bool TransferDataToWindow() override
void onSize(wxSizeEvent &event) 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
wxString result
Test unit parsing edge cases and error handling.
@ PCB_T
Definition typeinfo.h:82
@ SCHEMATIC_T
Definition typeinfo.h:206