#include "Widget.h" #include "BoxController.h" #include "Channel.h" #include "Config.h" #include "TcpServer.h" #include "ui_Widget.h" #include #include #include extern char* VideoPath; extern QList channelList; extern Widget::Widget(QWidget* parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); menu = new Menu(this); menu->hide(); // set window background transparent QPalette pal = palette(); pal.setBrush(QPalette::Base, Qt::transparent); setPalette(pal); setAttribute(Qt::WA_TranslucentBackground, true); // set list no scroll bar ui->listWidget->horizontalScrollBar()->hide(); ui->listWidget->verticalScrollBar()->hide(); ui->listWidget->hide(); // serial port initSerialPort(); // init command map initCommandMap(); } Widget::~Widget() { serialPort->close(); delete ui; } /** * @brief open serial port and link the signals and slots */ void Widget::initSerialPort() { // timer used to reopen serial port timer = new QTimer(); timer->setInterval(1000); timer->stop(); // serialport serialPort = new QSerialPort(); serialPort->setPortName("ttyAMA2"); serialPort->setBaudRate(QSerialPort::Baud115200); serialPort->setDataBits(QSerialPort::Data8); serialPort->setParity(QSerialPort::NoParity); serialPort->setStopBits(QSerialPort::OneStop); serialPort->setFlowControl(QSerialPort::NoFlowControl); if (serialPort->open(QIODevice::ReadWrite)) { qDebug() << "open serial port: /dev/ttyAMA2 success"; } else { qDebug() << "open serial port: /dev/ttyAMA2 failed, ready to retry..."; timer->start(); } connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(onSerialError(QSerialPort::SerialPortError))); connect(serialPort, SIGNAL(readyRead()), this, SLOT(onReadyRead())); } /** * @brief get the video list of current channel, and show */ void Widget::showPlayList() { // find the videos in dictionary "/root/usb/video" QString dirPath = QString("%1/%2").arg(VideoPath).arg(curChannelName); QDir dir(dirPath); if (!dir.exists()) { qDebug() << "dictionary /root/usb/videos dont exists"; return; } dir.setFilter(QDir::Files); dir.setSorting(QDir::Name); QStringList nameFilters; nameFilters << "*.mp4"; dir.setNameFilters(nameFilters); fileList = dir.entryList(); ui->listWidget->clear(); // append fileList to qlistwidget and show for (QString& file : fileList) { QListWidgetItem* item = new QListWidgetItem(file); ui->listWidget->addItem(item); } ui->listWidget->show(); ui->listWidget->setCurrentRow(0); } /** * @brief try to reopen serial port */ void Widget::onTimeout() { timer->stop(); bool ret = serialPort->open(QIODevice::ReadWrite); // if open failed, then start timer and reopen if (!ret) { qDebug() << "reopen serial port: /dev/ttyAMA2 success"; timer->start(); } else { qDebug() << "reopen serial port: /dev/ttyAMA2 failed, ready to retry..."; } } /** * @brief handling error * @param error */ void Widget::onSerialError(QSerialPort::SerialPortError error) { if (error == QSerialPort::ResourceError) { qDebug() << "serial port break, try to reopen"; serialPort->close(); timer->start(); } } /** * @brief map command from string to int */ void Widget::initCommandMap() { commandMap.insert("A1", Playback); commandMap.insert("A2", Next); commandMap.insert("A3", Previous); commandMap.insert("A4", Forward); commandMap.insert("A5", Back); commandMap.insert("A6", Pause); commandMap.insert("A7", Return); commandMap.insert("A8", Enter); } /** * @brief receive data from serial port, and handle it */ void Widget::onReadyRead() { serialPort->waitForReadyRead(100); QString data = serialPort->readLine(); if (!commandMap.contains(data)) { qDebug() << QString("receive unkown command %1 from serial port: /dev/ttyAMA2").arg(data); return; } Command cmd = commandMap.value(data); switch (cmd) { case Playback: { if (menu->isVisible()) return; menu->show(); } case Previous: { // if show menu, then switch menu select if (menu->isVisible()) { menu->setCurChannel("HDMI-A"); } // if menu hide, then select previous of listwidget else { int curRow = ui->listWidget->currentRow(); if (curRow == 0) curRow = ui->listWidget->count(); ui->listWidget->setCurrentRow(curRow - 1); } break; } case Next: { if (menu->isVisible()) { menu->setCurChannel("HDMI-B"); } else { int curRow = ui->listWidget->currentRow(); if (curRow == ui->listWidget->count() - 1) { curRow = -1; } ui->listWidget->setCurrentRow(curRow + 1); } break; } case Forward: if (isPlayback) { Channel* chn = Config::findChannelByName(curChannelName); chn->forward(); } break; case Back: if (isPlayback) { Channel* chn = Config::findChannelByName(curChannelName); chn->back(); } break; case Pause: if (isPlayback) { Channel* chn = Config::findChannelByName(curChannelName); chn->togglePause(); } break; case Enter: // if menu show, then hide menu and show play list if (menu->isVisible()) { curChannelName = menu->getCurChannel(); menu->hide(); showPlayList(); } // if menu hide, then current channel playback and other channel play live else { QString fileName = ui->listWidget->currentItem()->text(); Channel* channel = nullptr; for (int i = 0; i < channelList.count(); i++) { if (channelList.at(i)->channelName == curChannelName) { channel = channelList.at(i); } if (channelList.at(i)->isPlayback) { channelList.at(i)->startPlayLive(); } } if (channel) channel->startPlayback(fileName); ui->listWidget->hide(); } break; case Return: if (menu->isVisible()) { menu->hide(); } if (ui->listWidget->isVisible()) { ui->listWidget->hide(); } if (isPlayback) { Channel* chn = Config::findChannelByName(curChannelName); chn->startPlayLive(); } break; default: break; } }