2007-08-23 04:28:46 +00:00
|
|
|
/*
|
2011-09-30 18:15:37 +00:00
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2007-08-23 04:28:46 +00:00
|
|
|
*
|
2008-01-22 20:34:32 +00:00
|
|
|
* Copyright (C) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
|
2022-08-20 09:27:35 +00:00
|
|
|
* Copyright (C) 2004-2022 KiCad Developers, see change_log.txt for contributors.
|
2009-11-23 20:18:47 +00:00
|
|
|
*
|
2007-08-23 04:28:46 +00: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-23 20:18:47 +00:00
|
|
|
*
|
2007-08-23 04:28:46 +00: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-23 20:18:47 +00:00
|
|
|
*
|
2007-08-23 04:28:46 +00: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-23 20:18:47 +00: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 04:28:46 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COLLECTOR_H
|
|
|
|
#define COLLECTOR_H
|
|
|
|
|
|
|
|
#include <vector>
|
2021-10-01 10:52:34 +00:00
|
|
|
#include <core/kicad_algo.h>
|
2020-10-14 01:06:53 +00:00
|
|
|
#include <eda_item.h> // SEARCH_RESULT
|
2007-08-23 04:28:46 +00:00
|
|
|
|
|
|
|
|
2010-12-08 20:12:46 +00:00
|
|
|
class EDA_ITEM;
|
2007-08-23 04:28:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* An abstract class that will find and hold all the objects according to
|
2007-08-23 04:28:46 +00:00
|
|
|
* an inspection done by the Inspect() function which must be implemented by
|
2020-12-18 14:04:03 +00:00
|
|
|
* any derived class.
|
2007-08-23 04:28:46 +00:00
|
|
|
*
|
2020-12-18 14:04:03 +00:00
|
|
|
* When Inspect() finds an object that it wants to collect, i.e. one that it "likes", then
|
|
|
|
* it only has to do an Append( testItem )on it to add it to its collection, but in all cases
|
|
|
|
* for the scan to continue, Inspect() must return SEARCH_CONTINUE. Later, after collection,
|
|
|
|
* the user can iterate through all the objects in the remembered collection using GetCount()
|
|
|
|
* and the [int] operator.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2016-07-12 19:05:54 +00:00
|
|
|
class COLLECTOR
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-07-12 19:05:54 +00:00
|
|
|
COLLECTOR() :
|
2020-12-18 14:04:03 +00:00
|
|
|
m_Threshold( 0 ),
|
|
|
|
m_MenuCancelled( false ),
|
2024-06-01 02:30:23 +00:00
|
|
|
m_scanTypes( {} )
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2024-06-01 02:30:23 +00:00
|
|
|
// Inspect() is virtual so calling it from a class common inspector preserves
|
|
|
|
// polymorphism.
|
|
|
|
m_inspector = [this]( EDA_ITEM* aItem, void* aTestData )
|
|
|
|
{
|
|
|
|
return this->Inspect( aItem, aTestData );
|
|
|
|
};
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
Modular KiCad Blueprint Milestone B), major portions:
*) When kicad.exe closes a project, close any open KIFACEs so that they cannot
get disassociated from their true PROJECT.
*) Allow loading eeschema library editor from kicad.exe
*) Allow loading pcbnew library editor from kicad.exe
*) Rename LIB_COMPONENT to LIB_PART.
*) Add class PART_LIBS, and PART_LIB.
*) Make PART_LIBS non-global, i.e. PROJECT specific.
*) Implement "data on demand" for PART_LIBS
*) Implement "data on demand" for schematic SEARCH_STACK.
*) Use RSTRINGs to retain eeschema editor's notion of last library and part being edited.
*) Get rid of library search on every SCH_COMPONENT::Draw() call, instead use
a weak pointer.
*) Remove all chdir() calls so projects don't need to be CWD.
*) Romove APPEND support from OpenProjectFiles().
*) Make OpenProjectFiles() robust, even for creating new projects.
*) Load EESCHEMA colors in the KIWAY::OnKiwayStart() rather in window open,
and save them in the .eeschema config file, not in the project file.
*) Fix bug with wxDir() while accessing protected dirs in kicad.exe
*) Consolidate template copying into PROJECT class, not in kicad.exe source.
*) Generally untangle eeschema, making its libraries not global but rather
held in the PROJECT.
2014-08-13 20:28:54 +00:00
|
|
|
virtual ~COLLECTOR() {}
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2023-07-13 10:24:33 +00:00
|
|
|
virtual INSPECT_RESULT Inspect( EDA_ITEM* aTestItem, void* aTestData )
|
2019-12-28 00:55:11 +00:00
|
|
|
{
|
2022-07-30 00:00:39 +00:00
|
|
|
return INSPECT_RESULT::QUIT;
|
2019-12-28 00:55:11 +00:00
|
|
|
};
|
2016-07-12 19:05:54 +00:00
|
|
|
|
2018-10-24 04:20:44 +00:00
|
|
|
using ITER = std::vector<EDA_ITEM*>::iterator;
|
|
|
|
using CITER = std::vector<EDA_ITEM*>::const_iterator;
|
|
|
|
|
2020-10-02 20:25:14 +00:00
|
|
|
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(); }
|
2018-10-24 04:20:44 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Return the number of objects in the list.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2007-09-26 20:10:12 +00:00
|
|
|
int GetCount() const
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
return (int) m_list.size();
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Clear the list.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
|
|
|
void Empty()
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
m_list.clear();
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Add an item to the end of the list.
|
|
|
|
*
|
2010-12-08 20:12:46 +00:00
|
|
|
* @param item An EDA_ITEM* to add.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
void Append( EDA_ITEM* item )
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
m_list.push_back( item );
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 08:23:53 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Remove the item at \a aIndex (first position is 0).
|
|
|
|
*
|
2011-12-01 16:49:28 +00:00
|
|
|
* @param aIndex The index into the list.
|
2007-09-25 08:23:53 +00:00
|
|
|
*/
|
2011-12-01 16:49:28 +00:00
|
|
|
void Remove( int aIndex )
|
2007-09-25 08:23:53 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
m_list.erase( m_list.begin() + aIndex );
|
2007-09-25 08:23:53 +00:00
|
|
|
}
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2015-02-17 23:36:27 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Remove the item aItem (if exists in the collector).
|
|
|
|
*
|
2015-02-17 23:36:27 +00:00
|
|
|
* @param aItem the item to be removed.
|
|
|
|
*/
|
2015-02-18 16:53:46 +00:00
|
|
|
void Remove( const EDA_ITEM* aItem )
|
2015-02-17 23:36:27 +00:00
|
|
|
{
|
2021-10-01 10:52:34 +00:00
|
|
|
alg::delete_if( m_list, [&aItem]( const EDA_ITEM* aCandidate )
|
|
|
|
{
|
|
|
|
return aCandidate == aItem;
|
|
|
|
} );
|
2015-02-17 23:36:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 04:51:12 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Test if the collector has heuristic backup items.
|
|
|
|
*
|
|
|
|
* @return true if Combine() can run to bring secondary items into the list.
|
2020-07-07 04:51:12 +00:00
|
|
|
*/
|
|
|
|
bool HasAdditionalItems()
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
return !m_backupList.empty();
|
2020-07-07 04:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Re-combine the backup list into the main list of the collector.
|
2020-07-07 04:51:12 +00:00
|
|
|
*/
|
|
|
|
void Combine()
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
std::copy( m_backupList.begin(), m_backupList.end(), std::back_inserter( m_list ) );
|
|
|
|
m_backupList.clear();
|
2020-07-07 04:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Move the item at \a aIndex (first position is 0) to the backup list.
|
|
|
|
*
|
2020-07-07 04:51:12 +00:00
|
|
|
* @param aIndex The index into the list.
|
|
|
|
*/
|
|
|
|
void Transfer( int aIndex )
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
m_backupList.push_back( m_list[aIndex] );
|
|
|
|
m_list.erase( m_list.begin() + aIndex );
|
2020-07-07 04:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Move \a aItem (if exists in the collector) to the backup list.
|
|
|
|
*
|
2020-07-07 04:51:12 +00:00
|
|
|
* @param aItem the item to be moved.
|
|
|
|
*/
|
|
|
|
void Transfer( EDA_ITEM* aItem )
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
for( size_t i = 0; i < m_list.size(); i++ )
|
2020-07-07 04:51:12 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
if( m_list[i] == aItem )
|
2020-07-07 04:51:12 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
m_list.erase( m_list.begin() + i );
|
|
|
|
m_backupList.push_back( aItem );
|
2020-07-07 04:51:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Used for read only access and returns the object at \a aIndex.
|
|
|
|
*
|
2011-12-01 16:49:28 +00:00
|
|
|
* @param aIndex The index into the list.
|
2020-12-18 14:04:03 +00:00
|
|
|
* @return the object at \a aIndex something derived from it or NULL.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2019-04-21 23:45:34 +00:00
|
|
|
virtual EDA_ITEM* operator[]( int aIndex ) const
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2011-12-01 16:49:28 +00:00
|
|
|
if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
|
2020-10-02 20:25:14 +00:00
|
|
|
return m_list[ aIndex ];
|
2011-12-01 16:49:28 +00:00
|
|
|
|
2020-12-18 14:04:03 +00:00
|
|
|
return nullptr;
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:05 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Tests if \a aItem has already been collected.
|
2011-03-25 19:16:05 +00:00
|
|
|
*
|
|
|
|
* @param aItem The EDA_ITEM* to be tested.
|
|
|
|
* @return True if \a aItem is already collected.
|
|
|
|
*/
|
|
|
|
bool HasItem( const EDA_ITEM* aItem ) const
|
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
for( size_t i = 0; i < m_list.size(); i++ )
|
2011-03-25 19:16:05 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
if( m_list[i] == aItem )
|
2011-03-25 19:16:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-30 22:20:52 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Record the list of #KICAD_T types to consider for collection by the Inspect() function.
|
|
|
|
*
|
2022-08-20 09:27:35 +00:00
|
|
|
* @param aScanTypes A list of KICAD_Ts.
|
2007-08-30 22:20:52 +00:00
|
|
|
*/
|
2022-08-21 19:54:07 +00:00
|
|
|
void SetScanTypes( const std::vector<KICAD_T>& aTypes ) { m_scanTypes = aTypes; }
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2022-08-20 09:27:35 +00:00
|
|
|
void SetRefPos( const VECTOR2I& aRefPos ) { m_refPos = aRefPos; }
|
2007-08-30 22:20:52 +00:00
|
|
|
|
2015-02-17 23:36:27 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Count the number of items matching \a aType.
|
|
|
|
*
|
|
|
|
* @param aType type we are interested in.
|
|
|
|
* @return number of occurrences.
|
2015-02-17 23:36:27 +00:00
|
|
|
*/
|
|
|
|
int CountType( KICAD_T aType )
|
|
|
|
{
|
|
|
|
int cnt = 0;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2020-10-02 20:25:14 +00:00
|
|
|
for( size_t i = 0; i < m_list.size(); i++ )
|
2015-02-17 23:36:27 +00:00
|
|
|
{
|
2020-10-02 20:25:14 +00:00
|
|
|
if( m_list[i]->Type() == aType )
|
2015-02-17 23:36:27 +00:00
|
|
|
cnt++;
|
|
|
|
}
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2015-02-17 23:36:27 +00:00
|
|
|
return cnt;
|
|
|
|
}
|
2020-12-18 14:04:03 +00:00
|
|
|
|
|
|
|
int m_Threshold; // Hit-test threshold in internal units.
|
|
|
|
|
|
|
|
wxString m_MenuTitle; // The title of selection disambiguation menu (if needed)
|
|
|
|
bool m_MenuCancelled; // Indicates selection disambiguation menu was canceled
|
|
|
|
|
|
|
|
protected:
|
2022-08-21 19:54:07 +00:00
|
|
|
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
|
2022-08-20 09:27:35 +00:00
|
|
|
|
2022-08-21 19:54:07 +00:00
|
|
|
std::vector<KICAD_T> m_scanTypes;
|
|
|
|
INSPECTOR_FUNC m_inspector;
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2022-08-21 19:54:07 +00:00
|
|
|
VECTOR2I m_refPos; // Reference pos used to generate the collection.
|
2007-08-23 04:28:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // COLLECTOR_H
|