From cf5c00bcf223ad38eb38e6897e58af4227d6d2cc Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sun, 10 Aug 2025 21:29:35 +0100 Subject: [PATCH] Make more use of shared dialogs. --- common/dialogs/panel_image_editor.cpp | 20 +++++---- .../dialogs/dialog_symbol_fields_table.cpp | 12 ++--- kicad/import_proj.cpp | 13 ------ kicad/import_proj.h | 1 - kicad/kicad.cpp | 12 ++--- kicad/tools/kicad_manager_control.cpp | 44 ++++++------------- .../dialogs/dialog_gen_footprint_position.cpp | 9 ++-- pcbnew/dialogs/dialog_gendrill.cpp | 10 ++--- pcbnew/dialogs/dialog_outset_items.cpp | 11 ++--- pcbnew/dialogs/dialog_plot.cpp | 7 +-- pcbnew/tools/board_editor_control.cpp | 11 ++--- 11 files changed, 44 insertions(+), 106 deletions(-) diff --git a/common/dialogs/panel_image_editor.cpp b/common/dialogs/panel_image_editor.cpp index 83c64b8f22..885e6f76ff 100644 --- a/common/dialogs/panel_image_editor.cpp +++ b/common/dialogs/panel_image_editor.cpp @@ -68,6 +68,8 @@ void PANEL_IMAGE_EDITOR::OnGreyScaleConvert( wxCommandEvent& event ) */ bool PANEL_IMAGE_EDITOR::CheckValues() { + wxWindow* host = wxGetTopLevelParent( this ); + #define MIN_SIZE 15 // Min size in pixels after scaling (50 mils) #define MAX_SIZE 6000 // Max size in pixels after scaling (20 inches) double tmp; @@ -76,7 +78,7 @@ bool PANEL_IMAGE_EDITOR::CheckValues() // Test number correctness if( !msg.ToDouble( &tmp ) || tmp < 0.0 ) { - wxMessageBox( _( "Incorrect scale number" ) ); + DisplayErrorMessage( host, _( "Scale must be a positive number." ) ); return false; } @@ -86,10 +88,10 @@ bool PANEL_IMAGE_EDITOR::CheckValues() if( size_min < MIN_SIZE ) // if the size is too small, the image will be hard to locate { - wxMessageDialog( wxGetTopLevelParent( this ), - wxString::Format( _( "This scale results in an image which is too " - "small (%.2f mm or %.1f mil)." ), - 25.4 / 300 * size_min, 1000.0 / 300.0 * size_min ) ); + DisplayErrorMessage( host, wxString::Format( _( "This scale results in an image which is too small " + "(%.2f mm or %.1f mil)." ), + 25.4 / 300 * size_min, + 1000.0 / 300.0 * size_min ) ); return false; } @@ -98,10 +100,10 @@ bool PANEL_IMAGE_EDITOR::CheckValues() if( size_max > MAX_SIZE ) { // the actual size is 25.4/300 * size_max in mm - if( !IsOK( wxGetTopLevelParent( this ), - wxString::Format( _( "This scale results in an image which is very large " - "(%.1f mm or %.2f in). Are you sure?" ), - 25.4 / 300 * size_max, size_max / 300.0 ) ) ) + if( !IsOK( host, wxString::Format( _( "This scale results in an image which is very large " + "(%.1f mm or %.2f in). Are you sure?" ), + 25.4 / 300 * size_max, + size_max / 300.0 ) ) ) { return false; } diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 7c7cd3345b..225d8aa15d 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -1314,19 +1314,13 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnOutputFileBrowseClicked( wxCommandEvent& even wxFileName file = wxFileName( saveDlg.GetPath() ); wxString defaultPath = fn.GetPathWithSep(); - wxString msg; - msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ); - wxMessageDialog dialog( this, msg, _( "BOM Output File" ), - wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT ); - - if( dialog.ShowModal() == wxID_YES ) + if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) ) { if( !file.MakeRelativeTo( defaultPath ) ) { - wxMessageBox( _( "Cannot make path relative (target volume different from schematic " - "file volume)!" ), - _( "BOM Output File" ), wxOK | wxICON_ERROR ); + DisplayErrorMessage( this, _( "Cannot make path relative (target volume different from schematic " + "file volume)!" ) ); } } diff --git a/kicad/import_proj.cpp b/kicad/import_proj.cpp index 45d07ce5b6..3e20c2db5b 100644 --- a/kicad/import_proj.cpp +++ b/kicad/import_proj.cpp @@ -77,19 +77,6 @@ void IMPORT_PROJ_HELPER::FindEmptyTargetDir() } -void IMPORT_PROJ_HELPER::OutputCopyError( const wxFileName& aSrc, const wxFileName& aFileCopy ) -{ - wxString msg; - msg.Printf( _( "Cannot copy file '%s'\n" - "to '%s'\n" - "The project cannot be imported." ), - aSrc.GetFullPath(), aFileCopy.GetFullPath() ); - - wxMessageDialog fileCopyErrorDlg( m_frame, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR ); - fileCopyErrorDlg.ShowModal(); -} - - class SCOPED_FILE_REMOVER { wxString m_file; diff --git a/kicad/import_proj.h b/kicad/import_proj.h index cf626e609f..0adad61009 100644 --- a/kicad/import_proj.h +++ b/kicad/import_proj.h @@ -68,7 +68,6 @@ private: std::vector m_schExtenstions; std::vector m_pcbExtenstions; - void OutputCopyError( const wxFileName& aSrc, const wxFileName& aFileCopy ); void ImportIndividualFile( KICAD_T aKicad_T, int aImportedFileType ); void doImport( const wxString& aFile, FRAME_T aFrameType, int aImportedFileType ); diff --git a/kicad/kicad.cpp b/kicad/kicad.cpp index b02f16d69a..c1b44198e4 100644 --- a/kicad/kicad.cpp +++ b/kicad/kicad.cpp @@ -324,15 +324,11 @@ bool PGM_KICAD::OnPgmInit() { wxFileName tmp = parser.GetParam( 0 ); - if( tmp.GetExt() != FILEEXT::ProjectFileExtension - && tmp.GetExt() != FILEEXT::LegacyProjectFileExtension ) + if( tmp.GetExt() != FILEEXT::ProjectFileExtension && tmp.GetExt() != FILEEXT::LegacyProjectFileExtension ) { - wxString msg; - - msg.Printf( _( "File '%s'\ndoes not appear to be a valid KiCad project file." ), - tmp.GetFullPath() ); - wxMessageDialog dlg( nullptr, msg, _( "Error" ), wxOK | wxICON_EXCLAMATION ); - dlg.ShowModal(); + DisplayErrorMessage( nullptr, wxString::Format( _( "File '%s'\n" + "does not appear to be a KiCad project file." ), + tmp.GetFullPath() ) ); } else { diff --git a/kicad/tools/kicad_manager_control.cpp b/kicad/tools/kicad_manager_control.cpp index 261035a422..36bc78b0fb 100644 --- a/kicad/tools/kicad_manager_control.cpp +++ b/kicad/tools/kicad_manager_control.cpp @@ -314,27 +314,18 @@ int KICAD_MANAGER_CONTROL::NewFromTemplate( const TOOL_EVENT& aEvent ) fn.AppendDir( fn.GetName() ); // Check if the project directory is empty if it already exists. - - if( !fn.DirExists() ) + if( !fn.DirExists() && !fn.Mkdir() ) { - if( !fn.Mkdir() ) - { - wxString msg; - msg.Printf( _( "Folder '%s' could not be created.\n\n" - "Make sure you have write permissions and try again." ), - fn.GetPath() ); - DisplayErrorMessage( m_frame, msg ); - return -1; - } + DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n" + "Make sure you have write permissions and try again." ), + fn.GetPath() ) ); + return -1; } if( !fn.IsDirWritable() ) { - wxString msg; - - msg.Printf( _( "Insufficient permissions to write to folder '%s'." ), fn.GetPath() ); - wxMessageDialog msgDlg( m_frame, msg, _( "Error" ), wxICON_ERROR | wxOK | wxCENTER ); - msgDlg.ShowModal(); + DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ), + fn.GetPath() ) ); return -1; } @@ -375,13 +366,7 @@ int KICAD_MANAGER_CONTROL::NewFromTemplate( const TOOL_EVENT& aEvent ) // create a project if( !ps.GetSelectedTemplate()->CreateProject( fn, &errorMsg ) ) { - wxMessageDialog createDlg( m_frame, _( "A problem occurred creating new project from template." ), - _( "Error" ), wxOK | wxICON_ERROR ); - - if( !errorMsg.empty() ) - createDlg.SetExtendedMessage( errorMsg ); - - createDlg.ShowModal(); + DisplayErrorMessage( m_frame, _( "A problem occurred creating new project from template." ), errorMsg ); return -1; } @@ -757,19 +742,16 @@ int KICAD_MANAGER_CONTROL::SaveProjectAs( const TOOL_EVENT& aEvent ) if( !wxMkdir( newProjectDir.GetFullPath() ) ) { - msg.Printf( _( "Folder '%s' could not be created.\n\n" - "Please make sure you have write permissions and try again." ), - newProjectDir.GetPath() ); - DisplayErrorMessage( m_frame, msg ); + DisplayErrorMessage( m_frame, wxString::Format( _( "Folder '%s' could not be created.\n\n" + "Please make sure you have sufficient permissions." ), + newProjectDir.GetPath() ) ); return -1; } if( !newProjectDir.IsDirWritable() ) { - msg.Printf( _( "Insufficient permissions to write to folder '%s'." ), - newProjectDir.GetFullPath() ); - wxMessageDialog msgDlg( m_frame, msg, _( "Error!" ), wxICON_ERROR | wxOK | wxCENTER ); - msgDlg.ShowModal(); + DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to write to folder '%s'." ), + newProjectDir.GetFullPath() ) ); return -1; } diff --git a/pcbnew/dialogs/dialog_gen_footprint_position.cpp b/pcbnew/dialogs/dialog_gen_footprint_position.cpp index 0377f21407..2a920643e7 100644 --- a/pcbnew/dialogs/dialog_gen_footprint_position.cpp +++ b/pcbnew/dialogs/dialog_gen_footprint_position.cpp @@ -187,17 +187,14 @@ void DIALOG_GEN_FOOTPRINT_POSITION::onOutputDirectoryBrowseClicked( wxCommandEve wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() ); - wxMessageDialog dialog( this, _( "Use a relative path?"), _( "Plot Output Directory" ), - wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT ); - - if( dialog.ShowModal() == wxID_YES ) + if( IsOK( this, _( "Use a relative path?" ) ) ) { wxString boardFilePath = ( (wxFileName) m_editFrame->GetBoard()->GetFileName() ).GetPath(); if( !dirName.MakeRelativeTo( boardFilePath ) ) { - wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ), - _( "Plot Output Directory" ), wxOK | wxICON_ERROR ); + DisplayErrorMessage( this, _( "Cannot make path relative (target volume different from board " + "file volume)!" ) ); } } diff --git a/pcbnew/dialogs/dialog_gendrill.cpp b/pcbnew/dialogs/dialog_gendrill.cpp index f201c668ad..164d8d5abe 100644 --- a/pcbnew/dialogs/dialog_gendrill.cpp +++ b/pcbnew/dialogs/dialog_gendrill.cpp @@ -273,17 +273,13 @@ void DIALOG_GENDRILL::onOutputDirectoryBrowseClicked( wxCommandEvent& event ) wxFileName dirName = wxFileName::DirName( dirDialog.GetPath() ); wxFileName fn( Prj().AbsolutePath( m_board->GetFileName() ) ); wxString defaultPath = fn.GetPathWithSep(); - wxString msg; - msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ); - wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ), wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT ); - - if( dialog.ShowModal() == wxID_YES ) + if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) ) { if( !dirName.MakeRelativeTo( defaultPath ) ) { - wxMessageBox( _( "Cannot make path relative (target volume different from board file volume)!" ), - _( "Plot Output Directory" ), wxOK | wxICON_ERROR ); + DisplayErrorMessage( this, _( "Cannot make path relative (target volume different from board " + "file volume)!" ) ); } } diff --git a/pcbnew/dialogs/dialog_outset_items.cpp b/pcbnew/dialogs/dialog_outset_items.cpp index 7308a85eca..f98a3f67c3 100644 --- a/pcbnew/dialogs/dialog_outset_items.cpp +++ b/pcbnew/dialogs/dialog_outset_items.cpp @@ -28,6 +28,7 @@ #include #include #include +#include /** * Some handy preset values for common outset distances. @@ -171,19 +172,13 @@ bool DIALOG_OUTSET_ITEMS::TransferDataFromWindow() { if( m_outset.GetIntValue() <= 0 ) { - wxString msg = _( "Outset must be a positive value." ); - - wxMessageDialog errdlg( this, msg, _( "Error" ) ); - errdlg.ShowModal(); + DisplayErrorMessage( this, _( "Outset must be a positive value." ) ); return false; } if( m_lineWidth.GetIntValue() <= 0 ) { - wxString msg = _( "Line width must be a positive value." ); - - wxMessageDialog errdlg( this, msg, _( "Error" ) ); - errdlg.ShowModal(); + DisplayErrorMessage( this, _( "Line width must be a positive value." ) ); return false; } diff --git a/pcbnew/dialogs/dialog_plot.cpp b/pcbnew/dialogs/dialog_plot.cpp index 3a3fdfcc5a..81ea1f5761 100644 --- a/pcbnew/dialogs/dialog_plot.cpp +++ b/pcbnew/dialogs/dialog_plot.cpp @@ -785,12 +785,7 @@ void DIALOG_PLOT::onOutputDirectoryBrowseClicked( wxCommandEvent& event ) // Test if making the path relative is possible before asking the user if they want to do it if( relPathTest.MakeRelativeTo( defaultPath ) ) { - msg.Printf( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ); - - wxMessageDialog dialog( this, msg, _( "Plot Output Directory" ), - wxYES_NO | wxICON_QUESTION | wxYES_DEFAULT ); - - if( dialog.ShowModal() == wxID_YES ) + if( IsOK( this, wxString::Format( _( "Do you want to use a path relative to\n'%s'?" ), defaultPath ) ) ) dirName.MakeRelativeTo( defaultPath ); } diff --git a/pcbnew/tools/board_editor_control.cpp b/pcbnew/tools/board_editor_control.cpp index ecdfa28e0b..9412099cc2 100644 --- a/pcbnew/tools/board_editor_control.cpp +++ b/pcbnew/tools/board_editor_control.cpp @@ -441,10 +441,8 @@ int BOARD_EDITOR_CONTROL::ExportNetlist( const TOOL_EVENT& aEvent ) if( !fn.IsDirWritable() ) { - wxString msg; - - msg.Printf( _( "Path `%s` is read only." ), fn.GetPath() ); - wxMessageDialog( m_frame, msg, _( "I/O Error" ), wxOK | wxCENTER | wxICON_EXCLAMATION ); + DisplayErrorMessage( m_frame, wxString::Format( _( "Insufficient permissions to folder '%s'." ), + fn.GetPath() ) ); return 0; } @@ -465,10 +463,7 @@ int BOARD_EDITOR_CONTROL::ExportNetlist( const TOOL_EVENT& aEvent ) const wxString& netname = pad->GetShortNetname(); if( !netname.IsEmpty() ) - { - component->AddNet( pad->GetNumber(), netname, pad->GetPinFunction(), - pad->GetPinType() ); - } + component->AddNet( pad->GetNumber(), netname, pad->GetPinFunction(), pad->GetPinType() ); } nlohmann::ordered_map fields;