2018-08-28 13:26:39 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-04-02 00:17:25 +00:00
|
|
|
* Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2018-08-28 13:26:39 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "symbol_preview_widget.h"
|
|
|
|
#include <wx/stattext.h>
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <kiway.h>
|
|
|
|
#include <sch_view.h>
|
|
|
|
#include <gal/gal_display_options.h>
|
|
|
|
#include <class_libentry.h>
|
|
|
|
#include <symbol_lib_table.h>
|
|
|
|
#include <sch_preview_panel.h>
|
|
|
|
#include <pgm_base.h>
|
2018-09-02 20:19:22 +00:00
|
|
|
#include <sch_painter.h>
|
2018-10-17 10:13:32 +00:00
|
|
|
#include <draw_frame.h>
|
|
|
|
|
2018-08-28 13:26:39 +00:00
|
|
|
|
2018-09-15 07:00:13 +00:00
|
|
|
SYMBOL_PREVIEW_WIDGET::SYMBOL_PREVIEW_WIDGET( wxWindow* aParent, KIWAY& aKiway,
|
|
|
|
EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType ) :
|
2018-08-30 21:44:50 +00:00
|
|
|
wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 ),
|
2018-08-28 13:26:39 +00:00
|
|
|
m_kiway( aKiway ),
|
2019-01-25 19:18:22 +00:00
|
|
|
m_preview( nullptr ), m_status( nullptr ), m_statusSizer( nullptr ), m_previewItem( nullptr )
|
2018-08-28 13:26:39 +00:00
|
|
|
{
|
2018-10-17 10:13:32 +00:00
|
|
|
wxString eeschemaFrameKey( SCH_EDIT_FRAME_NAME );
|
2019-04-27 13:26:19 +00:00
|
|
|
|
|
|
|
std::unique_ptr<wxConfigBase> eeschemaConfig = GetNewConfig( Pgm().App().GetAppName() );
|
|
|
|
wxConfigBase& commonConfig = *Pgm().CommonSettings();
|
|
|
|
|
|
|
|
m_galDisplayOptions.ReadConfig( commonConfig, *eeschemaConfig, eeschemaFrameKey, this );
|
2018-08-28 22:32:36 +00:00
|
|
|
|
2018-09-15 07:00:13 +00:00
|
|
|
EDA_DRAW_PANEL_GAL::GAL_TYPE canvasType = aCanvasType;
|
|
|
|
|
|
|
|
// Allows only a CAIRO or OPENGL canvas:
|
|
|
|
if( canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL &&
|
|
|
|
canvasType != EDA_DRAW_PANEL_GAL::GAL_TYPE_CAIRO )
|
|
|
|
canvasType = EDA_DRAW_PANEL_GAL::GAL_TYPE_OPENGL;
|
|
|
|
|
2018-08-28 13:26:39 +00:00
|
|
|
m_preview = new SCH_PREVIEW_PANEL( aParent, wxID_ANY, wxDefaultPosition, wxSize( -1, -1 ),
|
2018-09-15 07:00:13 +00:00
|
|
|
m_galDisplayOptions, canvasType );
|
2018-08-28 13:26:39 +00:00
|
|
|
m_preview->SetStealsFocus( false );
|
|
|
|
|
2018-11-12 19:40:41 +00:00
|
|
|
// Do not display the grid: the look is not good for a small canvas area.
|
|
|
|
// But mainly, due to some strange bug I (JPC) was unable to fix, the grid creates
|
2020-04-02 00:17:25 +00:00
|
|
|
// strange artifacts on Windows when eeschema is run from Kicad manager (but not in
|
|
|
|
// stand alone...).
|
2018-11-12 19:40:41 +00:00
|
|
|
m_preview->GetGAL()->SetGridVisibility( false );
|
|
|
|
|
2019-01-25 19:18:22 +00:00
|
|
|
// Early initialization of the canvas background color,
|
|
|
|
// before any OnPaint event is fired for the canvas using a wrong bg color
|
|
|
|
KIGFX::VIEW* view = m_preview->GetView();
|
|
|
|
auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
|
|
|
|
m_preview->GetGAL()->SetClearColor( settings->GetBackgroundColor() );
|
|
|
|
|
2018-08-28 13:26:39 +00:00
|
|
|
SetBackgroundColour( *wxWHITE );
|
|
|
|
SetForegroundColour( *wxBLACK );
|
|
|
|
|
2018-11-12 19:40:41 +00:00
|
|
|
m_status = new wxStaticText( this, wxID_ANY, wxEmptyString );
|
2018-08-30 21:44:50 +00:00
|
|
|
m_statusSizer = new wxBoxSizer( wxVERTICAL );
|
2018-11-12 19:40:41 +00:00
|
|
|
m_statusSizer->Add( 0, 0, 1 ); // add a spacer
|
|
|
|
m_statusSizer->Add( m_status, 0, wxALIGN_CENTER );
|
|
|
|
m_statusSizer->Add( 0, 0, 1 ); // add a spacer
|
2018-08-28 13:26:39 +00:00
|
|
|
|
|
|
|
auto outer_sizer = new wxBoxSizer( wxVERTICAL );
|
2018-11-12 19:40:41 +00:00
|
|
|
outer_sizer->Add( m_preview, 1, wxTOP | wxEXPAND, 5 );
|
|
|
|
outer_sizer->Add( m_statusSizer, 1, wxALIGN_CENTER );
|
2018-08-28 13:26:39 +00:00
|
|
|
|
2018-08-30 21:44:50 +00:00
|
|
|
m_statusSizer->ShowItems( false );
|
2018-08-28 13:26:39 +00:00
|
|
|
|
|
|
|
SetSizer( outer_sizer );
|
2018-09-17 09:22:11 +00:00
|
|
|
|
|
|
|
Connect( wxEVT_SIZE, wxSizeEventHandler( SYMBOL_PREVIEW_WIDGET::onSize ), NULL, this );
|
2018-08-28 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-29 18:31:43 +00:00
|
|
|
SYMBOL_PREVIEW_WIDGET::~SYMBOL_PREVIEW_WIDGET()
|
|
|
|
{
|
|
|
|
if( m_previewItem )
|
|
|
|
m_preview->GetView()->Remove( m_previewItem );
|
2018-10-31 14:52:10 +00:00
|
|
|
|
|
|
|
delete m_previewItem;
|
2018-08-29 18:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-28 13:26:39 +00:00
|
|
|
void SYMBOL_PREVIEW_WIDGET::SetStatusText( wxString const& aText )
|
|
|
|
{
|
|
|
|
m_status->SetLabel( aText );
|
2018-08-30 21:44:50 +00:00
|
|
|
m_statusSizer->ShowItems( true );
|
2018-08-28 13:26:39 +00:00
|
|
|
m_preview->Hide();
|
|
|
|
Layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 09:22:11 +00:00
|
|
|
void SYMBOL_PREVIEW_WIDGET::onSize( wxSizeEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( m_previewItem )
|
|
|
|
{
|
|
|
|
fitOnDrawArea();
|
|
|
|
m_preview->ForceRefresh();
|
|
|
|
}
|
2018-11-12 19:40:41 +00:00
|
|
|
|
|
|
|
aEvent.Skip();
|
2018-09-17 09:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SYMBOL_PREVIEW_WIDGET::fitOnDrawArea()
|
|
|
|
{
|
|
|
|
if( !m_previewItem )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// set the view scale to fit the item on screen
|
|
|
|
KIGFX::VIEW* view = m_preview->GetView();
|
|
|
|
|
|
|
|
// Calculate the drawing area size, in internal units, for a scaling factor = 1.0
|
|
|
|
view->SetScale( 1.0 );
|
|
|
|
VECTOR2D clientSize = view->ToWorld( m_preview->GetClientSize(), false );
|
2018-11-12 19:40:41 +00:00
|
|
|
// Calculate the draw scale to fit the drawing area
|
2018-09-17 09:22:11 +00:00
|
|
|
double scale = std::min( fabs( clientSize.x / m_itemBBox.GetWidth() ),
|
|
|
|
fabs( clientSize.y / m_itemBBox.GetHeight() ) );
|
|
|
|
|
|
|
|
// Above calculation will yield an exact fit; add a bit of whitespace around symbol
|
|
|
|
scale /= 1.2;
|
|
|
|
|
|
|
|
// Now fix the best scale
|
|
|
|
view->SetScale( scale );
|
|
|
|
view->SetCenter( m_itemBBox.Centre() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-02 00:17:25 +00:00
|
|
|
void SYMBOL_PREVIEW_WIDGET::DisplaySymbol( const LIB_ID& aSymbolID, int aUnit, int aConvert )
|
2018-08-28 13:26:39 +00:00
|
|
|
{
|
|
|
|
KIGFX::VIEW* view = m_preview->GetView();
|
2018-09-02 20:19:22 +00:00
|
|
|
auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
|
2018-08-28 13:26:39 +00:00
|
|
|
LIB_ALIAS* alias = nullptr;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
alias = m_kiway.Prj().SchSymbolLibTable()->LoadSymbol( aSymbolID );
|
|
|
|
}
|
|
|
|
catch( const IO_ERROR& ioe )
|
|
|
|
{
|
|
|
|
wxLogError( wxString::Format( _( "Error loading symbol %s from library %s.\n\n%s" ),
|
|
|
|
aSymbolID.GetLibItemName().wx_str(),
|
|
|
|
aSymbolID.GetLibNickname().wx_str(),
|
|
|
|
ioe.What() ) );
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:31:43 +00:00
|
|
|
if( m_previewItem )
|
|
|
|
{
|
|
|
|
view->Remove( m_previewItem );
|
2018-10-31 14:52:10 +00:00
|
|
|
delete m_previewItem;
|
2018-08-29 18:31:43 +00:00
|
|
|
m_previewItem = nullptr;
|
|
|
|
}
|
2018-08-28 13:26:39 +00:00
|
|
|
|
|
|
|
if( alias )
|
|
|
|
{
|
2018-08-28 17:45:30 +00:00
|
|
|
LIB_PART* part = alias->GetPart();
|
|
|
|
|
|
|
|
// If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
|
|
|
|
// draw all of them.)
|
2020-04-02 00:17:25 +00:00
|
|
|
settings->m_ShowUnit = ( part->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
|
2018-08-29 18:31:43 +00:00
|
|
|
|
2018-09-16 11:30:18 +00:00
|
|
|
// For symbols having a De Morgan body style, use the first style
|
2020-04-02 00:17:25 +00:00
|
|
|
settings->m_ShowConvert =
|
|
|
|
( part->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
|
2018-09-16 11:30:18 +00:00
|
|
|
|
2018-10-31 14:52:10 +00:00
|
|
|
m_previewItem = new LIB_ALIAS( *alias, part );
|
|
|
|
view->Add( m_previewItem );
|
2018-08-28 13:26:39 +00:00
|
|
|
|
2020-04-02 00:17:25 +00:00
|
|
|
m_itemBBox = part->GetUnitBoundingBox( settings->m_ShowUnit, settings->m_ShowConvert );
|
2018-08-28 13:26:39 +00:00
|
|
|
|
2018-11-12 19:40:41 +00:00
|
|
|
if( !m_preview->IsShown() )
|
|
|
|
{
|
|
|
|
m_preview->Show();
|
|
|
|
m_statusSizer->ShowItems( false );
|
|
|
|
Layout(); // Ensure panel size is up to date.
|
|
|
|
}
|
|
|
|
|
2018-09-17 09:22:11 +00:00
|
|
|
// Calculate the draw scale to fit the drawing area
|
|
|
|
fitOnDrawArea();
|
2018-08-28 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_preview->ForceRefresh();
|
2018-08-30 21:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-02 00:17:25 +00:00
|
|
|
void SYMBOL_PREVIEW_WIDGET::DisplayPart( LIB_PART* aPart, int aUnit, int aConvert )
|
2018-08-30 21:44:50 +00:00
|
|
|
{
|
|
|
|
KIGFX::VIEW* view = m_preview->GetView();
|
|
|
|
|
|
|
|
if( m_previewItem )
|
|
|
|
{
|
|
|
|
view->Remove( m_previewItem );
|
2018-10-31 14:52:10 +00:00
|
|
|
delete m_previewItem;
|
2018-08-30 21:44:50 +00:00
|
|
|
m_previewItem = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aPart )
|
|
|
|
{
|
2020-04-02 00:17:25 +00:00
|
|
|
m_previewItem = new LIB_PART( *aPart );
|
2018-08-30 21:44:50 +00:00
|
|
|
|
2018-09-16 11:30:18 +00:00
|
|
|
// For symbols having a De Morgan body style, use the first style
|
|
|
|
auto settings = static_cast<KIGFX::SCH_RENDER_SETTINGS*>( view->GetPainter()->GetSettings() );
|
2020-04-02 00:17:25 +00:00
|
|
|
// If unit isn't specified for a multi-unit part, pick the first. (Otherwise we'll
|
|
|
|
// draw all of them.)
|
|
|
|
settings->m_ShowUnit = ( aPart->IsMulti() && aUnit == 0 ) ? 1 : aUnit;
|
|
|
|
|
|
|
|
settings->m_ShowConvert =
|
|
|
|
( aPart->HasConversion() && aConvert == 0 ) ? 1 : aConvert;
|
|
|
|
|
2018-10-31 14:52:10 +00:00
|
|
|
view->Add( m_previewItem );
|
2018-08-30 21:44:50 +00:00
|
|
|
|
2018-09-17 09:22:11 +00:00
|
|
|
// Get the symbole size, in internal units
|
2020-04-02 00:17:25 +00:00
|
|
|
m_itemBBox = aPart->GetUnitBoundingBox( settings->m_ShowUnit, settings->m_ShowConvert );
|
2018-09-15 07:00:13 +00:00
|
|
|
|
2018-09-17 09:22:11 +00:00
|
|
|
// Calculate the draw scale to fit the drawing area
|
|
|
|
fitOnDrawArea();
|
2018-08-30 21:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_preview->ForceRefresh();
|
|
|
|
|
|
|
|
m_preview->Show();
|
|
|
|
m_statusSizer->ShowItems( false );
|
2018-08-28 13:26:39 +00:00
|
|
|
}
|