kicad/include/collector.h

299 lines
8.6 KiB
C
Raw Normal View History

2007-08-23 12:28:46 +08:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
2007-08-23 12:28:46 +08:00
*
2008-01-23 04:34:32 +08:00
* Copyright (C) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
2009-11-24 04:18:47 +08:00
*
2007-08-23 12:28:46 +08:00
* 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.
2009-11-24 04:18:47 +08:00
*
2007-08-23 12:28:46 +08:00
* 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.
2009-11-24 04:18:47 +08:00
*
2007-08-23 12:28:46 +08:00
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
2009-11-24 04:18:47 +08:00
* 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.,
2007-08-23 12:28:46 +08:00
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file collector.h
* @brief COLLECTOR class definition.
*/
2007-08-23 12:28:46 +08:00
#ifndef COLLECTOR_H
#define COLLECTOR_H
#include <vector>
#include <base_struct.h> // SEARCH_RESULT
#include <common.h> // GetNewTimeStamp()
2018-01-29 18:37:29 +08:00
#include <eda_rect.h>
2007-08-23 12:28:46 +08:00
class EDA_ITEM;
2007-08-23 12:28:46 +08:00
/**
* COLLECTOR
2009-11-24 04:18:47 +08:00
* is an abstract class that will find and hold all the objects according to
2007-08-23 12:28:46 +08:00
* an inspection done by the Inspect() function which must be implemented by
* any derived class. When Inspect() finds an object that it wants to collect,
2009-11-24 04:18:47 +08:00
* i.e. one that it "likes", then it only has to do an Append( testItem )
2007-08-23 12:28:46 +08:00
* on it to add it to its collection, but in all cases for the scan to continue,
* Inspect() must return SEARCH_CONTINUE.
*
2009-11-24 04:18:47 +08:00
* Later, after collection, the user can iterate through all the objects
2007-08-23 12:28:46 +08:00
* in the remembered collection using GetCount() and the [int] operator.
*/
class COLLECTOR
2007-08-23 12:28:46 +08:00
{
protected:
std::vector<EDA_ITEM*> m_List; // Primary list of most likely items
std::vector<EDA_ITEM*> m_BackupList; // Secondary list with items removed by heuristics
const KICAD_T* m_ScanTypes;
INSPECTOR_FUNC m_inspector;
wxPoint m_RefPos; // Reference position used to generate the collection.
EDA_RECT m_RefBox; // Selection rectangle used to generate the collection.
2007-08-23 12:28:46 +08:00
public:
int m_Threshold; // Hit-test threshold in internal units.
2009-11-24 04:18:47 +08:00
wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
bool m_MenuCancelled; // Indicates selection disambiguation menu was cancelled
2009-11-24 04:18:47 +08:00
2007-08-23 12:28:46 +08:00
public:
COLLECTOR() :
m_ScanTypes( 0 ),
// Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } ),
m_Threshold( 0 ),
m_MenuCancelled( false )
2007-08-23 12:28:46 +08:00
{
}
virtual ~COLLECTOR() {}
2009-11-24 04:18:47 +08:00
2019-12-28 08:55:11 +08:00
virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData )
{
return SEARCH_RESULT::QUIT;
};
using ITER = std::vector<EDA_ITEM*>::iterator;
using CITER = std::vector<EDA_ITEM*>::const_iterator;
ITER begin() { return m_List.begin(); }
ITER end() { return m_List.end(); }
CITER begin() const { return m_List.cbegin(); }
CITER end() const { return m_List.cend(); }
2007-08-23 12:28:46 +08:00
/**
* Function GetCount
* returns the number of objects in the list
*/
2007-09-27 04:10:12 +08:00
int GetCount() const
2007-08-23 12:28:46 +08:00
{
2007-09-27 04:10:12 +08:00
return (int) m_List.size();
2007-08-23 12:28:46 +08:00
}
/**
* Function Empty
* sets the list to empty
*/
void Empty()
{
2007-08-31 06:20:52 +08:00
m_List.clear();
2007-08-23 12:28:46 +08:00
}
/**
* Function Append
* adds an item to the end of the list.
* @param item An EDA_ITEM* to add.
2007-08-23 12:28:46 +08:00
*/
void Append( EDA_ITEM* item )
2007-08-23 12:28:46 +08:00
{
2007-08-31 06:20:52 +08:00
m_List.push_back( item );
2007-08-23 12:28:46 +08:00
}
/**
* Function Remove
* removes the item at \a aIndex (first position is 0);
* @param aIndex The index into the list.
*/
void Remove( int aIndex )
{
m_List.erase( m_List.begin() + aIndex );
}
2007-08-23 12:28:46 +08:00
/**
* Function Remove
* removes the item aItem (if exists in the collector).
* @param aItem the item to be removed.
*/
2015-02-19 00:53:46 +08:00
void Remove( const EDA_ITEM* aItem )
{
for( size_t i = 0; i < m_List.size(); i++ )
{
if( m_List[i] == aItem )
{
m_List.erase( m_List.begin() + i);
return;
}
}
}
/**
* Test if the collector has heuristic backup items
* @return true if Combine() can run to bring secondary items into the list
*/
bool HasAdditionalItems()
{
return !m_BackupList.empty();
}
/**
* Re-combines the backup list into the main list of the collector
*/
void Combine()
{
std::copy( m_BackupList.begin(), m_BackupList.end(), std::back_inserter( m_List ) );
m_BackupList.clear();
}
/**
* Moves the item at \a aIndex (first position is 0) to the backup list
* @param aIndex The index into the list.
*/
void Transfer( int aIndex )
{
m_BackupList.push_back( m_List[aIndex] );
m_List.erase( m_List.begin() + aIndex );
}
/**
* Moves the item aItem (if exists in the collector) to the backup list
* @param aItem the item to be moved.
*/
void Transfer( EDA_ITEM* aItem )
{
for( size_t i = 0; i < m_List.size(); i++ )
{
if( m_List[i] == aItem )
{
m_List.erase( m_List.begin() + i );
m_BackupList.push_back( aItem );
return;
}
}
}
2007-08-23 12:28:46 +08:00
/**
* Function operator[int]
* is used for read only access and returns the object at \a aIndex.
* @param aIndex The index into the list.
* @return EDA_ITEM* - or something derived from it, or NULL.
2007-08-23 12:28:46 +08:00
*/
virtual EDA_ITEM* operator[]( int aIndex ) const
2007-08-23 12:28:46 +08:00
{
if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
return m_List[ aIndex ];
2007-08-23 12:28:46 +08:00
return NULL;
}
2008-01-25 23:13:57 +08:00
/**
* Function BasePtr
* returns the address of the first element in the array. Only call this
* if there is at least one element in the vector m_List, otherwise a
* C++ exception should get thrown.
*/
EDA_ITEM* const* BasePtr() const
2008-01-25 23:13:57 +08:00
{
return &m_List[0];
}
/**
* Function HasItem
* tests if \a aItem has already been collected.
*
* @param aItem The EDA_ITEM* to be tested.
* @return True if \a aItem is already collected.
*/
bool HasItem( const EDA_ITEM* aItem ) const
{
for( size_t i = 0; i < m_List.size(); i++ )
{
if( m_List[i] == aItem )
return true;
}
return false;
}
2009-11-24 04:18:47 +08:00
2007-08-31 06:20:52 +08:00
/**
* Function SetScanTypes
* records the list of KICAD_T types to consider for collection by
* the Inspect() function.
* @param scanTypes An array of KICAD_T, terminated by EOT. No copy is
* is made of this array (so cannot come from caller's stack).
2007-08-31 06:20:52 +08:00
*/
2007-08-23 12:28:46 +08:00
void SetScanTypes( const KICAD_T* scanTypes )
{
m_ScanTypes = scanTypes;
}
2009-11-24 04:18:47 +08:00
2007-08-31 06:20:52 +08:00
void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
2009-11-24 04:18:47 +08:00
/**
* Function CountType
* counts the number of items matching aType
* @param aType type we are interested in
* @return number of occurences
*/
int CountType( KICAD_T aType )
{
int cnt = 0;
for( size_t i = 0; i < m_List.size(); i++ )
{
if( m_List[i]->Type() == aType )
cnt++;
}
return cnt;
}
2007-08-23 12:28:46 +08:00
/**
2007-08-31 06:20:52 +08:00
* Function Collect
* scans an EDA_ITEM using this class's Inspector method, which does
2007-08-31 06:20:52 +08:00
* the collection.
* @param container An EDA_ITEM to scan, including those items it contains.
2007-08-31 06:20:52 +08:00
* @param aRefPos A wxPoint to use in hit-testing.
2007-08-23 12:28:46 +08:00
*
* example implementation, in derived class:
*
void Collect( EDA_ITEM* container, const wxPoint& aRefPos )
2007-08-23 12:28:46 +08:00
{
example implementation:
2009-11-24 04:18:47 +08:00
2007-08-31 06:20:52 +08:00
SetRefPos( aRefPos ); // remember where the snapshot was taken from
2009-11-24 04:18:47 +08:00
2007-08-23 12:28:46 +08:00
Empty(); // empty the collection
2009-11-24 04:18:47 +08:00
2007-08-23 12:28:46 +08:00
// visit the board with the INSPECTOR (me).
container->Visit( inspector, &aRefPos,
2007-08-31 06:20:52 +08:00
m_ScanTypes);
SetTimeNow(); // when it was taken
2007-08-23 12:28:46 +08:00
}
*/
};
#endif // COLLECTOR_H