Move SaveCanvasImageToFile to EDA_DRAW_FRAME rather than leaking things to bitmaps

This commit is contained in:
Marek Roszko 2023-10-16 18:24:53 -04:00
parent 9d4e0ba439
commit ba83e84b91
6 changed files with 51 additions and 53 deletions

View File

@ -42,11 +42,10 @@
#include <bitmap_store.h>
#include <bitmaps/bitmap_opaque.h> // for pcb_calculator compatibility shim
#include <pgm_base.h>
#include <eda_base_frame.h>
#include <eda_draw_frame.h>
#include <paths.h>
#include <kiplatform/ui.h>
#include <math/util.h>
#include <settings/common_settings.h>
static std::unique_ptr<BITMAP_STORE> 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:

View File

@ -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;
}

View File

@ -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() ) );
}

View File

@ -30,7 +30,6 @@
// test if it works under stable release
// #include <wx/bitmap.h> // 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_

View File

@ -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:

View File

@ -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 );
}