kicad/pcb_calculator/pcb_calculator_frame.cpp

356 lines
12 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 1992-2015 jean-pierre.charras
* Copyright (C) 1992-2015 Kicad Developers, see AUTHORS.txt for contributors.
*
* 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 <wx/wx.h>
#include <wx/config.h>
#include <pgm_base.h>
#include <pcb_calculator.h>
#include <UnitSelector.h>
#include <bitmaps.h>
#define KEYWORD_FRAME_POSX wxT( "Pcb_calculator_Pos_x" )
#define KEYWORD_FRAME_POSY wxT( "Pcb_calculator_Pos_y" )
#define KEYWORD_FRAME_SIZEX wxT( "Pcb_calculator_Size_x" )
#define KEYWORD_FRAME_SIZEY wxT( "Pcb_calculator_Size_y" )
#define KEYWORD_TRANSLINE_SELECTION wxT( "Transline_selection" )
#define KEYWORD_PAGE_SELECTION wxT( "Page_selection" )
#define KEYWORD_COLORCODE_SELECTION wxT( "CC_selection" )
#define KEYWORD_ATTENUATORS_SELECTION wxT( "Att_selection" )
#define KEYWORD_BRDCLASS_SELECTION wxT( "BrdClass_selection" )
#define KEYWORD_ELECTRICAL_SPACING_SELECTION wxT( "ElectSpacing_selection" )
#define KEYWORD_ELECTRICAL_SPACING_VOLTAGE wxT( "ElectSpacing_voltage" )
#define KEYWORD_REGUL_R1 wxT( "RegulR1" )
#define KEYWORD_REGUL_R2 wxT( "RegulR2" )
#define KEYWORD_REGUL_VREF wxT( "RegulVREF" )
#define KEYWORD_REGUL_VOUT wxT( "RegulVOUT" )
#define KEYWORD_REGUL_SELECTED wxT( "RegulName" )
#define KEYWORD_REGUL_TYPE wxT( "RegulType" )
#define KEYWORD_REGUL_LAST_PARAM wxT( "RegulLastParam" )
#define KEYWORD_DATAFILE_FILENAME wxT( "DataFilename" )
2012-04-05 18:56:06 +00:00
// extention of pcb_calculator data filename:
const wxString DataFileNameExt( wxT("pcbcalc") );
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
PCB_CALCULATOR_FRAME_BASE( aParent )
{
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
2014-03-20 00:42:08 +00:00
SetKiway( this, aKiway );
m_currTransLine = NULL;
m_currTransLineType = DEFAULT_TYPE;
m_currAttenuator = NULL;
m_RegulatorListChanged = false;
m_TWMode = TW_MASTER_CURRENT;
m_TWNested = false;
// Populate transline list ordered like in dialog menu list
const static TRANSLINE_TYPE_ID tltype_list[8] =
{
MICROSTRIP_TYPE, CPW_TYPE, GROUNDED_CPW_TYPE,
RECTWAVEGUIDE_TYPE, COAX_TYPE, C_MICROSTRIP_TYPE,
STRIPLINE_TYPE, TWISTEDPAIR_TYPE
};
for( int ii = 0; ii < 8; ii++ )
m_transline_list.push_back( new TRANSLINE_IDENT( tltype_list[ii] ) );
// Populate attenuator list ordered like in dialog menu list
m_attenuator_list.push_back( new ATTENUATOR_PI() );
m_attenuator_list.push_back( new ATTENUATOR_TEE() );
m_attenuator_list.push_back( new ATTENUATOR_BRIDGE() );
m_attenuator_list.push_back( new ATTENUATOR_SPLITTER() );
m_currAttenuator = m_attenuator_list[0];
auto config = GetNewConfig( Pgm().App().GetAppName() );
LoadSettings( config.get() );
ReadDataFile();
TranslineTypeSelection( m_currTransLineType );
m_TranslineSelection->SetSelection( m_currTransLineType );
TW_Init( config.get() );
2011-08-07 16:02:58 +00:00
VS_Init( config.get() );
SetAttenuator( m_AttenuatorsSelection->GetSelection() );
ToleranceSelection( m_rbToleranceSelection->GetSelection() );
BoardClassesUpdateData( m_BoardClassesUnitsSelector->GetUnitScale() );
ElectricalSpacingUpdateData( m_ElectricalSpacingUnitsSelector->GetUnitScale() );
m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
SelectLastSelectedRegulator();
// Give an icon
wxIcon icon;
icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_xpm ) );
SetIcon( icon );
GetSizer()->SetSizeHints( this );
// Set previous size and position
SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
if( m_FramePos == wxDefaultPosition )
Centre();
}
PCB_CALCULATOR_FRAME::~PCB_CALCULATOR_FRAME()
{
for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
delete m_transline_list[ii];
for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
delete m_attenuator_list[ii];
/* This needed for OSX: avoids furter OnDraw processing after this
* destructor and before the native window is destroyed
*/
this->Freeze();
}
Modular-Kicad milestone B), major portions: *) Rework the set language support, simplify it by using KIWAY. Now any major frame with a "change language" menu can change the language for all KIWAY_PLAYERs in the whole KIWAY. Multiple KIWAYs are not supported yet. *) Simplify "modal wxFrame" support, and add that support exclusively to KIWAY_PLAYER where it is inherited by all derivatives. The function KIWAY_PLAYER::ShowModal() is in the vtable and so is cross module capable. *) Remove the requirements and assumptions that the wxFrame hierarchy always had PCB_EDIT_FRAME and SCH_EDIT_FRAME as immediate parents of their viewers and editors. This is no longer the case, nor required. *) Use KIWAY::Player() everywhere to make KIWAY_PLAYERs, this registers the KIWAY_PLAYER within the KIWAY and makes it very easy to find an open frame quickly. It also gives control to the KIWAY as to frame hierarchical relationships. *) Change single_top to use the KIWAY for loading a KIFACE and instantiating the single KIWAY_PLAYER, see bullet immediately above. *) Add KIWAY::OnKiwayEnd() and call it from PGM_BASE at program termination, this gives the KIFACEs a chance to save their final configuration dope to disk. *) Add dedicated FRAME_T's for the modal frames, so m_Ident can be tested and these modal frames are distinctly different than their non-modal equivalents. KIWAY_PLAYER::IsModal() is !not! a valid test during the wxFrame's constructor, so this is another important reason for having a dedicated FRAME_T for each modal wxFrame. On balance, more lines were deleted than were added to achieve all this.
2014-05-03 17:40:19 +00:00
void PCB_CALCULATOR_FRAME::OnClosePcbCalc( wxCloseEvent& event )
{
if( m_RegulatorListChanged )
{
if( GetDataFilename().IsEmpty() )
{
int opt = wxMessageBox(
_("Data modified, and no data filename to save modifications\n"\
"Do you want to exit and abandon your change?"),
_("Regulator list change"),
wxYES_NO | wxICON_QUESTION );
if( opt == wxNO )
return;
}
else
{
if( !WriteDataFile() )
{
wxString msg;
2017-12-18 16:47:20 +00:00
msg.Printf( _("Unable to write file \"%s\"\n"\
"Do you want to exit and abandon your change?"),
GetDataFilename().c_str() );
2014-12-30 17:15:46 +00:00
int opt = wxMessageBox( msg, _("Write Data File Error"),
wxYES_NO | wxICON_ERROR );
if( opt == wxNO )
return;
}
}
}
event.Skip();
// Destroy();
}
void PCB_CALCULATOR_FRAME::LoadSettings( wxConfigBase* aCfg )
{
if( aCfg == NULL )
return;
EDA_BASE_FRAME::LoadSettings( aCfg );
long ltmp;
wxString msg;
aCfg->Read( KEYWORD_TRANSLINE_SELECTION, &ltmp, (long) DEFAULT_TYPE );
m_currTransLineType = (enum TRANSLINE_TYPE_ID) ltmp;
aCfg->Read( KEYWORD_PAGE_SELECTION, &ltmp, 0 );
m_Notebook->ChangeSelection( ltmp );
aCfg->Read( KEYWORD_COLORCODE_SELECTION, &ltmp, 1 );
m_rbToleranceSelection->SetSelection( ltmp );
aCfg->Read( KEYWORD_ATTENUATORS_SELECTION, &ltmp, 0 );
m_AttenuatorsSelection->SetSelection( ltmp );
aCfg->Read( KEYWORD_BRDCLASS_SELECTION, &ltmp, 0 );
m_BoardClassesUnitsSelector->SetSelection( ltmp );
// Regul panel config:
aCfg->Read( KEYWORD_REGUL_R1, &msg, wxT( "10" ) );
m_RegulR1Value->SetValue( msg );
aCfg->Read( KEYWORD_REGUL_R2, &msg, wxT( "10" ) );
m_RegulR2Value->SetValue( msg );
aCfg->Read( KEYWORD_REGUL_VREF, &msg, wxT( "3" ) );
m_RegulVrefValue->SetValue( msg );
aCfg->Read( KEYWORD_REGUL_VOUT, &msg, wxT( "12" ) );
m_RegulVoutValue->SetValue( msg );
aCfg->Read( KEYWORD_DATAFILE_FILENAME, &msg, wxT( "" ) );
SetDataFilename( msg );
aCfg->Read( KEYWORD_REGUL_SELECTED, &msg, wxT( "" ) );
m_lastSelectedRegulatorName = msg;
aCfg->Read( KEYWORD_REGUL_TYPE, &ltmp, 0 );
m_choiceRegType->SetSelection( ltmp );
aCfg->Read( KEYWORD_REGUL_LAST_PARAM, &ltmp, 0 );
wxRadioButton * regprms[3] =
{ m_rbRegulR1, m_rbRegulR2, m_rbRegulVout
};
if( (unsigned)ltmp >= 3 )
ltmp = 0;
for( int ii = 0; ii < 3; ii++ )
regprms[ii]->SetValue( ltmp == ii );
// Electrical panel config
aCfg->Read( KEYWORD_ELECTRICAL_SPACING_SELECTION, &ltmp, 0 );
m_ElectricalSpacingUnitsSelector->SetSelection( ltmp );
aCfg->Read( KEYWORD_ELECTRICAL_SPACING_VOLTAGE, &msg, wxT( "500" ) );
m_ElectricalSpacingVoltage->SetValue( msg );
for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
m_transline_list[ii]->ReadConfig( aCfg );
for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
m_attenuator_list[ii]->ReadConfig( aCfg );
}
void PCB_CALCULATOR_FRAME::SaveSettings( wxConfigBase* aCfg )
{
if( aCfg == NULL )
return;
EDA_BASE_FRAME::SaveSettings( aCfg );
aCfg->Write( KEYWORD_TRANSLINE_SELECTION, (long) m_currTransLineType );
aCfg->Write( KEYWORD_PAGE_SELECTION, m_Notebook->GetSelection() );
aCfg->Write( KEYWORD_COLORCODE_SELECTION, m_rbToleranceSelection->GetSelection() );
aCfg->Write( KEYWORD_ATTENUATORS_SELECTION, m_AttenuatorsSelection->GetSelection() );
aCfg->Write( KEYWORD_BRDCLASS_SELECTION, m_BoardClassesUnitsSelector->GetSelection() );
aCfg->Write( KEYWORD_REGUL_R1, m_RegulR1Value->GetValue() );
aCfg->Write( KEYWORD_REGUL_R2, m_RegulR2Value->GetValue() );
aCfg->Write( KEYWORD_REGUL_VREF, m_RegulVrefValue->GetValue() );
aCfg->Write( KEYWORD_REGUL_VOUT, m_RegulVoutValue->GetValue() );
aCfg->Write( KEYWORD_DATAFILE_FILENAME, GetDataFilename() );
aCfg->Write( KEYWORD_REGUL_SELECTED, m_lastSelectedRegulatorName );
aCfg->Write( KEYWORD_REGUL_TYPE,
m_choiceRegType->GetSelection() );
wxRadioButton * regprms[3] =
{ m_rbRegulR1, m_rbRegulR2, m_rbRegulVout
};
for( int ii = 0; ii < 3; ii++ )
{
if( regprms[ii]->GetValue() )
{
aCfg->Write( KEYWORD_REGUL_LAST_PARAM, ii );
break;
}
}
aCfg->Write( KEYWORD_ELECTRICAL_SPACING_SELECTION,
m_ElectricalSpacingUnitsSelector->GetSelection() );
aCfg->Write( KEYWORD_ELECTRICAL_SPACING_VOLTAGE,
m_ElectricalSpacingVoltage->GetValue() );
TW_WriteConfig( aCfg );
VS_WriteConfig( aCfg );
for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
m_transline_list[ii]->WriteConfig( aCfg );
for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
m_attenuator_list[ii]->WriteConfig( aCfg );
}
/**
* Function OnTranslineAnalyse
* Run a new analyse for the current transline with current parameters
* and displays the electrical parmeters
*/
void PCB_CALCULATOR_FRAME::OnTranslineAnalyse( wxCommandEvent& event )
{
if( m_currTransLine )
{
TransfDlgDataToTranslineParams();
m_currTransLine->analyze();
}
}
/**
* Function OnTranslineSynthetize
* Run a new synthezis for the current transline with current parameters
* and displays the geometrical parmeters
*/
void PCB_CALCULATOR_FRAME::OnTranslineSynthetize( wxCommandEvent& event )
{
if( m_currTransLine )
{
TransfDlgDataToTranslineParams();
m_currTransLine->synthesize();
}
}
void PCB_CALCULATOR_FRAME::OnPaintTranslinePanel( wxPaintEvent& event )
{
wxPaintDC dc( m_panelDisplayshape );
TRANSLINE_IDENT* tr_ident = m_transline_list[m_currTransLineType];
if( tr_ident )
{
wxSize size = m_panelDisplayshape->GetSize();
size.x -= tr_ident->m_Icon->GetWidth();
size.y -= tr_ident->m_Icon->GetHeight();
dc.DrawBitmap( *tr_ident->m_Icon, size.x / 2, size.y / 2 );
}
event.Skip();
}
2012-04-05 18:56:06 +00:00
/* returns the full filename of the selected pcb_calculator data file
* the extention file is forced
*/
const wxString PCB_CALCULATOR_FRAME::GetDataFilename()
{
if( m_regulators_fileNameCtrl->GetValue().IsEmpty() )
return wxEmptyString;
wxFileName fn( m_regulators_fileNameCtrl->GetValue() );
fn.SetExt( DataFileNameExt );
return fn.GetFullPath();
}
/* Initialize the full filename of the selected pcb_calculator data file
* force the standard extension of the file (.pcbcalc)
* aFilename = the full filename, with or without extension
*/
void PCB_CALCULATOR_FRAME::SetDataFilename( const wxString & aFilename)
{
if( aFilename.IsEmpty() )
m_regulators_fileNameCtrl->SetValue( wxEmptyString );
else
{
wxFileName fn( aFilename );
fn.SetExt( DataFileNameExt );
m_regulators_fileNameCtrl->SetValue( fn.GetFullPath() );
}
}