00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "colorcombo.h"
00012 #include "diode.h"
00013 #include "ecled.h"
00014 #include "ecnode.h"
00015 #include "libraryitem.h"
00016 #include "simulator.h"
00017
00018 #include <klocale.h>
00019 #include <qpainter.h>
00020
00021 Item* ECLed::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00022 {
00023 return new ECLed( (ICNDocument*)itemDocument, newItem, id );
00024 }
00025
00026 LibraryItem* ECLed::libraryItem()
00027 {
00028 return new LibraryItem(
00029 QString::QString("ec/led"),
00030 i18n("LED"),
00031 i18n("Outputs"),
00032 "led.png",
00033 LibraryItem::lit_component,
00034 ECLed::construct
00035 );
00036 }
00037
00038 ECLed::ECLed( ICNDocument *icnDocument, bool newItem, const char *id )
00039 : ECDiode( icnDocument, newItem, (id) ? id : "led" )
00040 {
00041 m_bDynamicContent = true;
00042 m_name = i18n("LED");
00043 m_desc = i18n("Light Emitting Diode");
00044 setSize( -8, -16, 24, 24, true );
00045 avg_brightness = 255;
00046 lastUpdatePeriod = 0.;
00047 r=g=b=0;
00048 last_brightness = 255;
00049
00050 createProperty( "0-color", Variant::Type::Color );
00051 property("0-color")->setCaption( i18n("Color") );
00052 property("0-color")->setColorScheme( ColorCombo::LED );
00053 }
00054
00055 ECLed::~ECLed()
00056 {
00057 }
00058
00059 void ECLed::dataChanged()
00060 {
00061 QColor color = dataColor("0-color");
00062 r = color.red();
00063 g = color.green();
00064 b = color.blue();
00065 r /= 0x100;
00066 g /= 0x100;
00067 b /= 0x100;
00068 }
00069
00070 void ECLed::stepNonLogic()
00071 {
00072 double interval = 1./LINEAR_UPDATE_RATE;
00073 avg_brightness += brightness(m_diode->current())*interval;
00074 lastUpdatePeriod += interval;
00075 }
00076
00077 void ECLed::drawShape( QPainter &p )
00078 {
00079 int _x = int(x());
00080 int _y = int(y());
00081
00082 initPainter(p);
00083
00084
00085 uint _b;
00086 if ( lastUpdatePeriod == 0. )
00087 _b = last_brightness;
00088 else
00089 {
00090 _b = (uint)(avg_brightness/lastUpdatePeriod);
00091 last_brightness = _b;
00092 }
00093 avg_brightness = 0.;
00094 lastUpdatePeriod = 0.;
00095
00096 p.setBrush( QColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ) );
00097
00098 QPointArray pa(3);
00099 pa[0] = QPoint( 8, 0 );
00100 pa[1] = QPoint( -8, -8 );
00101 pa[2] = QPoint( -8, 8 );
00102 pa.translate( _x, _y );
00103 p.drawPolygon(pa);
00104 p.drawPolyline(pa);
00105
00106 p.drawLine( _x+8, _y-8, _x+8, _y+8 );
00107
00108
00109
00110
00111
00112 p.drawLine( _x+7, _y-10, _x+10, _y-13 );
00113 p.drawLine( _x+10, _y-13, _x+8, _y-13 );
00114 p.drawLine( _x+10, _y-13, _x+10, _y-11 );
00115
00116 p.drawLine( _x+10, _y-7, _x+13, _y-10 );
00117 p.drawLine( _x+13, _y-10, _x+11, _y-10 );
00118 p.drawLine( _x+13, _y-10, _x+13, _y-8 );
00119
00120 p.drawLine( _x+8, _y-13, _x+13, _y-8 );
00121
00122
00123
00124 deinitPainter(p);
00125 }
00126
00127
00128 uint ECLed::brightness( double i )
00129 {
00130 if ( i > 0.018 ) return 0;
00131 if ( i < 0.002 ) return 255;
00132 return (uint)(255*(1-((i-0.002)/0.016)));
00133 }
00134