1
/***************************************************************************
2
* Copyright (C) 2012 by santiago González *
3
* santigoro@gmail.com *
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 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "potentiometer.h"
21
#include "connector.h"
22
#include "simulator.h"
24
#include "itemlibrary.h"
26
static const char* Potentiometer_properties[] = {
27
QT_TRANSLATE_NOOP("App::Property","Value Ohm")
30
Component* Potentiometer::construct( QObject* parent, QString type, QString id )
32
return new Potentiometer( parent, type, id );
35
LibraryItem* Potentiometer::libraryItem()
37
return new LibraryItem(
38
tr( "Potentiometer" ),
42
Potentiometer::construct );
45
Potentiometer::Potentiometer( QObject* parent, QString type, QString id )
46
: Component( parent, type, id )
47
, eElement( (id+"-eElement") )
48
, m_pinA( 180, QPoint(-16,0 ), id+"-PinA", 0, this )
49
, m_pinM( 270, QPoint( 0,16), id+"-PinM", 0, this )
50
, m_pinB( 0, QPoint( 16,0 ), id+"-PinB", 0, this )
51
, m_ePinA( (id+"-ePinA"), 1 )
52
, m_ePinB( (id+"-ePinB"), 1 )
53
, m_resA( (id+"-resA") )
54
, m_resB( (id+"-resB") )
56
Q_UNUSED( Potentiometer_properties );
60
m_area = QRectF( -12, -4.5, 24, 12.5 );
62
setLabelPos(-16,-40, 0);
71
m_dialW.setupWidget();
72
m_dialW.setFixedSize( 24, 24 );
73
m_dialW.dial->setMinimum(0);
74
m_dialW.dial->setMaximum(1000);
75
m_dialW.dial->setValue(500);
76
m_dialW.dial->setSingleStep(25);
78
m_proxy = Circuit::self()->addWidget( &m_dialW );
79
m_proxy->setParentItem( this );
80
m_proxy->setPos( QPoint( -12, -24-5) );
81
//m_proxy->setFlag(QGraphicsItem::ItemNegativeZStacksBehindParent, true );
83
m_dial = m_dialW.dial;
85
m_resA.setEpin( 0, &m_pinA );
86
m_resA.setEpin( 1, &m_ePinA );
88
m_resB.setEpin( 1, &m_pinB );
89
m_resB.setEpin( 0, &m_ePinB );
93
setValLabelPos( 10,-20, 0);
97
Simulator::self()->addToUpdateList( this );
99
connect( m_dial, SIGNAL(valueChanged(int)),
100
this, SLOT (resChanged(int)), Qt::UniqueConnection );
103
Potentiometer::~Potentiometer() {}
105
void Potentiometer::stamp()
107
eNode* enod = m_pinM.getEnode(); // Get eNode from middle Pin
109
if( !enod ) // Not connected: Create mid eNode
111
m_midEnode = new eNode( m_id+"-mideNode" );
113
m_pinM.setEnode( enod );
115
else if( enod != m_midEnode ) // Connected to external eNode: Delete mid eNode
119
m_ePinA.setEnode( m_midEnode ); // Set eNode to internal eResistors ePins
120
m_ePinB.setEnode( m_midEnode );
125
void Potentiometer::updateStep()
127
if( !m_changed ) return;
129
double res1 = double( m_resist*m_dial->value()/1000 );
130
double res2 = m_resist-res1;
135
res2 = m_resist-res1;
140
res1 = m_resist-res2;
142
//qDebug()<<"Potentiometer::updateStep"<<res1<<res2;
143
m_resA.setRes( res1 );
144
m_resB.setRes( res2 );
147
Simulator::self()->addEvent( 0, 0l );
150
void Potentiometer::resChanged( int res ) // Called when dial is rotated
152
//qDebug() << res << m_resist;
156
void Potentiometer::setRes( double res ) // Called when property resistance is changed
158
if( res == 0 ) res = 0.001;
159
Component::setValue( res ); // Takes care about units multiplier
160
m_resist = m_value*m_unitMult;
165
void Potentiometer::setUnit( QString un )
167
Component::setUnit( un );
168
m_resist = m_value*m_unitMult;
173
void Potentiometer::setVal( int val )
175
m_dial->setValue( val*1000/m_resist );
179
int Potentiometer::val()
181
return m_resist*m_dial->value()/1000;
184
void Potentiometer::remove()
186
if( m_pinA.isConnected() ) m_pinA.connector()->remove();
187
if( m_pinB.isConnected() ) m_pinB.connector()->remove();
188
if( m_pinM.isConnected() )
190
Connector* con = m_pinM.connector();
191
if( con ) con->remove();
194
//if( m_midEnode ) Simulator::self()->remFromEnodeList( m_midEnode, true );
196
Simulator::self()->remFromUpdateList( this );
201
void Potentiometer::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget )
203
if( m_hidden ) return;
204
//p->setBrush(Qt::white);
205
//p->drawRoundedRect( QRect( 0, 0, 48, 48 ), 1, 1 );
206
//p->setBrush(Qt::darkGray);
207
//p->fillRect( QRect( 3, 3, 45, 45 ), Qt::darkGray );
210
//p->drawRoundedRect( QRect( 8, -56, 8, 40 ), 1, 1 );
212
Component::paint( p, option, widget );
213
p->drawRect( -10.5, -4, 21, 8 );
218
p->drawLine( 0, 6, -3, 9 );
219
p->drawLine( 0, 6, 3, 9 );
222
#include "moc_potentiometer.cpp"