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

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 "resistordip.h"
0012 #include "libraryitem.h"
0013 #include "node.h"
0014 #include "resistance.h"
0015 
0016 #include <KLocalizedString>
0017 #include <QPainter>
0018 
0019 Item *ResistorDIP::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0020 {
0021     return new ResistorDIP(static_cast<ICNDocument *>(itemDocument), newItem, id);
0022 }
0023 
0024 LibraryItem *ResistorDIP::libraryItem()
0025 {
0026     return new LibraryItem(QStringList(QString("ec/resistordip")), i18n("Resistor DIP"), i18n("Passive"), "resistordip.png", LibraryItem::lit_component, ResistorDIP::construct);
0027 }
0028 
0029 ResistorDIP::ResistorDIP(ICNDocument *icnDocument, bool newItem, const char *id)
0030     : Component(icnDocument, newItem, id ? id : "multiplexer")
0031 {
0032     m_name = i18n("Resistor DIP");
0033 
0034     m_resistorCount = 0;
0035     for (int i = 0; i < maxCount; ++i)
0036         m_resistance[i] = nullptr;
0037 
0038     createProperty("resistance", Variant::Type::Double);
0039     property("resistance")->setCaption(i18n("Resistance"));
0040     property("resistance")->setUnit(QChar(0x3a9));
0041     property("resistance")->setValue(1e4);
0042     property("resistance")->setMinValue(1e-6);
0043 
0044     createProperty("count", Variant::Type::Int);
0045     property("count")->setCaption(i18n("Count"));
0046     property("count")->setMinValue(2);
0047     property("count")->setMaxValue(maxCount);
0048     property("count")->setValue(8);
0049 }
0050 
0051 ResistorDIP::~ResistorDIP()
0052 {
0053 }
0054 
0055 void ResistorDIP::dataChanged()
0056 {
0057     initPins();
0058     const double resistance = dataDouble("resistance");
0059     for (int i = 0; i < m_resistorCount; ++i)
0060         m_resistance[i]->setResistance(resistance);
0061 
0062     const QString display = QString::number(resistance / getMultiplier(resistance), 'g', 3) + getNumberMag(resistance) + QChar(0x3a9);
0063     addDisplayText("res", QRect(offsetX(), offsetY() - 16, 32, 12), display);
0064 }
0065 
0066 void ResistorDIP::initPins()
0067 {
0068     const int count = dataInt("count");
0069     const double resistance = dataDouble("resistance");
0070 
0071     if (count == m_resistorCount)
0072         return;
0073 
0074     if (count < m_resistorCount) {
0075         for (int i = count; i < m_resistorCount; ++i) {
0076             removeElement(m_resistance[i], false);
0077             m_resistance[i] = nullptr;
0078             removeNode("n" + QString::number(i));
0079             removeNode("p" + QString::number(i));
0080         }
0081     } else {
0082         for (int i = m_resistorCount; i < count; ++i) {
0083             const QString nid = "n" + QString::number(i);
0084             const QString pid = "p" + QString::number(i);
0085             m_resistance[i] = createResistance(createPin(-24, 0, 0, nid), createPin(24, 0, 180, pid), resistance);
0086         }
0087     }
0088     m_resistorCount = count;
0089 
0090     setSize(-16, -count * 8, 32, count * 16, true);
0091     updateDIPNodePositions();
0092 }
0093 
0094 void ResistorDIP::updateDIPNodePositions()
0095 {
0096     for (int i = 0; i < m_resistorCount; ++i) {
0097         m_nodeMap["n" + QString::number(i)].y = offsetY() + 8 + 16 * i;
0098         m_nodeMap["p" + QString::number(i)].y = offsetY() + 8 + 16 * i;
0099     }
0100     updateAttachedPositioning();
0101 }
0102 
0103 void ResistorDIP::drawShape(QPainter &p)
0104 {
0105     int _x = int(x() + offsetX());
0106     int _y = int(y() + offsetY());
0107 
0108     initPainter(p);
0109     for (int i = 0; i < m_resistorCount; ++i)
0110         p.drawRect(_x, _y + 16 * i + 2, 32, 12);
0111     deinitPainter(p);
0112 }