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-2020 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  bool success;
131 
132  if( !isDirectory )
133  success = wxRemoveFile( GetFileName() );
134  else
135  success = DeleteDirectory( GetFileName() );
136 
137  if( success )
138  m_parent->Delete( GetId() );
139 }
140 
141 
143 {
144  PrintFile( GetFileName() );
145 }
146 
147 
149 {
150  wxString sep = wxFileName::GetPathSeparator();
151  wxString fullFileName = GetFileName();
152  wxTreeItemId id = GetId();
153  std::string packet;
154 
155  KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
156  TOOL_MANAGER* toolMgr = frame->GetToolManager();
157  KIWAY& kiway = frame->Kiway();
158 
159  switch( GetType() )
160  {
161  case TREE_PROJECT:
162  // Select a new project if this is not the current project:
163  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
164  frame->LoadProject( fullFileName );
165  break;
166 
167  case TREE_DIRECTORY:
168  m_parent->Toggle( id );
169  break;
170 
173  if( fullFileName == frame->SchFileName() )
174  {
176  }
177  else
178  {
179  // schematics not part of the project are opened in a separate process.
180  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
181  }
182  break;
183 
184  case TREE_LEGACY_PCB:
185  case TREE_SEXPR_PCB:
186  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
187  {
188  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true );
189  }
190  else
191  {
192  // boards not part of the project are opened in a separate process.
193  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
194  }
195  break;
196 
197  case TREE_GERBER:
198  case TREE_DRILL:
199  case TREE_DRILL_NC:
200  case TREE_DRILL_XNC:
201  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::viewGerbers, true, &fullFileName );
202  break;
203 
204  case TREE_HTML:
205  wxLaunchDefaultBrowser( fullFileName );
206  break;
207 
208  case TREE_PDF:
209  OpenPDF( fullFileName );
210  break;
211 
212  case TREE_NET:
213  case TREE_TXT:
214  case TREE_REPORT:
215  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &fullFileName );
216  break;
217 
219  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editWorksheet, true, &fullFileName );
220  break;
221 
222  case TREE_FOOTPRINT_FILE:
224  packet = fullFileName.ToStdString();
226  break;
227 
231  packet = fullFileName.ToStdString();
233  break;
234 
235  default:
236  OpenFile( fullFileName );
237  break;
238  }
239 }
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
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:140
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:51
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:428
static TOOL_ACTION editOtherSch
static TOOL_ACTION editPCB
const wxString PcbLegacyFileName()
const char * name
Definition: DXF_plotter.cpp:60
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
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:74
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