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

0001 /***************************************************************************
0002  *   Copyright (C) 2003-2004 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  *   Peak/RMS added (c)19/06/2007 by Jason Lucas               *
0011  ***************************************************************************/
0012 
0013 #include "ecvoltagesignal.h"
0014 #include "ecnode.h"
0015 #include "libraryitem.h"
0016 #include "pin.h"
0017 #include "simulator.h"
0018 #include "voltagesignal.h"
0019 
0020 #include <KLocalizedString>
0021 #include <QPainter>
0022 #include <QString>
0023 #include <cmath>
0024 
0025 Item *ECVoltageSignal::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0026 {
0027     return new ECVoltageSignal(static_cast<ICNDocument *>(itemDocument), newItem, id);
0028 }
0029 
0030 LibraryItem *ECVoltageSignal::libraryItem()
0031 {
0032     return new LibraryItem(QStringList(QString("ec/voltage_signal")), i18n("Voltage Signal"), i18n("Sources"), "voltagesignal.png", LibraryItem::lit_component, ECVoltageSignal::construct);
0033 }
0034 
0035 ECVoltageSignal::ECVoltageSignal(ICNDocument *icnDocument, bool newItem, const char *id)
0036     : Component(icnDocument, newItem, id ? id : "voltage_signal")
0037 {
0038     m_name = i18n("Voltage Signal");
0039     setSize(-8, -8, 16, 16);
0040 
0041     init1PinLeft();
0042     init1PinRight();
0043 
0044     m_pNNode[0]->pin()->setGroundType(Pin::gt_medium);
0045     m_voltageSignal = createVoltageSignal(m_pNNode[0], m_pPNode[0], 0.);
0046     m_voltageSignal->setStep(ElementSignal::st_sinusoidal, 50.);
0047 
0048     createProperty("frequency", Variant::Type::Double);
0049     property("frequency")->setCaption(i18n("Frequency"));
0050     property("frequency")->setUnit("Hz");
0051     property("frequency")->setMinValue(1e-9);
0052     property("frequency")->setMaxValue(1e3);
0053     property("frequency")->setValue(50.0);
0054 
0055     createProperty("voltage", Variant::Type::Double);
0056     property("voltage")->setCaption(i18n("Voltage Range"));
0057     property("voltage")->setUnit("V");
0058     property("voltage")->setMinValue(-1e12);
0059     property("voltage")->setMaxValue(1e12);
0060     property("voltage")->setValue(5.0);
0061 
0062     addDisplayText("~", QRect(-8, -8, 16, 16), "~");
0063     addDisplayText("voltage", QRect(-16, -24, 32, 16), "");
0064 
0065     createProperty("peak-rms", Variant::Type::Select);
0066     property("peak-rms")->setCaption(i18n("Output"));
0067     QStringMap allowed;
0068     allowed["Peak"] = i18n("Peak");
0069     allowed["RMS"] = i18n("RMS");
0070     property("peak-rms")->setAllowed(allowed);
0071     property("peak-rms")->setValue("Peak");
0072 }
0073 
0074 ECVoltageSignal::~ECVoltageSignal()
0075 {
0076 }
0077 
0078 void ECVoltageSignal::dataChanged()
0079 {
0080     const double voltage = dataDouble("voltage");
0081     const double frequency = dataDouble("frequency");
0082     bool rms = dataString("peak-rms") == "RMS";
0083 
0084     m_voltageSignal->setStep(ElementSignal::st_sinusoidal, frequency);
0085     if (rms) {
0086         QString display = QString::number(voltage / getMultiplier(voltage), 'g', 3) + getNumberMag(voltage) + "V RMS";
0087         setDisplayText("voltage", display);
0088         m_voltageSignal->setVoltage(voltage * M_SQRT2);
0089     } else {
0090         QString display = QString::number(voltage / getMultiplier(voltage), 'g', 3) + getNumberMag(voltage) + "V Peak";
0091         setDisplayText("voltage", display);
0092         m_voltageSignal->setVoltage(voltage);
0093     }
0094 }
0095 
0096 void ECVoltageSignal::drawShape(QPainter &p)
0097 {
0098     initPainter(p);
0099     p.drawEllipse(int(x()) - 8, int(y()) - 8, width(), height());
0100     deinitPainter(p);
0101 }