From ba83e84b91be3870e9bd0f5e2389d4de67e09a4e Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Mon, 16 Oct 2023 18:24:53 -0400 Subject: [PATCH] Move SaveCanvasImageToFile to EDA_DRAW_FRAME rather than leaking things to bitmaps --- common/bitmap.cpp | 41 ++---------------------- common/eda_draw_frame.cpp | 34 ++++++++++++++++++++ eeschema/tools/symbol_editor_control.cpp | 2 +- include/bitmaps/bitmap_types.h | 12 ------- include/eda_draw_frame.h | 13 ++++++++ pcbnew/footprint_edit_frame.cpp | 2 +- 6 files changed, 51 insertions(+), 53 deletions(-) diff --git a/common/bitmap.cpp b/common/bitmap.cpp index 68a3627977..b547ae746a 100644 --- a/common/bitmap.cpp +++ b/common/bitmap.cpp @@ -42,11 +42,10 @@ #include #include // for pcb_calculator compatibility shim #include -#include -#include #include #include - +#include +#include static std::unique_ptr s_BitmapStore; @@ -213,42 +212,6 @@ wxBitmap* KiBitmapNew( BITMAPS aBitmap ) } -bool SaveCanvasImageToFile( EDA_DRAW_FRAME* aFrame, const wxString& aFileName, - BITMAP_TYPE aBitmapType ) -{ - wxCHECK( aFrame != nullptr, false ); - - bool retv = true; - - // Make a screen copy of the canvas: - wxSize image_size = aFrame->GetCanvas()->GetClientSize(); - - wxClientDC dc( aFrame->GetCanvas() ); - wxBitmap bitmap( image_size.x, image_size.y ); - wxMemoryDC memdc; - - memdc.SelectObject( bitmap ); - memdc.Blit( 0, 0, image_size.x, image_size.y, &dc, 0, 0 ); - memdc.SelectObject( wxNullBitmap ); - - wxImage image = bitmap.ConvertToImage(); - - wxBitmapType type = wxBITMAP_TYPE_PNG; - switch( aBitmapType ) - { - case BITMAP_TYPE::PNG: type = wxBITMAP_TYPE_PNG; break; - case BITMAP_TYPE::BMP: type = wxBITMAP_TYPE_BMP; break; - case BITMAP_TYPE::JPG: type = wxBITMAP_TYPE_JPEG; break; - } - - if( !image.SaveFile( aFileName, type ) ) - retv = false; - - image.Destroy(); - return retv; -} - - void AddBitmapToMenuItem( wxMenuItem* aMenu, const wxBitmap& aImage ) { // Retrieve the global application show icon option: diff --git a/common/eda_draw_frame.cpp b/common/eda_draw_frame.cpp index e61a024718..a89e1a2e86 100644 --- a/common/eda_draw_frame.cpp +++ b/common/eda_draw_frame.cpp @@ -1266,3 +1266,37 @@ void EDA_DRAW_FRAME::onActivate( wxActivateEvent& aEvent ) aEvent.Skip(); } + + +bool EDA_DRAW_FRAME::SaveCanvasImageToFile( const wxString& aFileName, + BITMAP_TYPE aBitmapType ) +{ + bool retv = true; + + // Make a screen copy of the canvas: + wxSize image_size = GetCanvas()->GetClientSize(); + + wxClientDC dc( GetCanvas() ); + wxBitmap bitmap( image_size.x, image_size.y ); + wxMemoryDC memdc; + + memdc.SelectObject( bitmap ); + memdc.Blit( 0, 0, image_size.x, image_size.y, &dc, 0, 0 ); + memdc.SelectObject( wxNullBitmap ); + + wxImage image = bitmap.ConvertToImage(); + + wxBitmapType type = wxBITMAP_TYPE_PNG; + switch( aBitmapType ) + { + case BITMAP_TYPE::PNG: type = wxBITMAP_TYPE_PNG; break; + case BITMAP_TYPE::BMP: type = wxBITMAP_TYPE_BMP; break; + case BITMAP_TYPE::JPG: type = wxBITMAP_TYPE_JPEG; break; + } + + if( !image.SaveFile( aFileName, type ) ) + retv = false; + + image.Destroy(); + return retv; +} \ No newline at end of file diff --git a/eeschema/tools/symbol_editor_control.cpp b/eeschema/tools/symbol_editor_control.cpp index a5da82f57f..135415a2df 100644 --- a/eeschema/tools/symbol_editor_control.cpp +++ b/eeschema/tools/symbol_editor_control.cpp @@ -584,7 +584,7 @@ int SYMBOL_EDITOR_CONTROL::ExportView( const TOOL_EVENT& aEvent ) // to refresh the screen before creating the PNG or JPEG image from screen wxYield(); - if( !SaveCanvasImageToFile( editFrame, dlg.GetPath(), BITMAP_TYPE::PNG ) ) + if( !editFrame->SaveCanvasImageToFile( dlg.GetPath(), BITMAP_TYPE::PNG ) ) { wxMessageBox( wxString::Format( _( "Can't save file '%s'." ), dlg.GetPath() ) ); } diff --git a/include/bitmaps/bitmap_types.h b/include/bitmaps/bitmap_types.h index 40211d22d3..c275c74eff 100644 --- a/include/bitmaps/bitmap_types.h +++ b/include/bitmaps/bitmap_types.h @@ -30,7 +30,6 @@ // test if it works under stable release // #include // only to define wxBitmap class wxBitmap; // only to define wxBitmap -class EDA_DRAW_FRAME; class wxWindow; class wxString; struct BITMAP_OPAQUE; @@ -102,15 +101,4 @@ int KiIconScale( wxWindow* aWindow ); */ wxBitmap* KiBitmapNew( BITMAPS aBitmap ); -/** - * Save the current view as an image file. - * - * @param aFrame The current draw frame view to save. - * @param aFileName The file name to save the image. This will overwrite an existing file. - * @param aBitmapType The type of bitmap create as defined by wxImage. - * @return True if the file was successfully saved or false if the file failed to be saved. - */ -bool SaveCanvasImageToFile( EDA_DRAW_FRAME* aFrame, const wxString& aFileName, - BITMAP_TYPE aBitmapType = BITMAP_TYPE::PNG ); - #endif // BITMAP_TYPES_H_ diff --git a/include/eda_draw_frame.h b/include/eda_draw_frame.h index d1ff5d42a0..37c68ea187 100644 --- a/include/eda_draw_frame.h +++ b/include/eda_draw_frame.h @@ -46,6 +46,7 @@ class wxFindReplaceData; class SEARCH_PANE; class HOTKEY_CYCLE_POPUP; class PROPERTIES_PANEL; +enum class BITMAP_TYPE; namespace KIGFX { @@ -470,6 +471,18 @@ public: virtual void CreateHotkeyPopup(); + + /** + * Save the current view as an image file. + * + * @param aFrame The current draw frame view to save. + * @param aFileName The file name to save the image. This will overwrite an existing file. + * @param aBitmapType The type of bitmap create as defined by wxImage. + * @return True if the file was successfully saved or false if the file failed to be saved. + */ + bool SaveCanvasImageToFile( const wxString& aFileName, BITMAP_TYPE aBitmapType ); + + DECLARE_EVENT_TABLE() protected: diff --git a/pcbnew/footprint_edit_frame.cpp b/pcbnew/footprint_edit_frame.cpp index 2b14b2ce0e..5be6c5a77b 100644 --- a/pcbnew/footprint_edit_frame.cpp +++ b/pcbnew/footprint_edit_frame.cpp @@ -1359,5 +1359,5 @@ void FOOTPRINT_EDIT_FRAME::OnSaveFootprintAsPng( wxCommandEvent& event ) // calling wxYield is mandatory under Linux, after closing the file selector dialog // to refresh the screen before creating the PNG or JPEG image from screen wxYield(); - SaveCanvasImageToFile( this, dlg.GetPath() ); + this->SaveCanvasImageToFile( dlg.GetPath(), BITMAP_TYPE::PNG ); }