KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2024 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>
35#include <kiplatform/io.h>
36#include <kiway.h>
37#include <tool/tool_manager.h>
39
40#include "kicad_manager_frame.h"
41#include "project_tree.h"
42#include "pgm_kicad.h"
43#include "project_tree_pane.h"
44#include "project_tree_item.h"
45#include "kicad_id.h"
46
47
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
62{
63 int treeEnumMax = static_cast<int>( TREE_FILE_TYPE::MAX );
64
65 if( state < 0 || state >= m_parent->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{
77 if( m_type == TREE_FILE_TYPE::DIRECTORY
78 || m_type == TREE_FILE_TYPE::LEGACY_PROJECT
79 || m_type == TREE_FILE_TYPE::JSON_PROJECT
80 || m_type == TREE_FILE_TYPE::LEGACY_SCHEMATIC
81 || m_type == TREE_FILE_TYPE::SEXPR_SCHEMATIC
82 || m_type == TREE_FILE_TYPE::LEGACY_PCB
83 || m_type == TREE_FILE_TYPE::SEXPR_PCB
84 || m_type == TREE_FILE_TYPE::DRAWING_SHEET
85 || m_type == TREE_FILE_TYPE::FOOTPRINT_FILE
86 || m_type == TREE_FILE_TYPE::SCHEMATIC_LIBFILE
87 || m_type == TREE_FILE_TYPE::SEXPR_SYMBOL_LIB_FILE
88 || m_type == TREE_FILE_TYPE::DESIGN_RULES )
89 return false;
90
91 return true;
92}
93
94
95const wxString PROJECT_TREE_ITEM::GetDir() const
96{
97 if( TREE_FILE_TYPE::DIRECTORY == m_type )
98 return GetFileName();
99
100 return wxFileName( GetFileName() ).GetPath();
101}
102
103
104bool 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 // If required, prompt the user if the filename extension has changed:
126 wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() ).Lower();
127 wxString full_ext = wxT( "." ) + ext;
128
129 if( check && !ext.IsEmpty() && !newFile.Lower().EndsWith( full_ext ) )
130 {
131 wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
132 "Do you want to continue ?" ),
133 _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
134
135 if( wxID_YES != dialog.ShowModal() )
136 return false;
137 }
138
139 if( !wxRenameFile( GetFileName(), newFile, false ) )
140 {
141 wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
142 wxICON_ERROR | wxOK );
143 return false;
144 }
145
146 return true;
147}
148
149
151{
152 if( !CanDelete() )
153 return;
154
155 wxString errMsg;
156
157 if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
158 {
159#ifdef __WINDOWS__
160 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
161 GetFileName() );
162#else
163 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
164 GetFileName() );
165#endif
166
167 DisplayErrorMessage( m_parent, dialogMsg, errMsg );
168 return;
169 }
170
171 m_parent->Delete( GetId() );
172}
173
174
176{
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 {
187 case TREE_FILE_TYPE::LEGACY_PROJECT:
188 case TREE_FILE_TYPE::JSON_PROJECT:
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
195 case TREE_FILE_TYPE::DIRECTORY:
196 m_parent->Toggle( id );
197 break;
198
199 case TREE_FILE_TYPE::LEGACY_SCHEMATIC:
200 case TREE_FILE_TYPE::SEXPR_SCHEMATIC:
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<wxString*>( KICAD_MANAGER_ACTIONS::editOtherSch, &fullFileName );
206
207 break;
208
209 case TREE_FILE_TYPE::LEGACY_PCB:
210 case TREE_FILE_TYPE::SEXPR_PCB:
211 // Boards not part of the project are opened in a separate process.
212 if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
214 else
215 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherPCB, &fullFileName );
216
217 break;
218
219 case TREE_FILE_TYPE::GERBER:
220 case TREE_FILE_TYPE::GERBER_JOB_FILE:
221 case TREE_FILE_TYPE::DRILL:
222 case TREE_FILE_TYPE::DRILL_NC:
223 case TREE_FILE_TYPE::DRILL_XNC:
224 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::viewGerbers, &fullFileName );
225 break;
226
227 case TREE_FILE_TYPE::HTML:
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:
237 case TREE_FILE_TYPE::MD:
238 case TREE_FILE_TYPE::REPORT:
239 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::openTextEditor, &fullFileName );
240 break;
241
242 case TREE_FILE_TYPE::DRAWING_SHEET:
243 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editDrawingSheet, &fullFileName );
244 break;
245
246 case TREE_FILE_TYPE::FOOTPRINT_FILE:
248 packet = fullFileName.ToStdString();
250 break;
251
252 case TREE_FILE_TYPE::SCHEMATIC_LIBFILE:
253 case TREE_FILE_TYPE::SEXPR_SYMBOL_LIB_FILE:
255 packet = fullFileName.ToStdString();
257 break;
258
259 default:
260 OpenFile( fullFileName );
261 break;
262 }
263}
const char * name
Definition: DXF_plotter.cpp:57
static TOOL_ACTION editPCB
static TOOL_ACTION editOtherPCB
static TOOL_ACTION editOtherSch
static TOOL_ACTION editSchematic
static TOOL_ACTION openTextEditor
static TOOL_ACTION editDrawingSheet
static TOOL_ACTION editFootprints
static TOOL_ACTION viewGerbers
static TOOL_ACTION editSymbols
The main KiCad project manager frame.
const wxString SchLegacyFileName()
const wxString SchFileName()
void LoadProject(const wxFileName &aProjectFileName)
const wxString PcbLegacyFileName()
const wxString PcbFileName()
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:279
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:553
bool CanDelete() const
Determine if a file can be deleted via the project tree pane.
void SetRootFile(bool aValue)
TREE_FILE_TYPE m_type
bool Rename(const wxString &name, bool check=true)
wxTreeCtrl * m_parent
void SetState(int state)
const wxString & GetFileName() const
void SetType(TREE_FILE_TYPE aType)
void SetPopulated(bool aValue)
TREE_FILE_TYPE GetType() const
void SetFileName(const wxString &name)
bool CanRename() const
const wxString GetDir() const
void Activate(PROJECT_TREE_PANE *aTreePrjFrame)
PROJECT_TREE_PANE Window to display the tree files.
PROJECT_TREE * m_TreeProject
KICAD_MANAGER_FRAME * m_Parent
static wxString GetFileExt(TREE_FILE_TYPE type)
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:55
Master controller class:
Definition: tool_manager.h:57
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:145
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
@ FRAME_SCH_SYMBOL_EDITOR
Definition: frame_type.h:35
@ FRAME_FOOTPRINT_EDITOR
Definition: frame_type.h:43
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:250
void OpenFile(const wxString &file)
Definition: gestfich.cpp:286
IDs used in KiCad main frame foe menuitems and tools.
@ MAIL_LIB_EDIT
Definition: mail_type.h:54
@ MAIL_FP_EDIT
Definition: mail_type.h:55
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
TREE_FILE_TYPE