Commit c2389e8f authored by charras's avatar charras

Kicad enhancement (see changelog) and serious code cleaning

parent 9ed9a723
......@@ -6,6 +6,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}
set(KICAD_SRCS
class_treeprojectfiles.cpp
class_treeproject_item.cpp
commandframe.cpp
files-io.cpp
kicad.cpp
......
/*
* file class_treeproject_item.cpp
*
* Class TREEPROJECT_ITEM is a derived class from wxTreeItemData that
* store info about a file or directory shown in the Kicad tree project files
*/
#ifdef KICAD_PYTHON
#include <pyhandler.h>
#endif
#include "fctsys.h"
#include "common.h"
#include "gestfich.h"
#include "kicad.h"
#include "tree_project_frame.h"
#include "class_treeprojectfiles.h"
#include "class_treeproject_item.h"
#include "wx/regex.h"
#include "wx/dir.h"
/* sort function for tree items.
* items are sorted :
* directory names first by alphabetic order
* root file names after
* file names last by alphabetic order
*/
TREEPROJECT_ITEM::TREEPROJECT_ITEM( enum TreeFileType type, const wxString& data,
wxTreeCtrl* parent ) :
wxTreeItemData()
{
m_Type = type;
m_Parent = parent;
m_FileName = data;
m_IsRootFile = false; // true only for the root item of the tree (the project name)
m_WasPopulated = false;
}
#ifdef KICAD_PYTHON
using namespace boost::python;
// Convert the data to an id
object TREEPROJECT_ITEM::GetIdPy() const
{
wxTreeItemId* id = new wxTreeItemId();
*id = GetId();
return object( handle<>( borrowed( wxPyConstructObject( id,
wxT( "wxTreeItemId" ),
true ) ) ) );
}
#endif
// Set the state used in the icon list
void TREEPROJECT_ITEM::SetState( int state )
{
wxImageList* imglist = m_Parent->GetImageList();
if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
return;
m_State = state;
int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
m_Parent->SetItemImage( GetId(), imgid );
m_Parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
}
/* Get the directory containing the file */
wxString TREEPROJECT_ITEM::GetDir() const
{
if( TREE_DIRECTORY == m_Type )
return m_FileName;
wxFileName filename = wxFileName( m_FileName );
filename.MakeRelativeTo( wxGetCwd() );
wxArrayString dirs = filename.GetDirs();
wxString dir;
for( unsigned int i = 0; i < dirs.Count(); i++ )
{
dir += dirs[i] + filename.GetPathSeparator();
}
return dir;
}
/* Called upon tree item rename */
void TREEPROJECT_ITEM::OnRename( wxTreeEvent& event, bool check )
{
//this segfaults on linux (in wxEvtHandler::ProcessEvent), wx version 2.8.7
//therefore, until it is fixed, we must cancel the rename.
event.Veto();
return;
if( !Rename( event.GetLabel(), check ) )
event.Veto();
}
// Move the object to dest
void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )
{
//function not safe.
return;
const wxString sep = wxFileName().GetPathSeparator();
if( m_Type == TREE_DIRECTORY )
return;
if( !dest )
return;
if( m_Parent != dest->m_Parent )
return; // Can not cross move!
if( dest == this )
return; // Can not move to ourself...
wxTreeItemId parent = m_Parent->GetItemParent( GetId() );
if( dest == dynamic_cast<TREEPROJECT_ITEM*>( m_Parent->GetItemData( parent ) ) )
return; // same parent ?
// 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();
if( destName == GetFileName() )
return; // Same place ??
// 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
object param = 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<TREEPROJECTFILES*>( m_Parent )->GetParent()->m_Parent->OnRefresh( dummy );
}
}
/* rename the file checking if extension change occurs */
bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
{
//this is broken & unsafe to use on linux.
if( m_Type == TREE_DIRECTORY )
return false;
if( name.IsEmpty() )
return false;
const wxString sep = wxFileName().GetPathSeparator();
wxString newFile;
wxString dirs = GetDir();
if( !dirs.IsEmpty() && GetType() != TREE_DIRECTORY )
newFile = dirs + sep + name;
else
newFile = name;
if( newFile == m_FileName )
return false;
wxString ext = TREE_PROJECT_FRAME::GetFileExt( GetType() );
wxRegEx reg( wxT ( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
{
wxMessageDialog dialog( m_Parent,
_( "Changing file extension will change file \
type.\n Do you want to continue ?" ),
_( "Rename File" ),
wxYES_NO | wxICON_QUESTION );
if( wxID_YES != dialog.ShowModal() )
return false;
}
#if ( ( wxMAJOR_VERSION < 2 ) || ( ( wxMAJOR_VERSION == 2 ) \
&& ( wxMINOR_VERSION < 7 ) ) )
if( !wxRenameFile( m_FileName, newFile ) )
#else
if( !wxRenameFile( m_FileName, newFile, false ) )
#endif
{
wxMessageDialog( m_Parent, _( "Unable to rename file ... " ),
_( "Permission error ?" ), wxICON_ERROR | wxOK );
return false;
}
SetFileName( newFile );
#ifdef KICAD_PYTHON
object param = make_tuple( PyHandler::Convert( m_FileName ),
PyHandler::Convert( newFile ) );
PyHandler::GetInstance()->TriggerEvent( wxT( "kicad::RenameFile" ), param );
#endif
return true;
}
/*******************************************/
bool TREEPROJECT_ITEM::Delete( bool check )
/*******************************************/
/* delete a file */
{
wxMessageDialog dialog( m_Parent,
_ ("Do you really want to delete ") + GetFileName(),
_( "Delete File" ), wxYES_NO | wxICON_QUESTION );
if( !check || wxID_YES == dialog.ShowModal() )
{
if( !wxDirExists( m_FileName ) )
{
wxRemoveFile( m_FileName );
}
else
{
wxArrayString filelist;
wxDir::GetAllFiles( m_FileName, &filelist );
for( unsigned int i = 0; i < filelist.Count(); i++ )
wxRemoveFile( filelist[i] );
wxRmdir( m_FileName );
}
m_Parent->Delete( GetId() );
#ifdef KICAD_PYTHON
PyHandler::GetInstance()->TriggerEvent( wxT( "kicad::DeleteFile" ),
PyHandler::Convert( m_FileName ) );
#endif
return true;
}
return false;
}
/* Called under item activation */
void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe )
{
wxString sep = wxFileName().GetPathSeparator();
wxString FullFileName = GetFileName();
wxTreeItemId id = GetId();
AddDelimiterString( FullFileName );
switch( GetType() )
{
case TREE_PROJECT:
break;
case TREE_DIRECTORY:
m_Parent->Toggle( id );
break;
case TREE_SCHEMA:
ExecuteFile( m_Parent, EESCHEMA_EXE, FullFileName );
break;
case TREE_PCB:
ExecuteFile( m_Parent, PCBNEW_EXE, FullFileName );
break;
#ifdef KICAD_PYTHON
case TREE_PY:
PyHandler::GetInstance()->RunScript( FullFileName );
break;
#endif
case TREE_GERBER:
ExecuteFile( m_Parent, GERBVIEW_EXE, FullFileName );
break;
case TREE_PDF:
OpenPDF( FullFileName );
break;
case TREE_NET:
ExecuteFile( m_Parent, CVPCB_EXE, FullFileName );
break;
case TREE_TXT:
{
wxString editorname = wxGetApp().GetEditorName();
if( !editorname.IsEmpty() )
ExecuteFile( m_Parent, editorname, FullFileName );
break;
}
default:
OpenFile( FullFileName );
break;
}
}
/*
* class_treeproject_item.h
*/
/** class TREEPROJECT_ITEM
* Handle one item (a file or a directory name) for the tree file
*/
class TREEPROJECT_ITEM : public wxTreeItemData
{
public:
TreeFileType m_Type; // = TREE_PROJECT, TREE_DIRECTORY ...
wxString m_FileName; // Filename for a file, or directory name
bool m_IsRootFile; // True if m_Filename is a root schematic (same name as project)
bool m_WasPopulated; // True the name is a directory, and its containt was read
private:
wxTreeCtrl* m_Parent;
wxMenu m_fileMenu;
int m_State;
public:
TREEPROJECT_ITEM( TreeFileType type, const wxString& data,
wxTreeCtrl* parent );
TREEPROJECT_ITEM() : m_Parent( NULL ) { }
TREEPROJECT_ITEM( const TREEPROJECT_ITEM& src ) :
m_Type( src.m_Type ),
m_FileName( src.m_FileName ),
m_Parent( src.m_Parent )
{
SetState( src.m_State );
m_WasPopulated = false;
}
TreeFileType GetType() const
{
return m_Type;
}
void SetType( TreeFileType aType )
{
m_Type = aType;
}
wxString GetFileName() const
{
return m_FileName;
}
void SetFileName( const wxString& name )
{
m_FileName = name;
}
wxString GetDir() const;
void OnRename( wxTreeEvent& event, bool check = true );
bool Rename( const wxString& name, bool check = true );
bool Delete( bool check = true );
void Move( TREEPROJECT_ITEM* dest );
void Activate( TREE_PROJECT_FRAME* prjframe );
const wxMenu* GetMenu()
{
return &m_fileMenu;
}
void SetState( int state );
#ifdef KICAD_PYTHON
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;
boost::python::object GetMenuPy();
#endif
};
This diff is collapsed.
......@@ -178,10 +178,10 @@ void TREE_PROJECT_FRAME::AddFilePy( const str& file, object& root )
/**
* @brief convert wxTreeItem into TreePrjItemData
* @brief convert wxTreeItem into TREEPROJECT_ITEM
*/
/*****************************************************************************/
TreePrjItemData* TREE_PROJECT_FRAME::GetItemData( const object& item )
TREEPROJECT_ITEM* TREE_PROJECT_FRAME::GetItemData( const object& item )
/*****************************************************************************/
{
wxTreeItemId* id = NULL;
......@@ -191,36 +191,36 @@ TreePrjItemData* TREE_PROJECT_FRAME::GetItemData( const object& item )
return NULL;
}
return dynamic_cast<TreePrjItemData*>( m_TreeProject->GetItemData( *id ) );
return dynamic_cast<TREEPROJECT_ITEM*>( m_TreeProject->GetItemData( *id ) );
}
/*****************************************************************************/
// TreePrjItemData Special binding functions
// TREEPROJECT_ITEM Special binding functions
// (one line functions are simple wrappers)
/*****************************************************************************/
// Python rename
bool TreePrjItemData::RenamePy( const str& newname, bool check )
bool TREEPROJECT_ITEM::RenamePy( const str& newname, bool check )
{
return Rename( PyHandler::MakeStr( newname ), check );
}
// Get python directory
object TreePrjItemData::GetDirPy() const
object TREEPROJECT_ITEM::GetDirPy() const
{
return PyHandler::Convert( GetDir() );
}
// Get python filename
object TreePrjItemData::GetFileNamePy() const
object TREEPROJECT_ITEM::GetFileNamePy() const
{
return PyHandler::Convert( GetFileName() );
}
// Get python menu
object TreePrjItemData::GetMenuPy()
object TREEPROJECT_ITEM::GetMenuPy()
{
return object( handle<>( borrowed( wxPyMake_wxObject( &m_fileMenu, false ) ) ) );
}
......@@ -238,21 +238,21 @@ static void py_kicad_init()
return_value_policy< reference_existing_object >() );
def( "GetTypeExtension", &GetTypeExt );
class_<TreePrjItemData>( "PrjItem" )
class_<TREEPROJECT_ITEM>( "PrjItem" )
// Internal data:
.def( "GetFileName", &TreePrjItemData::GetFileNamePy )
.def( "GetDir", &TreePrjItemData::GetDirPy )
.def( "GetType", &TreePrjItemData::GetType )
.def( "GetId", &TreePrjItemData::GetIdPy )
.def( "GetMenu", &TreePrjItemData::GetMenuPy )
.def( "GetFileName", &TREEPROJECT_ITEM::GetFileNamePy )
.def( "GetDir", &TREEPROJECT_ITEM::GetDirPy )
.def( "GetType", &TREEPROJECT_ITEM::GetType )
.def( "GetId", &TREEPROJECT_ITEM::GetIdPy )
.def( "GetMenu", &TREEPROJECT_ITEM::GetMenuPy )
// Item control
.def( "SetState", &TreePrjItemData::SetState )
.def( "Rename", &TreePrjItemData::RenamePy )
.def( "Move", &TreePrjItemData::Move )
.def( "Delete", &TreePrjItemData::Delete )
.def( "Activate", &TreePrjItemData::Activate )
.def( "SetState", &TREEPROJECT_ITEM::SetState )
.def( "Rename", &TREEPROJECT_ITEM::RenamePy )
.def( "Move", &TREEPROJECT_ITEM::Move )
.def( "Delete", &TREEPROJECT_ITEM::Delete )
.def( "Activate", &TREEPROJECT_ITEM::Activate )
;
enum_<TreeFileType>( "FileType" )
......
......@@ -38,9 +38,7 @@ enum id_kicad_frm {
ID_PROJECT_TXTEDIT,
ID_PROJECT_TREE_REFRESH,
ID_PROJECT_RUNPY,
ID_PROJECT_NEWFILE,
ID_PROJECT_NEWPY,
ID_PROJECT_NEWTXT,
ID_PROJECT_NEWDIR,
ID_PROJECT_DELETE,
ID_PROJECT_RENAME,
......@@ -188,87 +186,4 @@ private:
DECLARE_EVENT_TABLE()
};
/*********************************/
/* Classes for the project tree. */
/*********************************/
/** class TreePrjItemData
* Handle one item (a file or a directory name) for the tree file
*/
class TreePrjItemData : public wxTreeItemData
{
public:
TreeFileType m_Type;
bool m_IsRootFile; // True if m_Filename is a root schematic (same name as project)
wxString m_FileName; // Filename for a file, or directory name
private:
wxTreeCtrl* m_Parent;
wxMenu m_fileMenu;
int m_State;
public:
TreePrjItemData( TreeFileType type, const wxString& data,
wxTreeCtrl* parent );
TreePrjItemData() : m_Parent( NULL ) { }
TreePrjItemData( const TreePrjItemData& src ) :
m_Type( src.m_Type ),
m_FileName( src.m_FileName ),
m_Parent( src.m_Parent )
{
SetState( src.m_State );
}
TreeFileType GetType() const
{
return m_Type;
}
wxString GetFileName() const
{
return m_FileName;
}
void SetFileName( const wxString& name )
{
m_FileName = name;
}
wxString GetDir() const;
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( TREE_PROJECT_FRAME* prjframe );
const wxMenu* GetMenu()
{
return &m_fileMenu;
}
void SetState( int state );
#ifdef KICAD_PYTHON
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;
boost::python::object GetMenuPy();
#endif
};
#endif
This diff is collapsed.
......@@ -29,6 +29,8 @@
#ifndef TREEPRJ_FRAME_H
#define TREEPRJ_FRAME_H
class TREEPROJECT_ITEM;
/** class TREE_PROJECT_FRAME
* Window to display the tree files
*/
......@@ -48,7 +50,18 @@ protected:
void NewFile( TreeFileType type );
void NewFile( const wxString& name, TreeFileType type,
wxTreeItemId& root );
TreePrjItemData* GetSelectedData();
/** function GetSelectedData
* return the item data from item currently selected (highlighted)
* Note this is not necessary the "clicked" item,
* because when expanding, collapsing an item this item is not selected
*/
TREEPROJECT_ITEM* GetSelectedData();
/** function GetItemIdData
* return the item data corresponding to a wxTreeItemId identifier
* @param aId = the wxTreeItemId identifier.
* @return a TREEPROJECT_ITEM pointer correspondinfg to item id aId
*/
TREEPROJECT_ITEM* GetItemIdData(wxTreeItemId aId);
public:
WinEDA_MainFrame* m_Parent;
......@@ -63,6 +76,7 @@ public:
TREE_PROJECT_FRAME( WinEDA_MainFrame* parent );
~TREE_PROJECT_FRAME();
void OnSelect( wxTreeEvent& Event );
void OnExpand( wxTreeEvent& Event );
void OnRenameAsk( wxTreeEvent& Event );
void OnRename( wxTreeEvent& Event );
void OnDragStart( wxTreeEvent& event );
......@@ -75,14 +89,8 @@ public:
void OnDeleteFile( wxCommandEvent& event );
void OnRenameFile( wxCommandEvent& event );
void OnNewFile( wxCommandEvent& event );
void OnNewDirectory( wxCommandEvent& event );
void OnNewSchFile( wxCommandEvent& event );
void OnNewBrdFile( wxCommandEvent& event );
void OnNewPyFile( wxCommandEvent& event );
void OnNewGerberFile( wxCommandEvent& event );
void OnNewTxtFile( wxCommandEvent& event );
void OnNewNetFile( wxCommandEvent& event );
void ClearFilters();
......@@ -112,21 +120,32 @@ public:
void AddFilter( const boost::python::str& filter );
boost::python::object GetTreeCtrl();
TreePrjItemData* GetItemData( const boost::python::object& item );
TREEPROJECT_ITEM* GetItemData( const boost::python::object& item );
void AddFilePy( const boost::python::str& name,
boost::python::object& root );
void NewFilePy( const boost::python::str& name,
TreeFileType type,
boost::python::object& root );
TreePrjItemData* FindItemData( const boost::python::str& name );
TREEPROJECT_ITEM* FindItemData( const boost::python::str& name );
boost::python::object GetCurrentMenu();
int AddStatePy( boost::python::object& bitmap );
#endif
bool AddFile( const wxString& name, wxTreeItemId& root );
/** function AddFile
* @brief Add filename "name" to the tree \n
* if name is a directory, add the sub directory file names
* @param aName = the filename or the dirctory name to add
* @param aRoot = the wxTreeItemId item where to add sub tree items
* @param aRecurse = true to filenames or sub dir names to the current tree item
* false to stop file add.
* @return true if the file (or directory) is added.
*/
bool AddFile( const wxString& aName,
wxTreeItemId& aRoot, bool aRecurse = true);
DECLARE_EVENT_TABLE()
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment