File indexing completed on 2024-05-05 05:46:03

0001 /***************************************************************************
0002  *   Copyright (C) 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 "bidirled.h"
0012 #include "colorutils.h"
0013 #include "diode.h"
0014 #include "ecnode.h"
0015 #include "led.h"
0016 #include "libraryitem.h"
0017 #include "simulator.h"
0018 
0019 #include <KLocalizedString>
0020 #include <QPainter>
0021 
0022 Item *BiDirLED::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0023 {
0024     return new BiDirLED(static_cast<ICNDocument *>(itemDocument), newItem, id);
0025 }
0026 
0027 LibraryItem *BiDirLED::libraryItem()
0028 {
0029     return new LibraryItem(QStringList(QString("ec/bidir_led")), i18n("Bidirectional LED"), i18n("Outputs"), "bidirled.png", LibraryItem::lit_component, BiDirLED::construct);
0030 }
0031 
0032 BiDirLED::BiDirLED(ICNDocument *icnDocument, bool newItem, const char *id)
0033     : Component(icnDocument, newItem, id ? id : "bidir_led")
0034 {
0035     m_name = i18n("Bidirectional LED");
0036     m_bDynamicContent = true;
0037 
0038     setSize(-8, -16, 16, 32);
0039     init1PinLeft();
0040     init1PinRight();
0041     setSize(-8, -24, 24, 40);
0042 
0043     m_pDiode[0] = createDiode(m_pNNode[0], m_pPNode[0]);
0044     m_pDiode[1] = createDiode(m_pPNode[0], m_pNNode[0]);
0045 
0046     avg_brightness[0] = avg_brightness[1] = 255;
0047     lastUpdatePeriod = 1.;
0048     r[0] = r[1] = g[0] = g[1] = b[0] = b[1] = 0;
0049     last_brightness[0] = last_brightness[1] = 255;
0050 
0051     createProperty("0-color1", Variant::Type::Color);
0052     property("0-color1")->setCaption(i18n("Color 1"));
0053     property("0-color1")->setColorScheme(ColorUtils::LED);
0054 
0055     createProperty("0-color2", Variant::Type::Color);
0056     property("0-color2")->setCaption(i18n("Colour 2"));
0057     property("0-color2")->setColorScheme(ColorUtils::LED);
0058 }
0059 
0060 BiDirLED::~BiDirLED()
0061 {
0062 }
0063 
0064 void BiDirLED::dataChanged()
0065 {
0066     QString colors[] = {"0-color1", "0-color2"};
0067     for (unsigned i = 0; i < 2; i++) {
0068         QColor color = dataColor(colors[i]);
0069         r[i] = color.red() / 0x100;
0070         g[i] = color.green() / 0x100;
0071         b[i] = color.blue() / 0x100;
0072     }
0073 }
0074 
0075 void BiDirLED::stepNonLogic()
0076 {
0077     lastUpdatePeriod += LINEAR_UPDATE_PERIOD;
0078 
0079     for (unsigned i = 0; i < 2; i++)
0080         avg_brightness[i] += LED::brightness(m_pDiode[i]->current()) * LINEAR_UPDATE_PERIOD;
0081 }
0082 
0083 void BiDirLED::drawShape(QPainter &p)
0084 {
0085     initPainter(p);
0086 
0087     for (unsigned i = 0; i < 2; i++) {
0088         uint _b;
0089         if (lastUpdatePeriod == 0.)
0090             _b = last_brightness[i];
0091 
0092         else {
0093             _b = uint(avg_brightness[i] / lastUpdatePeriod);
0094             last_brightness[i] = _b;
0095         }
0096         avg_brightness[i] = 0.;
0097 
0098         p.setBrush(QColor(uint(255 - (255 - _b) * (1 - r[i])), uint(255 - (255 - _b) * (1 - g[i])), uint(255 - (255 - _b) * (1 - b[i]))));
0099 
0100         QPolygon pa(3);
0101         if (i == 0) {
0102             pa[0] = QPoint(8, -8);
0103             pa[1] = QPoint(-8, -16);
0104             pa[2] = QPoint(-8, 0);
0105         } else {
0106             pa[0] = QPoint(-8, 8);
0107             pa[1] = QPoint(8, 0);
0108             pa[2] = QPoint(8, 16);
0109         }
0110 
0111         pa.translate(int(x()), int(y()));
0112         p.drawPolygon(pa);
0113         p.drawPolyline(pa);
0114     }
0115     lastUpdatePeriod = 0.;
0116 
0117     // Draw the arrows indicating it's a LED
0118     int _x = int(x()) - 2;
0119     int _y = int(y()) - 21;
0120 
0121     p.drawLine(_x + 9, _y + 3, _x + 12, _y);  // Tail of left arrow
0122     p.drawLine(_x + 12, _y, _x + 10, _y);     // Left edge of left arrow tip
0123     p.drawLine(_x + 12, _y, _x + 12, _y + 2); // Right edge of left arrow tip
0124 
0125     p.drawLine(_x + 12, _y + 6, _x + 15, _y + 3); // Tail of right arrow
0126     p.drawLine(_x + 15, _y + 3, _x + 13, _y + 3); // Left edge of right arrow tip
0127     p.drawLine(_x + 15, _y + 3, _x + 15, _y + 5); // Right edge of right arrow tip
0128 
0129     p.drawLine(_x + 10, _y, _x + 15, _y + 5); // Diagonal line that forms base of both arrow tips
0130 
0131     _x = int(x());
0132     _y = int(y());
0133     p.drawLine(_x + 8, _y - 16, _x + 8, _y);
0134     p.drawLine(_x - 8, _y, _x - 8, _y + 16);
0135 
0136     deinitPainter(p);
0137 }