ecsignallamp.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2003-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 
00012 #include "ecnode.h"
00013 #include "ecsignallamp.h"
00014 #include "element.h"
00015 #include "libraryitem.h"
00016 #include "pin.h"
00017 
00018 #include <klocale.h>
00019 #include <qpainter.h>
00020 
00021 Item* ECSignalLamp::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00022 {
00023         return new ECSignalLamp( (ICNDocument*)itemDocument, newItem, id );
00024 }
00025 
00026 LibraryItem* ECSignalLamp::libraryItem()
00027 {
00028         return new LibraryItem(
00029                 QString("ec/signal_lamp"),
00030                 i18n("Signal Lamp"),
00031                 i18n("Outputs"),
00032                 "signal_lamp.png",
00033                 LibraryItem::lit_component,
00034                 ECSignalLamp::construct );
00035 }
00036 
00037 ECSignalLamp::ECSignalLamp( ICNDocument *icnDocument, bool newItem, const char *id )
00038         : Component( icnDocument, newItem, (id) ? id : "signal_lamp" )
00039 {
00040         m_name = i18n("Signal Lamp");
00041         m_desc = i18n("A simple filament signal lamp, with a 100 ohms series resistance.");
00042         setSize( -8, -8, 16, 16 );
00043         
00044         init1PinLeft();
00045         init1PinRight();
00046         
00047         createResistance( m_pPNode[0], m_pNNode[0], 100. );
00048         
00049         advanceSinceUpdate = 0;
00050         avgPower = 0.;
00051         m_bDynamicContent = true;
00052 }
00053 
00054 ECSignalLamp::~ECSignalLamp()
00055 {
00056 }
00057 
00058 void ECSignalLamp::stepNonLogic()
00059 {
00060         const double voltage = m_pPNode[0]->pin()->voltage()-m_pNNode[0]->pin()->voltage();
00061         avgPower = QABS(avgPower*advanceSinceUpdate + (voltage*voltage/100))/++advanceSinceUpdate;
00062 }
00063 
00064 void ECSignalLamp::drawShape( QPainter &p )
00065 {
00066         initPainter(p);
00067         
00068         int _x = int(x());
00069         int _y = int(y());
00070         
00071         // Calculate the brightness as a linear function of power, bounded below by
00072         // 25 milliWatts and above by 500 milliWatts.
00073         int brightness = (avgPower<0.025) ? 255 : ((avgPower>0.5) ? 0 : (int)(255*(1-((avgPower-0.025)/0.475))));
00074         advanceSinceUpdate = 0;
00075         
00076         p.setBrush( QColor( 255, 255, brightness ) );
00077         p.drawEllipse( _x-8, _y-8, 16, 16 );
00078         
00079         // 2*sqrt(2) = 2.828427125...
00080         int pos = 8 - int(16/2.828);
00081         
00082         p.drawLine( _x-8+pos,   _y-8+pos, _x+8-pos,     _y+8-pos );
00083         p.drawLine( _x+8-pos,   _y-8+pos, _x-8+pos,     _y+8-pos );
00084         
00085         deinitPainter(p);
00086 }

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