2014-02-27 16:29:08 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KICAD, a free EDA CAD application.
|
|
|
|
*
|
2017-02-08 07:45:52 +00:00
|
|
|
* Copyright (C) 2014-2017 CERN
|
2014-02-27 16:29:08 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gal/graphics_abstraction_layer.h>
|
2014-03-18 15:55:06 +00:00
|
|
|
#include "edit_points.h"
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
|
|
|
|
{
|
2014-03-12 09:46:11 +00:00
|
|
|
// Corners of the EDIT_POINT square
|
2014-03-07 09:26:33 +00:00
|
|
|
VECTOR2I topLeft = GetPosition() - aSize;
|
|
|
|
VECTOR2I bottomRight = GetPosition() + aSize;
|
|
|
|
|
|
|
|
return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
|
|
|
|
aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-26 13:30:03 +00:00
|
|
|
EDIT_POINTS::EDIT_POINTS( EDA_ITEM* aParent )
|
|
|
|
: EDA_ITEM( NOT_USED ), m_parent( aParent ), m_allowPoints( true )
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2019-08-26 13:30:03 +00:00
|
|
|
unsigned size = std::abs<int>( KiROUND( aView->ToWorld( EDIT_POINT::POINT_SIZE ) ) );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2019-08-26 13:30:03 +00:00
|
|
|
if( m_allowPoints )
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2019-08-26 13:30:03 +00:00
|
|
|
for( auto& point : m_points )
|
|
|
|
{
|
|
|
|
if( point.WithinPoint( aLocation, size ) )
|
|
|
|
return &point;
|
|
|
|
}
|
2014-03-06 16:34:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 13:30:03 +00:00
|
|
|
for( auto& line : m_lines )
|
2014-03-06 16:34:04 +00:00
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( line.WithinPoint( aLocation, size ) )
|
|
|
|
return &line;
|
2014-02-27 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 15:44:42 +00:00
|
|
|
int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
|
|
|
|
{
|
|
|
|
int lastIdx = 0;
|
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx >= aPointIdx )
|
|
|
|
return lastIdx;
|
|
|
|
|
|
|
|
lastIdx = idx + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx >= aPointIdx )
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_points.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx + 1 == aPointIdx )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// the list is sorted, so we cannot expect it any further
|
|
|
|
if( idx > aPointIdx )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( aPointIdx == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
|
|
|
|
{
|
2016-06-29 20:07:55 +00:00
|
|
|
for( int idx : m_contours )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
|
|
|
if( idx == aPointIdx )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// the list is sorted, so we cannot expect it any further
|
|
|
|
if( idx > aPointIdx )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the end of the list surely is the end of a contour
|
|
|
|
return ( aPointIdx == (int) m_points.size() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < m_points.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_points[i] == aPoint )
|
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( !aTraverseContours && IsContourStart( i ) )
|
|
|
|
return &m_points[GetContourEndIdx( i )];
|
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
if( i == 0 )
|
|
|
|
return &m_points[m_points.size() - 1];
|
|
|
|
else
|
|
|
|
return &m_points[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 15:18:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_LINE* EDIT_POINTS::Previous( const EDIT_LINE& aLine )
|
|
|
|
{
|
2014-03-07 09:26:33 +00:00
|
|
|
for( unsigned int i = 0; i < m_lines.size(); ++i )
|
|
|
|
{
|
2014-04-01 15:18:43 +00:00
|
|
|
if( m_lines[i] == aLine )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
if( i == 0 )
|
|
|
|
return &m_lines[m_lines.size() - 1];
|
|
|
|
else
|
|
|
|
return &m_lines[i - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 15:44:42 +00:00
|
|
|
EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
for( unsigned int i = 0; i < m_points.size(); ++i )
|
|
|
|
{
|
|
|
|
if( m_points[i] == aPoint )
|
|
|
|
{
|
2015-01-16 15:44:42 +00:00
|
|
|
if( !aTraverseContours && IsContourEnd( i ) )
|
|
|
|
return &m_points[GetContourStartIdx( i )];
|
|
|
|
|
2014-03-07 09:26:33 +00:00
|
|
|
if( i == m_points.size() - 1 )
|
|
|
|
return &m_points[0];
|
|
|
|
else
|
|
|
|
return &m_points[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 15:18:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EDIT_LINE* EDIT_POINTS::Next( const EDIT_LINE& aLine )
|
|
|
|
{
|
2014-03-07 09:26:33 +00:00
|
|
|
for( unsigned int i = 0; i < m_lines.size(); ++i )
|
|
|
|
{
|
2014-04-01 15:18:43 +00:00
|
|
|
if( m_lines[i] == aLine )
|
2014-03-07 09:26:33 +00:00
|
|
|
{
|
|
|
|
if( i == m_lines.size() - 1 )
|
|
|
|
return &m_lines[0];
|
|
|
|
else
|
|
|
|
return &m_lines[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-08 07:45:52 +00:00
|
|
|
const BOX2I EDIT_POINTS::ViewBBox() const
|
|
|
|
{
|
|
|
|
BOX2I box;
|
|
|
|
bool empty = true;
|
|
|
|
|
|
|
|
for( const auto& point : m_points )
|
|
|
|
{
|
|
|
|
if( empty )
|
|
|
|
{
|
|
|
|
box.SetOrigin( point.GetPosition() );
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.Merge( point.GetPosition() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const auto& line : m_lines )
|
|
|
|
{
|
|
|
|
if( empty )
|
|
|
|
{
|
|
|
|
box.SetOrigin( line.GetOrigin().GetPosition() );
|
|
|
|
box.SetEnd( line.GetEnd().GetPosition() );
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box.Merge( line.GetOrigin().GetPosition() );
|
|
|
|
box.Merge( line.GetEnd().GetPosition() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
|
2014-02-27 16:29:08 +00:00
|
|
|
{
|
2016-12-02 17:58:12 +00:00
|
|
|
auto gal = aView->GetGAL();
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->SetFillColor( KIGFX::COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
|
|
|
|
gal->SetIsFill( true );
|
|
|
|
gal->SetIsStroke( false );
|
|
|
|
gal->PushDepth();
|
|
|
|
gal->SetLayerDepth( gal->GetMinDepth() );
|
|
|
|
|
|
|
|
float size = aView->ToWorld( EDIT_POINT::POINT_SIZE );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( const EDIT_POINT& point : m_points )
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->DrawRectangle( point.GetPosition() - size / 2, point.GetPosition() + size / 2 );
|
2014-02-27 16:29:08 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( const EDIT_LINE& line : m_lines )
|
2015-01-16 15:44:42 +00:00
|
|
|
{
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->DrawCircle( line.GetPosition(), size / 2 );
|
2015-01-16 15:44:42 +00:00
|
|
|
}
|
2014-03-06 16:34:04 +00:00
|
|
|
|
2016-12-02 17:58:12 +00:00
|
|
|
gal->PopDepth();
|
2014-02-27 16:29:08 +00:00
|
|
|
}
|