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#include <wx/msgdlg.h>
40
41#include "kicad_manager_frame.h"
42#include "project_tree.h"
43#include "pgm_kicad.h"
44#include "project_tree_pane.h"
45#include "project_tree_item.h"
46#include "kicad_id.h"
47
48
50 wxTreeCtrl* parent ) :
51 wxTreeItemData()
52{
53 m_parent = parent;
54 SetType( type );
55 SetFileName( data );
56 SetRootFile( false ); // true only for the root item of the tree (the project name)
57 SetPopulated( false );
58 m_state = 0;
59}
60
61
63{
64 int treeEnumMax = static_cast<int>( TREE_FILE_TYPE::MAX );
65
66 if( state < 0 || state >= m_parent->GetImageCount() / ( treeEnumMax - 2 ) )
67 return;
68
69 m_state = state;
70 int imgid = static_cast<int>( m_type ) - 1 + state * ( treeEnumMax - 1 );
71 m_parent->SetItemImage( GetId(), imgid );
72 m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
73}
74
75
77{
78 if( m_type == TREE_FILE_TYPE::DIRECTORY
79 || m_type == TREE_FILE_TYPE::LEGACY_PROJECT
80 || m_type == TREE_FILE_TYPE::JSON_PROJECT
81 || m_type == TREE_FILE_TYPE::LEGACY_SCHEMATIC
82 || m_type == TREE_FILE_TYPE::SEXPR_SCHEMATIC
83 || m_type == TREE_FILE_TYPE::LEGACY_PCB
84 || m_type == TREE_FILE_TYPE::SEXPR_PCB
85 || m_type == TREE_FILE_TYPE::DRAWING_SHEET
86 || m_type == TREE_FILE_TYPE::FOOTPRINT_FILE
87 || m_type == TREE_FILE_TYPE::SCHEMATIC_LIBFILE
88 || m_type == TREE_FILE_TYPE::SEXPR_SYMBOL_LIB_FILE
89 || m_type == TREE_FILE_TYPE::DESIGN_RULES )
90 return false;
91
92 return true;
93}
94
95
96const wxString PROJECT_TREE_ITEM::GetDir() const
97{
98 if( TREE_FILE_TYPE::DIRECTORY == m_type )
99 return GetFileName();
100
101 return wxFileName( GetFileName() ).GetPath();
102}
103
104
105bool PROJECT_TREE_ITEM::Rename( const wxString& name, bool check )
106{
107 // this is broken & unsafe to use on linux.
108 if( !CanRename() )
109 return false;
110
111 if( name.IsEmpty() )
112 return false;
113
114 const wxString sep = wxFileName().GetPathSeparator();
115 wxString newFile;
116 wxString dirs = GetDir();
117
118 if( !dirs.IsEmpty() && GetType() != TREE_FILE_TYPE::DIRECTORY )
119 newFile = dirs + sep + name;
120 else
121 newFile = name;
122
123 if( newFile == GetFileName() )
124 return false;
125
126 // If required, prompt the user if the filename extension has changed:
127 wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() ).Lower();
128 wxString full_ext = wxT( "." ) + ext;
129
130 if( check && !ext.IsEmpty() && !newFile.Lower().EndsWith( full_ext ) )
131 {
132 wxMessageDialog dialog( m_parent, _( "Changing file extension will change file type.\n"
133 "Do you want to continue ?" ),
134 _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
135
136 if( wxID_YES != dialog.ShowModal() )
137 return false;
138 }
139
140 if( !wxRenameFile( GetFileName(), newFile, false ) )
141 {
142 wxMessageDialog( m_parent, _( "Unable to rename file ... " ), _( "Permission error?" ),
143 wxICON_ERROR | wxOK );
144 return false;
145 }
146
147 return true;
148}
149
150
152{
153 if( !CanDelete() )
154 return;
155
156 wxString errMsg;
157
158 if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
159 {
160#ifdef __WINDOWS__
161 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
162 GetFileName() );
163#else
164 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
165 GetFileName() );
166#endif
167
168 DisplayErrorMessage( m_parent, dialogMsg, errMsg );
169 return;
170 }
171
172 m_parent->Delete( GetId() );
173}
174
175
177{
178 wxString fullFileName = GetFileName();
179 wxTreeItemId id = GetId();
180 std::string packet;
181
182 KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
183 TOOL_MANAGER* toolMgr = frame->GetToolManager();
184 KIWAY& kiway = frame->Kiway();
185
186 switch( GetType() )
187 {
188 case TREE_FILE_TYPE::LEGACY_PROJECT:
189 case TREE_FILE_TYPE::JSON_PROJECT:
190 // Select a new project if this is not the current project:
191 if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
192 frame->LoadProject( fullFileName );
193
194 break;
195
196 case TREE_FILE_TYPE::DIRECTORY:
197 m_parent->Toggle( id );
198 break;
199
200 case TREE_FILE_TYPE::LEGACY_SCHEMATIC:
201 case TREE_FILE_TYPE::SEXPR_SCHEMATIC:
202 // Schematics not part of the project are opened in a separate process.
203 if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() )
205 else
206 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherSch, &fullFileName );
207
208 break;
209
210 case TREE_FILE_TYPE::LEGACY_PCB:
211 case TREE_FILE_TYPE::SEXPR_PCB:
212 // Boards not part of the project are opened in a separate process.
213 if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
215 else
216 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherPCB, &fullFileName );
217
218 break;
219
220 case TREE_FILE_TYPE::GERBER:
221 case TREE_FILE_TYPE::GERBER_JOB_FILE:
222 case TREE_FILE_TYPE::DRILL:
223 case TREE_FILE_TYPE::DRILL_NC:
224 case TREE_FILE_TYPE::DRILL_XNC:
225 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::viewGerbers, &fullFileName );
226 break;
227
228 case TREE_FILE_TYPE::HTML:
229 wxLaunchDefaultBrowser( fullFileName );
230 break;
231
232 case TREE_FILE_TYPE::PDF:
233 OpenPDF( fullFileName );
234 break;
235
236 case TREE_FILE_TYPE::NET:
237 case TREE_FILE_TYPE::TXT:
238 case TREE_FILE_TYPE::MD:
239 case TREE_FILE_TYPE::REPORT:
240 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::openTextEditor, &fullFileName );
241 break;
242
243 case TREE_FILE_TYPE::DRAWING_SHEET:
244 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editDrawingSheet, &fullFileName );
245 break;
246
247 case TREE_FILE_TYPE::FOOTPRINT_FILE:
249 packet = fullFileName.ToStdString();
251 break;
252
253 case TREE_FILE_TYPE::SCHEMATIC_LIBFILE:
254 case TREE_FILE_TYPE::SEXPR_SYMBOL_LIB_FILE:
256 packet = fullFileName.ToStdString();
258 break;
259
260 default:
261 OpenFile( fullFileName );
262 break;
263 }
264}
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:55
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:527
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:62
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:150
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:186
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