1 /* 2 * This program source code file is part of KiCad, a free EDA CAD application. 3 * 4 * Copyright (C) 2013-2017 CERN 5 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors. 6 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch> 7 * @author Maciej Suminski <maciej.suminski@cern.ch> 8 * 9 * This program is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU General Public License 11 * as published by the Free Software Foundation; either version 2 12 * of the License, or (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, you may find one here: 21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 22 * or you may search the http://www.gnu.org website for the version 2 license, 23 * or you may write to the Free Software Foundation, Inc., 24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 25 */ 26 27 #ifndef SELECTION_H 28 #define SELECTION_H 29 30 #include <core/optional.h> 31 #include <core/typeinfo.h> 32 #include <deque> 33 #include <eda_rect.h> 34 #include <view/view_group.h> 35 36 class EDA_ITEM; 37 38 class SELECTION : public KIGFX::VIEW_GROUP 39 { 40 public: SELECTION()41 SELECTION() : 42 KIGFX::VIEW_GROUP::VIEW_GROUP() 43 { 44 m_isHover = false; 45 } 46 SELECTION(const SELECTION & aOther)47 SELECTION( const SELECTION& aOther ) : 48 KIGFX::VIEW_GROUP::VIEW_GROUP() 49 { 50 m_items = aOther.m_items; 51 m_isHover = aOther.m_isHover; 52 } 53 54 SELECTION& operator= ( const SELECTION& aOther ) 55 { 56 m_items = aOther.m_items; 57 m_isHover = aOther.m_isHover; 58 return *this; 59 } 60 61 using ITER = std::deque<EDA_ITEM*>::iterator; 62 using CITER = std::deque<EDA_ITEM*>::const_iterator; 63 begin()64 ITER begin() { return m_items.begin(); } end()65 ITER end() { return m_items.end(); } begin()66 CITER begin() const { return m_items.cbegin(); } end()67 CITER end() const { return m_items.cend(); } 68 SetIsHover(bool aIsHover)69 void SetIsHover( bool aIsHover ) 70 { 71 m_isHover = aIsHover; 72 } 73 IsHover()74 bool IsHover() const 75 { 76 return m_isHover; 77 } 78 79 virtual void Add( EDA_ITEM* aItem ); 80 81 virtual void Remove( EDA_ITEM *aItem ); 82 Clear()83 virtual void Clear() override 84 { 85 m_items.clear(); 86 } 87 GetSize()88 virtual unsigned int GetSize() const override 89 { 90 return m_items.size(); 91 } 92 93 virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override; 94 95 bool Contains( EDA_ITEM* aItem ) const; 96 97 /// Checks if there is anything selected Empty()98 bool Empty() const 99 { 100 return m_items.empty(); 101 } 102 103 /// Returns the number of selected parts Size()104 int Size() const 105 { 106 return m_items.size(); 107 } 108 GetItems()109 const std::deque<EDA_ITEM*> GetItems() const 110 { 111 return m_items; 112 } 113 114 /// Returns the center point of the selection area bounding box. 115 virtual VECTOR2I GetCenter() const; 116 ViewBBox()117 virtual const BOX2I ViewBBox() const override 118 { 119 BOX2I r; 120 r.SetMaximum(); 121 return r; 122 } 123 124 /// Returns the top left point of the selection area bounding box. GetPosition()125 VECTOR2I GetPosition() const 126 { 127 return static_cast<VECTOR2I>( GetBoundingBox().GetPosition() ); 128 } 129 130 virtual EDA_RECT GetBoundingBox() const; 131 132 virtual EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const 133 { 134 return nullptr; 135 } 136 137 EDA_ITEM* operator[]( const size_t aIdx ) const 138 { 139 if( aIdx < m_items.size() ) 140 return m_items[ aIdx ]; 141 142 return nullptr; 143 } 144 Front()145 EDA_ITEM* Front() const 146 { 147 return m_items.size() ? m_items.front() : nullptr; 148 } 149 Items()150 std::deque<EDA_ITEM*>& Items() 151 { 152 return m_items; 153 } 154 155 template<class T> FirstOfKind()156 T* FirstOfKind() const 157 { 158 for( auto item : m_items ) 159 { 160 if( IsA<T, EDA_ITEM>( item ) ) 161 return static_cast<T*> ( item ); 162 } 163 164 return nullptr; 165 } 166 167 /** 168 * Checks if there is at least one item of requested kind. 169 * 170 * @param aType is the type to check for. 171 * @return True if there is at least one item of such kind. 172 */ 173 bool HasType( KICAD_T aType ) const; 174 175 size_t CountType( KICAD_T aType ) const; 176 177 virtual const std::vector<KIGFX::VIEW_ITEM*> updateDrawList() const override; 178 HasReferencePoint()179 bool HasReferencePoint() const 180 { 181 return m_referencePoint != NULLOPT; 182 } 183 GetReferencePoint()184 VECTOR2I GetReferencePoint() const 185 { 186 return *m_referencePoint; 187 } 188 SetReferencePoint(const VECTOR2I & aP)189 void SetReferencePoint( const VECTOR2I& aP ) 190 { 191 m_referencePoint = aP; 192 } 193 ClearReferencePoint()194 void ClearReferencePoint() 195 { 196 m_referencePoint = NULLOPT; 197 } 198 199 /** 200 * Checks if all items in the selection are the same KICAD_T type 201 * 202 * @return True if all items are the same type, this includes zero or single items 203 */ 204 bool AreAllItemsIdentical() const; 205 206 /** 207 * Checks if all items in the selection have a type in aList 208 * @return False if any item in the selection has a type not included in aList 209 */ 210 bool OnlyContains( std::vector<KICAD_T> aList ) const; 211 212 protected: 213 OPT<VECTOR2I> m_referencePoint; 214 std::deque<EDA_ITEM*> m_items; 215 bool m_isHover; 216 217 // mute hidden overloaded virtual function warnings 218 using VIEW_GROUP::Add; 219 using VIEW_GROUP::Remove; 220 }; 221 222 223 #endif 224