You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

360 lines
13 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2011 jean-pierre.charras
  5. * Copyright (C) 1992-2011 Kicad Developers, see change_log.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, 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. #include <wx/wx.h>
  25. #include <wx/config.h>
  26. #include <pcb_calculator.h>
  27. #include <UnitSelector.h>
  28. #include <bitmaps.h>
  29. #define KEYWORD_FRAME_POSX wxT( "Pcb_calculator_Pos_x" )
  30. #define KEYWORD_FRAME_POSY wxT( "Pcb_calculator_Pos_y" )
  31. #define KEYWORD_FRAME_SIZEX wxT( "Pcb_calculator_Size_x" )
  32. #define KEYWORD_FRAME_SIZEY wxT( "Pcb_calculator_Size_y" )
  33. #define KEYWORD_TRANSLINE_SELECTION wxT( "Transline_selection" )
  34. #define KEYWORD_PAGE_SELECTION wxT( "Page_selection" )
  35. #define KEYWORD_COLORCODE_SELECTION wxT( "CC_selection" )
  36. #define KEYWORD_ATTENUATORS_SELECTION wxT( "Att_selection" )
  37. #define KEYWORD_BRDCLASS_SELECTION wxT( "BrdClass_selection" )
  38. #define KEYWORD_ELECTRICAL_SPACING_SELECTION wxT( "ElectSpacing_selection" )
  39. #define KEYWORD_ELECTRICAL_SPACING_VOLTAGE wxT( "ElectSpacing_voltage" )
  40. #define KEYWORD_REGUL_R1 wxT( "RegulR1" )
  41. #define KEYWORD_REGUL_R2 wxT( "RegulR2" )
  42. #define KEYWORD_REGUL_VREF wxT( "RegulVREF" )
  43. #define KEYWORD_REGUL_VOUT wxT( "RegulVOUT" )
  44. #define KEYWORD_REGUL_SELECTED wxT( "RegulName" )
  45. #define KEYWORD_REGUL_TYPE wxT( "RegulType" )
  46. #define KEYWORD_REGUL_LAST_PARAM wxT( "RegulLastParam" )
  47. #define KEYWORD_DATAFILE_FILENAME wxT( "DataFilename" )
  48. // extention of pcb_calculator data filename:
  49. const wxString DataFileNameExt( wxT("pcbcalc") );
  50. PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( wxWindow* parent ) :
  51. PCB_CALCULATOR_FRAME_BASE( parent )
  52. {
  53. m_currTransLine = NULL;
  54. m_currTransLineType = default_type;
  55. m_currAttenuator = NULL;
  56. m_RegulatorListChanged = false;
  57. m_Config = new wxConfig();
  58. // Populate transline list ordered like in dialog menu list
  59. transline_type_id tltype_list[8] =
  60. {
  61. microstrip_type, cpw_type, grounded_cpw_type,
  62. rectwaveguide_type, coax_type, c_microstrip_type,
  63. stripline_type, twistedpair_type
  64. };
  65. for( int ii = 0; ii < 8; ii++ )
  66. m_transline_list.push_back( new TRANSLINE_IDENT( tltype_list[ii] ) );
  67. // Populate attenuator list ordered like in dialog menu list
  68. m_attenuator_list.push_back( new ATTENUATOR_PI() );
  69. m_attenuator_list.push_back( new ATTENUATOR_TEE() );
  70. m_attenuator_list.push_back( new ATTENUATOR_BRIDGE() );
  71. m_attenuator_list.push_back( new ATTENUATOR_SPLITTER() );
  72. m_currAttenuator = m_attenuator_list[0];
  73. ReadConfig();
  74. ReadDataFile();
  75. TranslineTypeSelection( m_currTransLineType );
  76. m_TranslineSelection->SetSelection( m_currTransLineType );
  77. TW_Init();
  78. SetAttenuator( m_AttenuatorsSelection->GetSelection() );
  79. ToleranceSelection( m_rbToleranceSelection->GetSelection() );
  80. BoardClassesUpdateData( m_BoardClassesUnitsSelector->GetUnitScale() );
  81. ElectricalSpacingUpdateData( m_ElectricalSpacingUnitsSelector->GetUnitScale() );
  82. m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
  83. SelectLastSelectedRegulator();
  84. // Give an icon
  85. wxIcon icon;
  86. icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_xpm ) );
  87. SetIcon( icon );
  88. GetSizer()->SetSizeHints( this );
  89. // Set previous size and position
  90. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  91. if( m_FramePos == wxDefaultPosition )
  92. Centre();
  93. }
  94. PCB_CALCULATOR_FRAME::~PCB_CALCULATOR_FRAME()
  95. {
  96. WriteConfig();
  97. for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
  98. delete m_transline_list[ii];
  99. for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
  100. delete m_attenuator_list[ii];
  101. delete m_Config;
  102. /* This needed for OSX: avoids furter OnDraw processing after this
  103. * destructor and before the native window is destroyed
  104. */
  105. this->Freeze();
  106. }
  107. void PCB_CALCULATOR_FRAME::OnClosePcbCalc( wxCloseEvent& event )
  108. {
  109. if( m_RegulatorListChanged )
  110. {
  111. if( GetDataFilename().IsEmpty() )
  112. {
  113. int opt = wxMessageBox( _("Data modified, and no data filename to save modifications\n"\
  114. "Do you want to exit and abandon your change?"),
  115. _("Regulator list change"),
  116. wxYES_NO | wxICON_QUESTION );
  117. if( opt == wxNO )
  118. return;
  119. }
  120. else
  121. {
  122. if( !WriteDataFile() )
  123. {
  124. wxString msg;
  125. msg.Printf( _("Unable to write file<%s>\n"\
  126. "Do you want to exit and abandon your change?"), GetDataFilename().c_str() );
  127. int opt = wxMessageBox( msg, _("Write Data File Errror"),
  128. wxYES_NO | wxICON_QUESTION );
  129. if( opt == wxNO )
  130. return;
  131. }
  132. }
  133. }
  134. Destroy();
  135. }
  136. void PCB_CALCULATOR_FRAME::ReadConfig()
  137. {
  138. if( m_Config == NULL )
  139. return;
  140. long ltmp;
  141. wxString msg;
  142. m_Config->Read( KEYWORD_FRAME_POSX, &m_FramePos.x, -1 );
  143. m_Config->Read( KEYWORD_FRAME_POSY, &m_FramePos.y, -1 );
  144. m_Config->Read( KEYWORD_FRAME_SIZEX, &m_FrameSize.x, -1 );
  145. m_Config->Read( KEYWORD_FRAME_SIZEY, &m_FrameSize.y, -1 );
  146. m_Config->Read( KEYWORD_TRANSLINE_SELECTION, &ltmp, (long) default_type );
  147. m_currTransLineType = (enum transline_type_id) ltmp;
  148. m_Config->Read( KEYWORD_PAGE_SELECTION, &ltmp, 0 );
  149. m_Notebook->ChangeSelection( ltmp );
  150. m_Config->Read( KEYWORD_COLORCODE_SELECTION, &ltmp, 1 );
  151. m_rbToleranceSelection->SetSelection( ltmp );
  152. m_Config->Read( KEYWORD_ATTENUATORS_SELECTION, &ltmp, 0 );
  153. m_AttenuatorsSelection->SetSelection( ltmp );
  154. m_Config->Read( KEYWORD_BRDCLASS_SELECTION, &ltmp, 0 );
  155. m_BoardClassesUnitsSelector->SetSelection( ltmp );
  156. // Regul panel config:
  157. m_Config->Read( KEYWORD_REGUL_R1, &msg, wxT( "10" ) );
  158. m_RegulR1Value->SetValue( msg );
  159. m_Config->Read( KEYWORD_REGUL_R2, &msg, wxT( "10" ) );
  160. m_RegulR2Value->SetValue( msg );
  161. m_Config->Read( KEYWORD_REGUL_VREF, &msg, wxT( "3" ) );
  162. m_RegulVrefValue->SetValue( msg );
  163. m_Config->Read( KEYWORD_REGUL_VOUT, &msg, wxT( "12" ) );
  164. m_RegulVoutValue->SetValue( msg );
  165. m_Config->Read( KEYWORD_DATAFILE_FILENAME, &msg, wxT( "" ) );
  166. SetDataFilename( msg );
  167. m_Config->Read( KEYWORD_REGUL_SELECTED, &msg, wxT( "" ) );
  168. m_lastSelectedRegulatorName = msg;
  169. m_Config->Read( KEYWORD_REGUL_TYPE, &ltmp, 0 );
  170. m_choiceRegType->SetSelection( ltmp );
  171. m_Config->Read( KEYWORD_REGUL_LAST_PARAM, &ltmp, 0 );
  172. wxRadioButton * regprms[3] =
  173. { m_rbRegulR1, m_rbRegulR2, m_rbRegulVout
  174. };
  175. if( (unsigned)ltmp >= 3 )
  176. ltmp = 0;
  177. for( int ii = 0; ii < 3; ii++ )
  178. regprms[ii]->SetValue( ltmp == ii );
  179. // Electrical panel config
  180. m_Config->Read( KEYWORD_ELECTRICAL_SPACING_SELECTION, &ltmp, 0 );
  181. m_ElectricalSpacingUnitsSelector->SetSelection( ltmp );
  182. m_Config->Read( KEYWORD_ELECTRICAL_SPACING_VOLTAGE, &msg, wxT( "500" ) );
  183. m_ElectricalSpacingVoltage->SetValue( msg );
  184. for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
  185. m_transline_list[ii]->ReadConfig( m_Config );
  186. for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
  187. m_attenuator_list[ii]->ReadConfig( m_Config );
  188. }
  189. void PCB_CALCULATOR_FRAME::WriteConfig()
  190. {
  191. if( m_Config == NULL )
  192. return;
  193. if( !IsIconized() )
  194. {
  195. m_FrameSize = GetSize();
  196. m_FramePos = GetPosition();
  197. m_Config->Write( KEYWORD_FRAME_POSX, (long) m_FramePos.x );
  198. m_Config->Write( KEYWORD_FRAME_POSY, (long) m_FramePos.y );
  199. m_Config->Write( KEYWORD_FRAME_SIZEX, (long) m_FrameSize.x );
  200. m_Config->Write( KEYWORD_FRAME_SIZEY, (long) m_FrameSize.y );
  201. }
  202. m_Config->Write( KEYWORD_TRANSLINE_SELECTION, (long) m_currTransLineType );
  203. m_Config->Write( KEYWORD_PAGE_SELECTION, m_Notebook->GetSelection() );
  204. m_Config->Write( KEYWORD_COLORCODE_SELECTION, m_rbToleranceSelection->GetSelection() );
  205. m_Config->Write( KEYWORD_ATTENUATORS_SELECTION, m_AttenuatorsSelection->GetSelection() );
  206. m_Config->Write( KEYWORD_BRDCLASS_SELECTION, m_BoardClassesUnitsSelector->GetSelection() );
  207. m_Config->Write( KEYWORD_REGUL_R1, m_RegulR1Value->GetValue() );
  208. m_Config->Write( KEYWORD_REGUL_R2, m_RegulR2Value->GetValue() );
  209. m_Config->Write( KEYWORD_REGUL_VREF, m_RegulVrefValue->GetValue() );
  210. m_Config->Write( KEYWORD_REGUL_VOUT, m_RegulVoutValue->GetValue() );
  211. m_Config->Write( KEYWORD_DATAFILE_FILENAME, GetDataFilename() );
  212. m_Config->Write( KEYWORD_REGUL_SELECTED, m_lastSelectedRegulatorName );
  213. m_Config->Write( KEYWORD_REGUL_TYPE,
  214. m_choiceRegType->GetSelection() );
  215. wxRadioButton * regprms[3] =
  216. { m_rbRegulR1, m_rbRegulR2, m_rbRegulVout
  217. };
  218. for( int ii = 0; ii < 3; ii++ )
  219. {
  220. if( regprms[ii]->GetValue() )
  221. {
  222. m_Config->Write( KEYWORD_REGUL_LAST_PARAM, ii );
  223. break;
  224. }
  225. }
  226. m_Config->Write( KEYWORD_ELECTRICAL_SPACING_SELECTION,
  227. m_ElectricalSpacingUnitsSelector->GetSelection() );
  228. m_Config->Write( KEYWORD_ELECTRICAL_SPACING_VOLTAGE,
  229. m_ElectricalSpacingVoltage->GetValue() );
  230. TW_WriteConfig();
  231. for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
  232. m_transline_list[ii]->WriteConfig( m_Config );
  233. for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
  234. m_attenuator_list[ii]->WriteConfig( m_Config );
  235. }
  236. /**
  237. * Function OnTranslineAnalyse
  238. * Run a new analyse for the current transline with current parameters
  239. * and displays the electrical parmeters
  240. */
  241. void PCB_CALCULATOR_FRAME::OnTranslineAnalyse( wxCommandEvent& event )
  242. {
  243. if( m_currTransLine )
  244. {
  245. TransfDlgDataToTranslineParams();
  246. m_currTransLine->analyze();
  247. }
  248. }
  249. /**
  250. * Function OnTranslineSynthetize
  251. * Run a new synthezis for the current transline with current parameters
  252. * and displays the geometrical parmeters
  253. */
  254. void PCB_CALCULATOR_FRAME::OnTranslineSynthetize( wxCommandEvent& event )
  255. {
  256. if( m_currTransLine )
  257. {
  258. TransfDlgDataToTranslineParams();
  259. m_currTransLine->synthesize();
  260. }
  261. }
  262. void PCB_CALCULATOR_FRAME::OnPaintTranslinePanel( wxPaintEvent& event )
  263. {
  264. wxPaintDC dc( m_panelDisplayshape );
  265. TRANSLINE_IDENT* tr_ident = m_transline_list[m_currTransLineType];
  266. if( tr_ident )
  267. {
  268. wxSize size = m_panelDisplayshape->GetSize();
  269. size.x -= tr_ident->m_Icon->GetWidth();
  270. size.y -= tr_ident->m_Icon->GetHeight();
  271. dc.DrawBitmap( *tr_ident->m_Icon, size.x / 2, size.y / 2 );
  272. }
  273. event.Skip();
  274. }
  275. /* returns the full filename of the selected pcb_calculator data file
  276. * the extention file is forced
  277. */
  278. const wxString PCB_CALCULATOR_FRAME::GetDataFilename()
  279. {
  280. if( m_regulators_fileNameCtrl->GetValue().IsEmpty() )
  281. return wxEmptyString;
  282. wxFileName fn( m_regulators_fileNameCtrl->GetValue() );
  283. fn.SetExt( DataFileNameExt );
  284. return fn.GetFullPath();
  285. }
  286. /* Initialize the full filename of the selected pcb_calculator data file
  287. * force the standard extension of the file (.pcbcalc)
  288. * aFilename = the full filename, with or without extension
  289. */
  290. void PCB_CALCULATOR_FRAME::SetDataFilename( const wxString & aFilename)
  291. {
  292. if( aFilename.IsEmpty() )
  293. m_regulators_fileNameCtrl->SetValue( wxEmptyString );
  294. else
  295. {
  296. wxFileName fn( aFilename );
  297. fn.SetExt( DataFileNameExt );
  298. m_regulators_fileNameCtrl->SetValue( fn.GetFullPath() );
  299. }
  300. }