2017-02-28 03:04:44 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2017 CERN
|
2022-08-30 23:28:18 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-02-28 03:04:44 +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
|
|
|
|
*/
|
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
#ifndef SELECTION_H
|
|
|
|
#define SELECTION_H
|
2017-02-28 03:04:44 +00:00
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
#include <optional>
|
2021-06-06 15:07:55 +00:00
|
|
|
#include <core/typeinfo.h>
|
2018-10-24 02:14:11 +00:00
|
|
|
#include <deque>
|
2022-02-10 18:38:40 +00:00
|
|
|
#include <eda_item.h>
|
2017-02-28 03:04:44 +00:00
|
|
|
#include <view/view_group.h>
|
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
|
2017-02-28 03:04:44 +00:00
|
|
|
class SELECTION : public KIGFX::VIEW_GROUP
|
|
|
|
{
|
|
|
|
public:
|
2020-12-25 23:37:17 +00:00
|
|
|
SELECTION() :
|
|
|
|
KIGFX::VIEW_GROUP::VIEW_GROUP()
|
2018-03-20 22:44:04 +00:00
|
|
|
{
|
|
|
|
m_isHover = false;
|
2022-07-19 15:00:35 +00:00
|
|
|
m_lastAddedItem = nullptr;
|
2022-09-07 16:16:02 +00:00
|
|
|
m_orderCounter = 0;
|
2018-03-20 22:44:04 +00:00
|
|
|
}
|
2017-03-03 12:42:28 +00:00
|
|
|
|
2020-12-25 23:37:17 +00:00
|
|
|
SELECTION( const SELECTION& aOther ) :
|
|
|
|
KIGFX::VIEW_GROUP::VIEW_GROUP()
|
2017-03-03 12:42:28 +00:00
|
|
|
{
|
|
|
|
m_items = aOther.m_items;
|
2022-09-07 16:16:02 +00:00
|
|
|
m_itemsOrders = aOther.m_itemsOrders;
|
2017-03-03 12:42:28 +00:00
|
|
|
m_isHover = aOther.m_isHover;
|
2022-07-19 15:00:35 +00:00
|
|
|
m_lastAddedItem = aOther.m_lastAddedItem;
|
2022-09-07 16:16:02 +00:00
|
|
|
m_orderCounter = aOther.m_orderCounter;
|
2017-03-03 12:42:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 10:11:18 +00:00
|
|
|
SELECTION& operator= ( const SELECTION& aOther )
|
2017-03-03 12:42:28 +00:00
|
|
|
{
|
|
|
|
m_items = aOther.m_items;
|
2022-09-07 16:16:02 +00:00
|
|
|
m_itemsOrders = aOther.m_itemsOrders;
|
2017-03-03 12:42:28 +00:00
|
|
|
m_isHover = aOther.m_isHover;
|
2022-07-19 15:00:35 +00:00
|
|
|
m_lastAddedItem = aOther.m_lastAddedItem;
|
2022-09-07 16:16:02 +00:00
|
|
|
m_orderCounter = aOther.m_orderCounter;
|
2017-03-03 12:42:28 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:14:11 +00:00
|
|
|
using ITER = std::deque<EDA_ITEM*>::iterator;
|
|
|
|
using CITER = std::deque<EDA_ITEM*>::const_iterator;
|
2017-02-28 03:04:44 +00:00
|
|
|
|
|
|
|
ITER begin() { return m_items.begin(); }
|
|
|
|
ITER end() { return m_items.end(); }
|
|
|
|
CITER begin() const { return m_items.cbegin(); }
|
|
|
|
CITER end() const { return m_items.cend(); }
|
|
|
|
|
2017-03-03 12:42:28 +00:00
|
|
|
void SetIsHover( bool aIsHover )
|
|
|
|
{
|
|
|
|
m_isHover = aIsHover;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsHover() const
|
|
|
|
{
|
|
|
|
return m_isHover;
|
|
|
|
}
|
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
virtual void Add( EDA_ITEM* aItem );
|
2017-02-28 03:04:44 +00:00
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
virtual void Remove( EDA_ITEM *aItem );
|
2017-02-28 03:04:44 +00:00
|
|
|
|
|
|
|
virtual void Clear() override
|
|
|
|
{
|
|
|
|
m_items.clear();
|
2022-09-07 16:16:02 +00:00
|
|
|
m_itemsOrders.clear();
|
|
|
|
m_orderCounter = 0;
|
2017-02-28 03:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned int GetSize() const override
|
|
|
|
{
|
|
|
|
return m_items.size();
|
|
|
|
}
|
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override;
|
2018-10-24 02:14:11 +00:00
|
|
|
|
2021-06-06 15:07:55 +00:00
|
|
|
bool Contains( EDA_ITEM* aItem ) const;
|
2017-02-28 03:04:44 +00:00
|
|
|
|
|
|
|
/// Checks if there is anything selected
|
|
|
|
bool Empty() const
|
|
|
|
{
|
2019-06-08 21:48:22 +00:00
|
|
|
return m_items.empty();
|
2017-02-28 03:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of selected parts
|
|
|
|
int Size() const
|
|
|
|
{
|
|
|
|
return m_items.size();
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:14:11 +00:00
|
|
|
const std::deque<EDA_ITEM*> GetItems() const
|
2017-02-28 03:04:44 +00:00
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
2020-01-06 19:55:39 +00:00
|
|
|
|
2022-07-19 15:00:35 +00:00
|
|
|
EDA_ITEM* GetLastAddedItem() const
|
|
|
|
{
|
|
|
|
return m_lastAddedItem;
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:38:40 +00:00
|
|
|
/**
|
|
|
|
* Returns a copy of this selection of items sorted by their X then Y position.
|
|
|
|
*
|
|
|
|
* @return Vector of sorted items
|
|
|
|
*/
|
2022-02-14 22:52:34 +00:00
|
|
|
const std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
|
|
|
|
bool topBeforeBottom = true ) const
|
2022-02-10 18:38:40 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(),
|
|
|
|
m_items.end() );
|
2022-02-10 18:38:40 +00:00
|
|
|
|
2022-08-30 23:28:18 +00:00
|
|
|
std::sort( sorted_items.begin(), sorted_items.end(),
|
|
|
|
[&]( EDA_ITEM* a, EDA_ITEM* b )
|
2022-02-10 18:38:40 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
if( a->Type() == b->Type() )
|
|
|
|
{
|
|
|
|
if( a->GetSortPosition().x == b->GetSortPosition().x )
|
|
|
|
{
|
|
|
|
// Ensure deterministic sort
|
|
|
|
if( a->GetSortPosition().y == b->GetSortPosition().y )
|
|
|
|
return a->m_Uuid < b->m_Uuid;
|
|
|
|
|
|
|
|
if( topBeforeBottom )
|
|
|
|
return a->GetSortPosition().y < b->GetSortPosition().y;
|
|
|
|
else
|
|
|
|
return a->GetSortPosition().y > b->GetSortPosition().y;
|
|
|
|
}
|
|
|
|
else if( leftBeforeRight )
|
2022-10-12 14:14:41 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
return a->GetSortPosition().x < b->GetSortPosition().x;
|
2022-10-12 14:14:41 +00:00
|
|
|
}
|
2022-08-30 23:28:18 +00:00
|
|
|
else
|
2022-10-12 14:14:41 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
return a->GetSortPosition().x > b->GetSortPosition().x;
|
2022-10-12 14:14:41 +00:00
|
|
|
}
|
2022-08-30 23:28:18 +00:00
|
|
|
}
|
2022-02-14 22:52:34 +00:00
|
|
|
else
|
2022-10-12 14:14:41 +00:00
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
return a->Type() < b->Type();
|
2022-10-12 14:14:41 +00:00
|
|
|
}
|
2022-08-30 23:28:18 +00:00
|
|
|
} );
|
2022-02-10 18:38:40 +00:00
|
|
|
|
|
|
|
return sorted_items;
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:16:02 +00:00
|
|
|
const std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
|
|
|
|
|
2017-02-28 03:04:44 +00:00
|
|
|
/// Returns the center point of the selection area bounding box.
|
2021-06-06 15:07:55 +00:00
|
|
|
virtual VECTOR2I GetCenter() const;
|
2017-02-28 03:04:44 +00:00
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
virtual const BOX2I ViewBBox() const override
|
|
|
|
{
|
|
|
|
BOX2I r;
|
|
|
|
r.SetMaximum();
|
|
|
|
return r;
|
|
|
|
}
|
2017-03-02 23:42:23 +00:00
|
|
|
|
2017-04-22 20:07:29 +00:00
|
|
|
/// Returns the top left point of the selection area bounding box.
|
2019-06-08 21:48:22 +00:00
|
|
|
VECTOR2I GetPosition() const
|
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
return GetBoundingBox().GetPosition();
|
2019-06-08 21:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 23:28:18 +00:00
|
|
|
virtual BOX2I GetBoundingBox() const;
|
2020-01-06 19:55:39 +00:00
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-04-22 20:07:29 +00:00
|
|
|
|
2018-10-24 02:14:11 +00:00
|
|
|
EDA_ITEM* operator[]( const size_t aIdx ) const
|
2017-02-28 03:04:44 +00:00
|
|
|
{
|
2018-10-24 02:14:11 +00:00
|
|
|
if( aIdx < m_items.size() )
|
|
|
|
return m_items[ aIdx ];
|
2017-02-28 03:04:44 +00:00
|
|
|
|
2018-10-24 02:14:11 +00:00
|
|
|
return nullptr;
|
2017-02-28 03:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EDA_ITEM* Front() const
|
|
|
|
{
|
2019-05-02 10:59:36 +00:00
|
|
|
return m_items.size() ? m_items.front() : nullptr;
|
2017-02-28 03:04:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:14:11 +00:00
|
|
|
std::deque<EDA_ITEM*>& Items()
|
2017-02-28 03:04:44 +00:00
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:43:19 +00:00
|
|
|
const std::deque<EDA_ITEM*>& Items() const
|
|
|
|
{
|
|
|
|
return m_items;
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:42:28 +00:00
|
|
|
template<class T>
|
|
|
|
T* FirstOfKind() const
|
|
|
|
{
|
|
|
|
for( auto item : m_items )
|
|
|
|
{
|
2021-06-06 16:26:12 +00:00
|
|
|
if( IsA<T, EDA_ITEM>( item ) )
|
2017-03-03 12:42:28 +00:00
|
|
|
return static_cast<T*> ( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-12 09:17:35 +00:00
|
|
|
/**
|
|
|
|
* Checks if there is at least one item of requested kind.
|
|
|
|
*
|
|
|
|
* @param aType is the type to check for.
|
|
|
|
* @return True if there is at least one item of such kind.
|
|
|
|
*/
|
2021-06-06 15:07:55 +00:00
|
|
|
bool HasType( KICAD_T aType ) const;
|
2018-02-12 09:17:35 +00:00
|
|
|
|
2021-12-08 13:07:18 +00:00
|
|
|
size_t CountType( KICAD_T aType ) const;
|
|
|
|
|
2021-10-03 12:58:56 +00:00
|
|
|
virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override;
|
2017-02-28 03:04:44 +00:00
|
|
|
|
2017-09-22 15:17:38 +00:00
|
|
|
bool HasReferencePoint() const
|
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
return m_referencePoint != std::nullopt;
|
2017-09-22 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VECTOR2I GetReferencePoint() const
|
|
|
|
{
|
2022-02-04 01:08:19 +00:00
|
|
|
if( m_referencePoint )
|
|
|
|
return *m_referencePoint;
|
|
|
|
else
|
|
|
|
return GetBoundingBox().Centre();
|
2017-09-22 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetReferencePoint( const VECTOR2I& aP )
|
|
|
|
{
|
|
|
|
m_referencePoint = aP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearReferencePoint()
|
|
|
|
{
|
2022-08-25 22:50:47 +00:00
|
|
|
m_referencePoint = std::nullopt;
|
2017-09-22 15:17:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 04:05:51 +00:00
|
|
|
/**
|
|
|
|
* Checks if all items in the selection are the same KICAD_T type
|
|
|
|
*
|
|
|
|
* @return True if all items are the same type, this includes zero or single items
|
|
|
|
*/
|
2021-06-06 15:07:55 +00:00
|
|
|
bool AreAllItemsIdentical() const;
|
2019-12-31 04:05:51 +00:00
|
|
|
|
2021-11-14 16:54:04 +00:00
|
|
|
/**
|
|
|
|
* Checks if all items in the selection have a type in aList
|
|
|
|
* @return False if any item in the selection has a type not included in aList
|
|
|
|
*/
|
|
|
|
bool OnlyContains( std::vector<KICAD_T> aList ) const;
|
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
protected:
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<VECTOR2I> m_referencePoint;
|
2018-10-24 02:14:11 +00:00
|
|
|
std::deque<EDA_ITEM*> m_items;
|
2022-09-07 16:16:02 +00:00
|
|
|
std::deque<int> m_itemsOrders;
|
|
|
|
int m_orderCounter;
|
2022-07-19 15:00:35 +00:00
|
|
|
EDA_ITEM* m_lastAddedItem;
|
2019-06-08 21:48:22 +00:00
|
|
|
bool m_isHover;
|
2017-03-03 12:42:28 +00:00
|
|
|
|
2017-02-28 03:04:44 +00:00
|
|
|
// mute hidden overloaded virtual function warnings
|
|
|
|
using VIEW_GROUP::Add;
|
|
|
|
using VIEW_GROUP::Remove;
|
|
|
|
};
|
|
|
|
|
2017-03-03 12:42:28 +00:00
|
|
|
|
2017-02-28 03:04:44 +00:00
|
|
|
#endif
|