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-2019 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 <kiway.h>
36 #include <tool/tool_manager.h>
38 #include "treeprojectfiles.h"
39 #include "pgm_kicad.h"
40 #include "tree_project_frame.h"
41 #include "treeproject_item.h"
42 #include "kicad_id.h"
43 
44 
45 TREEPROJECT_ITEM::TREEPROJECT_ITEM( enum TreeFileType type, const wxString& data,
46  wxTreeCtrl* parent ) :
47  wxTreeItemData()
48 {
49  m_parent = parent;
50  SetType( type );
51  SetFileName( data );
52  SetRootFile( false ); // true only for the root item of the tree (the project name)
53  SetPopulated( false );
54  m_state = 0;
55 }
56 
57 
58 void TREEPROJECT_ITEM::SetState( int state )
59 {
60  wxImageList* imglist = m_parent->GetImageList();
61 
62  if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
63  return;
64 
65  m_state = state;
66  int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
67  m_parent->SetItemImage( GetId(), imgid );
68  m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
69 }
70 
71 
72 const wxString TREEPROJECT_ITEM::GetDir() const
73 {
74  if( TREE_DIRECTORY == m_Type )
75  return GetFileName();
76 
77  return wxFileName( GetFileName() ).GetPath();
78 }
79 
80 
81 bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
82 {
83  // this is broken & unsafe to use on linux.
84  if( m_Type == TREE_DIRECTORY )
85  return false;
86 
87  if( name.IsEmpty() )
88  return false;
89 
90  const wxString sep = wxFileName().GetPathSeparator();
91  wxString newFile;
92  wxString dirs = GetDir();
93 
94  if( !dirs.IsEmpty() && GetType() != TREE_DIRECTORY )
95  newFile = dirs + sep + name;
96  else
97  newFile = name;
98 
99  if( newFile == GetFileName() )
100  return false;
101 
102  wxString ext = TREE_PROJECT_FRAME::GetFileExt( GetType() );
103 
104  wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
105 
106  if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
107  {
108  wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
109  "Do you want to continue ?" ),
110  _( "Rename File" ), 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 ... " ), _( "Permission error?" ),
119  wxICON_ERROR | wxOK );
120  return false;
121  }
122 
123  return true;
124 }
125 
126 
128 {
129  bool isDirectory = wxDirExists( GetFileName() );
130 
131  wxString msg = wxString::Format( _( "Are you sure you want to delete '%s'?" ), GetFileName() );
132  wxMessageDialog dialog( m_parent, msg, isDirectory ? _( "Delete Directory" ) : _( "Delete File" ),
133  wxYES_NO | wxICON_QUESTION );
134 
135  if( dialog.ShowModal() == wxID_YES )
136  {
137  bool success;
138 
139  if( !isDirectory )
140  success = wxRemoveFile( GetFileName() );
141  else
142  success = DeleteDirectory( GetFileName() );
143 
144  if( success )
145  m_parent->Delete( GetId() );
146  }
147 }
148 
149 
151 {
152  PrintFile( GetFileName() );
153 }
154 
155 
157 {
158  wxString sep = wxFileName::GetPathSeparator();
159  wxString fullFileName = GetFileName();
160  wxTreeItemId id = GetId();
161  std::string packet;
162 
163  KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
164  TOOL_MANAGER* toolMgr = frame->GetToolManager();
165  KIWAY& kiway = frame->Kiway();
166 
167  switch( GetType() )
168  {
169  case TREE_PROJECT:
170  // Select a new project if this is not the current project:
171  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
172  frame->LoadProject( fullFileName );
173  break;
174 
175  case TREE_DIRECTORY:
176  m_parent->Toggle( id );
177  break;
178 
179  case TREE_SCHEMA:
180  if( fullFileName == frame->SchFileName() )
181  {
183  }
184  else
185  {
186  // schematics not part of the project are opened in a separate process.
187  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
188  }
189  break;
190 
191  case TREE_LEGACY_PCB:
192  case TREE_SEXP_PCB:
193  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
194  {
195  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true );
196  }
197  else
198  {
199  // boards not part of the project are opened in a separate process.
200  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
201  }
202  break;
203 
204  case TREE_GERBER:
205  case TREE_DRILL:
206  case TREE_DRILL_NC:
207  case TREE_DRILL_XNC:
208  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::viewGerbers, true, &fullFileName );
209  break;
210 
211  case TREE_HTML:
212  wxLaunchDefaultBrowser( fullFileName );
213  break;
214 
215  case TREE_PDF:
216  OpenPDF( fullFileName );
217  break;
218 
219  case TREE_NET:
220  case TREE_TXT:
221  case TREE_REPORT:
222  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &fullFileName );
223  break;
224 
226  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editWorksheet, true, &fullFileName );
227  break;
228 
229  case TREE_FOOTPRINT_FILE:
231  packet = fullFileName.ToStdString();
233  break;
234 
237  packet = fullFileName.ToStdString();
239  break;
240 
241  default:
242  OpenFile( fullFileName );
243  break;
244  }
245 }
IDs used in KiCad main frame foe menuitems and tools.
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
bool DeleteDirectory(const wxString &aDirName, bool aRecurse, bool aIncludeHidden)
Delete a directory and all of its contents recursively.
Definition: gestfich.cpp:384
This file is part of the common library TODO brief description.
static TOOL_ACTION viewGerbers
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:139
TreeFileType GetType() const
const wxString GetDir() const
void SetRootFile(bool aValue)
TREEPROJECTFILES * m_TreeProject
void SetFileName(const wxString &name)
static TOOL_ACTION editOtherPCB
TOOL_MANAGER.
Definition: tool_manager.h:50
const wxString SchFileName()
void Activate(TREE_PROJECT_FRAME *aTreePrjFrame)
static TOOL_ACTION editSchematic
wxTreeCtrl * m_parent
TREE_PROJECT_FRAME Window to display the tree files.
TreeFileType
KICAD_MANAGER_FRAME * m_Parent
static TOOL_ACTION editFootprints
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
static TOOL_ACTION editWorksheet
void PrintFile(const wxString &file)
Definition: gestfich.cpp:351
bool Rename(const wxString &name, bool check=true)
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:427
static TOOL_ACTION editOtherSch
static TOOL_ACTION editPCB
const wxString PcbLegacyFileName()
const char * name
Definition: DXF_plotter.cpp:60
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)
#define _(s)
Definition: 3d_actions.cpp:33
const wxString PcbFileName()
void SetType(TreeFileType aType)
void OpenFile(const wxString &file)
Definition: gestfich.cpp:257
void LoadProject(const wxFileName &aProjectFileName)
const wxString & GetFileName() const
static wxString GetFileExt(TreeFileType type)
static TOOL_ACTION editSymbols
The main KiCad project manager frame.
void SetPopulated(bool aValue)
static TOOL_ACTION openTextEditor
TreeFileType m_Type
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:205