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 <confirm.h>
35 #include <gestfich.h>
36 #include <kiplatform/environment.h>
37 #include <kiway.h>
38 #include <tool/tool_manager.h>
40 
41 #include "treeprojectfiles.h"
42 #include "pgm_kicad.h"
43 #include "tree_project_frame.h"
44 #include "treeproject_item.h"
45 #include "kicad_id.h"
46 
47 
48 TREEPROJECT_ITEM::TREEPROJECT_ITEM( enum TreeFileType type, const wxString& data,
49  wxTreeCtrl* parent ) :
50  wxTreeItemData()
51 {
52  m_parent = parent;
53  SetType( type );
54  SetFileName( data );
55  SetRootFile( false ); // true only for the root item of the tree (the project name)
56  SetPopulated( false );
57  m_state = 0;
58 }
59 
60 
61 void TREEPROJECT_ITEM::SetState( int state )
62 {
63  wxImageList* imglist = m_parent->GetImageList();
64 
65  if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( TREE_MAX - 2 ) )
66  return;
67 
68  m_state = state;
69  int imgid = m_Type - 1 + state * ( TREE_MAX - 1 );
70  m_parent->SetItemImage( GetId(), imgid );
71  m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
72 }
73 
74 
75 const wxString TREEPROJECT_ITEM::GetDir() const
76 {
77  if( TREE_DIRECTORY == m_Type )
78  return GetFileName();
79 
80  return wxFileName( GetFileName() ).GetPath();
81 }
82 
83 
84 bool TREEPROJECT_ITEM::Rename( const wxString& name, bool check )
85 {
86  // this is broken & unsafe to use on linux.
87  if( m_Type == TREE_DIRECTORY )
88  return false;
89 
90  if( name.IsEmpty() )
91  return false;
92 
93  const wxString sep = wxFileName().GetPathSeparator();
94  wxString newFile;
95  wxString dirs = GetDir();
96 
97  if( !dirs.IsEmpty() && GetType() != TREE_DIRECTORY )
98  newFile = dirs + sep + name;
99  else
100  newFile = name;
101 
102  if( newFile == GetFileName() )
103  return false;
104 
105  wxString ext = TREE_PROJECT_FRAME::GetFileExt( GetType() );
106 
107  wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
108 
109  if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
110  {
111  wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
112  "Do you want to continue ?" ),
113  _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
114 
115  if( wxID_YES != dialog.ShowModal() )
116  return false;
117  }
118 
119  if( !wxRenameFile( GetFileName(), newFile, false ) )
120  {
121  wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
122  wxICON_ERROR | wxOK );
123  return false;
124  }
125 
126  return true;
127 }
128 
129 
131 {
132  wxString errMsg;
133 
134  if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
135  {
136  wxString dialogMsg = wxString::Format( _( "Failed to delete '%s'"), GetFileName() );
137  DisplayErrorMessage( m_parent, dialogMsg, errMsg );
138  return;
139  }
140 
141  m_parent->Delete( GetId() );
142 }
143 
144 
146 {
147  PrintFile( GetFileName() );
148 }
149 
150 
152 {
153  wxString sep = wxFileName::GetPathSeparator();
154  wxString fullFileName = GetFileName();
155  wxTreeItemId id = GetId();
156  std::string packet;
157 
158  KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
159  TOOL_MANAGER* toolMgr = frame->GetToolManager();
160  KIWAY& kiway = frame->Kiway();
161 
162  switch( GetType() )
163  {
164  case TREE_LEGACY_PROJECT:
165  case TREE_JSON_PROJECT:
166  // Select a new project if this is not the current project:
167  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
168  frame->LoadProject( fullFileName );
169 
170  break;
171 
172  case TREE_DIRECTORY:
173  m_parent->Toggle( id );
174  break;
175 
178  // Schematics not part of the project are opened in a separate process.
179  if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() )
181  else
182  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
183 
184  break;
185 
186  case TREE_LEGACY_PCB:
187  case TREE_SEXPR_PCB:
188  // Boards not part of the project are opened in a separate process.
189  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
190  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true );
191  else
192  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
193 
194  break;
195 
196  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
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
This file is part of the common library TODO brief description.
This file is part of the common library.
const wxString SchLegacyFileName()
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:341
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:437
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:201
void SetState(int state)
#define _(s)
Definition: 3d_actions.cpp:33
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
const wxString PcbFileName()
void SetType(TreeFileType aType)
void OpenFile(const wxString &file)
Definition: gestfich.cpp:247
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:78
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:195