File indexing completed on 2024-05-05 05:46:06
0001 /*************************************************************************** 0002 * Copyright (C) 2003 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 "led.h" 0012 #include "colorutils.h" 0013 #include "diode.h" 0014 #include "ecnode.h" 0015 #include "libraryitem.h" 0016 #include "simulator.h" 0017 0018 #include <KLocalizedString> 0019 #include <QPainter> 0020 0021 Item *LED::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0022 { 0023 return new LED(static_cast<ICNDocument *>(itemDocument), newItem, id); 0024 } 0025 0026 LibraryItem *LED::libraryItem() 0027 { 0028 return new LibraryItem(QStringList(QString("ec/led")), i18n("LED"), i18n("Outputs"), "led.png", LibraryItem::lit_component, LED::construct); 0029 } 0030 0031 LED::LED(ICNDocument *icnDocument, bool newItem, const char *id) 0032 : ECDiode(icnDocument, newItem, id ? id : "led") 0033 { 0034 m_bDynamicContent = true; 0035 m_name = i18n("LED"); 0036 setSize(-8, -16, 24, 24, true); 0037 avg_brightness = 255; 0038 lastUpdatePeriod = 1.; 0039 r = g = b = 0; 0040 last_brightness = 255; 0041 0042 createProperty("0-color", Variant::Type::Color); 0043 property("0-color")->setCaption(i18n("Color")); 0044 property("0-color")->setColorScheme(ColorUtils::LED); 0045 } 0046 0047 LED::~LED() 0048 { 0049 } 0050 0051 void LED::dataChanged() 0052 { 0053 QColor color = dataColor("0-color"); 0054 r = color.red() / double(0x100); 0055 g = color.green() / double(0x100); 0056 b = color.blue() / double(0x100); 0057 } 0058 0059 void LED::stepNonLogic() 0060 { 0061 avg_brightness += brightness(m_diode->current()) * LINEAR_UPDATE_PERIOD; 0062 lastUpdatePeriod += LINEAR_UPDATE_PERIOD; 0063 } 0064 0065 void LED::drawShape(QPainter &p) 0066 { 0067 int _x = int(x()); 0068 int _y = int(y()); 0069 0070 initPainter(p); 0071 0072 // BEGIN draw "Diode" part 0073 uint _b; 0074 0075 if (lastUpdatePeriod != 0.) { 0076 last_brightness = uint(avg_brightness / lastUpdatePeriod); 0077 } 0078 0079 _b = last_brightness; 0080 0081 avg_brightness = 0.; 0082 lastUpdatePeriod = 0.; 0083 0084 p.setBrush(QColor(uint(255 - (255 - _b) * (1 - r)), uint(255 - (255 - _b) * (1 - g)), uint(255 - (255 - _b) * (1 - b)))); 0085 0086 QPolygon pa(3); 0087 pa[0] = QPoint(8, 0); 0088 pa[1] = QPoint(-8, -8); 0089 pa[2] = QPoint(-8, 8); 0090 pa.translate(_x, _y); 0091 p.drawPolygon(pa); 0092 p.drawPolyline(pa); 0093 0094 p.drawLine(_x + 8, _y - 8, _x + 8, _y + 8); 0095 // END draw "Diode" part 0096 0097 // BEGIN draw "Arrows" part 0098 p.drawLine(_x + 7, _y - 10, _x + 10, _y - 13); // Tail of left arrow 0099 p.drawLine(_x + 10, _y - 13, _x + 8, _y - 13); // Left edge of left arrow tip 0100 p.drawLine(_x + 10, _y - 13, _x + 10, _y - 11); // Right edge of left arrow tip 0101 p.drawLine(_x + 10, _y - 7, _x + 13, _y - 10); // Tail of right arrow 0102 p.drawLine(_x + 13, _y - 10, _x + 11, _y - 10); // Left edge of right arrow tip 0103 p.drawLine(_x + 13, _y - 10, _x + 13, _y - 8); // Right edge of right arrow tip 0104 p.drawLine(_x + 8, _y - 13, _x + 13, _y - 8); // Diagonal line that forms base of both arrow tips 0105 // END draw "Arrows" part1 0106 0107 deinitPainter(p); 0108 } 0109 0110 uint LED::brightness(double i) 0111 { 0112 if (i > 0.018) 0113 return 0; 0114 if (i < 0.002) 0115 return 255; 0116 return uint(255 * (1 - ((i - 0.002) / 0.016))); 0117 }