summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/ecpotentiometer.cpp
diff options
context:
space:
mode:
authortpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-02-24 01:49:02 +0000
committertpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2010-02-24 01:49:02 +0000
commit5de3dd4762ca33a0f92e79ffa4fe2ff67069d531 (patch)
treebad482b7afa4cdf47422d60a5dd2c61c7e333b09 /src/electronics/components/ecpotentiometer.cpp
downloadktechlab-5de3dd4762ca33a0f92e79ffa4fe2ff67069d531.tar.gz
ktechlab-5de3dd4762ca33a0f92e79ffa4fe2ff67069d531.zip
Added KDE3 version of ktechlab
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktechlab@1095338 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/electronics/components/ecpotentiometer.cpp')
-rw-r--r--src/electronics/components/ecpotentiometer.cpp119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/electronics/components/ecpotentiometer.cpp b/src/electronics/components/ecpotentiometer.cpp
new file mode 100644
index 0000000..db0fedd
--- /dev/null
+++ b/src/electronics/components/ecpotentiometer.cpp
@@ -0,0 +1,119 @@
+/***************************************************************************
+ * Copyright (C) 2003-2005 by David Saxton *
+ * david@bluehaze.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ ***************************************************************************/
+
+#include "canvasitemparts.h"
+#include "ecnode.h"
+#include "ecpotentiometer.h"
+#include "libraryitem.h"
+#include "resistance.h"
+
+#include <klocale.h>
+#include <qpainter.h>
+#include <qstyle.h>
+
+Item* ECPotentiometer::construct( ItemDocument *itemDocument, bool newItem, const char *id )
+{
+ return new ECPotentiometer( (ICNDocument*)itemDocument, newItem, id );
+}
+
+LibraryItem* ECPotentiometer::libraryItem()
+{
+ return new LibraryItem(
+ "ec/potentiometer",
+ i18n("Potentiometer"),
+ i18n("Discrete"),
+ "potentiometer.png",
+ LibraryItem::lit_component,
+ ECPotentiometer::construct );
+}
+
+ECPotentiometer::ECPotentiometer( ICNDocument *icnDocument, bool newItem, const char *id )
+ : Component( icnDocument, newItem, id ? id : "potentiometer" )
+{
+ m_name = i18n("Potentiometer");
+ m_desc =i18n("Consists of a resistor connected to the end pins, with a central pin connected at an adjustable point along the resistor");
+ setSize( -16, -16, 40, 32 );
+
+ m_p1 = createPin( 32, 0, 180, "p1" );
+
+ m_sliderProp = 0.0;
+ m_resistance = 5000.;
+ m_r1 = createResistance( createPin( -8, -24, 90, "n1" ), m_p1, 1. );
+ m_r2 = createResistance( createPin( -8, 24, 270, "n2" ), m_p1, 1. );
+
+ Slider * s = addSlider( "slider", 0, 100, 5, 50, Qt::Vertical, QRect( 0, -16, 16, 32 ) );
+ m_pSlider = static_cast<QSlider*>(s->widget());
+
+ createProperty( "resistance", Variant::Type::Double );
+ property("resistance")->setCaption( i18n("Resistance") );
+ property("resistance")->setUnit( QChar(0x3a9) );
+ property("resistance")->setMinValue(1e-6);
+ property("resistance")->setValue(1e5);
+
+ addDisplayText( "res", QRect( -56, -8, 40, 16 ), "" );
+}
+
+ECPotentiometer::~ECPotentiometer()
+{
+}
+
+void ECPotentiometer::dataChanged()
+{
+ m_resistance = dataDouble("resistance");
+
+ QString display = QString::number( m_resistance / getMultiplier(m_resistance), 'g', 3 ) + getNumberMag(m_resistance) + QChar(0x3a9);
+ setDisplayText( "res", display );
+
+ sliderValueChanged( "slider", slider("slider")->value() );
+}
+
+void ECPotentiometer::sliderValueChanged( const QString &id, int newValue )
+{
+ if ( id != "slider" )
+ return;
+
+ m_sliderProp = (newValue-50.0)/100.0;
+
+ m_r1->setResistance( m_resistance*(double)newValue/100. );
+ m_r2->setResistance( m_resistance*(double)(100.-newValue)/100. );
+}
+
+void ECPotentiometer::drawShape( QPainter &p )
+{
+ initPainter(p);
+ int _x = int(x());
+ int _y = int(y());
+
+ p.drawRect( _x-14, _y-16, 12, 32 );
+
+ QPointArray pa(3);
+ pa[0] = QPoint( 0, 0 );
+ pa[1] = QPoint( 4, -3 );
+ pa[2] = QPoint( 4, 3 );
+
+ int space = m_pSlider->style().pixelMetric( QStyle::PM_SliderSpaceAvailable, m_pSlider );
+ int base_y = _y + (( angleDegrees() == 0 || angleDegrees() == 270 ) ? 1 : -1) * int( space * m_sliderProp );
+
+ pa.translate( _x+16, base_y );
+
+ QColor c = m_p1->isSelected() ? m_selectedCol : black;
+
+ p.setPen(c);
+ p.setBrush(c);
+ p.drawPolygon(pa);
+
+ p.drawLine( _x+20, base_y, _x+24, base_y );
+ p.drawLine( _x+24, base_y, _x+24, _y );
+
+ deinitPainter(p);
+}
+
+
+