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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
24
25
26#include <wx/regex.h>
27#include <wx/textfile.h>
28#include <wx/filename.h>
29
30#include <set>
31
32#include <confirm.h>
33#include <mail_type.h>
34#include <gestfich.h>
36#include <kiplatform/io.h>
37#include <kiway.h>
38#include <project.h>
40#include <tool/tool_manager.h>
42#include <wx/msgdlg.h>
43
44#include "kicad_manager_frame.h"
45#include "project_tree.h"
46#include "pgm_kicad.h"
47#include "project_tree_pane.h"
48#include "project_tree_item.h"
49#include "kicad_id.h"
50
51
53 wxTreeCtrl* parent ) :
54 wxTreeItemData()
55{
56 m_parent = parent;
57 SetType( type );
58 SetFileName( data );
59 SetRootFile( false ); // true only for the root item of the tree (the project name)
60 SetPopulated( false );
61 m_state = 0;
62}
63
64
66{
67 int treeEnumMax = static_cast<int>( TREE_FILE_TYPE::MAX );
68
69 if( state < 0 || state >= m_parent->GetImageCount() / ( treeEnumMax - 2 ) )
70 return;
71
72 m_state = state;
73 int imgid = static_cast<int>( m_type ) - 1 + state * ( treeEnumMax - 1 );
74 m_parent->SetItemImage( GetId(), imgid );
75 m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
76}
77
78
97
98
99const wxString PROJECT_TREE_ITEM::GetDir() const
100{
102 return GetFileName();
103
104 return wxFileName( GetFileName() ).GetPath();
105}
106
107
108bool PROJECT_TREE_ITEM::Rename( const wxString& name, bool check )
109{
110 // this is broken & unsafe to use on linux.
111 if( !CanRename() )
112 return false;
113
114 if( name.IsEmpty() )
115 return false;
116
117 const wxString sep = wxFileName().GetPathSeparator();
118 wxString newFile;
119 wxString dirs = GetDir();
120
121 if( !dirs.IsEmpty() && GetType() != TREE_FILE_TYPE::DIRECTORY )
122 newFile = dirs + sep + name;
123 else
124 newFile = name;
125
126 if( newFile == GetFileName() )
127 return false;
128
129 // If required, prompt the user if the filename extension has changed:
130 wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() ).Lower();
131 wxString full_ext = wxT( "." ) + ext;
132
133 if( check && !ext.IsEmpty() && !newFile.Lower().EndsWith( full_ext ) )
134 {
135 KICAD_MESSAGE_DIALOG dialog( m_parent, _( "Changing file extension will change file type.\n"
136 "Do you want to continue ?" ),
137 _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
138
139 if( wxID_YES != dialog.ShowModal() )
140 return false;
141 }
142
143 if( !wxRenameFile( GetFileName(), newFile, false ) )
144 {
145 KICAD_MESSAGE_DIALOG( m_parent, _( "Unable to rename file ... " ), _( "Permission denied" ),
146 wxICON_ERROR | wxOK );
147 return false;
148 }
149
150 return true;
151}
152
153
155{
156 if( !CanDelete() )
157 return;
158
159 wxString errMsg;
160
161 if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
162 {
163#ifdef __WINDOWS__
164 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
165 GetFileName() );
166#else
167 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
168 GetFileName() );
169#endif
170
171 DisplayErrorMessage( m_parent, dialogMsg, errMsg );
172 return;
173 }
174
175 m_parent->Delete( GetId() );
176}
177
178
189static void ScanSchematicHierarchy( const wxString& aRootSchematic,
190 std::set<wxString>& aSheetFiles )
191{
192 if( aSheetFiles.count( aRootSchematic ) )
193 return;
194
195 aSheetFiles.insert( aRootSchematic );
196
197 wxTextFile file;
198
199 if( !file.Open( aRootSchematic ) )
200 return;
201
202 wxFileName rootFn( aRootSchematic );
203 wxString rootDir = rootFn.GetPath();
204
205 // Look for: (property "Sheetfile" "filename.kicad_sch"
206 wxRegEx sheetfileRe( "\\(property\\s+\"Sheetfile\"\\s+\"([^\"]+)\"" );
207
208 for( wxString line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine() )
209 {
210 if( sheetfileRe.Matches( line ) )
211 {
212 wxString sheetFile = sheetfileRe.GetMatch( line, 1 );
213
214 // Resolve relative path
215 wxFileName sheetFn( sheetFile );
216
217 if( !sheetFn.IsAbsolute() )
218 sheetFn.MakeAbsolute( rootDir );
219
220 wxString fullPath = sheetFn.GetFullPath();
221
222 if( wxFileExists( fullPath ) )
223 ScanSchematicHierarchy( fullPath, aSheetFiles );
224 }
225 }
226}
227
228
230{
231 wxString fullFileName = GetFileName();
232 wxTreeItemId id = GetId();
233 std::string packet;
234
235 KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
236 TOOL_MANAGER* toolMgr = frame->GetToolManager();
237 KIWAY& kiway = frame->Kiway();
238
239 switch( GetType() )
240 {
243 // Select a new project if this is not the current project:
244 if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
245 frame->LoadProject( fullFileName );
246
247 break;
248
250 frame->OpenJobsFile( fullFileName );
251
252 break;
253
255 m_parent->Toggle( id );
256 break;
257
260 {
261 wxString rootSchematic = frame->SchFileName();
262
263 if( rootSchematic.IsEmpty() )
264 rootSchematic = frame->SchLegacyFileName();
265
266 if( fullFileName == rootSchematic )
267 {
269 }
270 else
271 {
272 // Check if this file is part of the project hierarchy by scanning the schematic files
273 std::set<wxString> hierarchyFiles;
274
275 if( !rootSchematic.IsEmpty() && wxFileExists( rootSchematic ) )
276 ScanSchematicHierarchy( rootSchematic, hierarchyFiles );
277
278 // Flat designs keep every page in its own top-level sheet file
279 for( const TOP_LEVEL_SHEET_INFO& info : Prj().GetProjectFile().GetTopLevelSheets() )
280 {
281 wxFileName topSheet( info.filename );
282 topSheet.MakeAbsolute( Prj().GetProjectPath() );
283
284 if( topSheet.FileExists() )
285 ScanSchematicHierarchy( topSheet.GetFullPath(), hierarchyFiles );
286 }
287
288 bool isInHierarchy = hierarchyFiles.count( fullFileName ) > 0;
289
290 if( isInHierarchy )
291 {
292 // Open root schematic and navigate to the target sheet
293 KIWAY_PLAYER* schFrame = kiway.Player( FRAME_SCH, false );
294
295 if( !schFrame )
296 {
297 // Launch eeschema with the root schematic
299 schFrame = kiway.Player( FRAME_SCH, false );
300 }
301
302 if( schFrame )
303 {
304 packet = fullFileName.ToStdString();
306 }
307 }
308 else
309 {
310 // Not in hierarchy, open as standalone schematic
311 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherSch, &fullFileName );
312 }
313 }
314
315 break;
316 }
317
320 // Boards not part of the project are opened in a separate process.
321 if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
323 else
324 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherPCB, &fullFileName );
325
326 break;
327
333 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::viewGerbers, &fullFileName );
334 break;
335
337 wxLaunchDefaultBrowser( fullFileName );
338 break;
339
341 OpenPDF( fullFileName );
342 break;
343
348 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::openTextEditor, &fullFileName );
349 break;
350
352 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editDrawingSheet, &fullFileName );
353 break;
354
357 packet = fullFileName.ToStdString();
359 break;
360
364 packet = fullFileName.ToStdString();
366 break;
367
368 default:
369 wxLaunchDefaultApplication( fullFileName );
370 break;
371 }
372}
const char * name
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()
void OpenJobsFile(const wxFileName &aFileName, bool aCreate=false, bool aResaveProjectPreferences=true)
const wxString PcbLegacyFileName()
bool LoadProject(const wxFileName &aProjectFileName)
Loads a new project.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:407
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
Definition kiway.cpp:511
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)
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)
const wxString GetDir() const
void Activate(PROJECT_TREE_PANE *aTreePrjFrame)
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.
Master controller class:
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:48
#define _(s)
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ FRAME_SCH
Definition frame_type.h:30
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:39
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:380
PROJECT & Prj()
Definition kicad.cpp:730
@ MAIL_LIB_EDIT
Definition mail_type.h:51
@ MAIL_FP_EDIT
Definition mail_type.h:52
@ MAIL_SCH_NAVIGATE_TO_SHEET
Definition mail_type.h:58
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
static void ScanSchematicHierarchy(const wxString &aRootSchematic, std::set< wxString > &aSheetFiles)
Scan a schematic file hierarchy to collect all sheet file paths.
Information about a top-level schematic sheet.
TREE_FILE_TYPE