File indexing completed on 2024-05-05 05:46:05
0001 /*************************************************************************** 0002 * Copyright (C) 2003-2005 by David Saxton * 0003 * david@bluehaze.org * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 ***************************************************************************/ 0010 0011 #include "ecsignallamp.h" 0012 #include "ecnode.h" 0013 #include "element.h" 0014 #include "libraryitem.h" 0015 #include "pin.h" 0016 0017 #include <KLocalizedString> 0018 #include <QPainter> 0019 #include <cmath> 0020 0021 // TODO: resistance and power rating should be user definable properties. 0022 #define RESISTANCE 100 0023 #define WATTAGE 0.5 0024 // minimal power to create glow. (looks low...) 0025 #define LIGHTUP (WATTAGE / 20) 0026 0027 Item *ECSignalLamp::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0028 { 0029 return new ECSignalLamp(static_cast<ICNDocument *>(itemDocument), newItem, id); 0030 } 0031 0032 LibraryItem *ECSignalLamp::libraryItem() 0033 { 0034 return new LibraryItem(QStringList(QString("ec/signal_lamp")), i18n("Signal Lamp"), i18n("Outputs"), "signal_lamp.png", LibraryItem::lit_component, ECSignalLamp::construct); 0035 } 0036 0037 ECSignalLamp::ECSignalLamp(ICNDocument *icnDocument, bool newItem, const char *id) 0038 : Component(icnDocument, newItem, id ? id : "signal_lamp") 0039 { 0040 m_name = i18n("Signal Lamp"); 0041 setSize(-8, -8, 16, 16); 0042 0043 init1PinLeft(); 0044 init1PinRight(); 0045 0046 createResistance(m_pPNode[0], m_pNNode[0], RESISTANCE); 0047 0048 advanceSinceUpdate = 0; 0049 avgPower = 0.; 0050 m_bDynamicContent = true; 0051 } 0052 0053 ECSignalLamp::~ECSignalLamp() 0054 { 0055 } 0056 0057 void ECSignalLamp::stepNonLogic() 0058 { 0059 const double voltage = m_pPNode[0]->pin()->voltage() - m_pNNode[0]->pin()->voltage(); 0060 if (advanceSinceUpdate == 0) { 0061 advanceSinceUpdate = 1; // do not try to divide by 0 0062 } 0063 avgPower = fabs(avgPower * 0.99 + (voltage * voltage / RESISTANCE) * 0.01); 0064 ++advanceSinceUpdate; 0065 } 0066 0067 void ECSignalLamp::drawShape(QPainter &p) 0068 { 0069 initPainter(p); 0070 0071 int _x = int(x()); 0072 int _y = int(y()); 0073 0074 // Calculate the brightness as a linear function of power, bounded below by 0075 // 25 milliWatts and above by 500 milliWatts. 0076 int brightness = (avgPower < LIGHTUP) ? 255 : ((avgPower > WATTAGE) ? 0 : int(255 * (1 - ((avgPower - LIGHTUP) / (WATTAGE - LIGHTUP))))); 0077 advanceSinceUpdate = 0; 0078 0079 p.setBrush(QColor(255, 255, brightness)); 0080 p.drawEllipse(_x - 8, _y - 8, 16, 16); 0081 0082 int pos = 8 - int(8 * M_SQRT1_2); 0083 0084 p.drawLine(_x - 8 + pos, _y - 8 + pos, _x + 8 - pos, _y + 8 - pos); 0085 p.drawLine(_x + 8 - pos, _y - 8 + pos, _x - 8 + pos, _y + 8 - pos); 0086 0087 deinitPainter(p); 0088 }