New items are created using pointers (instead of copying automatic variables when drawing is finished).

This commit is contained in:
Maciej Suminski 2014-02-14 15:13:42 +01:00
parent b1fb59ad13
commit 9d3f7230ee
2 changed files with 117 additions and 48 deletions

View File

@ -40,7 +40,6 @@
#include <class_mire.h>
#include <class_zone.h>
#include <class_module.h>
#include <class_netinfo.h> // TODO to be removed if we do not need the flags
DRAWING_TOOL::DRAWING_TOOL() :
TOOL_INTERACTIVE( "pcbnew.InteractiveDrawing" )
@ -79,16 +78,16 @@ int DRAWING_TOOL::DrawCircle( TOOL_EVENT& aEvent )
int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
{
bool clockwise = true;
bool clockwise = true; // drawing direction of the arc
double startAngle; // angle of the first arc line
VECTOR2I cursorPos = m_controls->GetCursorPosition();
// Init the new item attributes
DRAWSEGMENT graphic;
graphic.SetShape( S_ARC );
graphic.SetAngle( 0.0 );
graphic.SetWidth( m_board->GetDesignSettings().m_DrawSegmentWidth );
graphic.SetCenter( wxPoint( cursorPos.x, cursorPos.y ) );
DRAWSEGMENT* graphic = new DRAWSEGMENT( m_board );
graphic->SetShape( S_ARC );
graphic->SetAngle( 0.0 );
graphic->SetWidth( m_board->GetDesignSettings().m_DrawSegmentWidth );
graphic->SetCenter( wxPoint( cursorPos.x, cursorPos.y ) );
DRAWSEGMENT helperLine;
helperLine.SetShape( S_SEGMENT );
@ -120,23 +119,26 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
cursorPos = m_controls->GetCursorPosition();
if( evt->IsCancel() )
{
delete graphic;
break;
}
else if( evt->IsKeyUp() )
{
int width = graphic.GetWidth();
int width = graphic->GetWidth();
// Modify the new item width
if( evt->KeyCode() == '-' && width > WIDTH_STEP )
graphic.SetWidth( width - WIDTH_STEP );
graphic->SetWidth( width - WIDTH_STEP );
else if( evt->KeyCode() == '=' )
graphic.SetWidth( width + WIDTH_STEP );
graphic->SetWidth( width + WIDTH_STEP );
else if( evt->KeyCode() == '/' )
{
if( clockwise )
graphic.SetAngle( graphic.GetAngle() - 3600.0 );
graphic->SetAngle( graphic->GetAngle() - 3600.0 );
else
graphic.SetAngle( graphic.GetAngle() + 3600.0 );
graphic->SetAngle( graphic->GetAngle() + 3600.0 );
clockwise = !clockwise;
}
@ -159,9 +161,9 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
}
else
{
helperLine.SetStart( graphic.GetCenter() );
graphic.SetLayer( layer );
preview.Add( &graphic );
helperLine.SetStart( graphic->GetCenter() );
graphic->SetLayer( layer );
preview.Add( graphic );
preview.Add( &helperLine );
}
}
@ -169,19 +171,23 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
case SET_END:
{
VECTOR2D startLine( graphic.GetArcStart() - graphic.GetCenter() );
VECTOR2D startLine( graphic->GetArcStart() - graphic->GetCenter() );
startAngle = startLine.Angle();
}
break;
case SET_ANGLE:
{
if( wxPoint( cursorPos.x, cursorPos.y ) != graphic.GetCenter() )
if( wxPoint( cursorPos.x, cursorPos.y ) != graphic->GetCenter() )
{
DRAWSEGMENT* newItem = new DRAWSEGMENT( graphic );
m_view->Add( newItem );
m_board->Add( newItem );
newItem->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
m_view->Add( graphic );
m_board->Add( graphic );
graphic->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
}
else
{
// Let's give the user another chance of drawing a proper arc (i.e. angle > 0)
--step;
}
}
break;
@ -196,18 +202,18 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
switch( step )
{
case SET_ORIGIN:
graphic.SetCenter( wxPoint( cursorPos.x, cursorPos.y ) );
graphic->SetCenter( wxPoint( cursorPos.x, cursorPos.y ) );
break;
case SET_END:
helperLine.SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
graphic.SetArcStart( wxPoint( cursorPos.x, cursorPos.y ) );
graphic->SetArcStart( wxPoint( cursorPos.x, cursorPos.y ) );
break;
case SET_ANGLE:
{
// Compute the current angle
VECTOR2D endLine( wxPoint( cursorPos.x, cursorPos.y ) - graphic.GetCenter() );
VECTOR2D endLine( wxPoint( cursorPos.x, cursorPos.y ) - graphic->GetCenter() );
double newAngle = RAD2DECIDEG( endLine.Angle() - startAngle );
if( clockwise && newAngle < 0.0 )
@ -215,7 +221,7 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent )
else if( !clockwise && newAngle > 0.0 )
newAngle -= 3600.0;
graphic.SetAngle( newAngle );
graphic->SetAngle( newAngle );
}
break;
}
@ -395,6 +401,12 @@ int DRAWING_TOOL::DrawDimension( TOOL_EVENT& aEvent )
}
break;
case SET_END:
// Dimensions that have origin and end in the same spot are not valid
if( dimension->GetOrigin() == wxPoint( cursorPos.x, cursorPos.y ) )
--step;
break;
case SET_HEIGHT:
{
if( wxPoint( cursorPos.x, cursorPos.y ) != dimension->GetPosition() )
@ -610,12 +622,11 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
// Only two shapes are currently supported
assert( aShape == S_SEGMENT || aShape == S_CIRCLE );
bool started = false;
DRAWSEGMENT graphic;
DRAWSEGMENT* graphic = new DRAWSEGMENT( m_board );
// Init the new item attributes
graphic.SetShape( (STROKE_T) aShape );
graphic.SetWidth( m_board->GetDesignSettings().m_DrawSegmentWidth );
graphic->SetShape( (STROKE_T) aShape );
graphic->SetWidth( m_board->GetDesignSettings().m_DrawSegmentWidth );
// Add a VIEW_GROUP that serves as a preview for the new item
KIGFX::VIEW_GROUP preview( m_view );
@ -626,6 +637,7 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
Activate();
bool started = false;
// Main loop: keep receiving events
while( OPT_TOOL_EVENT evt = Wait() )
{
@ -634,17 +646,22 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
VECTOR2I cursorPos = m_controls->GetCursorPosition();
if( evt->IsCancel() )
{
preview.FreeItems();
if( !started )
delete graphic;
break;
}
else if( evt->IsKeyUp() )
{
int width = graphic.GetWidth();
int width = graphic->GetWidth();
// Modify the new item width
if( evt->KeyCode() == '-' && width > WIDTH_STEP )
graphic.SetWidth( width - WIDTH_STEP );
graphic->SetWidth( width - WIDTH_STEP );
else if( evt->KeyCode() == '=' )
graphic.SetWidth( width + WIDTH_STEP );
graphic->SetWidth( width + WIDTH_STEP );
preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
}
@ -663,29 +680,37 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
{
m_controls->SetAutoPan( true );
graphic.SetStart( wxPoint( cursorPos.x, cursorPos.y ) );
graphic.SetLayer( layer );
preview.Add( &graphic );
graphic->SetStart( wxPoint( cursorPos.x, cursorPos.y ) );
graphic->SetLayer( layer );
preview.Add( graphic );
started = true;
}
}
else
{
if( wxPoint( cursorPos.x, cursorPos.y ) != graphic.GetStart() )
if( wxPoint( cursorPos.x, cursorPos.y ) != graphic->GetStart() )
{
DRAWSEGMENT* newItem = new DRAWSEGMENT( graphic );
m_view->Add( newItem );
m_board->Add( newItem );
newItem->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
m_view->Add( graphic );
m_board->Add( graphic );
graphic->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
preview.Remove( graphic );
if( aContinous )
graphic.SetStart( graphic.GetEnd() ); // This is the origin point for a new item
{
graphic = new DRAWSEGMENT( *graphic );
// Start the new line in the same spot where the previous one has ended
graphic->SetStart( graphic->GetEnd() );
preview.Add( graphic );
}
else
break;
}
else // User has clicked twice in the same spot
break; // seems like a clear sign that the drawing is finished
else
{ // User has clicked twice in the same spot
delete graphic; // seems like a clear sign that the drawing is finished
break; // and we should remove the latest DRAWSEGMENT we have created
}
}
}
@ -693,9 +718,9 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
{
// 45 degree lines
if( linesAngle45 && aShape == S_SEGMENT )
make45DegLine( &graphic );
make45DegLine( graphic );
else
graphic.SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
graphic->SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
// Show a preview of the item
preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
@ -809,8 +834,7 @@ int DRAWING_TOOL::drawZone( bool aKeepout )
{
// Add the first point
zone->Outline()->Start( zoneInfo.m_CurrentZone_Layer,
cursorPos.x,
cursorPos.y,
cursorPos.x, cursorPos.y,
zone->GetHatchStyle() );
helperLine->SetStart( wxPoint( cursorPos.x, cursorPos.y ) );
origin = cursorPos;

View File

@ -54,25 +54,70 @@ public:
/**
* Function DrawLine()
* Starts interactively drawing a line. After invoking the function it expects the user
* to click at least twice to determine the origin and the end for a line. If there are
* to click at least two times to determine the origin and the end for a line. If there are
* more clicks, the line is drawn as a continous polyline.
*/
int DrawLine( TOOL_EVENT& aEvent );
/**
* Function DrawCircle()
* Starts interactively drawing a circle. After invoking the function it expects the user
* to first click on a point that is going to be used as the center of the circle. The second
* click determines the circle radius.
*/
int DrawCircle( TOOL_EVENT& aEvent );
/**
* Function DrawArc()
* Starts interactively drawing an arc. After invoking the function it expects the user
* to first click on a point that is going to be used as the center of the arc. The second
* click determines the origin and radius, the third one - the angle.
*/
int DrawArc( TOOL_EVENT& aEvent );
/**
* Function DrawText()
* Displays a dialog that allows to input text and its settings and then lets the user decide
* where to place the text.
*/
int DrawText( TOOL_EVENT& aEvent );
/**
* Function DrawDimension()
* Starts interactively drawing a dimension. After invoking the function it expects the user
* to first click on a point that is going to be used as the origin of the dimension.
* The second click determines the end and the third click modifies its height.
*/
int DrawDimension( TOOL_EVENT& aEvent );
/**
* Function DrawZone()
* Starts interactively drawing a zone. After invoking the function a zone settings dialog
* is displayed. After confirmation it allows the user to set points that are going to be used
* as a boundary polygon of the zone. Double click or clicking on the origin of the boundary
* polyline finishes the drawing.
*/
int DrawZone( TOOL_EVENT& aEvent );
/**
* Function DrawKeepout()
* Starts interactively drawing a keepout area. After invoking the function an area settings
* dialog is displayed. After confirmation it allows the user to set points that are going to
* be used as a boundary polygon of the area. Double click or clicking on the origin of the
* boundary polyline finishes the drawing.
*/
int DrawKeepout( TOOL_EVENT& aEvent );
/**
* Function PlaceTarget()
* Allows user to place a layer alignment target.
*/
int PlaceTarget( TOOL_EVENT& aEvent );
/**
* Function PlaceModule()
* Displays a dialog to selected a module to be added and then allows user to set its position..
*/
int PlaceModule( TOOL_EVENT& aEvent );
private: