← Back to team overview

kicad-developers team mailing list archive

[patch 1/1] kicad-py-find-item-by-filename.patch

 

new python binding:

kicad.PrjItem.GetId()
kicad.TreeWindow.FindItemData( filename )

---
kicad/kicad.cpp | 2 +
kicad/kicad.h | 3 ++
kicad/treeprj.cpp | 72 
++++++++++++++++++++++++++++++++++++++++++++++++------
3 files changed, 70 insertions(+), 7 deletions(-)

--------------030305020401070101010101 Content-Type: text/x-patch;
name="kicad-py-find-item-by-filename.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="kicad-py-find-item-by-filename.patch"

python:

PrjItem.GetId()
TreeWindow.FindItemData( filename )

---
kicad/kicad.cpp | 2 +
kicad/kicad.h | 3 ++
kicad/treeprj.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++------
3 files changed, 70 insertions(+), 7 deletions(-)

Index: kicad-dev/kicad/kicad.cpp
===================================================================
--- kicad-dev.orig/kicad/kicad.cpp	2007-05-04 00:13:04.000000000 +0200
+++ kicad-dev/kicad/kicad.cpp	2007-05-04 00:48:39.000000000 +0200
@@ -125,6 +125,7 @@
.def( "GetFileName", &TreePrjItemData::GetFileNamePy )
.def( "GetDir",	&TreePrjItemData::GetDirPy )
.def( "GetType",	&TreePrjItemData::GetType )
+	.def( "GetId",	&TreePrjItemData::GetIdPy )
// Item control
.def( "Rename", &TreePrjItemData::RenamePy )
.def( "Move",	&TreePrjItemData::Move )
@@ -159,6 +160,7 @@
// Project tree control
.def( "GetTreeCtrl",	&WinEDA_PrjFrame::GetTreeCtrl )
.def( "GetItemData", &WinEDA_PrjFrame::GetItemData, return_value_policy < reference_existing_object >() )
+ .def( "FindItemData", &WinEDA_PrjFrame::FindItemData, return_value_policy < reference_existing_object >() )
.def( "NewFile", &WinEDA_PrjFrame::NewFilePy )
.def( "AddFile",	&WinEDA_PrjFrame::AddFilePy )
;
Index: kicad-dev/kicad/kicad.h
===================================================================
--- kicad-dev.orig/kicad/kicad.h	2007-05-03 21:21:16.000000000 +0200
+++ kicad-dev/kicad/kicad.h	2007-05-04 00:30:56.000000000 +0200
@@ -145,6 +145,7 @@
boost::python::object GetFileNamePy() const;
bool RenamePy( const boost::python::str & newname, bool check = true );
boost::python::object GetDirPy() const;
+	boost::python::object GetIdPy() const;
#endif
};

@@ -220,6 +221,8 @@
void AddFilePy( const boost::python::str & name, boost::python::object & root );
void NewFilePy( const boost::python::str & name, enum TreeFileType type, boost::python::object & root );

+	TreePrjItemData * FindItemData( const boost::python::str & name );
+
#endif

void AddFile( const wxString & name, wxTreeItemId & root );
Index: kicad-dev/kicad/treeprj.cpp
===================================================================
--- kicad-dev.orig/kicad/treeprj.cpp	2007-05-04 00:54:31.000000000 +0200
+++ kicad-dev/kicad/treeprj.cpp	2007-05-04 01:28:41.000000000 +0200
@@ -44,6 +44,16 @@
if ( data ) m_FileName = data;
}

+#ifdef KICAD_PYTHON
+using namespace boost::python;
+object TreePrjItemData::GetIdPy() const
+// Convert the data to an id
+{
+	wxTreeItemId * id = new wxTreeItemId();
+	*id = GetId();
+	return object( handle<>( borrowed( wxPyConstructObject( id, wxT("wxTreeItemId"), true ) ) ) );
+}
+#endif

wxString TreePrjItemData::GetDir() const
/* Get the directory containing the file */
@@ -92,8 +102,8 @@
}

#ifdef KICAD_PYTHON
-	boost::python::object param = boost::python::make_tuple( PyHandler::Convert( m_FileName )
-	, PyHandler::Convert( destName ) );
+	object param = make_tuple( PyHandler::Convert( m_FileName )
+	, PyHandler::Convert( destName ) );
PyHandler::GetInstance()->TriggerEvent( wxT("kicad::MoveFile"), param );
#endif

@@ -158,8 +168,8 @@
SetFileName( newFile );

#ifdef KICAD_PYTHON
-	boost::python::object param = boost::python::make_tuple( PyHandler::Convert( m_FileName )
-	, PyHandler::Convert( newFile ) );
+	object param = make_tuple( PyHandler::Convert( m_FileName )
+	, PyHandler::Convert( newFile ) );
PyHandler::GetInstance()->TriggerEvent( wxT("kicad::RenameFile"), param );
#endif
return true;
@@ -256,13 +266,11 @@
/**********************************************************************/
/* Methodes de la Frame de l'arbre de hierarchie de gestion du projet */
/**********************************************************************/
-
TreePrjItemData * WinEDA_PrjFrame::GetSelectedData()
{
return dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
}

-
void WinEDA_PrjFrame::OnDragStart( wxTreeEvent & event )
// Allowing drag&drop of file other than the currently opened project
{
@@ -317,7 +325,57 @@
}

#ifdef KICAD_PYTHON
-using namespace boost::python;
+
+TreePrjItemData * WinEDA_PrjFrame::FindItemData( const str & name )
+// Return the data corresponding to the file, or NULL
+{
+	// (Interative tree parsing)
+	std::vector< wxTreeItemId > roots1, roots2;
+	std::vector< wxTreeItemId > *root, *reserve;
+	wxString filename = PyHandler::MakeStr( name );
+
+	root = &roots1;
+	reserve = &roots2;
+	root->push_back( m_TreeProject->GetRootItem() );
+
+
+	// if we look for the root, return it ...
+	TreePrjItemData * data = dynamic_cast< TreePrjItemData *>( m_TreeProject->GetItemData( root->at(0) ) );
+	if ( data->GetFileName() == filename ) return data;
+
+	// Then find in its child
+	while ( root->size() )
+	{
+	// look in all roots
+	for ( unsigned int i = 0; i < root->size() ; i++ )
+	{
+	wxTreeItemId id = root->at( i );
+
+	// for each root check any child:
+	void * cookie = NULL;
+	wxTreeItemId child = m_TreeProject->GetFirstChild( id, cookie );
+	while ( child.IsOk() )
+	{
+	TreePrjItemData * data = dynamic_cast< TreePrjItemData *>( m_TreeProject->GetItemData( child ) );
+	if ( data )
+	{
+	std::cout << " ( " << data->GetFileName().fn_str() << ")" << std::endl;
+	if ( data->GetFileName() == filename ) return data;
+	if ( m_TreeProject->ItemHasChildren( child ) ) reserve->push_back( child );
+	}
+	child = m_TreeProject->GetNextSibling( child );
+	}
+
+	}
+	// Swap the roots
+	root->clear();
+	std::vector< wxTreeItemId > *tmp;
+	tmp = root;
+	root = reserve;
+	reserve = tmp;
+	}
+	return NULL;
+}

void WinEDA_PrjFrame::RemoveFilterPy( const str & filter )
{
 --------------030305020401070101010101--