File indexing completed on 2024-04-21 15:05:25

0001 
0002 #include "kledtest.h"
0003 
0004 #include "kled.h"
0005 #include <qapplication.h>
0006 
0007 KLedTest::KLedTest(QWidget *parent)
0008     : QWidget(parent)
0009     , LedWidth(16)
0010     , LedHeight(10)
0011     , Grid(3)
0012     , ledcolor(0)
0013     , ledlook(KLed::Flat)
0014     , kled_round(true) // Switch HERE between rectangle and circular leds
0015 {
0016     if (kled_round) {
0017         // KLed l(KLed::red, &qw);               // create lamp
0018         // KLed l(KLed::blue, &qw);              // create lamp
0019         l = new KLed(Qt::green, this); // create lamp
0020         // KLed l(KLed::yellow, &qw);                // create lamp
0021         // KLed l(KLed::orange, &qw);                // create lamp
0022 
0023         l->resize(16, 30);
0024         // l.setLook(KLed::flat);
0025         l->setShape(KLed::Circular);
0026         // l->setShape(KLed::Rectangular);
0027 
0028         // l->setLook(KLed::Flat);
0029         // l->setLook(KLed::Flat);
0030         // l->setLook(KLed::Flat);
0031 
0032         l->move(5, 5);
0033         //    ktmp tmpobj(l);
0034 
0035         t_toggle.setSingleShot(false);
0036         t_toggle.start(1000);
0037         t_color.setSingleShot(false);
0038         t_color.start(3500);
0039         t_look.setSingleShot(false);
0040         t_look.start(3500);
0041         QObject::connect(&t_toggle, &QTimer::timeout, l, &KLed::toggle);
0042         QObject::connect(&t_color, &QTimer::timeout, this, &KLedTest::nextColor);
0043         QObject::connect(&t_look, &QTimer::timeout, this, &KLedTest::nextLook);
0044         l->show();
0045         resize(240, 140);
0046     } else {
0047         y = Grid;
0048         index = 0;
0049         for (int shape = 0; (int)shape < 2; shape = (KLed::Shape)(shape + 1)) {
0050             x = Grid;
0051             for (int look = 0; (int)look < 3; look = (KLed::Look)(look + 1)) {
0052                 for (state = KLed::Off; (int)state < 2; state = (KLed::State)(state + 1)) {
0053                     leds[index] = new KLed(Qt::yellow, state, (KLed::Look)(look + 1), (KLed::Shape)(shape + 1), this);
0054                     leds[index]->setGeometry(x, y, LedWidth, LedHeight);
0055                     ++index;
0056                     x += Grid + LedWidth;
0057                 }
0058             }
0059             y += Grid + LedHeight;
0060         }
0061         setFixedSize(x + Grid, y + Grid);
0062         connect(&timer, &QTimer::timeout, this, &KLedTest::timeout);
0063         timer.start(500);
0064     }
0065 }
0066 
0067 KLedTest::~KLedTest()
0068 {
0069     if (kled_round) {
0070         delete l;
0071     }
0072 }
0073 
0074 void KLedTest::nextColor()
0075 {
0076     ledcolor++;
0077     ledcolor %= 4;
0078 
0079     switch (ledcolor) {
0080     default:
0081     case 0:
0082         l->setColor(Qt::green);
0083         break;
0084     case 1:
0085         l->setColor(Qt::blue);
0086         break;
0087     case 2:
0088         l->setColor(Qt::red);
0089         break;
0090     case 3:
0091         l->setColor(Qt::yellow);
0092         break;
0093     }
0094 }
0095 
0096 void KLedTest::nextLook()
0097 {
0098     int tmp;
0099     if (kled_round) {
0100         tmp = (static_cast<int>(ledlook) + 1) % 3;
0101     } else {
0102         tmp = (static_cast<int>(ledlook) + 1) % 3;
0103     }
0104     ledlook = static_cast<KLed::Look>(tmp);
0105     l->setLook(ledlook);
0106     // qDebug("painting look %i", ledlook);
0107     // l->repaint();
0108 }
0109 
0110 void KLedTest::timeout()
0111 {
0112     const int NoOfLeds = sizeof(leds) / sizeof(leds[0]);
0113     int count;
0114     // -----
0115     for (count = 0; count < NoOfLeds; ++count) {
0116         if (leds[count]->state() == KLed::Off) {
0117             leds[count]->setState(KLed::On);
0118         } else {
0119             leds[count]->setState(KLed::Off);
0120         }
0121     }
0122 }
0123 
0124 /*#include <stdio.h>*/
0125 
0126 int main(int argc, char **argv)
0127 {
0128     QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0129     QApplication a(argc, argv);
0130     KLedTest widget;
0131     // -----
0132     /*
0133     if (argc>1) { // look out for round or circular led command
0134       if (strncmp(argv[1],"-c",2)) {
0135     // paint circular
0136     printf("painting circular led\n");
0137     widget.kled_round = true;
0138       }
0139       else if (strncmp(argv[1],"-r",2)) {
0140     // paint rectangle
0141     printf("painting rectangular led\n");
0142     widget.kled_round = false;
0143       }
0144     }
0145     */
0146     widget.show();
0147     return a.exec(); // go
0148 }
0149 
0150 #include "moc_kledtest.cpp"