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

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 "dependentsource.h"
0012 #include "ecnode.h"
0013 #include "libraryitem.h"
0014 #include "pin.h"
0015 
0016 #include "cccs.h"
0017 #include "ccvs.h"
0018 #include "vccs.h"
0019 #include "vcvs.h"
0020 
0021 #include <KLocalizedString>
0022 #include <QPainter>
0023 
0024 // BEGIN class DependentSource
0025 DependentSource::DependentSource(ICNDocument *icnDocument, bool newItem, const char *id)
0026     : Component(icnDocument, newItem, id)
0027 {
0028     setSize(-16, -16, 32, 32);
0029 
0030     init2PinLeft();
0031     init2PinRight();
0032 
0033     m_pNNode[1]->setLength(13);
0034     m_pPNode[1]->setLength(13);
0035 
0036     createProperty("gain", Variant::Type::Double);
0037     property("gain")->setCaption(i18n("Gain"));
0038     property("gain")->setValue(1.0);
0039 
0040     addDisplayText("gain", QRect(-16, -32, 32, 16), "");
0041 }
0042 
0043 DependentSource::~DependentSource()
0044 {
0045 }
0046 
0047 void DependentSource::drawOutline(QPainter &p)
0048 {
0049     const int _x = int(x()) - 16;
0050     const int _y = int(y()) - 32;
0051 
0052     // Top rectangle
0053     p.drawRect(_x, _y + 19, width(), 11);
0054 
0055 #if 0
0056     p.save();
0057     bool canSetCol = (p.pen().color() != Qt::color0) && (p.pen().color() != Qt::color1);
0058     
0059     // Bottom lines
0060     if (canSetCol)
0061         p.setPen( m_pNNode[1]->isSelected() ? m_selectedCol : Qt::black );
0062     p.drawLine( _x, _y+40, _x+8, _y+40 ); // Left inny
0063     
0064     if (canSetCol)
0065         p.setPen( m_pPNode[1]->isSelected() ? m_selectedCol : Qt::black );
0066     p.drawLine( _x+width(), _y+40, _x+24, _y+40 ); // Right inny
0067     
0068     p.restore();
0069 #endif
0070 
0071     // Bottom diamond
0072     QPolygon pa4(4);
0073     pa4[0] = QPoint(_x + 6, _y + 40);
0074     pa4[1] = QPoint(_x + 16, _y + 32);
0075     pa4[2] = QPoint(_x + 26, _y + 40);
0076     pa4[3] = QPoint(_x + 16, _y + 48);
0077     p.drawPolygon(pa4);
0078 }
0079 
0080 void DependentSource::drawTopArrow(QPainter &p)
0081 {
0082     const int _x = int(x()) - 16;
0083     const int _y = int(y()) - 32;
0084 
0085     if (p.pen().color() == m_selectedCol)
0086         p.setPen(Qt::black);
0087 
0088     if (p.brush().color() == m_brushCol)
0089         p.setBrush(Qt::black);
0090 
0091     p.drawLine(_x + 8, _y + 24, _x + 24, _y + 24);
0092 
0093     QPolygon pa3(3);
0094     pa3[0] = QPoint(_x + 24, _y + 24);
0095     pa3[1] = QPoint(_x + 19, _y + 21);
0096     pa3[2] = QPoint(_x + 19, _y + 27);
0097     p.drawPolygon(pa3);
0098 }
0099 
0100 void DependentSource::drawBottomArrow(QPainter &p)
0101 {
0102     const int _x = int(x()) - 16;
0103     const int _y = int(y()) - 32;
0104 
0105     if (p.pen().color() == m_selectedCol)
0106         p.setPen(Qt::black);
0107 
0108     if (p.brush().color() == m_brushCol)
0109         p.setBrush(Qt::black);
0110 
0111     p.drawLine(_x + 11, _y + 40, _x + 21, _y + 40);
0112 
0113     QPolygon pa3(3);
0114     pa3[0] = QPoint(_x + 21, _y + 40);
0115     pa3[1] = QPoint(_x + 16, _y + 37);
0116     pa3[2] = QPoint(_x + 16, _y + 43);
0117     p.drawPolygon(pa3);
0118 }
0119 // END class DependentSource
0120 
0121 // BEGIN class ECCCCS
0122 Item *ECCCCS::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0123 {
0124     return new ECCCCS(static_cast<ICNDocument *>(itemDocument), newItem, id);
0125 }
0126 
0127 LibraryItem *ECCCCS::libraryItem()
0128 {
0129     return new LibraryItem(QStringList(QString("ec/cccs")), i18n("CCCS"), i18n("Sources"), "cccs.png", LibraryItem::lit_component, ECCCCS::construct);
0130 }
0131 
0132 ECCCCS::ECCCCS(ICNDocument *icnDocument, bool newItem, const char *id)
0133     : DependentSource(icnDocument, newItem, id ? id : "cccs")
0134 {
0135     m_name = i18n("Current Controlled Current Source");
0136     m_cccs = createCCCS(m_pNNode[0], m_pPNode[0], m_pNNode[1], m_pPNode[1], 1.);
0137     m_pNNode[1]->pin()->setGroundType(Pin::gt_medium);
0138 }
0139 
0140 ECCCCS::~ECCCCS()
0141 {
0142 }
0143 
0144 void ECCCCS::dataChanged()
0145 {
0146     double gain = dataDouble("gain");
0147 
0148     QString display = QString::number(gain / getMultiplier(gain), 'g', 3) + getNumberMag(gain) + QChar(' ');
0149     setDisplayText("gain", display);
0150 
0151     m_cccs->setGain(gain);
0152 }
0153 
0154 void ECCCCS::drawShape(QPainter &p)
0155 {
0156     initPainter(p);
0157     drawOutline(p);
0158     drawTopArrow(p);
0159     drawBottomArrow(p);
0160     deinitPainter(p);
0161 }
0162 // END class ECCCCS
0163 
0164 // BEGIN class ECCCVS
0165 Item *ECCCVS::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0166 {
0167     return new ECCCVS(static_cast<ICNDocument *>(itemDocument), newItem, id);
0168 }
0169 
0170 LibraryItem *ECCCVS::libraryItem()
0171 {
0172     return new LibraryItem(QStringList(QString("ec/ccvs")), i18n("CCVS"), i18n("Sources"), "ccvs.png", LibraryItem::lit_component, ECCCVS::construct);
0173 }
0174 
0175 ECCCVS::ECCCVS(ICNDocument *icnDocument, bool newItem, const char *id)
0176     : DependentSource(icnDocument, newItem, id ? id : "ccvs")
0177 {
0178     m_name = i18n("Current Controlled Voltage Source");
0179     m_ccvs = createCCVS(m_pNNode[0], m_pPNode[0], m_pNNode[1], m_pPNode[1], 1.);
0180     m_pNNode[1]->pin()->setGroundType(Pin::gt_medium);
0181 }
0182 
0183 ECCCVS::~ECCCVS()
0184 {
0185 }
0186 
0187 void ECCCVS::dataChanged()
0188 {
0189     double gain = dataDouble("gain");
0190 
0191     QString display = QString::number(gain / getMultiplier(gain), 'g', 3) + getNumberMag(gain) + QChar(' ');
0192     setDisplayText("gain", display);
0193 
0194     m_ccvs->setGain(gain);
0195 }
0196 
0197 void ECCCVS::drawShape(QPainter &p)
0198 {
0199     initPainter(p);
0200     drawOutline(p);
0201     drawTopArrow(p);
0202     deinitPainter(p);
0203 }
0204 // END class ECCCVS
0205 
0206 // BEGIN class ECVCCS
0207 Item *ECVCCS::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0208 {
0209     return new ECVCCS(static_cast<ICNDocument *>(itemDocument), newItem, id);
0210 }
0211 
0212 LibraryItem *ECVCCS::libraryItem()
0213 {
0214     return new LibraryItem(QStringList(QString("ec/vccs")), i18n("VCCS"), i18n("Sources"), "vccs.png", LibraryItem::lit_component, ECVCCS::construct);
0215 }
0216 
0217 ECVCCS::ECVCCS(ICNDocument *icnDocument, bool newItem, const char *id)
0218     : DependentSource(icnDocument, newItem, id ? id : "vccs")
0219 {
0220     m_name = i18n("Voltage Controlled Current Source");
0221     m_vccs = createVCCS(m_pNNode[0], m_pPNode[0], m_pNNode[1], m_pPNode[1], 1.);
0222     m_pNNode[1]->pin()->setGroundType(Pin::gt_medium);
0223 }
0224 
0225 ECVCCS::~ECVCCS()
0226 {
0227 }
0228 
0229 void ECVCCS::dataChanged()
0230 {
0231     double gain = dataDouble("gain");
0232 
0233     QString display = QString::number(gain / getMultiplier(gain), 'g', 3) + getNumberMag(gain) + QChar(' ');
0234     setDisplayText("gain", display);
0235 
0236     m_vccs->setGain(gain);
0237 }
0238 
0239 void ECVCCS::drawShape(QPainter &p)
0240 {
0241     initPainter(p);
0242     drawOutline(p);
0243     drawBottomArrow(p);
0244     deinitPainter(p);
0245 }
0246 // END class ECVCCS
0247 
0248 // BEGIN class ECVCVS
0249 Item *ECVCVS::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0250 {
0251     return new ECVCVS(static_cast<ICNDocument *>(itemDocument), newItem, id);
0252 }
0253 
0254 LibraryItem *ECVCVS::libraryItem()
0255 {
0256     return new LibraryItem(QStringList(QString("ec/vcvs")), i18n("VCVS"), i18n("Sources"), "vcvs.png", LibraryItem::lit_component, ECVCVS::construct);
0257 }
0258 
0259 ECVCVS::ECVCVS(ICNDocument *icnDocument, bool newItem, const char *id)
0260     : DependentSource(icnDocument, newItem, id ? id : "vcvs")
0261 {
0262     m_name = i18n("Voltage Controlled Voltage Source");
0263     m_vcvs = createVCVS(m_pNNode[0], m_pPNode[0], m_pNNode[1], m_pPNode[1], 1.);
0264     m_pNNode[1]->pin()->setGroundType(Pin::gt_medium);
0265 }
0266 
0267 ECVCVS::~ECVCVS()
0268 {
0269 }
0270 
0271 void ECVCVS::dataChanged()
0272 {
0273     double gain = dataDouble("gain");
0274 
0275     QString display = QString::number(gain / getMultiplier(gain), 'g', 3) + getNumberMag(gain) + QChar(' ');
0276     setDisplayText("gain", display);
0277 
0278     m_vcvs->setGain(gain);
0279 }
0280 
0281 void ECVCVS::drawShape(QPainter &p)
0282 {
0283     initPainter(p);
0284     drawOutline(p);
0285     deinitPainter(p);
0286 }
0287 // END class ECVCVS