Eeschema, load a new bitmap image from file: fix crash when aborting.

Remove duplicate code in edit_bitmap.cpp.
This commit is contained in:
jean-pierre charras 2018-10-21 10:56:44 +02:00
parent dafeb96765
commit 8ecdf58bad
2 changed files with 19 additions and 79 deletions

View File

@ -35,82 +35,22 @@
#include <view/view_group.h> #include <view/view_group.h>
static void abortMoveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
{
SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
SCH_BITMAP* item = (SCH_BITMAP*) screen->GetCurItem();
SCH_EDIT_FRAME* parent = (SCH_EDIT_FRAME*) aPanel->GetParent();
parent->SetRepeatItem( NULL );
if( item == NULL ) /* no current item */
return;
if( item->IsNew() )
{
delete item;
item = NULL;
}
else // Move command on an existing text item, restore the data of the original.
{
item->ClearFlags();
SCH_BITMAP * olditem = (SCH_BITMAP*) parent->GetUndoItem();
wxCHECK_RET( olditem != NULL && item->Type() == olditem->Type() &&
item->Type() == SCH_BITMAP_T,
wxT( "Cannot restore undefined last text item." ) );
// Never delete existing item, because it can be referenced by an undo/redo command
// Just restore its data
item->SwapData( olditem );
parent->SetUndoItem( NULL );
}
screen->SetCurItem( item );
aPanel->Refresh();
}
static void moveBitmap( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition, bool aErase )
{
auto panel = static_cast<SCH_DRAW_PANEL*> ( aPanel );
SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
SCH_BITMAP* image = (SCH_BITMAP*) screen->GetCurItem();
auto preview = panel->GetView()->GetPreview();
if ( ! image )
return;
// Draw the bitmap at it's new position.
image->SetPosition( aPanel->GetParent()->GetCrossHairPosition() - image->GetStoredPos() );
auto view = panel->GetView();
view->ClearPreview();
view->AddToPreview( image, false );
view->SetVisible( preview, true );
view->Update( preview );
view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
}
SCH_BITMAP* SCH_EDIT_FRAME::CreateNewImage( wxDC* aDC ) SCH_BITMAP* SCH_EDIT_FRAME::CreateNewImage( wxDC* aDC )
{ {
wxFileDialog fileDlg( this, _( "Choose Image" ), wxEmptyString, wxEmptyString, wxFileDialog fileDlg( this, _( "Choose Image" ), wxEmptyString, wxEmptyString,
_( "Image Files " ) + wxImage::GetImageExtWildcard(), _( "Image Files " ) + wxImage::GetImageExtWildcard(),
wxFD_OPEN ); wxFD_OPEN );
int diag = fileDlg.ShowModal();
if( diag != wxID_OK ) if( fileDlg.ShowModal() != wxID_OK )
return NULL; return nullptr;
wxString fullFilename = fileDlg.GetPath(); wxString fullFilename = fileDlg.GetPath();
if( !wxFileExists( fullFilename ) ) if( !wxFileExists( fullFilename ) )
{ {
wxMessageBox( _( "Couldn't load image from \"%s\"" ), GetChars( fullFilename ) ); wxMessageBox( _( "Couldn't load image from \"%s\"" ), fullFilename );
return NULL; return nullptr;
} }
wxPoint pos = GetCrossHairPosition(); wxPoint pos = GetCrossHairPosition();
@ -119,23 +59,15 @@ SCH_BITMAP* SCH_EDIT_FRAME::CreateNewImage( wxDC* aDC )
if( !image->ReadImageFile( fullFilename ) ) if( !image->ReadImageFile( fullFilename ) )
{ {
wxMessageBox( _( "Couldn't load image from \"%s\"" ), GetChars( fullFilename ) ); wxMessageBox( _( "Couldn't load image from \"%s\"" ), fullFilename );
delete image; delete image;
return NULL; return nullptr;
} }
image->SetFlags( IS_NEW | IS_MOVED ); image->SetFlags( IS_NEW );
PrepareMoveItem( image );
auto view = static_cast<SCH_DRAW_PANEL*>( m_canvas )->GetView(); // OnModify();
view->ClearPreview();
view->AddToPreview( image, false );
m_canvas->SetMouseCapture( moveBitmap, abortMoveBitmap );
GetScreen()->SetCurItem( image );
OnModify();
return image; return image;
} }

View File

@ -680,7 +680,6 @@ static void moveItemWithMouseCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen(); SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
SCH_ITEM* item = screen->GetCurItem(); SCH_ITEM* item = screen->GetCurItem();
auto panel = static_cast<SCH_DRAW_PANEL*>( aPanel ); auto panel = static_cast<SCH_DRAW_PANEL*>( aPanel );
auto view = panel->GetView();
wxCHECK_RET( (item != NULL), wxT( "Cannot move invalid schematic item." ) ); wxCHECK_RET( (item != NULL), wxT( "Cannot move invalid schematic item." ) );
@ -689,14 +688,23 @@ static void moveItemWithMouseCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
item->SetPosition( cpos ); item->SetPosition( cpos );
// Draw the item item at it's new position. // Draw the item at it's new position.
item->SetWireImage(); // While moving, the item may choose to render differently item->SetWireImage(); // While moving, the item may choose to render differently
auto view = panel->GetView();
view->ClearPreview(); view->ClearPreview();
view->AddToPreview( item, false ); view->AddToPreview( item, false );
// Needed when moving a bitmap image to avoid ugly rendering and artifacts,
// because a bitmap is drawn only as non cached
if( item->Type() == SCH_BITMAP_T )
view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
} }
/**
* Callback function called when aborting a move item with mouse cursor command.
*/
static void abortMoveItem( EDA_DRAW_PANEL* aPanel, wxDC* aDC ) static void abortMoveItem( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
{ {
SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen(); SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();