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

0001 /***************************************************************************
0002  *   Copyright (C) 2003-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 "ecbjt.h"
0012 #include "bjt.h"
0013 #include "ecnode.h"
0014 #include "libraryitem.h"
0015 
0016 #include <KLocalizedString>
0017 #include <QPainter>
0018 
0019 Item *ECBJT::constructNPN(ItemDocument *itemDocument, bool newItem, const char *id)
0020 {
0021     return new ECBJT(true, static_cast<ICNDocument *>(itemDocument), newItem, id);
0022 }
0023 
0024 Item *ECBJT::constructPNP(ItemDocument *itemDocument, bool newItem, const char *id)
0025 {
0026     return new ECBJT(false, static_cast<ICNDocument *>(itemDocument), newItem, id);
0027 }
0028 
0029 LibraryItem *ECBJT::libraryItemNPN()
0030 {
0031     return new LibraryItem(QStringList(QString("ec/npnbjt")), i18n("NPN"), i18n("Discrete"), "npn.png", LibraryItem::lit_component, ECBJT::constructNPN);
0032 }
0033 
0034 LibraryItem *ECBJT::libraryItemPNP()
0035 {
0036     return new LibraryItem(QStringList(QString("ec/pnpbjt")), i18n("PNP"), i18n("Discrete"), "pnp.png", LibraryItem::lit_component, ECBJT::constructPNP);
0037 }
0038 
0039 ECBJT::ECBJT(bool isNPN, ICNDocument *icnDocument, bool newItem, const char *id)
0040     : Component(icnDocument, newItem, id ? id : (isNPN ? "npnbjt" : "pnpbjt"))
0041 {
0042     m_bIsNPN = isNPN;
0043     if (m_bIsNPN)
0044         m_name = i18n("NPN Transistor");
0045     else
0046         m_name = i18n("PNP Transistor");
0047 
0048     setSize(-8, -8, 16, 16);
0049     m_pBJT = createBJT(createPin(8, -16, 90, "c"), createPin(-16, 0, 0, "b"), createPin(8, 16, 270, "e"), m_bIsNPN);
0050 
0051     BJTSettings s; // will be created with the default settings
0052 
0053     Variant *v = createProperty("I_S", Variant::Type::Double);
0054     v->setCaption(i18n("Saturation Current"));
0055     v->setUnit("A");
0056     v->setMinValue(1e-20);
0057     v->setMaxValue(1e-0);
0058     v->setValue(s.I_S);
0059     v->setAdvanced(true);
0060 
0061     v = createProperty("N_F", Variant::Type::Double);
0062     v->setCaption(i18n("Forward Coefficient"));
0063     v->setMinValue(1e0);
0064     v->setMaxValue(1e1);
0065     v->setValue(s.N_F);
0066     v->setAdvanced(true);
0067 
0068     v = createProperty("N_R", Variant::Type::Double);
0069     v->setCaption(i18n("Reverse Coefficient"));
0070     v->setMinValue(1e0);
0071     v->setMaxValue(1e1);
0072     v->setValue(s.N_R);
0073     v->setAdvanced(true);
0074 
0075     v = createProperty("B_F", Variant::Type::Double);
0076     v->setCaption(i18n("Forward Beta"));
0077     v->setMinValue(1e-1);
0078     v->setMaxValue(1e3);
0079     v->setValue(s.B_F);
0080     v->setAdvanced(true);
0081 
0082     v = createProperty("B_R", Variant::Type::Double);
0083     v->setCaption(i18n("Reverse Beta"));
0084     v->setMinValue(1e-1);
0085     v->setMaxValue(1e3);
0086     v->setValue(s.B_R);
0087     v->setAdvanced(true);
0088 }
0089 
0090 ECBJT::~ECBJT()
0091 {
0092 }
0093 
0094 void ECBJT::dataChanged()
0095 {
0096     BJTSettings s;
0097     s.I_S = dataDouble("I_S");
0098     s.N_F = dataDouble("N_F");
0099     s.N_R = dataDouble("N_R");
0100     s.B_F = dataDouble("B_F");
0101     s.B_R = dataDouble("B_R");
0102 
0103     m_pBJT->setBJTSettings(s);
0104 }
0105 
0106 void ECBJT::drawShape(QPainter &p)
0107 {
0108     const int _x = int(x());
0109     const int _y = int(y());
0110 
0111     initPainter(p);
0112 
0113     p.drawLine(_x - 8, _y - 8, _x - 8, _y + 8);
0114     p.drawLine(_x + 8, _y - 8, _x - 8, _y);
0115     p.drawLine(_x + 8, _y + 8, _x - 8, _y);
0116 
0117     QPolygon pa(3);
0118     if (m_bIsNPN) {
0119         pa[0] = QPoint(_x + 6, _y + 7);
0120         pa[1] = QPoint(_x + 2, _y + 8);
0121         pa[2] = QPoint(_x + 5, _y + 3);
0122     } else {
0123         pa[0] = QPoint(_x - 7, _y + 1);
0124         pa[1] = QPoint(_x - 4, _y + 5);
0125         pa[2] = QPoint(_x - 2, _y);
0126     }
0127     p.setBrush(p.pen().color());
0128     p.drawPolygon(pa);
0129 
0130     deinitPainter(p);
0131 }