RecordControlApplication/ChannelSetting.cpp

140 lines
4.0 KiB
C++
Raw Normal View History

#include "ChannelSetting.h"
#include "Constant.h"
#include "SerialPortTool.h"
#include "ui_ChannelSetting.h"
#include <Json.h>
#include <QApplication>
#include <QDebug>
#include <QKeyEvent>
#include <QThread>
extern SerialPortTool* serialPortTool;
ChannelSetting::ChannelSetting(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ChannelSetting)
{
ui->setupUi(this);
QPoint globalPos = parent->mapToGlobal(QPoint(0, 0));
int x = globalPos.x() + (parent->width() - this->width()) / 2;
int y = globalPos.y() + (parent->height() - this->height()) / 2;
this->move(x, y);
}
ChannelSetting::~ChannelSetting()
{
delete ui;
}
/**
* @brief show方法
*/
void ChannelSetting::show()
{
// 默认聚焦确定按钮
ui->btnDone->setFocus();
// 获取配置文件中保存的输入源类型(HDMI or VGA)
QVariantMap config = Json::loadFile(Constant::ConfigurationPath).toMap();
QVariantList list = config["interface"].toList();
for (int i = 0; i < list.count(); i++) {
QVariantMap cfg = list.at(i).toMap();
QString channelName = cfg["name"].toString();
QString protocol = cfg["protocol"].toString();
if (channelName == Constant::MainChannel) {
protocol1 = protocol;
} else {
protocol2 = protocol;
}
}
if (protocol1 == "HDMI") {
ui->btn_hdmi_1->setChecked(true);
} else {
ui->btn_vga_1->setChecked(true);
}
if (protocol2 == "HDMI") {
ui->btn_hdmi_2->setChecked(true);
} else {
ui->btn_vga_2->setChecked(true);
}
QWidget::show();
}
void ChannelSetting::onReceiveNext()
{
QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
QApplication::sendEvent(focusWidget(), &keyEvent);
QKeyEvent keyEvent1(QEvent::KeyRelease, Qt::Key_Down, Qt::NoModifier);
QApplication::sendEvent(focusWidget(), &keyEvent1);
}
void ChannelSetting::onReceivePre()
{
QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
QApplication::sendEvent(focusWidget(), &keyEvent);
QKeyEvent keyEvent1(QEvent::KeyRelease, Qt::Key_Up, Qt::NoModifier);
QApplication::sendEvent(focusWidget(), &keyEvent1);
}
void ChannelSetting::onReceiveEnter()
{
// 聚焦的控件
QString objName = focusWidget()->objectName();
// 通道一
if (objName.endsWith("1")) {
if (objName == "btn_hdmi_1") {
ui->btn_hdmi_1->setChecked(true);
ui->btn_vga_1->setChecked(false);
protocol1 = "HDMI";
} else {
ui->btn_hdmi_1->setChecked(false);
ui->btn_vga_1->setChecked(true);
protocol1 = "VGA";
}
}
// 通道二
else if (objName.endsWith("2")) {
if (objName == "btn_hdmi_2") {
ui->btn_hdmi_2->setChecked(true);
ui->btn_vga_2->setChecked(false);
protocol2 = "HDMI";
} else {
ui->btn_hdmi_2->setChecked(false);
ui->btn_vga_2->setChecked(true);
protocol2 = "VGA";
}
}
// 确认按钮
else {
// 保存配置
QVariantMap config = Json::loadFile(Constant::ConfigurationPath).toMap();
QVariantList list = config["interface"].toList();
for (int i = 0; i < list.count(); i++) {
QVariantMap cfg = list.at(i).toMap();
QString channelName = cfg["name"].toString();
if (channelName == Constant::MainChannel) {
cfg["protocol"] = protocol1;
} else {
cfg["protocol"] = protocol2;
}
list[i] = cfg;
}
config["interface"] = list;
Json::saveFile(config, Constant::ConfigurationPath);
// 发送指令
if (protocol1 == "HDMI") {
serialPortTool->onMainChannelHDMI();
} else {
serialPortTool->onMainChannelVGA();
}
QThread::msleep(100);
if (protocol2 == "HDMI") {
serialPortTool->onSecondaryChannelHDMI();
} else {
serialPortTool->onSecondaryChannelVGA();
}
hide();
}
}