Commit 45b01a5c authored by jean-pierre charras's avatar jean-pierre charras
Browse files

Kicad manager: first use of wxFileSystemWatcher (only with wxWidgets >= 2.9.2)...

Kicad manager: first use of wxFileSystemWatcher (only with wxWidgets >= 2.9.2) to monitor files list.
parent 6789e622
Loading
Loading
Loading
Loading
+106 −86
Original line number Diff line number Diff line
/**
 * @file class_treeproject_item.cpp
 *
 * Class TREEPROJECT_ITEM is a derived  class from wxTreeItemData that
 * @brief Class TREEPROJECT_ITEM is a derived  class from wxTreeItemData and
 * store info about a file or directory shown in the KiCad tree project files
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */


#include <fctsys.h>
#include <gestfich.h>
#include <macros.h>
@@ -18,37 +42,30 @@
#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_parent = parent;
    m_FileName = data;
    m_IsRootFile    = false;    // true only for the root item of the tree (the project name)
    m_WasPopulated  = false;
}


// Set the state used in the icon list
void TREEPROJECT_ITEM::SetState( int state )
{
    wxImageList* imglist = m_Parent->GetImageList();
    wxImageList* imglist = m_parent->GetImageList();

    if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
        return;
    m_State = state;

    m_state = state;
    int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
    m_Parent->SetItemImage( GetId(), imgid );
    m_Parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
    m_parent->SetItemImage( GetId(), imgid );
    m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
}


@@ -65,6 +82,7 @@ wxString TREEPROJECT_ITEM::GetDir() const
    wxArrayString   dirs = filename.GetDirs();

    wxString        dir;

    for( unsigned int i = 0; i < dirs.Count(); i++ )
    {
        dir += dirs[i] + filename.GetPathSeparator();
@@ -74,19 +92,6 @@ wxString TREEPROJECT_ITEM::GetDir() const
}


/* 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 )
{
@@ -97,15 +102,19 @@ void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )

    if( m_Type == TREE_DIRECTORY )
        return;

    if( !dest )
        return;
    if( m_Parent != dest->m_Parent )

    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 ) ) )
    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
@@ -115,8 +124,10 @@ void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )
    wxFileName  fname( m_FileName );

    wxString    destName;

    if( !dest->GetDir().IsEmpty() )
        destName = dest->GetDir() + sep;

    destName += fname.GetFullName();

    if( destName == GetFileName() )
@@ -125,7 +136,7 @@ void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )
    // Move the file on the disk:
    if( !wxRenameFile( GetFileName(), destName, false ) )
    {
        wxMessageDialog( m_Parent, _( "Unable to move file ... " ),
        wxMessageDialog( m_parent, _( "Unable to move file ... " ),
                         _( "Permission error ?" ), wxICON_ERROR | wxOK );
        return;
    }
@@ -136,25 +147,24 @@ void TREEPROJECT_ITEM::Move( TREEPROJECT_ITEM* dest )
    {
        // Move the tree item itself now:
        wxTreeItemId    oldId   = GetId();
        int          i    = m_Parent->GetItemImage( oldId );
        wxString     text = m_Parent->GetItemText( oldId );
        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 );
        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 );
        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 )
{
@@ -183,8 +193,9 @@ bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )

    if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
    {
        wxMessageDialog dialog( m_Parent,
                                _( "Changing file extension will change file \
        wxMessageDialog dialog( m_parent,
                                _(
                                    "Changing file extension will change file \
type.\n Do you want to continue ?"                                                                                 ),
                                _( "Rename File" ),
                                wxYES_NO | wxICON_QUESTION );
@@ -195,15 +206,18 @@ type.\n Do you want to continue ?" ),

#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 ... " ),
        wxMessageDialog( m_parent, _( "Unable to rename file ... " ),
                         _( "Permission error ?" ), wxICON_ERROR | wxOK );
        return false;
    }

    SetFileName( newFile );

    return true;
@@ -216,16 +230,17 @@ bool TREEPROJECT_ITEM::Delete( bool check )
/* delete a file */
{
    wxString        msg;

    msg.Printf( _( "Do you really want to delete '%s'" ), GetChars( GetFileName() ) );
    wxMessageDialog dialog( m_Parent, msg,
    wxMessageDialog dialog( m_parent, msg,
                            _( "Delete File" ), wxYES_NO | wxICON_QUESTION );

    if( !check || wxID_YES == dialog.ShowModal() )
    {
        bool success;

        if( !wxDirExists( m_FileName ) )
        {
            wxRemoveFile( m_FileName );
        }
            success = wxRemoveFile( m_FileName );
        else
        {
            wxArrayString filelist;
@@ -235,12 +250,15 @@ bool TREEPROJECT_ITEM::Delete( bool check )
            for( unsigned int i = 0; i < filelist.Count(); i++ )
                wxRemoveFile( filelist[i] );

            wxRmdir( m_FileName );
            success = wxRmdir( m_FileName );
        }
        m_Parent->Delete( GetId() );

        return true;
        if( success )
            m_parent->Delete( GetId() );

        return success;
    }

    return false;
}

@@ -253,26 +271,27 @@ void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe )
    wxTreeItemId    id = GetId();

    AddDelimiterString( FullFileName );

    switch( GetType() )
    {
    case TREE_PROJECT:
        break;

    case TREE_DIRECTORY:
        m_Parent->Toggle( id );
        m_parent->Toggle( id );
        break;

    case TREE_SCHEMA:
        ExecuteFile( m_Parent, EESCHEMA_EXE, FullFileName );
        ExecuteFile( m_parent, EESCHEMA_EXE, FullFileName );
        break;

    case TREE_LEGACY_PCB:
    case TREE_SEXP_PCB:
        ExecuteFile( m_Parent, PCBNEW_EXE, FullFileName );
        ExecuteFile( m_parent, PCBNEW_EXE, FullFileName );
        break;

    case TREE_GERBER:
        ExecuteFile( m_Parent, GERBVIEW_EXE, FullFileName );
        ExecuteFile( m_parent, GERBVIEW_EXE, FullFileName );
        break;

    case TREE_PDF:
@@ -280,14 +299,16 @@ void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe )
        break;

    case TREE_NET:
        ExecuteFile( m_Parent, CVPCB_EXE, FullFileName );
        ExecuteFile( m_parent, CVPCB_EXE, FullFileName );
        break;

    case TREE_TXT:
        {
            wxString editorname = wxGetApp().GetEditorName();

            if( !editorname.IsEmpty() )
            ExecuteFile( m_Parent, editorname, FullFileName );
                ExecuteFile( m_parent, editorname, FullFileName );

            break;
        }

@@ -296,4 +317,3 @@ void TREEPROJECT_ITEM::Activate( TREE_PROJECT_FRAME* prjframe )
        break;
    }
}
+16 −27
Original line number Diff line number Diff line
@@ -12,25 +12,21 @@ 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

    bool            m_WasPopulated; // True the name is a directory, and its content was read
private:
    wxTreeCtrl*  m_Parent;
    wxTreeCtrl*     m_parent;
    wxMenu          m_fileMenu;
    int          m_State;

    int             m_state;
public:

    TREEPROJECT_ITEM( TreeFileType type, const wxString& data,
                      wxTreeCtrl* parent );
    TREEPROJECT_ITEM() : m_Parent( NULL ) { }
    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 )
        m_Type( src.m_Type ), m_FileName( src.m_FileName ), m_parent( src.m_parent )
    {
        SetState( src.m_State );
        SetState( src.m_state );
        m_WasPopulated = false;
    }

@@ -39,28 +35,23 @@ public:
        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 );
@@ -71,7 +62,5 @@ public:
        return &m_fileMenu;
    }


    void SetState( int state );

};
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ IMPLEMENT_ABSTRACT_CLASS( TREEPROJECTFILES, wxTreeCtrl )
TREEPROJECTFILES::TREEPROJECTFILES( TREE_PROJECT_FRAME* parent ) :
    wxTreeCtrl( parent, ID_PROJECT_TREE,
                wxDefaultPosition, wxDefaultSize,
                wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS, wxDefaultValidator,
                wxTR_HAS_BUTTONS, wxDefaultValidator,
                wxT( "EDATreeCtrl" ) )
{
    m_Parent = parent;
+2 −1
Original line number Diff line number Diff line
@@ -258,8 +258,9 @@ void KICAD_MANAGER_FRAME::OnOpenFileInTextEditor( wxCommandEvent& event )
#endif

    mask = _( "Text file (" ) + mask + wxT( ")|" ) + mask;
    wxString default_dir = wxGetCwd();

    wxFileDialog dlg( this, _( "Load File to Edit" ), wxGetCwd(),
    wxFileDialog dlg( this, _( "Load File to Edit" ), default_dir,
                      wxEmptyString, mask, wxFD_OPEN );

    if( dlg.ShowModal() == wxID_CANCEL )
+10 −6
Original line number Diff line number Diff line
@@ -148,13 +148,14 @@ void KICAD_MANAGER_FRAME::OnLoadProject( wxCommandEvent& event )
{
    int style;
    wxString title;
    bool newProject = ( event.GetId() == ID_NEW_PROJECT ) ||
                      ( event.GetId() == ID_NEW_PROJECT_FROM_TEMPLATE );

    ClearMsg();

    if( event.GetId() != wxID_ANY )
    {
        if( ( event.GetId() == ID_NEW_PROJECT ) ||
            ( event.GetId() == ID_NEW_PROJECT_FROM_TEMPLATE ) )
        if( newProject )
        {
            title = _( "Create New Project" );
            style = wxFD_SAVE | wxFD_OVERWRITE_PROMPT;
@@ -165,15 +166,16 @@ void KICAD_MANAGER_FRAME::OnLoadProject( wxCommandEvent& event )
            style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
        }

        wxFileDialog dlg( this, title, wxGetCwd(), wxEmptyString, ProjectFileWildcard, style );
        wxString default_dir = wxGetCwd();
        wxFileDialog dlg( this, title, default_dir, wxEmptyString,
                          ProjectFileWildcard, style );

        if( dlg.ShowModal() == wxID_CANCEL )
            return;

        m_ProjectFileName = dlg.GetPath();

        if( ( event.GetId() == ID_NEW_PROJECT ) ||
            ( event.GetId() == ID_NEW_PROJECT_FROM_TEMPLATE ) )
        if( newProject )
        {
            if ( !m_ProjectFileName.GetFullPath().EndsWith( g_KicadPrjFilenameExtension ) )
            {
@@ -220,12 +222,14 @@ void KICAD_MANAGER_FRAME::OnLoadProject( wxCommandEvent& event )

    SetTitle( title );
    UpdateFileHistory( m_ProjectFileName.GetFullPath() );
#if wxCHECK_VERSION( 2, 9, 2  )
    m_LeftWin->FileWatcherReset();
#endif
    m_LeftWin->ReCreateTreePrj();

    PrintMsg( _( "Working dir: " ) + m_ProjectFileName.GetPath() +
              _( "\nProject: " ) + m_ProjectFileName.GetFullName() +
              wxT( "\n" ) );

}


Loading