File indexing completed on 2024-05-05 05:46:05

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "ecmosfet.h"
0012 #include "libraryitem.h"
0013 #include "mosfet.h"
0014 
0015 #include <KLocalizedString>
0016 
0017 #include <QDebug>
0018 #include <QPainter>
0019 
0020 Item *ECMOSFET::constructNEM(ItemDocument *itemDocument, bool newItem, const char *id)
0021 {
0022     return new ECMOSFET(MOSFET::neMOSFET, static_cast<ICNDocument *>(itemDocument), newItem, id ? id : "nemosfet");
0023 }
0024 Item *ECMOSFET::constructPEM(ItemDocument *itemDocument, bool newItem, const char *id)
0025 {
0026     return new ECMOSFET(MOSFET::peMOSFET, static_cast<ICNDocument *>(itemDocument), newItem, id ? id : "pemosfet");
0027 }
0028 #if 0
0029 Item * ECMOSFET::constructNDM( ItemDocument * itemDocument, bool newItem, const char * id )
0030 {
0031     return new ECMOSFET( MOSFET::ndMOSFET, (ICNDocument*)itemDocument, newItem, id ? id : "ndmosfet" );
0032 }
0033 Item * ECMOSFET::constructPDM( ItemDocument * itemDocument, bool newItem, const char * id )
0034 {
0035     return new ECMOSFET( MOSFET::pdMOSFET, (ICNDocument*)itemDocument, newItem, id ? id : "pdmosfet" );
0036 }
0037 #endif
0038 
0039 LibraryItem *ECMOSFET::libraryItemNEM()
0040 {
0041     return new LibraryItem(QStringList(QString("ec/nemosfet")),
0042                            //   i18n("n-e MOSFET"),
0043                            i18n("n-MOSFET"),
0044                            i18n("Discrete"),
0045                            "nemosfet.png",
0046                            LibraryItem::lit_component,
0047                            ECMOSFET::constructNEM);
0048 }
0049 
0050 LibraryItem *ECMOSFET::libraryItemPEM()
0051 {
0052     return new LibraryItem(QStringList(QString("ec/pemosfet")),
0053                            //   i18n("p-e MOSFET"),
0054                            i18n("p-MOSFET"),
0055                            i18n("Discrete"),
0056                            "pemosfet.png",
0057                            LibraryItem::lit_component,
0058                            ECMOSFET::constructPEM);
0059 }
0060 
0061 #if 0
0062 LibraryItem* ECMOSFET::libraryItemNDM()
0063 {
0064     return new LibraryItem(
0065             QStringList(QString("ec/ndmosfet")),
0066     i18n("n-d MOSFET"),
0067     i18n("Discrete"),
0068     "ndmosfet.png",
0069     LibraryItem::lit_component,
0070     ECMOSFET::constructNDM );
0071 }
0072 
0073 
0074 LibraryItem* ECMOSFET::libraryItemPDM()
0075 {
0076     return new LibraryItem(
0077             QStringList(QString("ec/pdmosfet")),
0078     i18n("p-d MOSFET"),
0079     i18n("Discrete"),
0080     "pdmosfet.png",
0081     LibraryItem::lit_component,
0082     ECMOSFET::constructPDM );
0083 }
0084 #endif
0085 
0086 ECMOSFET::ECMOSFET(int MOSFET_type, ICNDocument *icnDocument, bool newItem, const char *id)
0087     : Component(icnDocument, newItem, id)
0088 {
0089     m_MOSFET_type = MOSFET_type;
0090     switch (static_cast<MOSFET::MOSFET_type>(m_MOSFET_type)) {
0091     case MOSFET::neMOSFET: {
0092         m_name = i18n("N-Channel Enhancement MOSFET");
0093         break;
0094     }
0095 
0096     case MOSFET::peMOSFET: {
0097         m_name = i18n("P-Channel Enhancement MOSFET");
0098         break;
0099     }
0100 
0101 #if 0
0102         case MOSFET::ndMOSFET:
0103         {
0104             m_name = i18n("N-Channel Depletion MOSFET");
0105             break;
0106         }
0107         
0108         case MOSFET::pdMOSFET:
0109         {
0110             m_name = i18n("P-Channel Depletion MOSFET");
0111             break;
0112         }
0113 #endif
0114     }
0115 
0116     setSize(-8, -16, 16, 32);
0117     ECNode *NodeS = createPin(8, 24, 270, "s");
0118     m_pMOSFET = createMOSFET(createPin(8, -24, 90, "d"), createPin(-16, 8, 0, "g"), NodeS, NodeS, m_MOSFET_type);
0119     m_bHaveBodyPin = false;
0120 
0121     Variant *v = createProperty("bodyPin", Variant::Type::Bool);
0122     v->setCaption(i18nc("mosfet body/bulk pin", "Body Pin"));
0123     v->setValue(false);
0124 
0125 #if 0
0126     MOSFETSettings s; // will be created with the default settings
0127     v = createProperty( "I_S", Variant::Type::Double );
0128     v->setCaption( i18n("Saturation Current") );
0129     v->setUnit("A");
0130     v->setMinValue(1e-20);
0131     v->setMaxValue(1e-0);
0132     v->setValue( s.I_S );
0133     v->setAdvanced(true);
0134     
0135     v = createProperty( "N_F", Variant::Type::Double );
0136     v->setCaption( i18n("Forward Coefficient") );
0137     v->setMinValue(1e0);
0138     v->setMaxValue(1e1);
0139     v->setValue( s.N_F );
0140     v->setAdvanced(true);
0141     
0142     v = createProperty( "N_R", Variant::Type::Double );
0143     v->setCaption( i18n("Reverse Coefficient") );
0144     v->setMinValue(1e0);
0145     v->setMaxValue(1e1);
0146     v->setValue( s.N_R );
0147     v->setAdvanced(true);
0148     
0149     v = createProperty( "B_F", Variant::Type::Double );
0150     v->setCaption( i18n("Forward Beta") );
0151     v->setMinValue(1e-1);
0152     v->setMaxValue(1e3);
0153     v->setValue( s.B_F );
0154     v->setAdvanced(true);
0155     
0156     v = createProperty( "B_R", Variant::Type::Double );
0157     v->setCaption( i18n("Reverse Beta") );
0158     v->setMinValue(1e-1);
0159     v->setMaxValue(1e3);
0160     v->setValue( s.B_R );
0161     v->setAdvanced(true);
0162 #endif
0163 }
0164 
0165 ECMOSFET::~ECMOSFET()
0166 {
0167 }
0168 
0169 void ECMOSFET::dataChanged()
0170 {
0171     bool haveBodyPin = dataBool("bodyPin");
0172     if (haveBodyPin != m_bHaveBodyPin) {
0173         m_bHaveBodyPin = haveBodyPin;
0174         if (m_bHaveBodyPin) {
0175             // Creating a body pin
0176             ECNode *NodeB = createPin(16, 0, 180, "b");
0177             removeElement(m_pMOSFET, false);
0178             m_pMOSFET = createMOSFET(ecNodeWithID("d"), ecNodeWithID("g"), ecNodeWithID("s"), NodeB, m_MOSFET_type);
0179         } else {
0180             // Removing a body pin
0181             removeNode("b");
0182             removeElement(m_pMOSFET, false);
0183             m_pMOSFET = createMOSFET(ecNodeWithID("d"), ecNodeWithID("g"), ecNodeWithID("s"), ecNodeWithID("s"), m_MOSFET_type);
0184         }
0185     }
0186 
0187 #if 0
0188     MOSFETSettings s;
0189     s.I_S = dataDouble( "I_S" );
0190     s.N_F = dataDouble( "N_F" );
0191     s.N_R = dataDouble( "N_R" );
0192     s.B_F = dataDouble( "B_F" );
0193     s.B_R = dataDouble( "B_R" );
0194     
0195     m_pMOSFET->setMOSFETSettings( s );
0196 #endif
0197 }
0198 
0199 void ECMOSFET::drawShape(QPainter &p)
0200 {
0201     const int _x = int(x());
0202     const int _y = int(y());
0203 
0204     initPainter(p);
0205 
0206     // Middle three horizontal lines
0207     p.drawLine(_x - 3, _y - 11, _x + 8, _y - 11);
0208     p.drawLine(_x - 3, _y, _x + 8, _y);
0209     p.drawLine(_x - 3, _y + 11, _x + 8, _y + 11);
0210 
0211     // Right middle vertical line
0212     if (m_bHaveBodyPin)
0213         p.drawLine(_x + 8, _y + 11, _x + 8, _y + 16);
0214     else
0215         p.drawLine(_x + 8, _y, _x + 8, _y + 16);
0216 
0217     // Right top vertical line
0218     p.drawLine(_x + 8, _y - 11, _x + 8, _y - 16);
0219 
0220     QPen pen = p.pen();
0221     pen.setWidth(2);
0222     p.setPen(pen);
0223 
0224     // Back line
0225     p.drawLine(_x - 7, _y - 10, _x - 7, _y + 11);
0226 
0227     if (m_MOSFET_type == MOSFET::neMOSFET || m_MOSFET_type == MOSFET::peMOSFET) {
0228         // Middle three vertical lines
0229         p.drawLine(_x - 2, _y - 14, _x - 2, _y - 7);
0230         p.drawLine(_x - 2, _y - 3, _x - 2, _y + 4);
0231         p.drawLine(_x - 2, _y + 8, _x - 2, _y + 15);
0232     } else {
0233         // Middle vertical line
0234         p.drawLine(_x - 3, _y - 14, _x - 3, _y + 15);
0235     }
0236 
0237     QPolygon pa(3);
0238     if ( m_MOSFET_type == MOSFET::neMOSFET /*||
0239             m_MOSFET_type == MOSFET::ndMOSFET*/ )
0240     {
0241         // Inwards facing arrow
0242         pa[0] = QPoint(0, 0);
0243         pa[1] = QPoint(5, -3);
0244         pa[2] = QPoint(5, 3);
0245     } else {
0246         // Outwards facing arrow
0247         pa[0] = QPoint(2, -3);
0248         pa[1] = QPoint(7, 0);
0249         pa[2] = QPoint(2, 3);
0250     }
0251 
0252     pa.translate(_x, _y);
0253     p.setPen(p.pen().color());
0254     p.setBrush(p.pen().color());
0255     p.drawPolygon(pa);
0256 
0257     deinitPainter(p);
0258 }