KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_model_file_preview.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 as published by
8 * the Free Software Foundation; either version 3 of the License, or (at your option)
9 * 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-3.0.html
19 */
20
22
26#include <trace_helpers.h>
27
28#include <wx/app.h>
29#include <wx/checkbox.h>
30#include <wx/filedlg.h>
31#include <wx/filename.h>
32#include <wx/log.h>
33#include <wx/sizer.h>
34#include <wx/translation.h>
35
36
41
42
44 bool aEmbedFile ) :
45 wxPanel( aParent ),
46 m_request( std::move( aRequest ) ),
47 m_cancel( std::move( aCancel ) ),
48 m_lifetime( std::make_shared<LIFETIME>() )
49{
50 m_lifetime->panel = this;
51 auto* sizer = new wxBoxSizer( wxVERTICAL );
53 m_viewer->SetMinSize( FromDIP( wxSize( 520, 340 ) ) );
54 const int border = FromDIP( 4 );
55 sizer->Add( m_viewer, 1, wxEXPAND | wxALL, border );
56
57 m_embedFile = new wxCheckBox( this, wxID_ANY, _( "Embed file" ) );
58 m_embedFile->SetValue( aEmbedFile );
59 sizer->Add( m_embedFile, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, border );
60
61 SetSizerAndFit( sizer );
62
63 // The owning dialog cannot change for this panel's lifetime, and the handler below runs at
64 // idle rate, so resolve it once rather than casting on every tick.
65 m_dialog = dynamic_cast<wxFileDialog*>( wxGetTopLevelParent( this ) );
66
67 Bind( wxEVT_UPDATE_UI,
68 [this]( wxUpdateUIEvent& aEvent )
69 {
70 aEvent.Skip();
71
72 if( m_dialog )
73 SetSelectedFile( m_dialog->GetCurrentlySelectedFilename() );
74 } );
75}
76
77
79{
80 wxLogTrace( traceModelPreview, wxS( "destroy generation=%llu" ), static_cast<unsigned long long>( m_generation ) );
81 m_lifetime->panel = nullptr;
82
83 if( m_cancel )
84 m_cancel();
85
86 // The viewer holds a raw pointer into storage owned by m_modelOwner, so it has to go before
87 // that owner is released; wxPanel would otherwise destroy it after our members.
88 delete m_viewer;
89 m_viewer = nullptr;
90 m_modelOwner.reset();
91}
92
93
94void PANEL_MODEL_FILE_PREVIEW::SetSelectedFile( const wxString& aPath )
95{
96 if( aPath == m_selectedFile )
97 return;
98
99 m_selectedFile = aPath;
100 const uint64_t generation = ++m_generation;
101 wxLogTrace( traceModelPreview, wxS( "select generation=%llu path=%s" ),
102 static_cast<unsigned long long>( generation ), aPath.c_str() );
103 m_viewer->Clear3DModel();
104 m_modelOwner.reset();
105
106 if( aPath.IsEmpty() || !wxFileName::FileExists( aPath ) )
107 {
108 if( m_cancel )
109 m_cancel();
110
111 return;
112 }
113
114 if( !m_request )
115 return;
116
117 std::shared_ptr<LIFETIME> lifetime = m_lifetime;
118
119 m_request( aPath, generation,
120 [lifetime]( uint64_t aCompletedGeneration, RESULT aResult ) mutable
121 {
122 wxAppConsole* app = wxTheApp;
123
124 if( !app )
125 return;
126
127 app->CallAfter(
128 [lifetime, aCompletedGeneration, result = std::move( aResult )]() mutable
129 {
130 if( lifetime->panel )
131 lifetime->panel->ApplyResult( aCompletedGeneration, std::move( result ) );
132 else
133 wxLogTrace( traceModelPreview, wxS( "discard destroyed generation=%llu" ),
134 static_cast<unsigned long long>( aCompletedGeneration ) );
135 } );
136 } );
137}
138
139
141{
142 return m_embedFile->GetValue();
143}
144
145
146void PANEL_MODEL_FILE_PREVIEW::ApplyResult( uint64_t aGeneration, RESULT aResult )
147{
148 if( aGeneration != m_generation )
149 {
150 wxLogTrace( traceModelPreview, wxS( "discard stale generation=%llu current=%llu" ),
151 static_cast<unsigned long long>( aGeneration ), static_cast<unsigned long long>( m_generation ) );
152 return;
153 }
154
155 const bool usable = aResult.model && aResult.model->m_Materials && aResult.model->m_Meshes
156 && aResult.model->m_MaterialsSize > 0 && aResult.model->m_MeshesSize > 0;
157
158 if( aResult.status == S3D::MODEL_IMPORT_ERROR::NONE && aResult.owner && usable )
159 {
160 std::shared_ptr<const void> previousOwner = std::move( m_modelOwner );
161 m_modelOwner = aResult.owner;
162 m_viewer->Set3DModel( *aResult.model );
163 wxLogTrace( traceModelPreview, wxS( "ready generation=%llu" ), static_cast<unsigned long long>( aGeneration ) );
164 return;
165 }
166
167 m_viewer->Clear3DModel();
168 m_modelOwner.reset();
169
170 if( !aResult.message.IsEmpty() )
171 wxLogTrace( traceModelPreview, wxS( "import diagnostic: %s" ), aResult.message.c_str() );
172}
define an internal structure to be used by the 3D renders
Implement a canvas based on a wxGLCanvas.
static const wxGLAttributes GetAttributesList(ANTIALIASING_MODE aAntiAliasingMode, bool aAlpha=false)
Get a list of attributes to pass to wxGLCanvas.
std::shared_ptr< const void > m_modelOwner
std::shared_ptr< LIFETIME > m_lifetime
PANEL_MODEL_FILE_PREVIEW(wxWindow *aParent, REQUEST aRequest, CANCEL aCancel, bool aEmbedFile)
std::function< void(wxString, uint64_t, COMPLETION)> REQUEST
void SetSelectedFile(const wxString &aPath)
void ApplyResult(uint64_t aGeneration, RESULT aResult)
#define _(s)
Implements a model viewer canvas.
const wxChar *const traceModelPreview
Flag to enable native operating-system model preview debug output.
STL namespace.
Declaration of the cogl_att_list class.
std::shared_ptr< const void > owner
Keeps the imported model alive for as long as the panel shows it.
SMATERIAL * m_Materials
The materials list of this model.
Definition c3dmodel.h:116
unsigned int m_MeshesSize
Number of meshes in the array.
Definition c3dmodel.h:112
SMESH * m_Meshes
The meshes list of this model.
Definition c3dmodel.h:113
unsigned int m_MaterialsSize
Number of materials in the material array.
Definition c3dmodel.h:115
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.