2021-06-06 15:07:55 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2017 CERN
|
2022-08-20 09:27:35 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-06-06 15:07:55 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
* @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 <algorithm>
|
|
|
|
#include <eda_item.h>
|
|
|
|
#include <tool/selection.h>
|
|
|
|
|
|
|
|
|
2023-06-20 20:35:03 +00:00
|
|
|
bool SELECTION::operator==( const SELECTION& aOther ) const
|
2023-01-21 22:36:39 +00:00
|
|
|
{
|
|
|
|
return ( m_items == aOther.m_items
|
|
|
|
&& m_itemsOrders == aOther.m_itemsOrders
|
|
|
|
&& m_isHover == aOther.m_isHover
|
|
|
|
&& m_lastAddedItem == aOther.m_lastAddedItem
|
|
|
|
&& m_orderCounter == aOther.m_orderCounter );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
void SELECTION::Add( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
// We're not sorting here; this is just a time-optimized way to do an
|
|
|
|
// inclusion check. std::lower_bound will return the first i >= aItem
|
|
|
|
// and the second i > aItem check rules out i == aItem.
|
|
|
|
ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
|
|
|
|
|
|
|
|
if( i == m_items.end() || *i > aItem )
|
2022-07-19 15:00:35 +00:00
|
|
|
{
|
2022-09-07 16:16:02 +00:00
|
|
|
m_itemsOrders.insert( m_itemsOrders.begin() + std::distance( m_items.begin(), i ),
|
|
|
|
m_orderCounter );
|
2021-06-06 15:07:55 +00:00
|
|
|
m_items.insert( i, aItem );
|
2022-09-07 16:16:02 +00:00
|
|
|
m_orderCounter++;
|
2022-07-19 15:00:35 +00:00
|
|
|
m_lastAddedItem = aItem;
|
|
|
|
}
|
2021-06-06 15:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SELECTION::Remove( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
ITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
|
|
|
|
|
|
|
|
if( !( i == m_items.end() || *i > aItem ) )
|
2022-07-19 15:00:35 +00:00
|
|
|
{
|
2022-09-07 16:16:02 +00:00
|
|
|
m_itemsOrders.erase( m_itemsOrders.begin() + std::distance( m_items.begin(), i ) );
|
2021-06-06 15:07:55 +00:00
|
|
|
m_items.erase( i );
|
2022-07-19 15:00:35 +00:00
|
|
|
|
|
|
|
if( aItem == m_lastAddedItem )
|
|
|
|
m_lastAddedItem = nullptr;
|
|
|
|
}
|
2021-06-06 15:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KIGFX::VIEW_ITEM* SELECTION::GetItem( unsigned int aIdx ) const
|
|
|
|
{
|
|
|
|
if( aIdx < m_items.size() )
|
|
|
|
return m_items[aIdx];
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SELECTION::Contains( EDA_ITEM* aItem ) const
|
|
|
|
{
|
|
|
|
CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem );
|
|
|
|
|
|
|
|
return !( i == m_items.end() || *i > aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the center point of the selection area bounding box.
|
|
|
|
VECTOR2I SELECTION::GetCenter() const
|
|
|
|
{
|
2022-08-20 09:27:35 +00:00
|
|
|
bool hasOnlyText = true;
|
2021-06-06 15:07:55 +00:00
|
|
|
|
2021-12-04 04:28:14 +00:00
|
|
|
// If the selection contains only texts calculate the center as the mean of all positions
|
2021-12-03 15:17:02 +00:00
|
|
|
// instead of using the center of the total bounding box. Otherwise rotating the selection will
|
|
|
|
// also translate it.
|
2021-06-06 15:07:55 +00:00
|
|
|
|
|
|
|
for( EDA_ITEM* item : m_items )
|
|
|
|
{
|
2022-08-20 09:27:35 +00:00
|
|
|
if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
|
2021-06-06 15:07:55 +00:00
|
|
|
{
|
2021-12-04 04:28:14 +00:00
|
|
|
hasOnlyText = false;
|
2021-06-06 15:07:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 23:28:18 +00:00
|
|
|
BOX2I bbox;
|
2021-06-06 15:07:55 +00:00
|
|
|
|
2021-12-04 04:28:14 +00:00
|
|
|
if( hasOnlyText )
|
2021-12-03 15:17:02 +00:00
|
|
|
{
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I center( 0, 0 );
|
2021-12-03 15:17:02 +00:00
|
|
|
|
|
|
|
for( EDA_ITEM* item : m_items )
|
|
|
|
center += item->GetPosition();
|
|
|
|
|
2021-12-16 07:13:14 +00:00
|
|
|
center = center / static_cast<int>( m_items.size() );
|
2021-12-03 15:17:02 +00:00
|
|
|
return static_cast<VECTOR2I>( center );
|
|
|
|
}
|
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
for( EDA_ITEM* item : m_items )
|
|
|
|
{
|
2022-08-20 09:27:35 +00:00
|
|
|
if( !item->IsType( { SCH_TEXT_T, SCH_LABEL_LOCATE_ANY_T } ) )
|
2021-06-06 15:07:55 +00:00
|
|
|
bbox.Merge( item->GetBoundingBox() );
|
|
|
|
}
|
|
|
|
|
2021-12-03 15:17:02 +00:00
|
|
|
return static_cast<VECTOR2I>( bbox.GetCenter() );
|
2021-06-06 15:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-02 17:56:13 +00:00
|
|
|
BOX2I SELECTION::GetBoundingBox( bool aOnlyVisible ) const
|
2021-06-06 15:07:55 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
BOX2I bbox;
|
2021-06-06 15:07:55 +00:00
|
|
|
|
|
|
|
for( EDA_ITEM* item : m_items )
|
|
|
|
bbox.Merge( item->GetBoundingBox() );
|
|
|
|
|
|
|
|
return bbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SELECTION::HasType( KICAD_T aType ) const
|
|
|
|
{
|
2022-07-29 21:02:35 +00:00
|
|
|
for( const EDA_ITEM* item : m_items )
|
2021-06-06 15:07:55 +00:00
|
|
|
{
|
2022-11-18 14:29:51 +00:00
|
|
|
if( item->IsType( { aType } ) )
|
2021-06-06 15:07:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-08 13:07:18 +00:00
|
|
|
size_t SELECTION::CountType( KICAD_T aType ) const
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
|
2022-07-29 21:02:35 +00:00
|
|
|
for( const EDA_ITEM* item : m_items )
|
2021-12-08 13:07:18 +00:00
|
|
|
{
|
2022-11-18 14:29:51 +00:00
|
|
|
if( item->IsType( { aType } ) )
|
2021-12-08 13:07:18 +00:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-15 16:27:24 +00:00
|
|
|
VECTOR2I SELECTION::GetReferencePoint() const
|
|
|
|
{
|
|
|
|
if( m_referencePoint )
|
|
|
|
return *m_referencePoint;
|
|
|
|
else
|
|
|
|
return GetBoundingBox().Centre();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SELECTION::SetReferencePoint( const VECTOR2I& aP )
|
|
|
|
{
|
|
|
|
m_referencePoint = aP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SELECTION::ClearReferencePoint()
|
|
|
|
{
|
|
|
|
m_referencePoint = std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-03 12:58:56 +00:00
|
|
|
const std::vector<KIGFX::VIEW_ITEM*> SELECTION::updateDrawList() const
|
2021-06-06 15:07:55 +00:00
|
|
|
{
|
|
|
|
std::vector<VIEW_ITEM*> items;
|
|
|
|
|
2022-07-29 21:02:35 +00:00
|
|
|
for( EDA_ITEM* item : m_items )
|
2021-06-06 15:07:55 +00:00
|
|
|
items.push_back( item );
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SELECTION::AreAllItemsIdentical() const
|
|
|
|
{
|
|
|
|
return ( std::all_of( m_items.begin() + 1, m_items.end(),
|
|
|
|
[&]( const EDA_ITEM* r )
|
|
|
|
{
|
|
|
|
return r->Type() == m_items.front()->Type();
|
|
|
|
} ) );
|
2021-11-14 16:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
|
|
|
|
{
|
|
|
|
return ( std::all_of( m_items.begin(), m_items.end(),
|
|
|
|
[&]( const EDA_ITEM* r )
|
|
|
|
{
|
2022-11-18 14:29:51 +00:00
|
|
|
return r->IsType( aList );
|
2021-11-14 16:54:04 +00:00
|
|
|
} ) );
|
|
|
|
}
|
2022-09-07 16:16:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
|
|
|
|
{
|
|
|
|
using pairedIterators =
|
|
|
|
std::pair<decltype( m_items.begin() ), decltype( m_itemsOrders.begin() )>;
|
|
|
|
|
|
|
|
// Create a vector of all {selection item, selection order} iterator pairs
|
|
|
|
std::vector<pairedIterators> pairs;
|
|
|
|
auto item = m_items.begin();
|
|
|
|
auto order = m_itemsOrders.begin();
|
|
|
|
|
|
|
|
for( ; item != m_items.end(); ++item, ++order )
|
|
|
|
pairs.emplace_back( make_pair( item, order ) );
|
|
|
|
|
|
|
|
// Sort the pairs by the selection order
|
|
|
|
std::sort( pairs.begin(), pairs.end(),
|
|
|
|
[]( pairedIterators const& a, pairedIterators const& b )
|
|
|
|
{
|
|
|
|
return *a.second < *b.second;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Make a vector of just the sortedItems
|
|
|
|
std::vector<EDA_ITEM*> sortedItems;
|
|
|
|
|
|
|
|
for( pairedIterators sortedItem : pairs )
|
|
|
|
sortedItems.emplace_back( *sortedItem.first );
|
|
|
|
|
|
|
|
return sortedItems;
|
|
|
|
}
|