kicad/eeschema/symbdraw.cpp

365 lines
11 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2009-2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file symbdraw.cpp
* @brief Create, move .. graphic shapes used to build and draw a component (lines, arcs ..)
*/
2007-05-06 16:03:28 +00:00
#include <fctsys.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <class_sch_screen.h>
#include <base_units.h>
#include <msgpanel.h>
#include <eeschema_id.h>
#include <general.h>
#include <libeditframe.h>
#include <class_libentry.h>
#include <lib_arc.h>
#include <lib_circle.h>
#include <lib_polyline.h>
#include <lib_rectangle.h>
#include <lib_text.h>
#include <dialogs/dialog_lib_edit_draw_item.h>
2010-11-17 21:47:27 +00:00
2007-05-06 16:03:28 +00:00
static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
bool aErase );
static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
bool aErase );
2007-05-06 16:03:28 +00:00
void LIB_EDIT_FRAME::EditGraphicSymbol( wxDC* DC, LIB_ITEM* DrawItem )
2007-05-06 16:03:28 +00:00
{
if( DrawItem == NULL )
return;
LIB_PART* component = DrawItem->GetParent();
DIALOG_LIB_EDIT_DRAW_ITEM dialog( this, DrawItem->GetTypeName() );
dialog.SetWidthUnits( ReturnUnitSymbol( g_UserUnit ) );
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
wxString val = StringFromValue( g_UserUnit, DrawItem->GetWidth() );
dialog.SetWidth( val );
dialog.SetApplyToAllUnits( DrawItem->GetUnit() == 0 );
dialog.EnableApplyToAllUnits( component && component->GetUnitCount() > 1 );
dialog.SetApplyToAllConversions( DrawItem->GetConvert() == 0 );
dialog.EnableApplyToAllConversions( component && component->HasConversion() );
dialog.SetFillStyle( DrawItem->GetFillMode() );
dialog.EnableFillStyle( DrawItem->IsFillable() );
if( dialog.ShowModal() == wxID_CANCEL )
return;
// Init default values (used to create a new draw item)
val = dialog.GetWidth();
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
m_drawLineWidth = ValueFromString( g_UserUnit, val );
m_drawSpecificConvert = !dialog.GetApplyToAllConversions();
m_drawSpecificUnit = !dialog.GetApplyToAllUnits();
#if 0
/* TODO: see if m_drawFillStyle must retain the last fill option or not.
* if the last is Filled, having next new graphic items created
* with filled body is often bad.
* currently m_drawFillStyle is left with the default value (not filled)
*/
if( DrawItem->IsFillable() )
m_drawFillStyle = (FILL_T) dialog.GetFillStyle();
#endif
// Save copy for undo if not in edit (edit command already handle the save copy)
if( !DrawItem->InEditMode() )
SaveCopyInUndoList( DrawItem->GetParent() );
if( m_drawSpecificUnit )
DrawItem->SetUnit( GetUnit() );
else
DrawItem->SetUnit( 0 );
if( m_drawSpecificConvert )
DrawItem->SetConvert( GetConvert() );
else
DrawItem->SetConvert( 0 );
2007-05-06 16:03:28 +00:00
if( DrawItem->IsFillable() )
DrawItem->SetFillMode( (FILL_T) dialog.GetFillStyle() );
DrawItem->SetWidth( m_drawLineWidth );
if( component )
component->GetDrawItemList().sort();
OnModify( );
MSG_PANEL_ITEMS items;
DrawItem->GetMsgPanelInfo( items );
SetMsgPanel( items );
m_canvas->Refresh();
2007-05-06 16:03:28 +00:00
}
static void AbortSymbolTraceOn( EDA_DRAW_PANEL* Panel, wxDC* DC )
2007-05-06 16:03:28 +00:00
{
LIB_EDIT_FRAME* parent = (LIB_EDIT_FRAME*) Panel->GetParent();
LIB_ITEM* item = parent->GetDrawItem();
if( item == NULL )
return;
bool newItem = item->IsNew();
item->EndEdit( parent->GetCrossHairPosition( true ), true );
if( newItem )
{
delete item;
}
else
parent->RestoreComponent();
2010-03-28 14:46:49 +00:00
parent->SetDrawItem( NULL );
Panel->Refresh();
2007-05-06 16:03:28 +00:00
}
LIB_ITEM* LIB_EDIT_FRAME::CreateGraphicItem( LIB_PART* LibEntry, wxDC* DC )
2007-05-06 16:03:28 +00:00
{
m_canvas->SetMouseCapture( SymbolDisplayDraw, AbortSymbolTraceOn );
wxPoint drawPos = GetCrossHairPosition( true );
// no temp copy -> the current version of component will be used for Undo
// This is normal when adding new items to the current component
ClearTempCopyComponent();
switch( GetToolId() )
{
case ID_LIBEDIT_BODY_ARC_BUTT:
m_drawItem = new LIB_ARC( LibEntry );
break;
case ID_LIBEDIT_BODY_CIRCLE_BUTT:
m_drawItem = new LIB_CIRCLE( LibEntry );
break;
case ID_LIBEDIT_BODY_RECT_BUTT:
m_drawItem = new LIB_RECTANGLE( LibEntry );
break;
case ID_LIBEDIT_BODY_LINE_BUTT:
m_drawItem = new LIB_POLYLINE( LibEntry );
break;
case ID_LIBEDIT_BODY_TEXT_BUTT:
{
LIB_TEXT* text = new LIB_TEXT( LibEntry );
text->SetTextSize( wxSize( m_textSize, m_textSize ) );
text->SetTextAngle( m_current_text_angle );
// Enter the graphic text info
m_canvas->SetIgnoreMouseEvents( true );
EditSymbolText( NULL, text );
m_canvas->SetIgnoreMouseEvents( false );
m_canvas->MoveCursorToCrossHair();
if( text->GetText().IsEmpty() )
{
delete text;
m_drawItem = NULL;
}
else
{
m_drawItem = text;
}
}
break;
default:
DisplayError( this, wxT( "LIB_EDIT_FRAME::CreateGraphicItem() error" ) );
return NULL;
}
if( m_drawItem )
{
m_drawItem->BeginEdit( IS_NEW, drawPos );
m_drawItem->SetWidth( m_drawLineWidth );
m_drawItem->SetFillMode( m_drawFillStyle );
if( m_drawSpecificUnit )
m_drawItem->SetUnit( m_unit );
if( m_drawSpecificConvert )
m_drawItem->SetConvert( m_convert );
// Draw initial symbol:
m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
}
else
{
m_canvas->EndMouseCapture();
return NULL;
}
m_canvas->MoveCursorToCrossHair();
m_canvas->SetIgnoreMouseEvents( false );
return m_drawItem;
2007-05-06 16:03:28 +00:00
}
void LIB_EDIT_FRAME::GraphicItemBeginDraw( wxDC* DC )
2007-05-06 16:03:28 +00:00
{
if( m_drawItem == NULL )
return;
wxPoint pos = GetCrossHairPosition( true );
if( m_drawItem->ContinueEdit( pos ) )
{
m_drawItem->Draw( m_canvas, DC, pos, COLOR4D::UNSPECIFIED, g_XorMode, NULL, DefaultTransform );
return;
}
EndDrawGraphicItem( DC );
2007-05-06 16:03:28 +00:00
}
/*
* Redraw the graphic shape while moving
*/
static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
bool aErase )
2007-05-06 16:03:28 +00:00
{
LIB_ITEM* item;
item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
if( item == NULL )
return;
item->SetEraseLastDrawItem( aErase );
// if item is the reference field, we must add the current unit id
if( item->Type() == LIB_FIELD_T )
{
int unit = ((LIB_EDIT_FRAME*)aPanel->GetParent())->GetUnit();
wxString text = ((LIB_FIELD*)item)->GetFullText( unit );
item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
COLOR4D::UNSPECIFIED, g_XorMode, &text,
DefaultTransform );
}
else
item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ),
COLOR4D::UNSPECIFIED, g_XorMode, NULL,
DefaultTransform );
2007-05-06 16:03:28 +00:00
}
void LIB_EDIT_FRAME::StartMoveDrawSymbol( wxDC* DC )
2007-05-06 16:03:28 +00:00
{
if( m_drawItem == NULL )
return;
2007-05-06 16:03:28 +00:00
SetCursor( wxCURSOR_HAND );
2007-05-06 16:03:28 +00:00
TempCopyComponent();
2013-02-26 11:25:30 +00:00
// For fields only, move the anchor point of the field
// to the cursor position to allow user to see the text justification
if( m_drawItem->Type() == LIB_FIELD_T )
m_drawItem->BeginEdit( IS_MOVED, m_drawItem->GetPosition() );
else
m_drawItem->BeginEdit( IS_MOVED, GetCrossHairPosition( true ) );
2013-02-26 11:25:30 +00:00
m_canvas->SetMouseCapture( RedrawWhileMovingCursor, AbortSymbolTraceOn );
m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
}
2007-05-06 16:03:28 +00:00
void LIB_EDIT_FRAME::StartModifyDrawSymbol( wxDC* DC )
{
if( m_drawItem == NULL )
return;
TempCopyComponent();
m_drawItem->BeginEdit( IS_RESIZED, GetCrossHairPosition( true ) );
m_canvas->SetMouseCapture( SymbolDisplayDraw, AbortSymbolTraceOn );
m_canvas->CallMouseCapture( DC, wxDefaultPosition, true );
}
//! @brief Manage mouse events when creating new graphic object or modifying an graphic object.
static void SymbolDisplayDraw( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition,
bool aErase )
2007-05-06 16:03:28 +00:00
{
LIB_ITEM* item = ( (LIB_EDIT_FRAME*) aPanel->GetParent() )->GetDrawItem();
if( item == NULL )
return;
item->SetEraseLastDrawItem( aErase );
item->Draw( aPanel, aDC, aPanel->GetParent()->GetCrossHairPosition( true ), COLOR4D::UNSPECIFIED, g_XorMode, NULL,
DefaultTransform );
2007-05-06 16:03:28 +00:00
}
void LIB_EDIT_FRAME::EndDrawGraphicItem( wxDC* DC )
2007-05-06 16:03:28 +00:00
{
if( LIB_PART* part = GetCurPart() )
{
if( !m_drawItem )
return;
if( GetToolId() != ID_NO_TOOL_SELECTED )
SetCursor( wxCURSOR_PENCIL );
else
SetCursor( (wxStockCursor) m_canvas->GetDefaultCursor() );
if( GetTempCopyComponent() ) // used when editing an existing item
SaveCopyInUndoList( GetTempCopyComponent() );
else
{
// When creating a new item, there is still no change for the
// current component. So save it.
SaveCopyInUndoList( part );
}
if( m_drawItem->IsNew() )
part->AddDrawItem( m_drawItem );
m_drawItem->EndEdit( GetCrossHairPosition( true ) );
m_drawItem = NULL;
OnModify();
m_canvas->SetMouseCapture( NULL, NULL );
m_canvas->Refresh();
}
2007-05-06 16:03:28 +00:00
}