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>
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
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
95const wxString PROJECT_TREE_ITEM::GetDir() const
96{
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 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 fullFileName = GetFileName();
177 wxTreeItemId id = GetId();
178 std::string packet;
179
180 KICAD_MANAGER_FRAME* frame = aTreePrjFrame->m_Parent;
181 TOOL_MANAGER* toolMgr = frame->GetToolManager();
182 KIWAY& kiway = frame->Kiway();
183
184 switch( GetType() )
185 {
188 // Select a new project if this is not the current project:
189 if( id != aTreePrjFrame->m_TreeProject->GetRootItem() )
190 frame->LoadProject( fullFileName );
191
192 break;
193
195 m_parent->Toggle( id );
196 break;
197
200 // Schematics not part of the project are opened in a separate process.
201 if( fullFileName == frame->SchFileName() || fullFileName == frame->SchLegacyFileName() )
203 else
204 toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherSch, true, &fullFileName );
205
206 break;
207
210 // Boards not part of the project are opened in a separate process.
211 if( fullFileName == frame->PcbFileName() || fullFileName == frame->PcbLegacyFileName() )
213 else
214 toolMgr->RunAction( KICAD_MANAGER_ACTIONS::editOtherPCB, true, &fullFileName );
215
216 break;
217
223 toolMgr->RunAction( KICAD_MANAGER_ACTIONS::viewGerbers, true, &fullFileName );
224 break;
225
227 wxLaunchDefaultBrowser( fullFileName );
228 break;
229
231 OpenPDF( fullFileName );
232 break;
233
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 char * name
Definition: DXF_plotter.cpp:56
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:273
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:488
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:54
Master controller class:
Definition: tool_manager.h:55
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:299
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:41
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:159
void OpenFile(const wxString &file)
Definition: gestfich.cpp:195
IDs used in KiCad main frame foe menuitems and tools.
@ MAIL_LIB_EDIT
Definition: mail_type.h:53
@ MAIL_FP_EDIT
Definition: mail_type.h:54
bool MoveToTrash(const wxString &aPath, wxString &aError)
Move the specified file/directory to the trash bin/recycle bin.
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
TREE_FILE_TYPE