KiCad PCB EDA Suite
class_treeproject_item.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2012 KiCad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
32 #include <wx/regex.h>
33 
34 #include <gestfich.h>
35 #include <executable_names.h>
36 
37 #include "class_treeprojectfiles.h"
38 #include "pgm_kicad.h"
39 #include "tree_project_frame.h"
40 
41 #include "class_treeproject_item.h"
42 
43 
44 TREEPROJECT_ITEM::TREEPROJECT_ITEM( enum TreeFileType type, const wxString& data,
45  wxTreeCtrl* parent ) :
46  wxTreeItemData()
47 {
48  m_parent = parent;
49  SetType( type );
50  SetFileName( data );
51  SetRootFile( false ); // true only for the root item of the tree (the project name)
52  SetPopulated( false );
53  m_state = 0;
54 }
55 
56 
57 void TREEPROJECT_ITEM::SetState( int state )
58 {
59  wxImageList* imglist = m_parent->GetImageList();
60 
61  if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
62  return;
63 
64  m_state = state;
65  int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
66  m_parent->SetItemImage( GetId(), imgid );
67  m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
68 }
69 
70 
71 const wxString TREEPROJECT_ITEM::GetDir() const
72 {
73  if( TREE_DIRECTORY == m_Type )
74  return GetFileName();
75 
76  return wxFileName( GetFileName() ).GetPath();
77 }
78 
79 
80 bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
81 {
82  // this is broken & unsafe to use on linux.
83  if( m_Type == TREE_DIRECTORY )
84  return false;
85 
86  if( name.IsEmpty() )
87  return false;
88 
89  const wxString sep = wxFileName().GetPathSeparator();
90  wxString newFile;
91  wxString dirs = GetDir();
92 
93  if( !dirs.IsEmpty() && GetType() != TREE_DIRECTORY )
94  newFile = dirs + sep + name;
95  else
96  newFile = name;
97 
98  if( newFile == GetFileName() )
99  return false;
100 
101  wxString ext = TREE_PROJECT_FRAME::GetFileExt( GetType() );
102 
103  wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
104 
105  if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
106  {
107  wxMessageDialog dialog( m_parent, _(
108  "Changing file extension will change file type.\n Do you want to continue ?" ),
109  _( "Rename File" ),
110  wxYES_NO | wxICON_QUESTION );
111 
112  if( wxID_YES != dialog.ShowModal() )
113  return false;
114  }
115 
116  if( !wxRenameFile( GetFileName(), newFile, false ) )
117  {
118  wxMessageDialog( m_parent, _( "Unable to rename file ... " ),
119  _( "Permission error ?" ), wxICON_ERROR | wxOK );
120  return false;
121  }
122 
123  SetFileName( newFile );
124 
125  return true;
126 }
127 
128 
130 {
131  wxString msg = wxString::Format( _(
132  "Do you really want to delete '%s'" ),
133  GetChars( GetFileName() )
134  );
135 
136  wxMessageDialog dialog( m_parent, msg,
137  _( "Delete File" ), wxYES_NO | wxICON_QUESTION );
138 
139  if( !check || wxID_YES == dialog.ShowModal() )
140  {
141  bool success;
142 
143  if( !wxDirExists( GetFileName() ) )
144  success = wxRemoveFile( GetFileName() );
145  else
146  {
147  wxArrayString filelist;
148 
149  wxDir::GetAllFiles( GetFileName(), &filelist );
150 
151  for( unsigned int i = 0; i < filelist.Count(); i++ )
152  wxRemoveFile( filelist[i] );
153 
154  success = wxRmdir( GetFileName() );
155  }
156 
157  if( success )
158  m_parent->Delete( GetId() );
159 
160  return success;
161  }
162 
163  return false;
164 }
165 
166 
168 {
169  wxString sep = wxFileName().GetPathSeparator();
170  wxString fullFileName = GetFileName();
171  wxTreeItemId id = GetId();
172 
173  KICAD_MANAGER_FRAME* frame = prjframe->m_Parent;
174  wxASSERT( frame );
175 
176  switch( GetType() )
177  {
178  case TREE_PROJECT:
179  break;
180 
181  case TREE_DIRECTORY:
182  m_parent->Toggle( id );
183  break;
184 
185  case TREE_SCHEMA:
186  if( fullFileName == frame->SchFileName() )
187  {
188  // the project's schematic is opened using the *.kiface as part of this process.
189  // We do not call frame->RunEeschema( fullFileName ),
190  // because after the double click, for some reason,
191  // the tree project frame is brought to the foreground after Eeschema is called from here.
192  // Instead, we post an event, equivalent to click on the eeschema tool in command frame
193  wxCommandEvent evt( wxEVT_COMMAND_TOOL_CLICKED, ID_TO_SCH );
194  wxPostEvent( frame, evt );
195  }
196  else
197  {
198  // schematics not part of the project are opened in a separate process.
199  frame->Execute( m_parent, EESCHEMA_EXE, fullFileName );
200  }
201  break;
202 
203  case TREE_LEGACY_PCB:
204  case TREE_SEXP_PCB:
205  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
206  {
207  // the project's BOARD is opened using the *.kiface as part of this process.
208  // We do not call frame->RunPcbNew( fullFileName ),
209  // because after the double click, for some reason,
210  // the tree project frame is brought to the foreground after PcbNew is called from here.
211  // Instead, we post an event, equivalent to simple click on the pcb editor tool in command frame
212  wxCommandEvent evt( wxEVT_COMMAND_TOOL_CLICKED, ID_TO_PCB );
213  wxPostEvent( frame, evt );
214  }
215  else
216  {
217  // boards not part of the project are opened in a separate process.
218  frame->Execute( m_parent, PCBNEW_EXE, fullFileName );
219  }
220  break;
221 
222  case TREE_GERBER:
223  frame->Execute( m_parent, GERBVIEW_EXE, fullFileName );
224  break;
225 
226  case TREE_HTML:
227  wxLaunchDefaultBrowser( fullFileName );
228  break;
229 
230  case TREE_PDF:
231  OpenPDF( fullFileName );
232  break;
233 
234  case TREE_NET:
235  // Nothing to do ( can be read only by Pcbnew, or by a text editor)
236  break;
237 
238  case TREE_TXT:
239  {
240  wxString editorname = Pgm().GetEditorName();
241 
242  if( !editorname.IsEmpty() )
243  frame->Execute( m_parent, editorname, fullFileName );
244  }
245  break;
246 
248  frame->Execute( m_parent, PL_EDITOR_EXE, fullFileName );
249  break;
250 
251  default:
252  AddDelimiterString( fullFileName );
253  OpenFile( fullFileName );
254  break;
255  }
256 }
Definition: kicad.h:59
KiCad executable names.
TreeFileType
Definition: kicad.h:52
const wxString PL_EDITOR_EXE
This file is part of the common library TODO brief description.
const wxString PCBNEW_EXE
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
bool Delete(bool check=true)
void SetRootFile(bool aValue)
void SetFileName(const wxString &name)
Definition: kicad.h:61
const wxString GetDir() const
const wxString GERBVIEW_EXE
const wxString SchFileName()
Definition: mainframe.cpp:153
const wxString EESCHEMA_EXE
void Execute(wxWindow *frame, const wxString &execFile, wxString param=wxEmptyString)
Open another KiCad application and logs a message.
Definition: mainframe.cpp:272
class TREE_PROJECT_FRAME Window to display the tree files
static list_size_check check
TreeFileType GetType() const
KICAD_MANAGER_FRAME * m_Parent
void AddDelimiterString(wxString &string)
Function AddDelimiterString Add un " to the start and the end of string (if not already done)...
Definition: gestfich.cpp:44
Definition: kicad.h:72
void Activate(TREE_PROJECT_FRAME *prjframe)
bool Rename(const wxString &name, bool check=true)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
const wxString PcbLegacyFileName()
Definition: mainframe.cpp:173
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
const wxString PcbFileName()
Definition: mainframe.cpp:163
void SetType(TreeFileType aType)
void OpenFile(const wxString &file)
Definition: gestfich.cpp:392
const char * name
const wxString & GetFileName() const
VTBL_ENTRY const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:338
Definition: kicad.h:60
static wxString GetFileExt(TreeFileType type)
The main KiCad project manager frame.
Definition: kicad.h:136
void SetPopulated(bool aValue)
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345