1 /***************************************************************************
2 * Copyright (C) 2005 by David Saxton *
3 * david@bluehaze.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
10
11 #include "libraryitem.h"
12 #include "node.h"
13 #include "resistance.h"
14 #include "resistordip.h"
15
16 #include <KLocalizedString>
17 #include <QPainter>
18
construct(ItemDocument * itemDocument,bool newItem,const char * id)19 Item* ResistorDIP::construct( ItemDocument *itemDocument, bool newItem, const char *id )
20 {
21 return new ResistorDIP( (ICNDocument*)itemDocument, newItem, id );
22 }
23
libraryItem()24 LibraryItem* ResistorDIP::libraryItem()
25 {
26 return new LibraryItem(
27 QStringList(QString("ec/resistordip")),
28 i18n("Resistor DIP"),
29 i18n("Passive"),
30 "resistordip.png",
31 LibraryItem::lit_component,
32 ResistorDIP::construct
33 );
34 }
35
ResistorDIP(ICNDocument * icnDocument,bool newItem,const char * id)36 ResistorDIP::ResistorDIP( ICNDocument *icnDocument, bool newItem, const char *id )
37 : Component( icnDocument, newItem, id ? id : "multiplexer" )
38 {
39 m_name = i18n("Resistor DIP");
40
41 m_resistorCount = 0;
42 for ( int i=0; i<maxCount; ++i )
43 m_resistance[i] = nullptr;
44
45 createProperty( "resistance", Variant::Type::Double );
46 property("resistance")->setCaption( i18n("Resistance") );
47 property("resistance")->setUnit( QChar(0x3a9) );
48 property("resistance")->setValue(1e4);
49 property("resistance")->setMinValue(1e-6);
50
51 createProperty( "count", Variant::Type::Int );
52 property("count")->setCaption( i18n("Count") );
53 property("count")->setMinValue(2);
54 property("count")->setMaxValue(maxCount);
55 property("count")->setValue(8);
56 }
57
~ResistorDIP()58 ResistorDIP::~ResistorDIP()
59 {
60 }
61
62
dataChanged()63 void ResistorDIP::dataChanged()
64 {
65 initPins();
66 const double resistance = dataDouble("resistance");
67 for ( int i=0; i<m_resistorCount; ++i )
68 m_resistance[i]->setResistance(resistance);
69
70 const QString display = QString::number( resistance / getMultiplier(resistance), 'g', 3 ) + getNumberMag(resistance) + QChar(0x3a9);
71 addDisplayText( "res", QRect( offsetX(), offsetY()-16, 32, 12 ), display );
72 }
73
74
initPins()75 void ResistorDIP::initPins()
76 {
77 const int count = dataInt("count");
78 const double resistance = dataDouble("resistance");
79
80 if ( count == m_resistorCount )
81 return;
82
83 if ( count < m_resistorCount )
84 {
85 for ( int i=count; i<m_resistorCount; ++i )
86 {
87 removeElement( m_resistance[i], false );
88 m_resistance[i] = nullptr;
89 removeNode( "n"+QString::number(i) );
90 removeNode( "p"+QString::number(i) );
91 }
92 }
93 else
94 {
95 for ( int i=m_resistorCount; i<count; ++i )
96 {
97 const QString nid = "n"+QString::number(i);
98 const QString pid = "p"+QString::number(i);
99 m_resistance[i] = createResistance( createPin( -24, 0, 0, nid ), createPin( 24, 0, 180, pid ), resistance );
100 }
101 }
102 m_resistorCount = count;
103
104 setSize( -16, -count*8, 32, count*16, true );
105 updateDIPNodePositions();
106 }
107
108
updateDIPNodePositions()109 void ResistorDIP::updateDIPNodePositions()
110 {
111 for ( int i=0; i<m_resistorCount; ++i )
112 {
113 m_nodeMap["n"+QString::number(i)].y = offsetY() + 8 + 16*i;
114 m_nodeMap["p"+QString::number(i)].y = offsetY() + 8 + 16*i;
115 }
116 updateAttachedPositioning();
117 }
118
119
drawShape(QPainter & p)120 void ResistorDIP::drawShape( QPainter &p )
121 {
122 int _x = int(x()+offsetX());
123 int _y = int(y()+offsetY());
124
125 initPainter(p);
126 for ( int i=0; i<m_resistorCount; ++i )
127 p.drawRect( _x, _y+16*i+2, 32, 12 );
128 deinitPainter(p);
129 }
130
131