resistordip.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "libraryitem.h"
00012 #include "node.h"
00013 #include "resistance.h"
00014 #include "resistordip.h"
00015 
00016 #include <kiconloader.h>
00017 #include <klocale.h>
00018 #include <qpainter.h>
00019 
00020 Item* ResistorDIP::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00021 {
00022         return new ResistorDIP( (ICNDocument*)itemDocument, newItem, id );
00023 }
00024 
00025 LibraryItem* ResistorDIP::libraryItem()
00026 {
00027         return new LibraryItem(
00028                 "ec/resistordip",
00029                 i18n("Resistor DIP"),
00030                 i18n("Discrete"),
00031                 "resistordip.png",
00032                 LibraryItem::lit_component,
00033                 ResistorDIP::construct
00034                         );
00035 }
00036 
00037 ResistorDIP::ResistorDIP( ICNDocument *icnDocument, bool newItem, const char *id )
00038         : Component( icnDocument, newItem, id ? id : "multiplexer" )
00039 {
00040         m_name = i18n("Resistor DIP");
00041         m_desc = i18n("Set of resistors with identical values in a Dual Inline Package.");
00042         
00043         m_resistorCount = 0;
00044         for ( int i=0; i<maxCount; ++i )
00045                 m_resistance[i] = 0;
00046         
00047         createProperty( "resistance", Variant::Type::Double );
00048         property("resistance")->setCaption( i18n("Resistance") );
00049         property("resistance")->setUnit( QChar(0x3a9) );
00050         property("resistance")->setValue(1e4);
00051         property("resistance")->setMinValue(1e-6);
00052         
00053         createProperty( "count", Variant::Type::Int );
00054         property("count")->setCaption( i18n("Count") );
00055         property("count")->setMinValue(2);
00056         property("count")->setMaxValue(maxCount);
00057         property("count")->setValue(8);
00058 }
00059 
00060 ResistorDIP::~ResistorDIP()
00061 {
00062 }
00063 
00064 
00065 void ResistorDIP::dataChanged()
00066 {
00067         initPins();
00068         const double resistance = dataDouble("resistance");
00069         for ( int i=0; i<m_resistorCount; ++i )
00070                 m_resistance[i]->setResistance(resistance);
00071         
00072         const QString display = QString::number( resistance / getMultiplier(resistance), 'g', 3 ) + getNumberMag(resistance) + QChar(0x3a9);
00073         addDisplayText( "res", QRect( offsetX(), offsetY()-16, 32, 12 ), display );
00074 }
00075 
00076 
00077 void ResistorDIP::initPins()
00078 {
00079         const int count = dataInt("count");
00080         const double resistance = dataDouble("resistance");
00081         
00082         if ( count == m_resistorCount )
00083                 return;
00084         
00085         if ( count < m_resistorCount )
00086         {
00087                 for ( int i=count; i<m_resistorCount; ++i )
00088                 {
00089                         removeElement( m_resistance[i], false );
00090                         m_resistance[i] = 0;
00091                         removeNode( "n"+QString::number(i) );
00092                         removeNode( "p"+QString::number(i) );
00093                 }
00094         }
00095         else
00096         {
00097                 for ( int i=m_resistorCount; i<count; ++i )
00098                 {
00099                         const QString nid = "n"+QString::number(i);
00100                         const QString pid = "p"+QString::number(i);
00101                         m_resistance[i] = createResistance( createPin( -24, 0, 0, nid ), createPin( 24, 0, 180, pid ), resistance );
00102                 }
00103         }
00104         m_resistorCount = count;
00105         
00106         setSize( -16, -count*8, 32, count*16, true );
00107         updateDIPNodePositions();
00108 }
00109 
00110 
00111 void ResistorDIP::updateDIPNodePositions()
00112 {
00113         for ( int i=0; i<m_resistorCount; ++i )
00114         {
00115                 m_nodeMap["n"+QString::number(i)].y = offsetY() + 8 + 16*i;
00116                 m_nodeMap["p"+QString::number(i)].y = offsetY() + 8 + 16*i;
00117         }
00118         updateAttachedPositioning();
00119 }
00120 
00121 
00122 void ResistorDIP::drawShape( QPainter &p )
00123 {
00124         int _x = int(x()+offsetX());
00125         int _y = int(y()+offsetY());
00126         
00127         initPainter(p);
00128         for ( int i=0; i<m_resistorCount; ++i )
00129                 p.drawRect( _x, _y+16*i+2, 32, 12 );
00130         deinitPainter(p);
00131 }
00132 

Generated on Tue May 8 17:05:32 2007 for KTechLab by  doxygen 1.5.1