KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_tree.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) 2004-2012 Jean-Pierre Charras
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <bitmaps.h>
24#include <wx/settings.h>
25#include <wx/dcmemory.h>
26
27#include "project_tree_item.h"
28#include "project_tree_pane.h"
29#include "project_tree.h"
30#include "kicad_id.h"
31
32
33IMPLEMENT_ABSTRACT_CLASS( PROJECT_TREE, wxTreeCtrl )
34
35
36#ifdef __WXMSW__
37#define PLATFORM_STYLE wxTR_LINES_AT_ROOT
38#else
39#define PLATFORM_STYLE wxTR_NO_LINES
40#endif
41
43 wxTreeCtrl( parent, ID_PROJECT_TREE, wxDefaultPosition, wxDefaultSize,
44 PLATFORM_STYLE | wxTR_HAS_BUTTONS | wxTR_MULTIPLE, wxDefaultValidator,
45 wxT( "EDATreeCtrl" ) ),
46 m_statusImageList( nullptr )
47{
48 m_projectTreePane = parent;
49 m_gitCommon = std::make_unique<KIGIT_COMMON>( nullptr );
50
51 // Make sure the GUI font scales properly on GTK
52 SetFont( KIUI::GetControlFont( this ) );
53
54 LoadIcons();
55}
56
57
59{
60 // We pass ownership of these to wxWidgets in SetImageList() and SetStateImageList()
61 //delete m_statusImageList;
62}
63
64
66{
67#ifdef __WXMAC__
68 const int c_fileDefSize = 18;
69 const int c_gitDefSize = 16;
70#else
71 const int c_fileDefSize = 22;
72 const int c_gitDefSize = 16;
73#endif
74
75 auto getBundle = [&]( BITMAPS aBmp, int aDefSize )
76 {
77#ifdef __WXMSW__
78 // Add padding to bitmaps because MSW is the only platform that doesn't get it automatically
79 const int c_padding = 1;
80 wxVector<wxBitmap> bmps;
81
82 for( double scale : { 1.0, 1.25, 1.5, 1.75, 2.0, 3.0 } )
83 {
84 int size = aDefSize * scale;
85 int paddedSize = size + c_padding * 2 * scale;
86 wxBitmapBundle scaled = KiBitmapBundleDef( aBmp, size );
87 wxBitmap bmp = scaled.GetBitmap( wxDefaultSize );
88 wxBitmap padded( paddedSize, paddedSize, 32 );
89
90 {
91 padded.UseAlpha();
92 wxMemoryDC dc( padded );
93 dc.DrawBitmap( bmp, c_padding * scale, c_padding * scale );
94 }
95
96 bmps.push_back( padded );
97 }
98
99 return wxBitmapBundle::FromBitmaps( bmps );
100#else
101 return KiBitmapBundleDef( aBmp, aDefSize );
102#endif
103 };
104
105 wxVector<wxBitmapBundle> images;
106 images.push_back( getBundle( BITMAPS::project, c_fileDefSize ) ); // TREE_LEGACY_PROJECT
107 images.push_back( getBundle( BITMAPS::project_kicad, c_fileDefSize ) ); // TREE_JSON_PROJECT
108 images.push_back( getBundle( BITMAPS::icon_eeschema_24, c_fileDefSize ) ); // TREE_LEGACY_SCHEMATIC
109 images.push_back( getBundle( BITMAPS::icon_eeschema_24, c_fileDefSize ) ); // TREE_SEXPR_SCHEMATIC
110 images.push_back( getBundle( BITMAPS::icon_pcbnew_24, c_fileDefSize ) ); // TREE_LEGACY_PCB
111 images.push_back( getBundle( BITMAPS::icon_pcbnew_24, c_fileDefSize ) ); // TREE_SEXPR_PCB
112 images.push_back( getBundle( BITMAPS::icon_gerbview_24, c_fileDefSize ) ); // TREE_GERBER
113 images.push_back( getBundle( BITMAPS::file_gerber_job, c_fileDefSize ) ); // TREE_GERBER_JOB_FILE (.gbrjob)
114 images.push_back( getBundle( BITMAPS::file_html, c_fileDefSize ) ); // TREE_HTML
115 images.push_back( getBundle( BITMAPS::file_pdf, c_fileDefSize ) ); // TREE_PDF
116 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // TREE_TXT
117 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // TREE_MD
118 images.push_back( getBundle( BITMAPS::netlist, c_fileDefSize ) ); // TREE_NET
119 images.push_back( getBundle( BITMAPS::file_cir, c_fileDefSize ) ); // TREE_NET_SPICE
120 images.push_back( getBundle( BITMAPS::unknown, c_fileDefSize ) ); // TREE_UNKNOWN
121 images.push_back( getBundle( BITMAPS::directory, c_fileDefSize ) ); // TREE_DIRECTORY
122 images.push_back( getBundle( BITMAPS::icon_cvpcb_24, c_fileDefSize ) ); // TREE_CMP_LINK
123 images.push_back( getBundle( BITMAPS::tools, c_fileDefSize ) ); // TREE_REPORT
124 images.push_back( getBundle( BITMAPS::file_pos, c_fileDefSize ) ); // TREE_POS
125 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL
126 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL_NC (similar TREE_DRILL)
127 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL_XNC (similar TREE_DRILL)
128 images.push_back( getBundle( BITMAPS::file_svg, c_fileDefSize ) ); // TREE_SVG
129 images.push_back( getBundle( BITMAPS::file_csv, c_fileDefSize ) ); // TREE_CSV
130 images.push_back( getBundle( BITMAPS::icon_pagelayout_editor_24, c_fileDefSize ) ); // TREE_PAGE_LAYOUT_DESCR
131 images.push_back( getBundle( BITMAPS::module, c_fileDefSize ) ); // TREE_FOOTPRINT_FILE
132 images.push_back( getBundle( BITMAPS::library, c_fileDefSize ) ); // TREE_SCHEMATIC_LIBFILE
133 images.push_back( getBundle( BITMAPS::library, c_fileDefSize ) ); // TREE_SEXPR_SYMBOL_LIB_FILE
134 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // DESIGN_RULES
135 images.push_back( getBundle( BITMAPS::zip, c_fileDefSize ) ); // ZIP_ARCHIVE
136 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // JOBSET_FILE
137
138 SetImages( images );
139
140 // KiCad for macOS currently has backported SetStateImages for this control
141 // that is otherwise available since wxWidgets 3.3 on other platforms.
142#if wxCHECK_VERSION( 3, 3, 0 ) || defined( __WXMAC__ )
143 wxVector<wxBitmapBundle> stateImages;
144 stateImages.push_back( wxBitmapBundle( wxBitmap( c_gitDefSize, c_gitDefSize ) ) ); // GIT_STATUS_UNTRACKED
145 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_good_check, c_gitDefSize ) ); // GIT_STATUS_CURRENT
146 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_modified, c_gitDefSize ) ); // GIT_STATUS_MODIFIED
147 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_add, c_gitDefSize ) ); // GIT_STATUS_ADDED
148 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_delete, c_gitDefSize ) ); // GIT_STATUS_DELETED
149 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_out_of_date, c_gitDefSize ) ); // GIT_STATUS_BEHIND
150 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_changed_ahead, c_gitDefSize ) ); // GIT_STATUS_AHEAD
151 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_conflict, c_gitDefSize ) ); // GIT_STATUS_CONFLICTED
152 stateImages.push_back( wxBitmapBundle( wxBitmap( c_gitDefSize, c_gitDefSize ) ) ); // GIT_STATUS_IGNORED
153
154 SetStateImages( stateImages );
155#else
156 // Make an image list containing small icons
157 wxBitmap blank_bitmap( c_gitDefSize, c_gitDefSize );
158
159 delete m_statusImageList;
160 m_statusImageList = new wxImageList( c_gitDefSize, c_gitDefSize, true,
161 static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_LAST ) );
162
163 m_statusImageList->Add( blank_bitmap ); // GIT_STATUS_UNTRACKED
164 m_statusImageList->Add( KiBitmap( BITMAPS::git_good_check, c_gitDefSize ) ); // GIT_STATUS_CURRENT
165 m_statusImageList->Add( KiBitmap( BITMAPS::git_modified, c_gitDefSize ) ); // GIT_STATUS_MODIFIED
166 m_statusImageList->Add( KiBitmap( BITMAPS::git_add, c_gitDefSize ) ); // GIT_STATUS_ADDED
167 m_statusImageList->Add( KiBitmap( BITMAPS::git_delete, c_gitDefSize ) ); // GIT_STATUS_DELETED
168 m_statusImageList->Add( KiBitmap( BITMAPS::git_out_of_date, c_gitDefSize ) ); // GIT_STATUS_BEHIND
169 m_statusImageList->Add( KiBitmap( BITMAPS::git_changed_ahead, c_gitDefSize ) ); // GIT_STATUS_AHEAD
170 m_statusImageList->Add( KiBitmap( BITMAPS::git_conflict, c_gitDefSize ) ); // GIT_STATUS_CONFLICTED
171 m_statusImageList->Add( blank_bitmap ); // GIT_STATUS_IGNORED
172
173 SetStateImageList( m_statusImageList );
174#endif
175
176}
177
178
179void PROJECT_TREE::GetItemsRecursively( const wxTreeItemId& aParentId, std::vector<wxTreeItemId>& aItems )
180{
181 wxTreeItemIdValue cookie;
182 wxTreeItemId child = GetFirstChild( aParentId, cookie );
183
184 while( child.IsOk() )
185 {
186 aItems.push_back( child );
187 GetItemsRecursively( child, aItems );
188 child = GetNextChild( aParentId, cookie );
189 }
190}
191
192
193int PROJECT_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemId& item2 )
194{
195 PROJECT_TREE_ITEM* myitem1 = (PROJECT_TREE_ITEM*) GetItemData( item1 );
196 PROJECT_TREE_ITEM* myitem2 = (PROJECT_TREE_ITEM*) GetItemData( item2 );
197
198 if( !myitem1 || !myitem2 )
199 return 0;
200
201 if( myitem1->GetType() == TREE_FILE_TYPE::DIRECTORY
202 && myitem2->GetType() != TREE_FILE_TYPE::DIRECTORY )
203 return -1;
204
205 if( myitem2->GetType() == TREE_FILE_TYPE::DIRECTORY
206 && myitem1->GetType() != TREE_FILE_TYPE::DIRECTORY )
207 return 1;
208
209 if( myitem1->IsRootFile() && !myitem2->IsRootFile() )
210 return -1;
211
212 if( myitem2->IsRootFile() && !myitem1->IsRootFile() )
213 return 1;
214
215 return myitem1->GetFileName().CmpNoCase( myitem2->GetFileName() );
216}
wxBitmapBundle KiBitmapBundleDef(BITMAPS aBitmap, int aDefHeight)
Constructs and returns a bitmap bundle for the given icon ID, with the default bitmap size being aDef...
Definition bitmap.cpp:112
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition bitmap.cpp:100
BITMAPS
A list of all bitmap identifiers.
@ git_out_of_date
@ icon_eeschema_24
@ icon_pagelayout_editor_24
@ file_gerber_job
@ git_changed_ahead
@ icon_gerbview_24
Handle one item (a file or a directory name) for the tree file.
const wxString & GetFileName() const
TREE_FILE_TYPE GetType() const
bool IsRootFile() const
PROJECT_TREE_PANE Window to display the tree files.
PROJECT_TREE This is the class to show (as a tree) the files in the project directory.
PROJECT_TREE_PANE * m_projectTreePane
void GetItemsRecursively(const wxTreeItemId &aParentId, std::vector< wxTreeItemId > &aItems)
int OnCompareItems(const wxTreeItemId &item1, const wxTreeItemId &item2) override
wxImageList * m_statusImageList
PROJECT_TREE(PROJECT_TREE_PANE *parent)
std::unique_ptr< KIGIT_COMMON > m_gitCommon
@ ID_PROJECT_TREE
Definition kicad_id.h:32
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
#define PLATFORM_STYLE
const int scale