#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGridLayout>
#include <QDebug>
#include "ledwidget.h"
/**
* @brief The MarqueeLED class
* @ note 跑马灯类, 亮灯号一致则点亮,不一致则熄灭
*/
class MarqueeLED : public LEDWidget
{
public:
MarqueeLED(int num = -1, QWidget *parent=nullptr) :
LEDWidget(parent), m_num(num)
{
}
void setNum(int num) { m_num = num; }
public slots:
void onLightUpNumberChanged(int num)
{
controlLed(num == m_num);
}
private:
int m_num;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow), m_currIndex(0)
{
ui->setupUi(this);
uiInit();
//大“心型”的数据
int table1[64] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
};
//小“心型”的数据
int table2[64] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
ui->tableWidget->setRowCount(8);
ui->tableWidget->setColumnCount(8);
for (int r = 0; r < ui->tableWidget->rowCount(); ++r)
{
for (int l = 0; l < ui->tableWidget->columnCount(); ++l)
{
MarqueeLED *wgt = new MarqueeLED(ui->tableWidget->rowCount()*5 + l);
wgt->controlLed(table1[ui->tableWidget->rowCount()*r + l]);
ui->tableWidget->setColumnWidth(l, 100);
ui->tableWidget->setRowHeight(l, 100);
ui->tableWidget->setCellWidget(r, l, wgt);
}
}
}
MainWindow::~MainWindow()
{
widgetFree();
delete ui;
}
void MainWindow::uiInit()
{
ui->btnStop->setEnabled(false);
QGridLayout *ly = new QGridLayout(ui->wgtLeds);
ui->wgtLeds->setLayout(ly);
connect(ui->btnStart, &QPushButton::clicked, this, &MainWindow::onStart);
connect(ui->btnStop, &QPushButton::clicked, this, &MainWindow::onStop);
connect(&m_actTimer, &QTimer::timeout, this, &MainWindow::onActionTimerTimeout);
}
void MainWindow::widgetFree()
{
QGridLayout *ly = qobject_cast<QGridLayout*> (ui->wgtLeds->layout());
for (QWidget *wgt : m_ledWidgets)
{
ly->removeWidget(wgt);
delete wgt;
wgt = nullptr;
}
m_ledWidgets.clear();
}
void MainWindow::onActionTimerTimeout()
{
int lightUpNum = m_currIndex;
emit numberChanged(lightUpNum);
++m_currIndex;
if ( m_currIndex >= (ui->sbColumns->value() * ui->sbRows->value()) )
m_currIndex = 0;
}
void MainWindow::onStart()
{
ui->btnStop->setEnabled(false);
ui->btnStart->setEnabled(false);
QGridLayout *ly = qobject_cast<QGridLayout*> (ui->wgtLeds->layout());
for (int rowIndex = 0; rowIndex < ui->sbRows->value(); ++rowIndex)
{
for (int colIndex = 0; colIndex < ui->sbColumns->value(); ++colIndex)
{
MarqueeLED *wgt = new MarqueeLED(rowIndex * ui->sbColumns->value() + colIndex, this);
m_ledWidgets.append(wgt);
ly->addWidget(wgt, rowIndex, colIndex);
connect(this, &MainWindow::numberChanged, wgt, &MarqueeLED::onLightUpNumberChanged);
}
}
m_actTimer.setTimerType(Qt::PreciseTimer);
m_actTimer.setInterval(ui->sbIntervalTime->value());
m_actTimer.start();
ui->btnStop->setEnabled(true);
ui->btnStart->setEnabled(false);
}
void MainWindow::onStop()
{
m_actTimer.stop();
widgetFree();
m_currIndex = 0;
ui->btnStop->setEnabled(false);
ui->btnStart->setEnabled(true);
}