From 417dc0a9dc898adda94fb2965a757387a2d04a07 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 9 Jul 2014 15:02:56 +0200 Subject: [PATCH] Drawing tools used to crash when the drawing tool was interrupted - fixed. --- pcbnew/tools/drawing_tool.cpp | 81 ++++++++++++++++++++++------------- pcbnew/tools/drawing_tool.h | 4 +- 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 54d9c8e691..061fb41008 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -83,13 +83,16 @@ int DRAWING_TOOL::DrawLine( TOOL_EVENT& aEvent ) MODULE* module = m_board->m_Modules; EDGE_MODULE* line = new EDGE_MODULE( module ); - while( drawSegment( S_SEGMENT, line ) ) + while( drawSegment( S_SEGMENT, reinterpret_cast( line ) ) ) { - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); - line->SetLocalCoord(); - line->SetParent( module ); - module->GraphicalItems().PushFront( line ); + if( line ) + { + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); + line->SetLocalCoord(); + line->SetParent( module ); + module->GraphicalItems().PushFront( line ); + } line = new EDGE_MODULE( module ); } @@ -102,9 +105,12 @@ int DRAWING_TOOL::DrawLine( TOOL_EVENT& aEvent ) while( drawSegment( S_SEGMENT, line ) ) { - m_board->Add( line ); - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( line, UR_NEW ); + if( line ) + { + m_board->Add( line ); + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( line, UR_NEW ); + } line = new DRAWSEGMENT; } @@ -126,13 +132,16 @@ int DRAWING_TOOL::DrawCircle( TOOL_EVENT& aEvent ) MODULE* module = m_board->m_Modules; EDGE_MODULE* circle = new EDGE_MODULE( module ); - while( drawSegment( S_CIRCLE, circle ) ) + while( drawSegment( S_CIRCLE, reinterpret_cast( circle ) ) ) { - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); - circle->SetLocalCoord(); - circle->SetParent( module ); - module->GraphicalItems().PushFront( circle ); + if( circle ) + { + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); + circle->SetLocalCoord(); + circle->SetParent( module ); + module->GraphicalItems().PushFront( circle ); + } circle = new EDGE_MODULE( module ); } @@ -145,9 +154,12 @@ int DRAWING_TOOL::DrawCircle( TOOL_EVENT& aEvent ) while( drawSegment( S_CIRCLE, circle ) ) { - m_board->Add( circle ); - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( circle, UR_NEW ); + if( circle ) + { + m_board->Add( circle ); + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( circle, UR_NEW ); + } circle = new DRAWSEGMENT; } @@ -169,13 +181,16 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) MODULE* module = m_board->m_Modules; EDGE_MODULE* arc = new EDGE_MODULE( module ); - while( drawArc( arc ) ) + while( drawArc( reinterpret_cast( arc ) ) ) { - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); - arc->SetLocalCoord(); - arc->SetParent( module ); - module->GraphicalItems().PushFront( arc ); + if( arc ) + { + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( module, UR_MODEDIT ); + arc->SetLocalCoord(); + arc->SetParent( module ); + module->GraphicalItems().PushFront( arc ); + } arc = new EDGE_MODULE( module ); } @@ -188,9 +203,12 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) while( drawArc( arc ) ) { - m_board->Add( arc ); - m_frame->OnModify(); - m_frame->SaveCopyInUndoList( arc, UR_NEW ); + if( arc ) + { + m_board->Add( arc ); + m_frame->OnModify(); + m_frame->SaveCopyInUndoList( arc, UR_NEW ); + } arc = new DRAWSEGMENT; } @@ -939,7 +957,7 @@ int DRAWING_TOOL::SetAnchor( TOOL_EVENT& aEvent ) } -bool DRAWING_TOOL::drawSegment( int aShape, DRAWSEGMENT* aGraphic ) +bool DRAWING_TOOL::drawSegment( int aShape, DRAWSEGMENT*& aGraphic ) { // Only two shapes are currently supported assert( aShape == S_SEGMENT || aShape == S_CIRCLE ); @@ -989,6 +1007,7 @@ bool DRAWING_TOOL::drawSegment( int aShape, DRAWSEGMENT* aGraphic ) preview.Clear(); updatePreview = true; delete aGraphic; + aGraphic = NULL; break; } @@ -1049,7 +1068,8 @@ bool DRAWING_TOOL::drawSegment( int aShape, DRAWSEGMENT* aGraphic ) else // User has clicked twice in the same spot { // a clear sign that the current drawing is finished delete aGraphic; // but only if at least one graphic was created - started = false; // otherwise - force user to draw more or cancel + aGraphic = NULL; // otherwise - force user to draw more or cancel + started = false; } preview.Clear(); @@ -1081,7 +1101,7 @@ bool DRAWING_TOOL::drawSegment( int aShape, DRAWSEGMENT* aGraphic ) } -bool DRAWING_TOOL::drawArc( DRAWSEGMENT* aGraphic ) +bool DRAWING_TOOL::drawArc( DRAWSEGMENT*& aGraphic ) { bool clockwise = true; // drawing direction of the arc double startAngle = 0.0f; // angle of the first arc line @@ -1121,6 +1141,7 @@ bool DRAWING_TOOL::drawArc( DRAWSEGMENT* aGraphic ) preview.Clear(); preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); delete aGraphic; + aGraphic = NULL; break; } diff --git a/pcbnew/tools/drawing_tool.h b/pcbnew/tools/drawing_tool.h index b1bc2675b3..c0735a8ec2 100644 --- a/pcbnew/tools/drawing_tool.h +++ b/pcbnew/tools/drawing_tool.h @@ -156,14 +156,14 @@ private: ///> be already created. The tool deletes the object if it is not added to a BOARD. ///> @return False if the tool was cancelled before the origin was set or origin and end are ///> the same point. - bool drawSegment( int aShape, DRAWSEGMENT* aGraphic ); + bool drawSegment( int aShape, DRAWSEGMENT*& aGraphic ); ///> Starts drawing an arc. ///> @param aGraphic is an object that is going to be used by the tool for drawing. It has to ///> be already created. The tool deletes the object if it is not added to a BOARD. ///> @return False if the tool was cancelled before the origin was set or origin and end are ///> the same point. - bool drawArc( DRAWSEGMENT* aGraphic ); + bool drawArc( DRAWSEGMENT*& aGraphic ); ///> Draws a polygon, that is added as a zone or a keepout area. ///> @param aKeepout decides if the drawn polygon is a zone or a keepout area.