qt/LedOK/devicepanel.cpp
2022-09-26 18:10:47 +08:00

390 lines
16 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "devicepanel.h"
#include "devicectrlpanel.h"
#include "tools.h"
#include "communication/network.h"
#include "wDevicesManager/specialdlg.h"
#include <QAction>
#include <QBoxLayout>
#include <QEvent>
#include <QHeaderView>
#include <QLineEdit>
#include <QNetworkDatagram>
DevicePanel *gDevicePanel;
QList<LedCard*> *gSelCards;
NetComm *gUdp;
//设备管理响应页面
DevicePanel::DevicePanel(QWidget *parent) : QWidget(parent) {
gDevicePanel = this;
gSelCards = &mSelCards;
setAutoFillBackground(true);
QPalette pal;
pal.setBrush(QPalette::Window, QColor(0xeeeeee));
setPalette(pal);
auto vBox = new QVBoxLayout(this);
vBox->setContentsMargins(0, 0, 0, 0);
vBox->setSpacing(0);
auto hBox = new QHBoxLayout();
hBox->setContentsMargins(6, 2, 6, 2);
hBox->setSpacing(2);
auto label = new QLabel();
label->setPixmap({":/res/DeviceManager/DeviceNum_All.png"});
label->setScaledContents(true);
label->setFixedSize(33, 24);
hBox->addWidget(label);
label_3 = new QLabel(tr("All"));
hBox->addWidget(label_3);
nDeviceNum = new QLabel();
nDeviceNum->setFixedSize(40, 20);
hBox->addWidget(nDeviceNum);
auto fdSearch = new QLineEdit();
fdSearch->setClearButtonEnabled(true);
fdSearch->setFixedSize(220, 30);
fdSearch->addAction(new QAction(QIcon(":/res/ProgramManager/bnSearch.png"), QString()), QLineEdit::LeadingPosition);
fdSearch->setStyleSheet("border: 1px solid #aaaaaa;");
connect(fdSearch, &QLineEdit::textChanged, this, &DevicePanel::FilterProgram);
hBox->addWidget(fdSearch);
hBox->addSpacing(10);
fdCardNumInfo = new QLabel(tr("Current Screen")+": "+tr("none"));
fdCardNumInfo->setStyleSheet("QLabel{color: #15f;}");
fdCardNumInfo->hide();
hBox->addWidget(fdCardNumInfo);
hBox->addStretch();
auto areaFlash = new QWidget();
areaFlash->setFixedSize(100, 34);
bnSpecifyIP = new QComboBox(areaFlash);
bnSpecifyIP->addItem(tr("Specify IP"));
bnSpecifyIP->setGeometry(0, 0, areaFlash->width(), areaFlash->height());
bnSpecifyIP->setItemDelegate(new QStyledItemDelegate);
connect(bnSpecifyIP, &QComboBox::textActivated, this, [this] {
pNetComm->mUdpTimer.stop();
SpecialDlg pDlg(this, pNetComm);
pDlg.exec();
pNetComm->mUdpTimer.start(30000);
});
bnSpecifyIP->setStyleSheet(R"rrr(
QComboBox {
border: none;
border-radius: 5px;
background-color: #19c;
}
QComboBox:hover {background-color: #08b;}
QComboBox::drop-down {width: 25px;}
QComboBox::down-arrow {image: url(:/res/DeviceManager/FlashArrow.png);}
QComboBox QAbstractItemView::item {
height: 28px;
color: #fff;
}
QComboBox QAbstractItemView::item:selected {background-color: #09c;}
)rrr");
btnRefrash = new QPushButton(tr("Refresh"), areaFlash);
btnRefrash->setGeometry(0, 0, 75, areaFlash->height());
connect(btnRefrash, &QPushButton::clicked, this, [this] {
pNetComm->LedCardList.clear();
mDeviceTable->onCheckAll(false);
mDeviceTable->clear();
nDeviceNum->setText("0");
sendGetInfo();
});
btnRefrash->setStyleSheet(R"rrr(
QPushButton {
border-radius: 5px;
background-color: #19c;
color: #fff;
}
QPushButton:hover {background-color: #08b;}
)rrr");
hBox->addWidget(areaFlash);
vBox->addLayout(hBox);
mHBox = new QHBoxLayout();
mDeviceTable = new LoQTreeWidget(this);
mDeviceTable->setIndentation(0);
mDeviceTable->setSortingEnabled(true);
mHBox->addWidget(mDeviceTable);
vBox->addLayout(mHBox);
pNetComm = new NetComm(this);
connect(&pNetComm->mUdpTimer, &QTimer::timeout, this, &DevicePanel::sendGetInfo);
connect(&pNetComm->mUdpSocket, &QUdpSocket::readyRead, this, [this] {
while(pNetComm->mUdpSocket.hasPendingDatagrams()) {
auto gram = pNetComm->mUdpSocket.receiveDatagram();
auto data = gram.data();
if(data.isEmpty()) continue;
qDebug() << "data" << data;
auto senderAddress = gram.senderAddress();
bool ok = true;
if(senderAddress.protocol()==QUdpSocket::IPv6Protocol) senderAddress.setAddress(senderAddress.toIPv4Address(&ok));
auto addr = ok ? senderAddress.toString() : "";
if(data.startsWith("{\"")) {
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(gram.data(), &error);
if(error.error != QJsonParseError::NoError) {
qDebug()<<"DeviceItem OnProHttpResponse 解析json文件错误";
continue;
}
pNetComm->JieXiQJsonObjectOfLedCardInfo(document.object(), addr);
} else {
STREAM_PACKET *pStreamPacket = (STREAM_PACKET *)gram.data().data();
pNetComm->JieXiQJsonObjectOfLedCardInfoOld(pStreamPacket->ucSerialCode, addr);
}
}
});
gUdp = pNetComm;
connect(pNetComm, &NetComm::addLedCard, this, [this](LedCard *p){
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) {
auto item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
if(item->m_pLedCard->m_strCardId != p->m_strCardId) continue;
item->setData(DeviceTable_ID, 0, p->m_strCardId);
item->setData(DeviceTable_IP, 0, p->m_strCardIp);
item->setData(DeviceTable_ScreenSize, 0, QString("%1 x %2").arg(p->m_iWidth).arg(p->m_iHeight));
return;
}
new DeviceItem(p, mDeviceTable, this);
nDeviceNum->setText(QString::number(mDeviceTable->topLevelItemCount()));
});
connect(pNetComm, &NetComm::ClearHeartbeatFlagByUdp, this, [this](LedCard *p) {
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) {
auto item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
if(item->m_pLedCard->m_strCardId != p->m_strCardId) continue;
item->m_HeartbeatCount = 0;
if(item->m_ImageOnline) {
item->m_pLedCard->m_bOnLine = true;
item->m_ImageOnline->setPixmap(QPixmap(":/res/DeviceManager/O_Online.png"));
}
return;
}
});
m_headerItem = new QTreeWidgetItem();
for(int i=1; i<DeviceTable_End; i++) m_headerItem->setTextAlignment(i, Qt::AlignCenter);
mDeviceTable->setHeaderItem(m_headerItem);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Check, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Check, 48);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Online, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Online, 48);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_ID, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_IP, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_ScreenSize, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_ScreenSize, 108);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Remark, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Brightness, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Brightness, 108);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Power, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Power, 84);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Password, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Password, 72);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Info, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Info, 72);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Screenshot, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Screenshot, 72);
connect(mDeviceTable, SIGNAL(sigCheckStateChanged(int)), this, SLOT(onCheckStateChanged(int)));
mDeviceTable->setSelectionMode(QAbstractItemView::SingleSelection);
//开始轮训查找局域网内的控制卡
sendGetInfo();
pNetComm->mUdpTimer.start(30000);
mDeviceTable->hideColumn(0);
mDeviceTable->fdIsSelAll->hide();
transUi();
}
DevicePanel::~DevicePanel() {
pNetComm->mUdpSocket.close();
pNetComm->mUdpTimer.stop();
}
void DevicePanel::sendGetInfo() {
QByteArray data = QJsonDocument({{"action", "getInfo"}}).toJson(QJsonDocument::Compact);
// if(pNetComm->mUdpSocket.writeDatagram(data, QHostAddress("255.255.255.255"), 22222) != data.length()) {
// qDebug() << "getInfo writeDatagram to 255.255.255.255 Failed";
// }
QList<QNetworkInterface> networkinterfaces = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface face, networkinterfaces) {
auto flags = face.flags();
bool can = (flags & QNetworkInterface::IsRunning) && (flags & QNetworkInterface::CanBroadcast) && ! (flags & QNetworkInterface::IsLoopBack);
if(! can) continue;
qDebug() << face;
auto addrEntries = face.addressEntries();
foreach(QNetworkAddressEntry addrEntry, addrEntries) {
auto ip = addrEntry.ip();
if(ip.protocol()!=QAbstractSocket::IPv4Protocol) continue;
auto broadcast = addrEntry.broadcast();
if(pNetComm->mUdpSocket.writeDatagram(data, broadcast, 22222) != data.length()) qDebug() << "getInfo writeDatagram Failed." << ip.toString() << "->" << broadcast.toString();
}
}
}
void DevicePanel::ShowDevicesInfoOrControlWidget(int index){
m_intIndexFlagOfInfoOrControl=index;
if(index==MainPage_DeviceManager) { //设备信息列表页面
mDeviceTable->hideColumn(0);
mDeviceTable->fdIsSelAll->hide();
if(mDeviceCtrlPanel!=nullptr) {
for(int j=DeviceTable_ScreenSize;j<DeviceTable_End;j++) mDeviceTable->showColumn(j);
mDeviceTable->setMaximumWidth(0xffffff);
mDeviceCtrlPanel->hide();
fdCardNumInfo->hide();
}
} else if(index==MainPage_Setting) { //终端控制页面
mDeviceTable->showColumn(0);
mDeviceTable->fdIsSelAll->show();
for(int j=DeviceTable_ScreenSize; j<DeviceTable_End; j++) mDeviceTable->hideColumn(j);
if(mDeviceCtrlPanel==nullptr) mHBox->addWidget(mDeviceCtrlPanel = new DeviceCtrlPanel(this));
else mDeviceCtrlPanel->show();
fdCardNumInfo->show();
mDeviceTable->setMaximumWidth(340);
GetSelectDeviceNum();
emit sigSelectedDeviceList(&mSelCards);
}
int cnt = mDeviceTable->topLevelItemCount();
if(cnt>0)//切换到终点管理页面是刷新列表中控制卡的状态参数
for(int i=0; i<cnt; i++) static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->SetMainTabWidgetIndex(m_intIndexFlagOfInfoOrControl);
}
void DevicePanel::changeEvent(QEvent *event) {
QWidget::changeEvent(event);
if(event->type() == QEvent::LanguageChange) transUi();
}
void DevicePanel::transUi() {
btnRefrash->setText(tr("Refresh"));
bnSpecifyIP->setItemText(0,tr("Specify IP"));
label_3->setText(tr("All"));
m_headerItem->setData(DeviceTable_Check, 0, "");
m_headerItem->setData(DeviceTable_Online, 0, tr("Online"));
m_headerItem->setData(DeviceTable_ID, 0, tr("Screen ID"));
m_headerItem->setData(DeviceTable_IP, 0, tr("Screen IP"));
m_headerItem->setData(DeviceTable_ScreenSize, 0, tr("Screen Size"));
m_headerItem->setData(DeviceTable_Remark, 0, tr("Remark Name"));
m_headerItem->setData(DeviceTable_Screenshot, 0, tr("readback pic"));
m_headerItem->setData(DeviceTable_Brightness, 0, tr("Cur Brightness"));
m_headerItem->setData(DeviceTable_Power, 0, tr("Power Status"));
m_headerItem->setData(DeviceTable_Password, 0, tr("Security"));
m_headerItem->setData(DeviceTable_Info, 0, tr("More Info"));
auto cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->refreshLable();
}
void DevicePanel::FilterProgram(const QString &strtemp){
if (strtemp.isEmpty()) //显示全部
{
for (int i = 0; i< mDeviceTable->topLevelItemCount(); ++i){
QTreeWidgetItem* topItem = mDeviceTable->topLevelItem(i);
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),false);
}
}
else
{
QList<QTreeWidgetItem*> resultList = mDeviceTable->findItems(strtemp, Qt::MatchContains,DeviceTable_ID); //搜索结果
if (resultList.size() > 0)
{
for (int i = 0; i< mDeviceTable->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = mDeviceTable->topLevelItem(i);
if (resultList.contains(topItem))
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),false); //显示匹配的结果
else
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
else {
QList<QTreeWidgetItem*> resultList1 = mDeviceTable->findItems(strtemp, Qt::MatchContains,DeviceTable_Remark); //搜索结果
if (resultList1.size() > 0)
{
for (int i = 0; i< mDeviceTable->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = mDeviceTable->topLevelItem(i);
if (resultList1.contains(topItem))
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),false); //显示匹配的结果
else
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
else {
QList<QTreeWidgetItem*> resultList2 = mDeviceTable->findItems(strtemp, Qt::MatchContains,DeviceTable_IP); //搜索结果
if (resultList2.size() > 0)
{
for (int i = 0; i< mDeviceTable->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = mDeviceTable->topLevelItem(i);
if (resultList2.contains(topItem))
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),false); //显示匹配的结果
else
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
else {
for (int i = 0; i< mDeviceTable->topLevelItemCount(); ++i)
{
QTreeWidgetItem* topItem = mDeviceTable->topLevelItem(i);
mDeviceTable->setRowHidden(i,mDeviceTable->indexFromItem(topItem->parent()),true); //隐藏不匹配的结果
}
}
}
}
}
}
void DevicePanel::OnControlTcpSend(int iProgramIndex){
int cnt = mDeviceTable->topLevelItemCount();
QList<DeviceItem*> list;
int iSelectCount=0;
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) {
iSelectCount++;
static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->OnControlTcpSend(iProgramIndex);
}
}
void DevicePanel::OnControlSendJson(QJsonObject &json,QString strFanYi){
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) {
DeviceItem *item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
item->HttpPostByTypeJsonObject(json);
QString strCardId=item->m_pLedCard->m_strCardId;
mDeviceCtrlPanel->OnOutputInfo(strCardId+":"+tr("send")+"->"+strFanYi);
}
}
int DevicePanel::GetSelectDeviceNum() {
int cnt = mDeviceTable->topLevelItemCount();
int iSelectCount=0;
mSelCards.clear();
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) {
iSelectCount++;
mSelCards.append(static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->m_pLedCard);
}
return iSelectCount;
}
void DevicePanel::OnOutputInfo(QString strInfo) {
if(m_intIndexFlagOfInfoOrControl==MainPage_Setting && mDeviceCtrlPanel!=nullptr) mDeviceCtrlPanel->OnOutputInfo(strInfo);
}
void DevicePanel::onCheckStateChanged(int f){
Q_UNUSED(f);
if(m_intIndexFlagOfInfoOrControl==MainPage_Setting){
GetSelectDeviceNum();
emit sigSelectedDeviceList(&mSelCards);
}
}