← Back to team overview

kicad-developers team mailing list archive

PATCH: kicad-tree-intern-drag-drop.patch

 

Code cleanup
- introduced GetCurrentData()
- removed dummy event parameter for NewFile

Added drag files inside the tree to a different directory:
- Move a file in a directory
- Move a directory to another directory

bugfix:
- Handle rename error (and don't update the tree as well)
- prevented a rename file from overwriting existing file

python:
Move file operation

---
kicad/kicad.cpp | 1
kicad/kicad.h | 15 ++++-
kicad/treeprj.cpp | 140 
+++++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 135 insertions(+), 21 deletions(-)




 --------------090404020900050509020108 Content-Type: text/x-patch;
name="kicad-tree-intern-drag-drop.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="kicad-tree-intern-drag-drop.patch"

Subject: [patch @num@/@total@] @name@

Code cleanup 
- introduced GetCurrentData()
- removed dummy event parameter for NewFile

Added drag files inside the tree to a different directory:
- Move a file in a directory
- Move a directory to another directory

bugfix:
- Handle rename error (and don't update the tree as well)
- prevented a rename file from overwriting existing file

python:
Move file operation

---
kicad/kicad.cpp | 1 
kicad/kicad.h | 15 ++++-
kicad/treeprj.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 135 insertions(+), 21 deletions(-)




Index: kicad-dev/kicad/kicad.h
===================================================================
--- kicad-dev.orig/kicad/kicad.h	2007-05-02 15:22:19.000000000 +0200
+++ kicad-dev/kicad/kicad.h	2007-05-02 23:06:30.000000000 +0200
@@ -11,6 +11,7 @@

#include <wx/treectrl.h>
#include <vector>
+#include <wx/dragimag.h>

/* Message de presentation */
eda_global wxString g_Main_Title
@@ -137,6 +138,7 @@
void OnRename( wxTreeEvent & event, bool check = true );
bool Rename( const wxString & name, bool check = true );
bool Delete( bool check = true );
+	void Move( TreePrjItemData * dest );
void Activate();

#ifdef KICAD_PYTHON
@@ -152,13 +154,16 @@
private:

std::vector< wxMenu* > m_ContextMenus;
- std::vector< wxString > m_Filters;
+ std::vector< wxString > m_Filters;
+
+ wxCursor m_DragCursor;
+ wxCursor m_Default;

protected:
wxMenu * GetContextMenu( int type );
-	void NewFile( wxCommandEvent & event, enum TreeFileType type );
+	void NewFile( enum TreeFileType type );
void NewFile( const wxString & name, enum TreeFileType type, wxTreeItemId & root );
-
+	TreePrjItemData * GetSelectedData();

public:
WinEDA_MainFrame * m_Parent;
@@ -175,6 +180,8 @@
void OnSelect(wxTreeEvent & Event);
void OnRenameAsk(wxTreeEvent & Event);
void OnRename(wxTreeEvent & Event);
+	void OnDragStart( wxTreeEvent & event );
+	void OnDragEnd( wxTreeEvent & event );
void OnRight(wxTreeEvent & Event);
void ReCreateTreePrj(void);

@@ -228,6 +235,8 @@
wxImageList * m_ImageList;

public:
+
+	WinEDA_PrjFrame * GetParent() { return m_Parent; }
WinEDA_TreePrj(WinEDA_PrjFrame *parent);
~WinEDA_TreePrj();
};
Index: kicad-dev/kicad/treeprj.cpp
===================================================================
--- kicad-dev.orig/kicad/treeprj.cpp	2007-05-02 15:36:44.000000000 +0200
+++ kicad-dev/kicad/treeprj.cpp	2007-05-02 23:09:22.000000000 +0200
@@ -68,6 +68,61 @@
if ( !Rename( event.GetLabel(), check ) ) event.Veto();
}

+void TreePrjItemData::Move( TreePrjItemData * dest )
+// Move the object to dest
+{
+ const wxString sep = wxFileName().GetPathSeparator();
+	if (!dest) return;
+	if ( m_Parent != dest->m_Parent ) return;// Can not cross move!
+
+	// We need to create a new item from us, and move
+	// data to there ...
+
+	// First move file on the disk
+	wxFileName fname( m_FileName );
+	wxString destName;
+	if ( !dest->GetDir().IsEmpty() ) destName = dest->GetDir() + sep;
+	destName += fname.GetFullName();
+
+	// Move the file on the disk:
+	if ( !wxRenameFile( GetFileName(), destName, false ) )
+	{
+	wxMessageDialog( m_Parent, _( "Unable to move file ... "), _( "Permission error ?" ), wxICON_ERROR | wxOK );
+	return;
+	}
+
+	#ifdef KICAD_PYTHON
+	boost::python::object param = boost::python::make_tuple( PyHandler::Convert( m_FileName )
+	, PyHandler::Convert( destName ) );
+	PyHandler::GetInstance()->TriggerEvent( wxT("kicad::MoveFile"), param );
+	#endif
+
+	SetFileName( destName );
+
+	if ( TREE_DIRECTORY != GetType() )
+	{
+	// Move the tree item itself now:
+	wxTreeItemId oldId = GetId();
+	int i = m_Parent->GetItemImage( oldId );
+	wxString text = m_Parent->GetItemText( oldId );
+
+	// Bye bye old Id :'(
+	wxTreeItemId newId = m_Parent->AppendItem( dest->GetId(), text, i );
+	m_Parent->SetItemData( newId, this );
+	m_Parent->SetItemData( oldId, NULL );
+	m_Parent->Delete( oldId );
+	}
+	else
+	{
+	// We should move recursively all files, but that's quite boring
+	// let's just refresh that's all ... TODO (change this to a better code ...)
+	wxCommandEvent dummy;
+	dynamic_cast<WinEDA_TreePrj*>(m_Parent)->GetParent()->m_Parent->OnRefresh(dummy);
+	}
+
+
+}
+
bool TreePrjItemData::Rename( const wxString & name, bool check )
/* rename the file checking if extension change occurs */
{
@@ -95,7 +150,11 @@
if ( wxID_YES != dialog.ShowModal() ) return false;
}

-	wxRenameFile( m_FileName, newFile );
+	if (! wxRenameFile( m_FileName, newFile, false ) )
+	{
+	wxMessageDialog( m_Parent, _( "Unable to rename file ... "), _( "Permission error ?" ), wxICON_ERROR | wxOK );
+	return false;
+	}
SetFileName( newFile );

#ifdef KICAD_PYTHON
@@ -198,6 +257,48 @@
/* 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
+{
+	TreePrjItemData * data = GetSelectedData();
+	if ( data->GetFileName() == m_Parent->m_PrjFileName ) return;
+
+	wxTreeItemId id = m_TreeProject->GetSelection();
+
+	wxImage img = m_TreeProject->GetImageList()->GetBitmap( data->GetType() - 1 ).ConvertToImage();
+	m_DragCursor = wxCursor(img );
+	m_Parent->wxWindow::SetCursor( (wxCursor &) m_DragCursor );
+	event.Allow();
+}
+
+void WinEDA_PrjFrame::OnDragEnd( wxTreeEvent & event )
+{
+	m_Parent->SetCursor( wxNullCursor );
+
+	wxTreeItemId moved = m_TreeProject->GetSelection();
+	wxTreeItemId dest = event.GetItem();
+	if (!dest.IsOk() ) return; // Cancelled ...
+	TreePrjItemData * destData = dynamic_cast<TreePrjItemData *>(m_TreeProject->GetItemData( dest ) );
+	if (!destData ) return;
+
+	if ( TREE_DIRECTORY != destData->GetType() && !m_TreeProject->ItemHasChildren( dest ) )
+	{
+	dest = m_TreeProject->GetItemParent( dest );
+	}
+
+	if (!dest.IsOk() ) return; // no parent O_o ?
+
+	TreePrjItemData * data = GetSelectedData();
+
+	data->Move( destData );
+}
+
void WinEDA_PrjFrame::ClearFilters()
{
m_Filters.clear();
@@ -255,6 +356,7 @@
PyHandler::GetInstance()->DeclareEvent( wxT( "kicad::NewDirectory" ) );
PyHandler::GetInstance()->DeclareEvent( wxT( "kicad::DeleteFile" ) );
PyHandler::GetInstance()->DeclareEvent( wxT( "kicad::RenameFile" ) );
+	PyHandler::GetInstance()->DeclareEvent( wxT( "kicad::MoveFile" ) );
#endif

for ( int i = 0; i < TREE_MAX; i++ ) m_ContextMenus.push_back( new wxMenu() );
@@ -342,6 +444,8 @@
EVT_TREE_END_LABEL_EDIT(ID_PROJECT_TREE, WinEDA_PrjFrame::OnRename )
EVT_TREE_ITEM_ACTIVATED(ID_PROJECT_TREE, WinEDA_PrjFrame::OnSelect)
EVT_TREE_ITEM_RIGHT_CLICK(ID_PROJECT_TREE, WinEDA_PrjFrame::OnRight)
+	EVT_TREE_BEGIN_DRAG( ID_PROJECT_TREE, WinEDA_PrjFrame::OnDragStart)
+	EVT_TREE_END_DRAG( ID_PROJECT_TREE, WinEDA_PrjFrame::OnDragEnd)
EVT_MENU(ID_PROJECT_TXTEDIT, WinEDA_PrjFrame::OnTxtEdit)
EVT_MENU(ID_PROJECT_NEWFILE, WinEDA_PrjFrame::OnNewFile)
EVT_MENU(ID_PROJECT_NEWDIR, WinEDA_PrjFrame::OnNewDirectory)
@@ -358,16 +462,16 @@
#endif
END_EVENT_TABLE()

-void WinEDA_PrjFrame::OnNewDirectory(wxCommandEvent & event) { NewFile( event, TREE_DIRECTORY ); }
-void WinEDA_PrjFrame::OnNewFile(wxCommandEvent & event) { NewFile( event, TREE_UNKNOWN ); }
-void WinEDA_PrjFrame::OnNewSchFile(wxCommandEvent & event) { NewFile( event, TREE_SCHEMA ); }
-void WinEDA_PrjFrame::OnNewBrdFile(wxCommandEvent & event) { NewFile( event, TREE_PCB ); }
-void WinEDA_PrjFrame::OnNewPyFile(wxCommandEvent & event) { NewFile( event, TREE_PY ); }
-void WinEDA_PrjFrame::OnNewGerberFile(wxCommandEvent & event) { NewFile( event, TREE_GERBER ); }
-void WinEDA_PrjFrame::OnNewTxtFile(wxCommandEvent & event) { NewFile( event, TREE_TXT ); }
-void WinEDA_PrjFrame::OnNewNetFile(wxCommandEvent & event) { NewFile( event, TREE_NET ); }
+void WinEDA_PrjFrame::OnNewDirectory(wxCommandEvent & event) { NewFile( TREE_DIRECTORY ); }
+void WinEDA_PrjFrame::OnNewFile(wxCommandEvent & event) { NewFile( TREE_UNKNOWN ); }
+void WinEDA_PrjFrame::OnNewSchFile(wxCommandEvent & event) { NewFile( TREE_SCHEMA ); }
+void WinEDA_PrjFrame::OnNewBrdFile(wxCommandEvent & event) { NewFile( TREE_PCB ); }
+void WinEDA_PrjFrame::OnNewPyFile(wxCommandEvent & event) { NewFile( TREE_PY ); }
+void WinEDA_PrjFrame::OnNewGerberFile(wxCommandEvent & event) { NewFile( TREE_GERBER ); }
+void WinEDA_PrjFrame::OnNewTxtFile(wxCommandEvent & event) { NewFile( TREE_TXT ); }
+void WinEDA_PrjFrame::OnNewNetFile(wxCommandEvent & event) { NewFile( TREE_NET ); }

-void WinEDA_PrjFrame::NewFile( wxCommandEvent & event, enum TreeFileType type )
+void WinEDA_PrjFrame::NewFile( enum TreeFileType type )
{
wxString filename;
wxString mask = GetFileExt( type );
@@ -378,7 +482,7 @@

TreePrjItemData * treeData;
wxString FullFileName;
- treeData = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+ treeData = GetSelectedData();
if (!treeData) return;

dir = treeData->GetDir();
@@ -572,7 +676,7 @@
TreePrjItemData * tree_data;
wxString FullFileName;

-	tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	tree_data = GetSelectedData();
if (!tree_data) return;

tree_id = tree_data->GetType();
@@ -599,7 +703,7 @@

void WinEDA_PrjFrame::OnTxtEdit(wxCommandEvent & event )
{
-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*> (m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;

wxString FullFileName = tree_data->GetFileName();
@@ -614,7 +718,7 @@

void WinEDA_PrjFrame::OnDeleteFile(wxCommandEvent &)
{
-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;
tree_data->Delete();
}
@@ -622,7 +726,7 @@
#ifdef KICAD_PYTHON
void WinEDA_PrjFrame::OnRunPy(wxCommandEvent & event )
{
-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;

wxString FullFileName = tree_data->GetFileName();
@@ -634,7 +738,7 @@
void WinEDA_PrjFrame::OnRenameAsk(wxTreeEvent & event)
/* Prevent the main project to be renamed */
{
-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;
if ( m_Parent->m_PrjFileName == tree_data->GetFileName() ) event.Veto();
}
@@ -642,7 +746,7 @@
void WinEDA_PrjFrame::OnRename(wxTreeEvent & event)
/* rename a tree item on demand of the context menu */
{
-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;

tree_data->OnRename( event );
@@ -655,7 +759,7 @@
{
wxString FullFileName;

-	TreePrjItemData * tree_data = dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData(m_TreeProject->GetSelection()) );
+	TreePrjItemData * tree_data = GetSelectedData();
if (!tree_data) return;
tree_data->Activate();
}
Index: kicad-dev/kicad/kicad.cpp
===================================================================
--- kicad-dev.orig/kicad/kicad.cpp	2007-05-02 22:46:45.000000000 +0200
+++ kicad-dev/kicad/kicad.cpp	2007-05-02 22:47:02.000000000 +0200
@@ -127,6 +127,7 @@
.def( "GetType",	&TreePrjItemData::GetType )
// Item control
.def( "Rename", &TreePrjItemData::RenamePy )
+	.def( "Move",	&TreePrjItemData::Move )
.def( "Delete", &TreePrjItemData::Delete )
.def( "Activate", &TreePrjItemData::Activate )
;
 --------------090404020900050509020108--