KiCad PCB EDA Suite
dlg_select_3dmodel.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) 2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
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, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 
26 #include <set>
27 #include "dlg_select_3dmodel.h"
28 #include "project.h"
29 #include "3d_cache/3d_info.h"
30 #include "3d_cache/3d_cache.h"
31 #include "3d_cache_dialogs.h"
34 
35 #define ID_FILE_TREE ( wxID_LAST + 1 )
36 #define ID_SET_DIR ( ID_FILE_TREE + 1 )
37 #define ID_CFG_PATHS ( ID_SET_DIR + 1 )
38 
39 
41  EVT_DIRCTRL_SELECTIONCHANGED( ID_FILE_TREE, DLG_SELECT_3DMODEL::OnSelectionChanged )
42  EVT_DIRCTRL_FILEACTIVATED( ID_FILE_TREE, DLG_SELECT_3DMODEL::OnFileActivated )
43  EVT_CHOICE( ID_SET_DIR, DLG_SELECT_3DMODEL::SetRootDir )
44  EVT_BUTTON( ID_CFG_PATHS, DLG_SELECT_3DMODEL::Cfg3DPaths )
45 wxEND_EVENT_TABLE()
46 
47 
48 DLG_SELECT_3DMODEL::DLG_SELECT_3DMODEL( wxWindow* aParent, S3D_CACHE* aCacheManager,
49  S3D_INFO* aModelItem, wxString& prevModelSelectDir, int& prevModelWildcard ) :
50  wxDialog( aParent, wxID_ANY, _( "Select 3D Model" ), wxDefaultPosition,
51  wxSize( 500,200 ), wxCAPTION | wxRESIZE_BORDER | wxCLOSE_BOX
52  | wxMINIMIZE_BOX | wxMAXIMIZE_BOX | wxSYSTEM_MENU ),
53  m_model( aModelItem ), m_cache( aCacheManager ), m_previousDir( prevModelSelectDir ),
54  m_previousFilterIndex( prevModelWildcard )
55 {
56  this->SetSizeHints( wxSize( 500,200 ), wxDefaultSize );
57 
58  if( NULL != m_cache )
59  m_resolver = m_cache->GetResolver();
60  else
61  m_resolver = NULL;
62 
63  wxBoxSizer* bSizer0 = new wxBoxSizer( wxVERTICAL );
64 
65  wxBoxSizer* bSizer1 = new wxBoxSizer( wxHORIZONTAL );
66 
67  wxBoxSizer* bSizer2 = new wxBoxSizer( wxVERTICAL );
68 
69  // set to NULL to avoid segfaults when m_FileTree is instantiated
70  // and wxGenericDirCtrl events are posted
71  m_modelViewer = NULL;
72  dirChoices = NULL;
73 
74  m_FileTree = new wxGenericDirCtrl( this, ID_FILE_TREE, prevModelSelectDir, wxDefaultPosition,
75  wxSize( 300,100 ), wxDIRCTRL_3D_INTERNAL | wxDIRCTRL_EDIT_LABELS
76  | wxDIRCTRL_SELECT_FIRST | wxDIRCTRL_SHOW_FILTERS|wxSUNKEN_BORDER, wxEmptyString, 0 );
77 
78 
79  m_FileTree->ShowHidden( false );
80  m_FileTree->SetMinSize( wxSize( 300,100 ) );
81  m_FileTree->SetLabel( wxT( "3D_MODEL_SELECTOR" ) );
82 
83  bSizer2->Add( m_FileTree, 1, wxEXPAND | wxALL, 5 );
84  bSizer1->Add( bSizer2, 1, wxEXPAND, 5 );
85 
86  m_modelViewer = new C3D_MODEL_VIEWER( this,
88  m_cache );
89  m_modelViewer->SetMinSize( wxSize( 512, 384 ) );
90 
91  bSizer1->Add( m_modelViewer, 0, wxCENTER, 5 );
92 
93  // create the filter list
94  if( NULL != m_cache )
95  {
96  std::list< wxString > const* fl = m_cache->GetFileFilters();
97  std::list< wxString >::const_iterator sL = fl->begin();
98  std::list< wxString >::const_iterator eL = fl->end();
99  wxString filter;
100 
101  while( sL != eL )
102  {
103  filter.Append( *sL );
104 
105  ++sL;
106 
107  if( sL != eL )
108  filter.Append( wxT( "|" ) );
109 
110  }
111 
112  if( !filter.empty() )
113  m_FileTree->SetFilter( filter );
114  else
115  m_FileTree->SetFilter( wxT( "*.*" ) );
116 
117  if( prevModelWildcard >= 0 && prevModelWildcard < (int)fl->size() )
118  m_FileTree->SetFilterIndex( prevModelWildcard );
119  else
120  {
121  prevModelWildcard = 0;
122  m_FileTree->SetFilterIndex( 0 );
123  }
124  }
125  else
126  {
127  m_FileTree->SetFilter( wxT( "*.*" ) );
128  prevModelWildcard = 0;
129  m_FileTree->SetFilterIndex( 0 );
130  }
131 
132  // Add the path choice box and config button
133  wxBoxSizer* hboxDirChoice = new wxBoxSizer( wxHORIZONTAL );
134  dirChoices = new wxChoice( this, ID_SET_DIR, wxDefaultPosition, wxSize( 320, 20 ) );
135  dirChoices->SetMinSize( wxSize( 320, 12 ) );
136 
137  wxStaticText* stDirChoice = new wxStaticText( this, -1, _( "Paths:" ) );
138  wxButton* cfgPaths = new wxButton( this, ID_CFG_PATHS, _( "Configure Paths" ) );
139  hboxDirChoice->Add( stDirChoice, 0, wxALL | wxCENTER, 5 );
140  hboxDirChoice->Add( dirChoices, 1, wxEXPAND | wxALL, 5 );
141  hboxDirChoice->Add( cfgPaths, 0, wxALL, 5 );
142 
143  wxButton* btn_OK = new wxButton( this, wxID_OK, _( "OK" ) );
144  wxButton* btn_Cancel = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
145 
146  wxStdDialogButtonSizer* hSizer1 = new wxStdDialogButtonSizer();
147  hSizer1->AddButton( btn_OK );
148  hSizer1->AddButton( btn_Cancel );
149  hSizer1->Realize();
150 
151  bSizer0->Add( bSizer1, 1, wxALL | wxEXPAND, 5 );
152  bSizer0->Add( hboxDirChoice, 0, wxALL | wxEXPAND, 5 );
153  bSizer0->Add( hSizer1, 0, wxALL | wxEXPAND, 5 );
154 
155  updateDirChoiceList();
156 
157  this->SetSizerAndFit( bSizer0 );
158  this->Layout();
159  this->Centre( wxBOTH );
160 
161  m_modelViewer->Refresh();
162  m_modelViewer->SetFocus();
163 }
164 
165 
167 {
168  if( NULL == m_model || NULL == m_FileTree )
169  return true;
170 
171  m_model->m_Scale.x = 1.0;
172  m_model->m_Scale.y = 1.0;
173  m_model->m_Scale.z = 1.0;
174 
175  m_model->m_Rotation.x = 0.0;
176  m_model->m_Rotation.y = 0.0;
177  m_model->m_Rotation.z = 0.0;
178 
180  m_model->m_Filename.clear();
181 
182  wxString name = m_FileTree->GetFilePath();
183 
184  if( name.empty() )
185  return true;
186 
187  m_previousDir = m_FileTree->GetPath();
188  m_previousFilterIndex = m_FileTree->GetFilterIndex();
189 
190  // file selection mode: retrieve the filename and specify a
191  // path relative to one of the config paths
192  wxFileName fname = m_FileTree->GetFilePath();
193  fname.Normalize();
194  m_model->m_Filename = m_resolver->ShortenPath( fname.GetFullPath() );
195 
196  return true;
197 }
198 
199 
200 void DLG_SELECT_3DMODEL::OnSelectionChanged( wxTreeEvent& event )
201 {
202  if( m_modelViewer )
203  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
204 
205  event.Skip();
206  return;
207 }
208 
209 
210 void DLG_SELECT_3DMODEL::OnFileActivated( wxTreeEvent& event )
211 {
212  if( m_modelViewer )
213  m_modelViewer->Set3DModel( m_FileTree->GetFilePath() );
214 
215  event.Skip();
216  SetEscapeId( wxID_OK );
217  Close();
218 
219  return;
220 }
221 
222 
223 void DLG_SELECT_3DMODEL::SetRootDir( wxCommandEvent& event )
224 {
225  if( m_FileTree )
226  m_FileTree->SetPath( dirChoices->GetString( dirChoices->GetSelection() ) );
227 
228  return;
229 }
230 
231 
232 void DLG_SELECT_3DMODEL::Cfg3DPaths( wxCommandEvent& event )
233 {
234  if( S3D::Configure3DPaths( this, m_resolver ) )
236 }
237 
238 
240 {
241  if( NULL == m_FileTree || NULL == m_resolver || NULL == dirChoices )
242  return;
243 
244  std::list< S3D_ALIAS > const* md = m_resolver->GetPaths();
245  std::list< S3D_ALIAS >::const_iterator sL = md->begin();
246  std::list< S3D_ALIAS >::const_iterator eL = md->end();
247  std::set< wxString > cl;
248  wxString prjDir;
249 
250  // extract the current project dir
251  if( sL != eL )
252  {
253  prjDir = sL->m_pathexp;
254  ++sL;
255  }
256 
257  while( sL != eL )
258  {
259  if( !sL->m_pathexp.empty() && sL->m_pathexp.compare( prjDir ) )
260  cl.insert( sL->m_pathexp );
261 
262  ++sL;
263  }
264 
265  if( !cl.empty() )
266  {
267  dirChoices->Clear();
268 
269  if( !prjDir.empty() )
270  dirChoices->Append( prjDir );
271 
272  std::set< wxString >::const_iterator sI = cl.begin();
273  std::set< wxString >::const_iterator eI = cl.end();
274 
275  while( sI != eI )
276  {
277  dirChoices->Append( *sI );
278  ++sI;
279  }
280 
281  dirChoices->Select( 0 );
282  }
283 
284  return;
285 }
bool TransferDataFromWindow() override
static const int * GetAttributesList(bool aUseAntiAliasing)
Get a list of attributes to pass to wxGLCanvas.
double x
Definition: sg_base.h:70
C3D_MODEL_VIEWER * m_modelViewer
#define ID_FILE_TREE
void Cfg3DPaths(wxCommandEvent &event)
void OnFileActivated(wxTreeEvent &event)
double y
Definition: sg_base.h:71
SGPOINT m_Offset
an offset (unit = inch) for the 3D shape
Definition: 3d_info.h:44
S3D_FILENAME_RESOLVER * m_resolver
Class C3D_MODEL_VIEWER Implement a canvas based on a wxGLCanvas.
defines the basic data associated with a single 3D model.
wxBEGIN_EVENT_TABLE(DLG_SELECT_3DMODEL, wxDialog) wxEND_EVENT_TABLE() DLG_SELECT_3DMODEL
#define ID_CFG_PATHS
bool Configure3DPaths(wxWindow *aParent, S3D_FILENAME_RESOLVER *aResolver)
wxString ShortenPath(const wxString &aFullPathName)
Function ShortenPath produces a relative path based on the existing search directories or returns the...
Implements a model viewer canvas.
void Set3DModel(const S3DMODEL &a3DModel)
Set3DModel - Set this model to be displayed.
wxGenericDirCtrl * m_FileTree
Declaration of the cogl_att_list class.
const std::list< S3D_ALIAS > * GetPaths(void)
Function GetPaths returns a pointer to the internal path list; the items in:load. ...
#define ID_SET_DIR
void SetRootDir(wxCommandEvent &event)
void OnSelectionChanged(wxTreeEvent &event)
#define D(x)
Definition: ptree.cpp:41
creates a dialog to select 3D model files
SGPOINT m_Rotation
an X,Y,Z rotation (unit = degrees) for the 3D shape
Definition: 3d_info.h:43
double z
Definition: sg_base.h:72
defines the display data cache manager for 3D models
SGPOINT m_Scale
scaling factors for the 3D footprint shape
Definition: 3d_info.h:42
wxString m_Filename
The 3D shape filename in 3D library.
Definition: 3d_info.h:45