KiCad PCB EDA Suite
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 AUTHORS.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 #include <kiway.h>
37 #include "treeprojectfiles.h"
38 #include "pgm_kicad.h"
39 #include "tree_project_frame.h"
40 
41 #include "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 = aTreePrjFrame->m_Parent;
174  KIWAY& kiway = frame->Kiway();
175 
176  switch( GetType() )
177  {
178  case TREE_PROJECT:
179  // Select a new project if this is not the current project:
180  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
181  frame->LoadProject( fullFileName );
182  break;
183 
184  case TREE_DIRECTORY:
185  m_parent->Toggle( id );
186  break;
187 
188  case TREE_SCHEMA:
189  if( fullFileName == frame->SchFileName() )
190  {
191  // the project's schematic is opened using the *.kiface as part of this process.
192  // We do not call frame->RunEeschema( fullFileName ),
193  // because after the double click, for some reason,
194  // the tree project frame is brought to the foreground after Eeschema is called from here.
195  // Instead, we post an event, equivalent to click on the eeschema tool in command frame
196  wxCommandEvent evt( wxEVT_COMMAND_TOOL_CLICKED, ID_TO_SCH );
197  wxPostEvent( frame, evt );
198  }
199  else
200  {
201  // schematics not part of the project are opened in a separate process.
202  frame->Execute( m_parent, EESCHEMA_EXE, fullFileName );
203  }
204  break;
205 
206  case TREE_LEGACY_PCB:
207  case TREE_SEXP_PCB:
208  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
209  {
210  // the project's BOARD is opened using the *.kiface as part of this process.
211  // We do not call frame->RunPcbNew( fullFileName ),
212  // because after the double click, for some reason,
213  // the tree project frame is brought to the foreground after PcbNew is called from here.
214  // Instead, we post an event, equivalent to simple click on the pcb editor tool in command frame
215  wxCommandEvent evt( wxEVT_COMMAND_TOOL_CLICKED, ID_TO_PCB );
216  wxPostEvent( frame, evt );
217  }
218  else
219  {
220  // boards not part of the project are opened in a separate process.
221  frame->Execute( m_parent, PCBNEW_EXE, fullFileName );
222  }
223  break;
224 
225  case TREE_GERBER:
226  frame->Execute( m_parent, GERBVIEW_EXE, fullFileName );
227  break;
228 
229  case TREE_HTML:
230  wxLaunchDefaultBrowser( fullFileName );
231  break;
232 
233  case TREE_PDF:
234  OpenPDF( fullFileName );
235  break;
236 
237  case TREE_NET:
238  case TREE_DRILL:
239  case TREE_TXT:
240  case TREE_REPORT:
241  {
242  wxString editorname = Pgm().GetEditorName();
243 
244  if( !editorname.IsEmpty() )
245  frame->Execute( m_parent, editorname, fullFileName );
246  }
247  break;
248 
250  frame->Execute( m_parent, PL_EDITOR_EXE, fullFileName );
251  break;
252 
253  case TREE_FOOTPRINT_FILE:
254  {
255  wxCommandEvent dummy;
256  frame->OnRunPcbFpEditor( dummy );
257  kiway.ExpressMail( FRAME_PCB_MODULE_EDITOR, MAIL_FP_EDIT, fullFileName.ToStdString() );
258  }
259  break;
260 
262  {
263  wxCommandEvent dummy;
264  frame->OnRunSchLibEditor( dummy );
265  kiway.ExpressMail( FRAME_SCH_LIB_EDITOR, MAIL_LIB_EDIT, fullFileName.ToStdString() );
266  }
267  break;
268 
269  default:
270  AddDelimiterString( fullFileName );
271  OpenFile( fullFileName );
272  break;
273  }
274 }
Definition: kicad.h:59
KiCad executable names.
TreeFileType
Definition: kicad.h:52
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
const wxString PL_EDITOR_EXE
This file is part of the common library TODO brief description.
void OnRunPcbFpEditor(wxCommandEvent &event)
Definition: mainframe.cpp:404
const wxString PCBNEW_EXE
void OnRunSchLibEditor(wxCommandEvent &event)
Definition: mainframe.cpp:337
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
bool Delete(bool check=true)
void SetRootFile(bool aValue)
TREEPROJECTFILES * m_TreeProject
void SetFileName(const wxString &name)
Definition: kicad.h:61
const wxString GetDir() const
const wxString GERBVIEW_EXE
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:386
static list_size_check check
const wxString SchFileName()
Definition: mainframe.cpp:146
void Activate(TREE_PROJECT_FRAME *aTreePrjFrame)
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:265
wxTreeCtrl * m_parent
class TREE_PROJECT_FRAME Window to display the tree files
TreeFileType GetType() const
KICAD_MANAGER_FRAME * m_Parent
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
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
bool Rename(const wxString &name, bool check=true)
General-puspose messages.
Definition: mail_type.h:52
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:166
const char * name
Definition: DXF_plotter.cpp:61
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
void SetState(int state)
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
size_t i
Definition: json11.cpp:597
const wxString PcbFileName()
Definition: mainframe.cpp:156
void SetType(TreeFileType aType)
void OpenFile(const wxString &file)
Definition: gestfich.cpp:392
const wxString & GetFileName() const
VTBL_ENTRY const wxString & GetEditorName(bool aCanShowFileChooser=true)
Return the preferred editor name.
Definition: pgm_base.cpp:203
Definition: kicad.h:60
void LoadProject(const wxFileName &aProjectFileName)
Definition: prjconfig.cpp:61
static wxString GetFileExt(TreeFileType type)
The main KiCad project manager frame.
Definition: kicad.h:135
void SetPopulated(bool aValue)
TreeFileType m_Type
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345