KiCad PCB EDA Suite
project_tree_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 
30 #include <wx/regex.h>
31 
32 #include <confirm.h>
33 #include <gestfich.h>
34 #include <kiplatform/environment.h>
35 #include <kiway.h>
36 #include <tool/tool_manager.h>
38 
39 #include "kicad_manager_frame.h"
40 #include "project_tree.h"
41 #include "pgm_kicad.h"
42 #include "project_tree_pane.h"
43 #include "project_tree_item.h"
44 #include "kicad_id.h"
45 
46 
48  wxTreeCtrl* parent ) :
49  wxTreeItemData()
50 {
51  m_parent = parent;
52  SetType( type );
53  SetFileName( data );
54  SetRootFile( false ); // true only for the root item of the tree (the project name)
55  SetPopulated( false );
56  m_state = 0;
57 }
58 
59 
60 void PROJECT_TREE_ITEM::SetState( int state )
61 {
62  wxImageList* imglist = m_parent->GetImageList();
63  int treeEnumMax = static_cast<int>( TREE_FILE_TYPE::MAX );
64 
65  if( !imglist || state < 0 || state >= imglist->GetImageCount() / ( treeEnumMax - 2 ) )
66  return;
67 
68  m_state = state;
69  int imgid = static_cast<int>( m_type ) - 1 + state * ( treeEnumMax - 1 );
70  m_parent->SetItemImage( GetId(), imgid );
71  m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
72 }
73 
74 
75 const wxString PROJECT_TREE_ITEM::GetDir() const
76 {
78  return GetFileName();
79 
80  return wxFileName( GetFileName() ).GetPath();
81 }
82 
83 
84 bool PROJECT_TREE_ITEM::Rename( const wxString& name, bool check )
85 {
86  // this is broken & unsafe to use on linux.
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_FILE_TYPE::DIRECTORY )
98  newFile = dirs + sep + name;
99  else
100  newFile = name;
101 
102  if( newFile == GetFileName() )
103  return false;
104 
105  wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() );
106  wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
107 
108  if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
109  {
110  wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
111  "Do you want to continue ?" ),
112  _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
113 
114  if( wxID_YES != dialog.ShowModal() )
115  return false;
116  }
117 
118  if( !wxRenameFile( GetFileName(), newFile, false ) )
119  {
120  wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
121  wxICON_ERROR | wxOK );
122  return false;
123  }
124 
125  return true;
126 }
127 
128 
130 {
131  wxString errMsg;
132 
133  if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
134  {
135 #ifdef __WINDOWS__
136  wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
137  GetFileName() );
138 #else
139  wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
140  GetFileName() );
141 #endif
142 
143  DisplayErrorMessage( m_parent, dialogMsg, errMsg );
144  return;
145  }
146 
147  m_parent->Delete( GetId() );
148 }
149 
150 
152 {
153  PrintFile( GetFileName() );
154 }
155 
156 
158 {
159  wxString sep = wxFileName::GetPathSeparator();
160  wxString fullFileName = GetFileName();
161  wxTreeItemId id = GetId();
162  std::string packet;
163 
164  KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
165  TOOL_MANAGER* toolMgr = frame->GetToolManager();
166  KIWAY& kiway = frame->Kiway();
167 
168  switch( GetType() )
169  {
172  // Select a new project if this is not the current project:
173  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
174  frame->LoadProject( fullFileName );
175 
176  break;
177 
179  m_parent->Toggle( id );
180  break;
181 
184  // Schematics not part of the project are opened in a separate process.
185  if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() )
187  else
188  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
189 
190  break;
191 
194  // Boards not part of the project are opened in a separate process.
195  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
196  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true );
197  else
198  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
199 
200  break;
201 
207  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::viewGerbers, true, &fullFileName );
208  break;
209 
211  wxLaunchDefaultBrowser( fullFileName );
212  break;
213 
214  case TREE_FILE_TYPE::PDF:
215  OpenPDF( fullFileName );
216  break;
217 
218  case TREE_FILE_TYPE::NET:
219  case TREE_FILE_TYPE::TXT:
221  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &fullFileName );
222  break;
223 
225  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editDrawingSheet, true, &fullFileName );
226  break;
227 
230  packet = fullFileName.ToStdString();
232  break;
233 
237  packet = fullFileName.ToStdString();
239  break;
240 
241  default:
242  OpenFile( fullFileName );
243  break;
244  }
245 }
const wxString & GetFileName() const
IDs used in KiCad main frame foe menuitems and tools.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
void SetFileName(const wxString &name)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:292
This file is part of the common library.
void Activate(PROJECT_TREE_PANE *aTreePrjFrame)
TREE_FILE_TYPE GetType() const
TREE_FILE_TYPE
void SetPopulated(bool aValue)
const wxString SchLegacyFileName()
static TOOL_ACTION viewGerbers
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:143
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:477
void SetState(int state)
PROJECT_TREE_PANE Window to display the tree files.
static TOOL_ACTION editOtherPCB
Master controller class:
Definition: tool_manager.h:54
const wxString SchFileName()
static TOOL_ACTION editSchematic
PROJECT_TREE * m_TreeProject
static TOOL_ACTION editFootprints
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
#define _(s)
TREE_FILE_TYPE m_type
void PrintFile(const wxString &file)
Definition: gestfich.cpp:314
bool Rename(const wxString &name, bool check=true)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
const wxString GetDir() const
wxTreeCtrl * m_parent
static TOOL_ACTION editOtherSch
static TOOL_ACTION editPCB
const wxString PcbLegacyFileName()
const char * name
Definition: DXF_plotter.cpp:56
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
void SetType(TREE_FILE_TYPE aType)
const wxString PcbFileName()
static wxString GetFileExt(TREE_FILE_TYPE type)
void OpenFile(const wxString &file)
Definition: gestfich.cpp:209
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
void SetRootFile(bool aValue)
void LoadProject(const wxFileName &aProjectFileName)
KICAD_MANAGER_FRAME * m_Parent
static TOOL_ACTION editSymbols
The main KiCad project manager frame.
static TOOL_ACTION openTextEditor
static TOOL_ACTION editDrawingSheet
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:158