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>
|
2011-09-30 18:15:37 +00:00
|
|
|
* Copyright (C) 2004-2007 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
|
|
|
|
*/
|
|
|
|
|
2011-12-01 16:49:28 +00:00
|
|
|
/**
|
|
|
|
* @file class_collector.h
|
|
|
|
* @brief COLLECTOR class definition.
|
|
|
|
*/
|
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
#ifndef COLLECTOR_H
|
|
|
|
#define COLLECTOR_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <base_struct.h> // SEARCH_RESULT
|
|
|
|
#include <common.h> // GetNewTimeStamp()
|
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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class COLLECTOR
|
2009-11-23 20:18:47 +00:00
|
|
|
* is 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
|
|
|
|
* any derived class. When Inspect() finds an object that it wants to collect,
|
2009-11-23 20:18:47 +00:00
|
|
|
* i.e. one that it "likes", then it only has to do an Append( testItem )
|
2007-08-23 04:28:46 +00: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-23 20:18:47 +00:00
|
|
|
* Later, after collection, the user can iterate through all the objects
|
2007-08-23 04:28:46 +00:00
|
|
|
* in the remembered collection using GetCount() and the [int] operator.
|
|
|
|
*/
|
|
|
|
class COLLECTOR : public INSPECTOR
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// Which object types to scan
|
2011-12-01 16:49:28 +00:00
|
|
|
const KICAD_T* m_ScanTypes;
|
2007-08-23 04:28:46 +00:00
|
|
|
|
|
|
|
/// A place to hold collected objects without taking ownership of their memory.
|
2011-12-01 16:49:28 +00:00
|
|
|
std::vector<EDA_ITEM*> m_List;
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2011-12-01 16:49:28 +00:00
|
|
|
/// A point to test against, and that was used to make the collection.
|
|
|
|
wxPoint m_RefPos;
|
2007-08-23 04:28:46 +00:00
|
|
|
|
2007-08-30 22:20:52 +00:00
|
|
|
/// A bounding box to test against, and that was used to make the collection.
|
2011-12-01 16:49:28 +00:00
|
|
|
EDA_RECT m_RefBox;
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/// The time at which the collection was made.
|
2012-05-25 01:52:04 +00:00
|
|
|
time_t m_TimeAtCollection;
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
public:
|
|
|
|
COLLECTOR()
|
|
|
|
{
|
2011-12-01 16:49:28 +00:00
|
|
|
m_ScanTypes = 0;
|
2015-03-14 11:50:39 +00:00
|
|
|
m_TimeAtCollection = 0;
|
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
|
|
|
|
2011-12-01 16:49:28 +00:00
|
|
|
/**
|
|
|
|
* Function IsValidIndex
|
|
|
|
* tests if \a aIndex is with the limits of the list of collected items.
|
|
|
|
*
|
|
|
|
* @param aIndex The index to test.
|
|
|
|
* @return True if \a aIndex is with the limits of the list of collected items,
|
|
|
|
* otherwise false.
|
|
|
|
*/
|
|
|
|
bool IsValidIndex( int aIndex )
|
|
|
|
{
|
|
|
|
return ( (unsigned) aIndex < m_List.size() );
|
|
|
|
}
|
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/**
|
|
|
|
* Function GetCount
|
|
|
|
* returns the number of objects in the list
|
|
|
|
*/
|
2007-09-26 20:10:12 +00:00
|
|
|
int GetCount() const
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
2007-09-26 20:10:12 +00:00
|
|
|
return (int) m_List.size();
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Empty
|
|
|
|
* sets the list to empty
|
|
|
|
*/
|
|
|
|
void Empty()
|
|
|
|
{
|
2007-08-30 22:20:52 +00:00
|
|
|
m_List.clear();
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function Append
|
|
|
|
* adds 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
|
|
|
{
|
2007-08-30 22:20:52 +00:00
|
|
|
m_List.push_back( item );
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
|
2007-09-25 08:23:53 +00:00
|
|
|
/**
|
|
|
|
* Function Remove
|
2011-12-01 16:49:28 +00:00
|
|
|
* removes the item at \a aIndex (first position is 0);
|
|
|
|
* @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
|
|
|
{
|
2011-12-01 16:49:28 +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
|
|
|
/**
|
|
|
|
* Function Remove
|
|
|
|
* removes the item aItem (if exists in the collector).
|
|
|
|
* @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
|
|
|
{
|
|
|
|
for( size_t i = 0; i < m_List.size(); i++ )
|
|
|
|
{
|
|
|
|
if( m_List[i] == aItem )
|
|
|
|
{
|
|
|
|
m_List.erase( m_List.begin() + i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/**
|
|
|
|
* Function operator[int]
|
2011-12-01 16:49:28 +00:00
|
|
|
* is used for read only access and returns the object at \a aIndex.
|
|
|
|
* @param aIndex The index into the list.
|
2010-12-08 20:12:46 +00:00
|
|
|
* @return EDA_ITEM* - or something derived from it, or NULL.
|
2007-08-23 04:28:46 +00:00
|
|
|
*/
|
2011-12-01 16:49:28 +00:00
|
|
|
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
|
|
|
|
return m_List[ aIndex ];
|
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-25 15:13:57 +00: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.
|
|
|
|
*/
|
2010-12-08 20:12:46 +00:00
|
|
|
EDA_ITEM* const* BasePtr() const
|
2008-01-25 15:13:57 +00:00
|
|
|
{
|
|
|
|
return &m_List[0];
|
|
|
|
}
|
|
|
|
|
2011-03-25 19:16:05 +00:00
|
|
|
/**
|
|
|
|
* 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-23 20:18:47 +00:00
|
|
|
|
2007-08-30 22:20:52 +00: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
|
2011-12-01 16:49:28 +00:00
|
|
|
* is made of this array (so cannot come from caller's stack).
|
2007-08-30 22:20:52 +00:00
|
|
|
*/
|
2007-08-23 04:28:46 +00:00
|
|
|
void SetScanTypes( const KICAD_T* scanTypes )
|
|
|
|
{
|
|
|
|
m_ScanTypes = scanTypes;
|
|
|
|
}
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
void SetTimeNow()
|
|
|
|
{
|
2011-12-07 15:49:32 +00:00
|
|
|
m_TimeAtCollection = GetNewTimeStamp();
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
2011-03-25 19:16:05 +00:00
|
|
|
|
2012-05-25 01:52:04 +00:00
|
|
|
time_t GetTime()
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
|
|
|
return m_TimeAtCollection;
|
|
|
|
}
|
|
|
|
|
2007-08-30 22:20:52 +00:00
|
|
|
void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
|
|
|
|
const wxPoint& GetRefPos() const { return m_RefPos; }
|
|
|
|
|
2011-03-29 19:33:07 +00:00
|
|
|
void SetBoundingBox( const EDA_RECT& aRefBox ) { m_RefBox = aRefBox; }
|
|
|
|
const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
/**
|
|
|
|
* Function IsSimilarPointAndTime
|
2009-11-23 20:18:47 +00:00
|
|
|
* returns true if the given reference point is "similar" (defined here)
|
|
|
|
* to the internal reference point and the current time is within a few
|
2007-08-23 04:28:46 +00:00
|
|
|
* seconds of the internal m_TimeAtCollection.
|
|
|
|
*
|
|
|
|
* @param aRefPos A wxPoint to compare to.
|
|
|
|
* @return bool - true if the point and time are similar, else false.
|
|
|
|
*/
|
|
|
|
bool IsSimilarPointAndTime( const wxPoint& aRefPos )
|
|
|
|
{
|
|
|
|
const int distMax = 2; // adjust these here
|
2012-05-25 01:52:04 +00:00
|
|
|
const time_t timeMax = 3; // seconds
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
int dx = abs( aRefPos.x - m_RefPos.x );
|
|
|
|
int dy = abs( aRefPos.y - m_RefPos.y );
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2011-12-07 15:49:32 +00:00
|
|
|
if( dx <= distMax && dy <= distMax &&
|
|
|
|
(int)GetNewTimeStamp() - m_TimeAtCollection <= timeMax )
|
2007-08-23 04:28:46 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-17 23:36:27 +00: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 04:28:46 +00:00
|
|
|
/**
|
2007-08-30 22:20:52 +00:00
|
|
|
* Function Collect
|
2010-12-08 20:12:46 +00:00
|
|
|
* scans an EDA_ITEM using this class's Inspector method, which does
|
2007-08-30 22:20:52 +00:00
|
|
|
* the collection.
|
2010-12-08 20:12:46 +00:00
|
|
|
* @param container An EDA_ITEM to scan, including those items it contains.
|
2007-08-30 22:20:52 +00:00
|
|
|
* @param aRefPos A wxPoint to use in hit-testing.
|
2007-08-23 04:28:46 +00:00
|
|
|
*
|
|
|
|
* example implementation, in derived class:
|
|
|
|
*
|
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
|
|
|
void Collect( EDA_ITEM* container, const wxPoint& aRefPos )
|
2007-08-23 04:28:46 +00:00
|
|
|
{
|
|
|
|
example implementation:
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-30 22:20:52 +00:00
|
|
|
SetRefPos( aRefPos ); // remember where the snapshot was taken from
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
Empty(); // empty the collection
|
2009-11-23 20:18:47 +00:00
|
|
|
|
2007-08-23 04:28:46 +00:00
|
|
|
// visit the board with the INSPECTOR (me).
|
2007-08-30 22:20:52 +00:00
|
|
|
container->Visit( this, // INSPECTOR* inspector
|
2009-11-23 20:18:47 +00:00
|
|
|
NULL, // const void* testData,
|
2007-08-30 22:20:52 +00:00
|
|
|
m_ScanTypes);
|
|
|
|
SetTimeNow(); // when it was taken
|
2007-08-23 04:28:46 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // COLLECTOR_H
|