pcb_calculator_frame.cpp 12.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 1992-2011 jean-pierre.charras
 * Copyright (C) 1992-2011 Kicad Developers, see change_log.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 2
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
24 25
#include <wx/wx.h>
#include <wx/config.h>
26

27
#include <pgm_base.h>
28 29 30
#include <pcb_calculator.h>
#include <UnitSelector.h>
#include <bitmaps.h>
31

32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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" )
51
#define KEYWORD_DATAFILE_FILENAME               wxT( "DataFilename" )
52

53 54 55
// extention of pcb_calculator data filename:
const wxString DataFileNameExt( wxT("pcbcalc") );

56 57
PCB_CALCULATOR_FRAME::PCB_CALCULATOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
    PCB_CALCULATOR_FRAME_BASE( aParent )
58
{
59 60
    SetKiway( this, aKiway );

61
    m_currTransLine     = NULL;
62
    m_currTransLineType = DEFAULT_TYPE;
63 64
    m_currAttenuator    = NULL;
    m_RegulatorListChanged = false;
65
    m_Config = GetNewConfig( Pgm().App().GetAppName() );
66 67

    // Populate transline list ordered like in dialog menu list
68
    const static TRANSLINE_TYPE_ID tltype_list[8] =
69
    {
70 71 72
        MICROSTRIP_TYPE,    CPW_TYPE,  GROUNDED_CPW_TYPE,
        RECTWAVEGUIDE_TYPE, COAX_TYPE, C_MICROSTRIP_TYPE,
        STRIPLINE_TYPE,     TWISTEDPAIR_TYPE
73
    };
74

75 76 77 78 79 80 81 82 83 84 85 86
    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];

    ReadConfig();

87 88
    ReadDataFile();

89 90 91
    TranslineTypeSelection( m_currTransLineType );
    m_TranslineSelection->SetSelection( m_currTransLineType );

92 93
    TW_Init();

94 95 96 97 98 99
    SetAttenuator( m_AttenuatorsSelection->GetSelection() );

    ToleranceSelection( m_rbToleranceSelection->GetSelection() );

    BoardClassesUpdateData( m_BoardClassesUnitsSelector->GetUnitScale() );

100 101
    ElectricalSpacingUpdateData( m_ElectricalSpacingUnitsSelector->GetUnitScale() );

102 103 104
    m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
    SelectLastSelectedRegulator();

105 106 107 108
    // Give an icon
    wxIcon icon;
    icon.CopyFromBitmap( KiBitmap( icon_pcbcalculator_xpm ) );
    SetIcon( icon );
109 110 111 112 113 114 115 116 117 118 119 120 121 122

    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()
{
    WriteConfig();
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137
    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];

    delete m_Config;

    /* This needed for OSX: avoids furter OnDraw processing after this
     * destructor and before the native window is destroyed
     */
    this->Freeze();
}

138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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;
                msg.Printf( _("Unable to write file<%s>\n"\
158
"Do you want to exit and abandon your change?"), GetDataFilename().c_str() );
159

160
                int opt = wxMessageBox( msg, _("Write Data File Error"),
161 162 163 164 165 166 167 168 169
                                        wxYES_NO | wxICON_QUESTION );
                if( opt  == wxNO )
                    return;
            }
        }
    }

    Destroy();
}
170 171 172 173 174 175

void PCB_CALCULATOR_FRAME::ReadConfig()
{
    if( m_Config == NULL )
        return;

176 177
    long        ltmp;
    wxString    msg;
178 179 180 181
    m_Config->Read( KEYWORD_FRAME_POSX, &m_FramePos.x, -1 );
    m_Config->Read( KEYWORD_FRAME_POSY, &m_FramePos.y, -1 );
    m_Config->Read( KEYWORD_FRAME_SIZEX, &m_FrameSize.x, -1 );
    m_Config->Read( KEYWORD_FRAME_SIZEY, &m_FrameSize.y, -1 );
182 183
    m_Config->Read( KEYWORD_TRANSLINE_SELECTION, &ltmp, (long) DEFAULT_TYPE );
    m_currTransLineType = (enum TRANSLINE_TYPE_ID) ltmp;
184 185 186 187 188 189 190 191
    m_Config->Read( KEYWORD_PAGE_SELECTION, &ltmp, 0 );
    m_Notebook->ChangeSelection( ltmp );
    m_Config->Read( KEYWORD_COLORCODE_SELECTION, &ltmp, 1 );
    m_rbToleranceSelection->SetSelection( ltmp );
    m_Config->Read( KEYWORD_ATTENUATORS_SELECTION, &ltmp, 0 );
    m_AttenuatorsSelection->SetSelection( ltmp );
    m_Config->Read( KEYWORD_BRDCLASS_SELECTION, &ltmp, 0 );
    m_BoardClassesUnitsSelector->SetSelection( ltmp );
192 193 194

    // Regul panel config:
    m_Config->Read( KEYWORD_REGUL_R1, &msg, wxT( "10" ) );
195
    m_RegulR1Value->SetValue( msg );
196
    m_Config->Read( KEYWORD_REGUL_R2, &msg, wxT( "10" ) );
197
    m_RegulR2Value->SetValue( msg );
198
    m_Config->Read( KEYWORD_REGUL_VREF, &msg, wxT( "3" ) );
199
    m_RegulVrefValue->SetValue( msg );
200
    m_Config->Read( KEYWORD_REGUL_VOUT, &msg, wxT( "12" ) );
201
    m_RegulVoutValue->SetValue( msg );
202 203
    m_Config->Read( KEYWORD_DATAFILE_FILENAME, &msg, wxT( "" ) );
    SetDataFilename( msg );
204 205 206 207 208 209 210 211 212 213 214 215 216 217
    m_Config->Read( KEYWORD_REGUL_SELECTED, &msg, wxT( "" ) );
    m_lastSelectedRegulatorName = msg;
    m_Config->Read( KEYWORD_REGUL_TYPE, &ltmp, 0 );
    m_choiceRegType->SetSelection( ltmp );
    m_Config->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
218 219
    m_Config->Read( KEYWORD_ELECTRICAL_SPACING_SELECTION, &ltmp, 0 );
    m_ElectricalSpacingUnitsSelector->SetSelection( ltmp );
220
    m_Config->Read( KEYWORD_ELECTRICAL_SPACING_VOLTAGE, &msg, wxT( "500" ) );
221
    m_ElectricalSpacingVoltage->SetValue( msg );
222 223 224

    for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
        m_transline_list[ii]->ReadConfig( m_Config );
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
        m_attenuator_list[ii]->ReadConfig( m_Config );
}


void PCB_CALCULATOR_FRAME::WriteConfig()
{
    if( m_Config == NULL )
        return;

    if( !IsIconized() )
    {
        m_FrameSize = GetSize();
        m_FramePos  = GetPosition();

        m_Config->Write( KEYWORD_FRAME_POSX, (long) m_FramePos.x );
        m_Config->Write( KEYWORD_FRAME_POSY, (long) m_FramePos.y );
        m_Config->Write( KEYWORD_FRAME_SIZEX, (long) m_FrameSize.x );
        m_Config->Write( KEYWORD_FRAME_SIZEY, (long) m_FrameSize.y );
    }
246

247 248 249
    m_Config->Write( KEYWORD_TRANSLINE_SELECTION, (long) m_currTransLineType );
    m_Config->Write( KEYWORD_PAGE_SELECTION, m_Notebook->GetSelection() );
    m_Config->Write( KEYWORD_COLORCODE_SELECTION, m_rbToleranceSelection->GetSelection() );
250
    m_Config->Write( KEYWORD_ATTENUATORS_SELECTION, m_AttenuatorsSelection->GetSelection() );
251
    m_Config->Write( KEYWORD_BRDCLASS_SELECTION, m_BoardClassesUnitsSelector->GetSelection() );
252

253 254 255 256
    m_Config->Write( KEYWORD_REGUL_R1, m_RegulR1Value->GetValue() );
    m_Config->Write( KEYWORD_REGUL_R2, m_RegulR2Value->GetValue() );
    m_Config->Write( KEYWORD_REGUL_VREF, m_RegulVrefValue->GetValue() );
    m_Config->Write( KEYWORD_REGUL_VOUT, m_RegulVoutValue->GetValue() );
257
    m_Config->Write( KEYWORD_DATAFILE_FILENAME, GetDataFilename() );
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    m_Config->Write( KEYWORD_REGUL_SELECTED, m_lastSelectedRegulatorName );
    m_Config->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() )
        {
            m_Config->Write( KEYWORD_REGUL_LAST_PARAM, ii );
            break;
        }
    }


274 275 276 277
    m_Config->Write( KEYWORD_ELECTRICAL_SPACING_SELECTION,
                     m_ElectricalSpacingUnitsSelector->GetSelection() );
    m_Config->Write( KEYWORD_ELECTRICAL_SPACING_VOLTAGE,
                     m_ElectricalSpacingVoltage->GetValue() );
278

279
    TW_WriteConfig();
280 281 282

    for( unsigned ii = 0; ii < m_transline_list.size(); ii++ )
        m_transline_list[ii]->WriteConfig( m_Config );
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    for( unsigned ii = 0; ii < m_attenuator_list.size(); ii++ )
        m_attenuator_list[ii]->WriteConfig( m_Config );
}


/**
 * 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();
    }
}

303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
/**
 * 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 )
{
321 322 323
    wxPaintDC           dc( m_panelDisplayshape );

    TRANSLINE_IDENT*    tr_ident = m_transline_list[m_currTransLineType];
324 325 326 327

    if( tr_ident )
    {
        wxSize size = m_panelDisplayshape->GetSize();
328 329
        size.x  -= tr_ident->m_Icon->GetWidth();
        size.y  -= tr_ident->m_Icon->GetHeight();
330 331 332 333 334
        dc.DrawBitmap( *tr_ident->m_Icon, size.x / 2, size.y / 2 );
    }

    event.Skip();
}
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* 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() );
    }
}