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-2021 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 
76 {
89  return false;
90 
91  return true;
92 }
93 
94 
95 const wxString PROJECT_TREE_ITEM::GetDir() const
96 {
98  return GetFileName();
99 
100  return wxFileName( GetFileName() ).GetPath();
101 }
102 
103 
104 bool PROJECT_TREE_ITEM::Rename( const wxString& name, bool check )
105 {
106  // this is broken & unsafe to use on linux.
107  if( !CanRename() )
108  return false;
109 
110  if( name.IsEmpty() )
111  return false;
112 
113  const wxString sep = wxFileName().GetPathSeparator();
114  wxString newFile;
115  wxString dirs = GetDir();
116 
117  if( !dirs.IsEmpty() && GetType() != TREE_FILE_TYPE::DIRECTORY )
118  newFile = dirs + sep + name;
119  else
120  newFile = name;
121 
122  if( newFile == GetFileName() )
123  return false;
124 
125  wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() );
126  wxRegEx reg( wxT( "^.*\\" ) + ext + wxT( "$" ), wxRE_ICASE );
127 
128  if( check && !ext.IsEmpty() && !reg.Matches( newFile ) )
129  {
130  wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
131  "Do you want to continue ?" ),
132  _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
133 
134  if( wxID_YES != dialog.ShowModal() )
135  return false;
136  }
137 
138  if( !wxRenameFile( GetFileName(), newFile, false ) )
139  {
140  wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
141  wxICON_ERROR | wxOK );
142  return false;
143  }
144 
145  return true;
146 }
147 
148 
150 {
151  if( !CanDelete() )
152  return;
153 
154  wxString errMsg;
155 
156  if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
157  {
158 #ifdef __WINDOWS__
159  wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
160  GetFileName() );
161 #else
162  wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
163  GetFileName() );
164 #endif
165 
166  DisplayErrorMessage( m_parent, dialogMsg, errMsg );
167  return;
168  }
169 
170  m_parent->Delete( GetId() );
171 }
172 
173 
175 {
176  wxString sep = wxFileName::GetPathSeparator();
177  wxString fullFileName = GetFileName();
178  wxTreeItemId id = GetId();
179  std::string packet;
180 
181  KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
182  TOOL_MANAGER* toolMgr = frame->GetToolManager();
183  KIWAY& kiway = frame->Kiway();
184 
185  switch( GetType() )
186  {
189  // Select a new project if this is not the current project:
190  if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
191  frame->LoadProject( fullFileName );
192 
193  break;
194 
196  m_parent->Toggle( id );
197  break;
198 
201  // Schematics not part of the project are opened in a separate process.
202  if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() )
204  else
205  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
206 
207  break;
208 
211  // Boards not part of the project are opened in a separate process.
212  if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
213  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editPCB, true );
214  else
215  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
216 
217  break;
218 
224  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::viewGerbers, true, &fullFileName );
225  break;
226 
228  wxLaunchDefaultBrowser( fullFileName );
229  break;
230 
231  case TREE_FILE_TYPE::PDF:
232  OpenPDF( fullFileName );
233  break;
234 
235  case TREE_FILE_TYPE::NET:
236  case TREE_FILE_TYPE::TXT:
238  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::openTextEditor, true, &fullFileName );
239  break;
240 
242  toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editDrawingSheet, true, &fullFileName );
243  break;
244 
247  packet = fullFileName.ToStdString();
249  break;
250 
254  packet = fullFileName.ToStdString();
256  break;
257 
258  default:
259  OpenFile( fullFileName );
260  break;
261  }
262 }
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
bool CanRename() const
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()
bool CanDelete() const
Determine if a file can be deleted via the project tree pane.
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
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:195
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:159