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, 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
28
29
30#include <wx/regex.h>
31#include <wx/textfile.h>
32#include <wx/filename.h>
33
34#include <set>
35
36#include <confirm.h>
37#include <mail_type.h>
38#include <gestfich.h>
40#include <kiplatform/io.h>
41#include <kiway.h>
42#include <tool/tool_manager.h>
44#include <wx/msgdlg.h>
45
46#include "kicad_manager_frame.h"
47#include "project_tree.h"
48#include "pgm_kicad.h"
49#include "project_tree_pane.h"
50#include "project_tree_item.h"
51#include "kicad_id.h"
52
53
55 wxTreeCtrl* parent ) :
56 wxTreeItemData()
57{
58 m_parent = parent;
59 SetType( type );
60 SetFileName( data );
61 SetRootFile( false ); // true only for the root item of the tree (the project name)
62 SetPopulated( false );
63 m_state = 0;
64}
65
66
68{
69 int treeEnumMax = static_cast<int>( TREE_FILE_TYPE::MAX );
70
71 if( state < 0 || state >= m_parent->GetImageCount() / ( treeEnumMax - 2 ) )
72 return;
73
74 m_state = state;
75 int imgid = static_cast<int>( m_type ) - 1 + state * ( treeEnumMax - 1 );
76 m_parent->SetItemImage( GetId(), imgid );
77 m_parent->SetItemImage( GetId(), imgid, wxTreeItemIcon_Selected );
78}
79
80
99
100
101const wxString PROJECT_TREE_ITEM::GetDir() const
102{
104 return GetFileName();
105
106 return wxFileName( GetFileName() ).GetPath();
107}
108
109
110bool PROJECT_TREE_ITEM::Rename( const wxString& name, bool check )
111{
112 // this is broken & unsafe to use on linux.
113 if( !CanRename() )
114 return false;
115
116 if( name.IsEmpty() )
117 return false;
118
119 const wxString sep = wxFileName().GetPathSeparator();
120 wxString newFile;
121 wxString dirs = GetDir();
122
123 if( !dirs.IsEmpty() && GetType() != TREE_FILE_TYPE::DIRECTORY )
124 newFile = dirs + sep + name;
125 else
126 newFile = name;
127
128 if( newFile == GetFileName() )
129 return false;
130
131 // If required, prompt the user if the filename extension has changed:
132 wxString ext = PROJECT_TREE_PANE::GetFileExt( GetType() ).Lower();
133 wxString full_ext = wxT( "." ) + ext;
134
135 if( check && !ext.IsEmpty() && !newFile.Lower().EndsWith( full_ext ) )
136 {
137 KICAD_MESSAGE_DIALOG dialog( m_parent, _( "Changing file extension will change file type.\n"
138 "Do you want to continue ?" ),
139 _( "Rename File" ), wxYES_NO | wxICON_QUESTION );
140
141 if( wxID_YES != dialog.ShowModal() )
142 return false;
143 }
144
145 if( !wxRenameFile( GetFileName(), newFile, false ) )
146 {
147 KICAD_MESSAGE_DIALOG( m_parent, _( "Unable to rename file ... " ), _( "Permission denied" ),
148 wxICON_ERROR | wxOK );
149 return false;
150 }
151
152 return true;
153}
154
155
157{
158 if( !CanDelete() )
159 return;
160
161 wxString errMsg;
162
163 if( !KIPLATFORM::ENV::MoveToTrash( GetFileName(), errMsg ) )
164 {
165#ifdef __WINDOWS__
166 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to recycle bin."),
167 GetFileName() );
168#else
169 wxString dialogMsg = wxString::Format( _( "Can not move '%s' to trash."),
170 GetFileName() );
171#endif
172
173 DisplayErrorMessage( m_parent, dialogMsg, errMsg );
174 return;
175 }
176
177 m_parent->Delete( GetId() );
178}
179
180
191static void ScanSchematicHierarchy( const wxString& aRootSchematic,
192 std::set<wxString>& aSheetFiles )
193{
194 if( aSheetFiles.count( aRootSchematic ) )
195 return;
196
197 aSheetFiles.insert( aRootSchematic );
198
199 wxTextFile file;
200
201 if( !file.Open( aRootSchematic ) )
202 return;
203
204 wxFileName rootFn( aRootSchematic );
205 wxString rootDir = rootFn.GetPath();
206
207 // Look for: (property "Sheetfile" "filename.kicad_sch"
208 wxRegEx sheetfileRe( "\\(property\\s+\"Sheetfile\"\\s+\"([^\"]+)\"" );
209
210 for( wxString line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine() )
211 {
212 if( sheetfileRe.Matches( line ) )
213 {
214 wxString sheetFile = sheetfileRe.GetMatch( line, 1 );
215
216 // Resolve relative path
217 wxFileName sheetFn( sheetFile );
218
219 if( !sheetFn.IsAbsolute() )
220 sheetFn.MakeAbsolute( rootDir );
221
222 wxString fullPath = sheetFn.GetFullPath();
223
224 if( wxFileExists( fullPath ) )
225 ScanSchematicHierarchy( fullPath, aSheetFiles );
226 }
227 }
228}
229
230
232{
233 wxString fullFileName = GetFileName();
234 wxTreeItemId id = GetId();
235 std::string packet;
236
237 KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
238 TOOL_MANAGER* toolMgr = frame->GetToolManager();
239 KIWAY& kiway = frame->Kiway();
240
241 switch( GetType() )
242 {
245 // Select a new project if this is not the current project:
246 if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
247 frame->LoadProject( fullFileName );
248
249 break;
250
252 frame->OpenJobsFile( fullFileName );
253
254 break;
255
257 m_parent->Toggle( id );
258 break;
259
262 {
263 wxString rootSchematic = frame->SchFileName();
264
265 if( rootSchematic.IsEmpty() )
266 rootSchematic = frame->SchLegacyFileName();
267
268 if( fullFileName == rootSchematic )
269 {
271 }
272 else
273 {
274 // Check if this file is part of the project hierarchy by scanning the schematic files
275 std::set<wxString> hierarchyFiles;
276
277 if( !rootSchematic.IsEmpty() && wxFileExists( rootSchematic ) )
278 ScanSchematicHierarchy( rootSchematic, hierarchyFiles );
279
280 bool isInHierarchy = hierarchyFiles.count( fullFileName ) > 0;
281
282 if( isInHierarchy )
283 {
284 // Open root schematic and navigate to the target sheet
285 KIWAY_PLAYER* schFrame = kiway.Player( FRAME_SCH, false );
286
287 if( !schFrame )
288 {
289 // Launch eeschema with the root schematic
291 schFrame = kiway.Player( FRAME_SCH, false );
292 }
293
294 if( schFrame )
295 {
296 packet = fullFileName.ToStdString();
298 }
299 }
300 else
301 {
302 // Not in hierarchy, open as standalone schematic
303 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherSch, &fullFileName );
304 }
305 }
306
307 break;
308 }
309
312 // Boards not part of the project are opened in a separate process.
313 if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
315 else
316 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editOtherPCB, &fullFileName );
317
318 break;
319
325 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::viewGerbers, &fullFileName );
326 break;
327
329 wxLaunchDefaultBrowser( fullFileName );
330 break;
331
333 OpenPDF( fullFileName );
334 break;
335
340 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::openTextEditor, &fullFileName );
341 break;
342
344 toolMgr->RunAction<wxString*>( KICAD_MANAGER_ACTIONS::editDrawingSheet, &fullFileName );
345 break;
346
349 packet = fullFileName.ToStdString();
351 break;
352
356 packet = fullFileName.ToStdString();
358 break;
359
360 default:
361 wxLaunchDefaultApplication( fullFileName );
362 break;
363 }
364}
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)
void LoadProject(const wxFileName &aProjectFileName)
const wxString PcbLegacyFileName()
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:294
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition kiway.cpp:403
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:507
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)
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.
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:202
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:52
#define _(s)
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:35
@ FRAME_SCH
Definition frame_type.h:34
@ 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:257
@ MAIL_LIB_EDIT
Definition mail_type.h:55
@ MAIL_FP_EDIT
Definition mail_type.h:56
@ MAIL_SCH_NAVIGATE_TO_SHEET
Definition mail_type.h:60
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.
TREE_FILE_TYPE