diff --git a/pcbnew/edit_pcb_text.cpp b/pcbnew/edit_pcb_text.cpp index e4ecfcb5a2..277159a5ee 100644 --- a/pcbnew/edit_pcb_text.cpp +++ b/pcbnew/edit_pcb_text.cpp @@ -195,7 +195,8 @@ TEXTE_PCB* PCB_EDIT_FRAME::CreateTextePcb( wxDC* aDC, TEXTE_PCB* aText ) textePcb->Copy( aText ); GetBoard()->Add( textePcb ); textePcb->SetFlags( IS_NEW ); - StartMoveTextePcb( textePcb, aDC, false ); // Don't erase aText when copying + if( aDC ) + StartMoveTextePcb( textePcb, aDC, false ); // Don't erase aText when copying } else { @@ -222,7 +223,7 @@ TEXTE_PCB* PCB_EDIT_FRAME::CreateTextePcb( wxDC* aDC, TEXTE_PCB* aText ) textePcb->DeleteStructure(); textePcb = NULL; } - else + else if( aDC ) { StartMoveTextePcb( textePcb, aDC ); } diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index d7368b96c6..44217508bc 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -68,3 +68,7 @@ TOOL_ACTION COMMON_ACTIONS::drawCircle( "pcbnew.InteractiveDrawing.circle", TOOL_ACTION COMMON_ACTIONS::drawArc( "pcbnew.InteractiveDrawing.arc", AS_GLOBAL, 'A', "Draw an arc", "Draw an arc" ); + +TOOL_ACTION COMMON_ACTIONS::drawText( "pcbnew.InteractiveDrawing.text", + AS_GLOBAL, 'T', + "Add a text", "Add a text" ); diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index 3542490c24..992d9cbdf8 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -65,4 +65,7 @@ public: /// Activation of the drawing tool (arc) static TOOL_ACTION drawArc; + + /// Activation of the drawing tool (text) + static TOOL_ACTION drawText; }; diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 1f2a64ec1a..d658f3c23d 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -159,7 +160,7 @@ int DRAWING_TOOL::DrawArc( TOOL_EVENT& aEvent ) { DRAWSEGMENT* newItem = new DRAWSEGMENT( graphic ); view->Add( newItem ); - getModel( PCB_T )->Add( newItem ); + board->Add( newItem ); newItem->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); } } @@ -291,7 +292,7 @@ int DRAWING_TOOL::draw( STROKE_T aShape ) { DRAWSEGMENT* newItem = new DRAWSEGMENT( graphic ); view->Add( newItem ); - getModel( PCB_T )->Add( newItem ); + board->Add( newItem ); newItem->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); if( m_continous ) @@ -342,9 +343,70 @@ int DRAWING_TOOL::draw( STROKE_T aShape ) } +int DRAWING_TOOL::DrawText( TOOL_EVENT& aEvent ) +{ + KIGFX::VIEW* view = getView(); + KIGFX::VIEW_CONTROLS* controls = getViewControls(); + + // Init the new item attributes + TEXTE_PCB* newText = getEditFrame()->CreateTextePcb( NULL ); + + // Add a VIEW_GROUP that serves as a preview for the new item + KIGFX::VIEW_GROUP preview( view ); + preview.Add( newText ); + view->Add( &preview ); + + controls->ShowCursor( true ); + controls->SetSnapping( true ); + controls->SetAutoPan( true ); + + Activate(); + + // Main loop: keep receiving events + while( OPT_TOOL_EVENT evt = Wait() ) + { + VECTOR2D cursorPos = view->ToWorld( controls->GetCursorPosition() ); + + if( evt->IsCancel() ) + { + // it was already added by CreateTextPcb() + getModel( PCB_T )->Delete( newText ); + break; + } + + else if( evt->IsClick( BUT_LEFT ) ) + { + newText->ClearFlags(); + view->Add( newText ); + // board->Add( newText ); // it is already added by CreateTextePcb() + newText->ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + break; + } + + else if( evt->IsMotion() ) + { + newText->SetTextPosition( wxPoint( cursorPos.x, cursorPos.y ) ); + + // Show a preview of the item + preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); + } + } + + controls->ShowCursor( false ); + controls->SetSnapping( false ); + controls->SetAutoPan( false ); + view->Remove( &preview ); + + setTransitions(); + + return 0; +} + + void DRAWING_TOOL::setTransitions() { Go( &DRAWING_TOOL::DrawLine, COMMON_ACTIONS::drawLine.MakeEvent() ); Go( &DRAWING_TOOL::DrawCircle, COMMON_ACTIONS::drawCircle.MakeEvent() ); Go( &DRAWING_TOOL::DrawArc, COMMON_ACTIONS::drawArc.MakeEvent() ); + Go( &DRAWING_TOOL::DrawText, COMMON_ACTIONS::drawText.MakeEvent() ); } diff --git a/pcbnew/tools/drawing_tool.h b/pcbnew/tools/drawing_tool.h index 4cef5744cd..4e969860db 100644 --- a/pcbnew/tools/drawing_tool.h +++ b/pcbnew/tools/drawing_tool.h @@ -55,6 +55,8 @@ public: int DrawArc( TOOL_EVENT& aEvent ); + int DrawText( TOOL_EVENT& aEvent ); + private: ///> Starts drawing a selected shape. int draw( STROKE_T aShape ); diff --git a/pcbnew/tools/pcb_tools.cpp b/pcbnew/tools/pcb_tools.cpp index 2f893cf509..73f9f14a5f 100644 --- a/pcbnew/tools/pcb_tools.cpp +++ b/pcbnew/tools/pcb_tools.cpp @@ -58,6 +58,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager->RegisterAction( &COMMON_ACTIONS::drawLine ); m_toolManager->RegisterAction( &COMMON_ACTIONS::drawCircle ); m_toolManager->RegisterAction( &COMMON_ACTIONS::drawArc ); + m_toolManager->RegisterAction( &COMMON_ACTIONS::drawText ); // Register tools m_toolManager->RegisterTool( new SELECTION_TOOL );