#include "realtimeform.h" #include "ui_realtimeform.h" #include #include #include #include #include #include #include #include "MyCustomGraphicsItem.h" #include "NetMgr.h" #include #include "QGraphicsMovieItem.h" #include #include "log.h" #include #include "customrectitem.h" #include "mygraphicrectitem.h" CRealTimeForm::CRealTimeForm(QWidget *parent) : QWidget(parent), ui(new Ui::CRealTimeForm) { ui->setupUi(this); ui->graphicsView->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(ui->graphicsView ,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(on_graphView_customContextMenuRequested(const QPoint &))); ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate); m_pGraphicsScene = new QGraphicsScene(); m_pGraphicsScene->setSceneRect(0,0,600,550); ui->graphicsView->setScene(m_pGraphicsScene); ui->graphicsView->setDragMode(QGraphicsView::RubberBandDrag); ui->graphicsView_2->setContextMenuPolicy(Qt::CustomContextMenu); QObject::connect(ui->graphicsView_2 ,SIGNAL(customContextMenuRequested(const QPoint &)),this,SLOT(on_graphView_customContextMenuRequested(const QPoint &))); ui->graphicsView_2->setViewportUpdateMode(QGraphicsView::FullViewportUpdate); m_pGraphicsScene_2 = new QGraphicsScene(); m_pGraphicsScene_2->setSceneRect(0,0,600,550); ui->graphicsView_2->setScene(m_pGraphicsScene_2); ui->graphicsView_2->setDragMode(QGraphicsView::RubberBandDrag); ui->tabWidget->setTabPosition(QTabWidget::South); ui->tabWidget->setTabShape(QTabWidget::Triangular); //绑定HTTP消息响应 connect(g_NetMgr,SIGNAL(sigNetMgr(QString, const QVariant&)), this, SLOT(slotNetMgr(QString,const QVariant&))); m_pSocket = new QTcpSocket(this); //QObject::connect(m_pSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(slotStateChanged(QAbstractSocket::SocketState))); m_EditMode = 0; m_MachineStatus = 1; //读取ini QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini",QSettings::IniFormat); m_RealtimeLog = settingsread.value("Log/realtime").toInt(); LoadGraphicsConfig(0); InitChannelInfo(); //m_strServerIp = IP; m_nServerPort = 7305; m_pSocket = new QTcpSocket(this); // /*下面是几种常用的信号,更多的信号可以查看QAbstractSocket和QIODevice文档*/ // connect(m_pSocket, &QTcpSocket::connected, this, [] () { // qDebug() << "socket已连接"; // }); // connect(m_pSocket, &QTcpSocket::disconnected, this, [] () { // qDebug() << "socket已断开连接"; // }); // connect(m_pSocket, &QTcpSocket::stateChanged, this, [] (QAbstractSocket::SocketState socketState) { // qDebug() << "socket状态改变" << socketState; // }); // connect(m_pSocket, &QTcpSocket::readyRead, this, [] () { // qDebug() << "有数据可读"; // }); //connect(m_pSocket, SIGNAL(readyRead()), this, SLOT(slotRecieve())); connect(m_pSocket, &QTcpSocket::readyRead, this, &CRealTimeForm::slotRecieve); connect(m_pSocket, &QTcpSocket::disconnected, this, &CRealTimeForm::disConnect); #ifdef QT_NO_DEBUG id1 = startTimer(2000); //参数1 间隔 单位 毫秒 //定时器第二种方式 QTimer * timer = new QTimer(this); //启动定时器 timer->start(500); #endif } CRealTimeForm::~CRealTimeForm() { delete ui; } void CRealTimeForm::timerEvent(QTimerEvent *ev) { if(ev->timerId() == id1) { QJsonObject allObj; allObj.insert("cmd", "09"); // 设置IP和端口号连接 m_pSocket->connectToHost(IP, m_nServerPort); // 设置超时连接时间 m_bConnected = m_pSocket->waitForConnected(1 * 1000); // QNetworkRequest req; // QString sUrl = QString("http://%1/cgi-bin/General.cgi/").arg(IP); // req.setUrl(sUrl); // g_NetMgr->PostJson(req,allObj); QJsonDocument doc(allObj); QString strData = QString(doc.toJson(QJsonDocument::Indented)); int nRet = m_pSocket->write(strData.toStdString().c_str(), strlen(strData.toStdString().c_str())); if(nRet !=0){ //qDebug() << "09 通信失败" << endl; //customLogMessageHandler(QtWarningMsg,"09通信失败"); } } } void CRealTimeForm::InitChannelInfo() { g_channelSetting = g_SqliteDB->GetDataMultiLine("t_ChannelSetting"); g_ChannelBaseInfo.clear(); QVector().swap(g_ChannelBaseInfo); for (int i = 0; i < g_channelSetting.size(); i++) { QMap mapChannelType; mapChannelType.insert(g_channelSetting[i].sensorType,g_channelSetting[i].channelName); g_MapChannel.insert(g_channelSetting[i].channelId,mapChannelType); channelBaseInfo tempchannelbaseinfo; tempchannelbaseinfo.bordNo = g_channelSetting[i].sensorModuleNo; tempchannelbaseinfo.channelID = g_channelSetting[i].channelId; tempchannelbaseinfo.channelNoInBoard = g_channelSetting[i].sensorNo; tempchannelbaseinfo.channelName = g_channelSetting[i].channelName; tempchannelbaseinfo.channelType = g_channelSetting[i].sensorType; tempchannelbaseinfo.boardType = g_channelSetting[i].boardType; tempchannelbaseinfo.sensorEngineeringUnit = g_channelSetting[i].sensorEngineeringUnit; tempchannelbaseinfo.defaultDisplay = g_channelSetting[i].defaultDisplay; tempchannelbaseinfo.pairChannelId = g_channelSetting[i].pairChannelId; tempchannelbaseinfo.speedRefChannelId = g_channelSetting[i].speedRefChannelId; tempchannelbaseinfo.isEnable = g_channelSetting[i].isEnable; tempchannelbaseinfo.ChUnitDot = g_channelSetting[i].ChUnitDot; g_ChannelBaseInfo.append(tempchannelbaseinfo); } qDebug() <<"InitChannelInfo"<< g_ChannelBaseInfo.size()<< endl; } void CRealTimeForm::on_graphView_customContextMenuRequested(const QPoint &pos) { qDebug() << " custom menu!!!!!!!!!"<< "##"<< pos.x() << " ; "<< pos.y(); // if(ui->graphicsView->itemAt(pos) || ui->graphicsView_2->itemAt(pos)) // return; QMenu menu(this); pActionVerticalLine = menu.addAction("添加竖线"); pActionHorizontalLine = menu.addAction("添加横线"); pActionRect = menu.addAction("添加通道"); pActionRect2 = menu.addAction("矩形框"); pActionPixmap = menu.addAction("添加图片"); pActionText = menu.addAction("添加文字"); pActionEdit = menu.addAction("编辑"); pActionSave = menu.addAction("保存"); pActionVerticalLine->setData(1); pActionHorizontalLine->setData(2); pActionRect->setData(3); pActionPixmap->setData(4); pActionText->setData(5); pActionSave->setData(6); pActionEdit->setData(7); pActionRect2->setData(8); connect(pActionVerticalLine, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionHorizontalLine, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionRect, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionPixmap, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionText, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionSave, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionEdit, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); connect(pActionRect2, SIGNAL(triggered()), this, SLOT(onTaskBoxContextMenuEvent())); menu.exec(QCursor::pos()); //menu.exec(ui->graphicsView->mapToGlobal(pos)); // if(pActionLine == menu.exec(ui->graphicsView->mapToGlobal(pos))){ // qDebug() << " Line!!!!!!!!!"<< "##"<< pos.x() << " ; "<< pos.y(); // SetCurDrawType("Line",pos); // }else if(pActionRect == menu.exec(ui->graphicsView->mapToGlobal(pos))){ // qDebug() << " Rect!!!!!!!!!"<< "##"<< pos.x() << " ; "<< pos.y(); // SetCurDrawType("Rect",pos); // } } void CRealTimeForm::onTaskBoxContextMenuEvent() { QAction *pEven = qobject_cast(this->sender()); int iType = pEven->data().toInt(); switch (iType) { case 1: SetCurDrawType("vertical"); break; case 2: SetCurDrawType("Horizonta"); break; case 3: SetCurDrawType("Channel"); break; case 4: SetCurDrawType("Pixmap"); break; case 5: SetCurDrawType("Text"); break; case 6: SetCurDrawType("Save"); break; case 7: SetEdit(); break; case 8: SetCurDrawType("Rect"); break; } } void CRealTimeForm::SetEdit() { m_EditMode = 1; LoadGraphicsConfig(1); } void CRealTimeForm::on_addchannel(channelBaseInfo channelbaseInfo,QString str) { QPen pen; // 定义一个画笔,设置画笔颜色和宽度 pen.setColor(QColor(0, 0, 0,0)); pen.setWidth(1); CMyCustomGraphicsItem* pItemGroup = new CMyCustomGraphicsItem(); QGraphicsRectItem* pRect = new QGraphicsRectItem(0, 0, 290, 30); pRect->setBrush(QBrush(QColor(255, 255, 255))); pRect->setPen(pen); pRect->setPos(0, 0); pRect->setData(1,"Rect"); pItemGroup->addToGroup(pRect); pItemGroup->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); QGraphicsTextItem* pTextUnits = NULL; if(channelbaseInfo.channelType == "TACHOMETER"){ if(str == "偏置电压" || str == "最大正向峰值" || str == "最大负向峰值" || str == "平均值"){ pTextUnits = new QGraphicsTextItem("V"); }else{ pTextUnits = new QGraphicsTextItem("RPM"); } }else if(channelbaseInfo.channelType == "ACCELEROMETER"){ if(str == "偏置电压"){ pTextUnits = new QGraphicsTextItem("V"); } else if(str == "最大正向峰值" || str == "最大负向峰值" || str == "诊断峰峰值" || str == "平均值"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else if(str == "速度峰值" || str == "速度有效值"){ pTextUnits = new QGraphicsTextItem("mm/s"); }else{ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } }else if(channelbaseInfo.channelType == "PROXIMETER"){ if(str == "平均值"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } else if(str == "最大正向峰值" || str == "最大负向峰值" || str == "诊断峰峰值"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else{ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } }else if(channelbaseInfo.channelType == "THRUST"){ if(str == "平均值"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else if(str == "偏置电压"){ pTextUnits = new QGraphicsTextItem("V"); }else{ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } }else if(channelbaseInfo.channelType == "VELOCITY"){ if(str == "偏置电压"){ pTextUnits = new QGraphicsTextItem("V"); }else if(str == "有效值"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else if(str == "位移峰值" || str == "位移有效值"){ pTextUnits = new QGraphicsTextItem("um"); }else{ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } }else if(channelbaseInfo.channelType == "SLOW_CURRENT"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else if(channelbaseInfo.channelType == "FAST_VOLTAGE"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); }else if(channelbaseInfo.channelType == "PULSE_CURRENT"){ pTextUnits = new QGraphicsTextItem(channelbaseInfo.sensorEngineeringUnit); } if(str == "峰值因子"){ pTextUnits = new QGraphicsTextItem(""); } QGraphicsTextItem* pTextChannel = new QGraphicsTextItem(channelbaseInfo.channelName); QGraphicsTextItem* pTextValue = new QGraphicsTextItem("0.0"); pTextUnits->setData(1,"units"); pTextChannel->setData(2,"channel"); pTextValue->setData(3,"value"); pTextValue->setData(4,str); QFont font; font.setPixelSize(16); QFont font2 ( "黑体", 18, 50); font.setFamily(QStringLiteral("黑体")); pTextUnits->setFont(font); pTextUnits->setDefaultTextColor(QColor(27, 30, 35)); pTextChannel->setFont(font); pTextValue->setFont(font2); pTextValue->setPos(150,0); pTextValue->setDefaultTextColor(QColor(31, 81, 136)); pTextChannel->setPos(0,2); pTextChannel->setDefaultTextColor(QColor(27, 30, 35)); pTextUnits->setPos(240,5); // 设置可移动、可选择 pItemGroup->addToGroup(pTextChannel); pItemGroup->addToGroup(pTextValue); pItemGroup->addToGroup(pTextUnits); pItemGroup->setData(0,channelbaseInfo.channelID); if(ui->tabWidget->currentIndex() == 0){ m_pGraphicsScene->addItem(pItemGroup); }else if(ui->tabWidget->currentIndex() == 1){ m_pGraphicsScene_2->addItem(pItemGroup); } } void CRealTimeForm::SetCurDrawType(QString strType) { if(!m_EditMode){ QMessageBox::information(this, QStringLiteral("提示"), "请先进入编辑模式!"); return; } if(strType == "vertical"){ QGraphicsLineItem* pLine = new QGraphicsLineItem(0, 0, 100, 100); pLine->setPos(0, 0); pLine->setRotation(45); pLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pLine->setZValue(100); if(ui->tabWidget->currentIndex() == 0){ m_pGraphicsScene->addItem(pLine); }else if(ui->tabWidget->currentIndex() == 1){ m_pGraphicsScene_2->addItem(pLine); } }else if(strType == "Horizonta"){ QGraphicsLineItem* pLine = new QGraphicsLineItem(0, 0, 100, 100); pLine->setPos(0, 0); pLine->setRotation(-45); pLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pLine->setZValue(100); if(ui->tabWidget->currentIndex() == 0){ m_pGraphicsScene->addItem(pLine); }else if(ui->tabWidget->currentIndex() == 1){ m_pGraphicsScene_2->addItem(pLine); } } else if(strType == "Channel"){ CAddChannel *pAddChannel = new CAddChannel(); pAddChannel->setWindowModality(Qt::ApplicationModal); pAddChannel->show(); connect(pAddChannel,SIGNAL(addChannel_sg(channelBaseInfo,QString)),this,SLOT(on_addchannel(channelBaseInfo,QString))); }else if (strType == "Pixmap"){ QString name = QCoreApplication::applicationDirPath() + "\\config\\UnitParameters.json"; QFile loadFile(name); if(!loadFile.open(QIODevice::ReadOnly)) { qDebug() << "could't open projects json"; return; } QString value = loadFile.readAll(); loadFile.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); if (jsonObject.contains(QStringLiteral("UnitStyle"))) { QJsonValue jsonValue = jsonObject.value(QStringLiteral("UnitStyle")); QString strContent = jsonValue["content"].toString(); QGraphicsPixmapItem* pPixmap = NULL; if(strContent == "混流式机组"){ pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/hunliushi.png")); }else if(strContent == "轴流定桨式机组" || strContent == "轴流转浆式机组"){ pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/zhouliushi.png")); }else if(strContent == "可逆式机组"){ pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/kenishi.png")); }else if(strContent == "灯泡式机组"){ pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/dengpaoshi.png")); } // 设置可移动、可选择 pPixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); if(ui->tabWidget->currentIndex() == 0){ m_pGraphicsScene->addItem(pPixmap); }else if(ui->tabWidget->currentIndex() == 1){ m_pGraphicsScene_2->addItem(pPixmap); } pPixmap->setPos(0,0); } }else if (strType == "Text") { QGraphicsTextItem* pText = new QGraphicsTextItem("请输入..."); // 设置可移动、可选择 pText->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pText->setTextInteractionFlags(Qt::TextEditorInteraction); if(ui->tabWidget->currentIndex() == 0){ m_pGraphicsScene->addItem(pText); }else if(ui->tabWidget->currentIndex() == 1){ m_pGraphicsScene_2->addItem(pText); } pText->setPos(0,0); }else if (strType == "Save") { SaveGraphicsConfig(); if(m_MachineStatus == 0){ LoadGraphicsConfig(2); }else if(m_MachineStatus == 1){ LoadGraphicsConfig(0); } }else if(strType == "Rect"){ CustomRectItem *m_rect = new CustomRectItem(); m_rect->setPos(50, 50); m_pGraphicsScene->addItem(m_rect); m_rect->setRect(0, 0, 50, 50); m_rect->setTransformOriginPoint(25, 25); m_rect->setRotation(45); } ui->graphicsView->update(); } void CRealTimeForm::SaveGraphicsConfig() { m_EditMode = 0; QJsonObject jsonObject; QJsonArray RectjsonArray; QJsonArray LinejsonArray; QJsonArray PixmapjsonArray; QJsonArray TextjsonArray; QJsonArray GroupjsonArray; QList items; if(ui->tabWidget->currentIndex() == 0){ items = m_pGraphicsScene->items(); }else if(ui->tabWidget->currentIndex() == 1){ items = m_pGraphicsScene_2->items(); } foreach (QGraphicsItem *item, items) { QGraphicsItem* graphicsItem = static_cast(item); int itemType = graphicsItem->type(); //qDebug() << "itemType" << itemType << endl; switch (itemType) { // case 3: {//Rect // QPointF pos = graphicsItem->pos(); // QRectF rect = graphicsItem->boundingRect(); // qDebug() << "Rect" << pos << rect; // QJsonObject RectjsonObject; // RectjsonObject.insert("x", pos.x()); // RectjsonObject.insert("y", pos.y()); // RectjsonObject.insert("w", rect.width()); // RectjsonObject.insert("h", rect.height()); // RectjsonArray.append(RectjsonObject); // } break; case 4://Ellipse break; case 6://Line { QPointF pos = graphicsItem->pos(); QRectF rect = graphicsItem->boundingRect(); //qDebug() << "Line" << pos << rect; QJsonObject LinejsonObject; LinejsonObject.insert("x", pos.x()); LinejsonObject.insert("y", pos.y()); LinejsonObject.insert("w", rect.width()); LinejsonObject.insert("h", rect.height()); LinejsonObject.insert("rotation", graphicsItem->rotation()); LinejsonArray.append(LinejsonObject); } break; case 7://Pixmap { QPointF pos = graphicsItem->pos(); QRectF rect = graphicsItem->boundingRect(); //qDebug() << "Pixmap" << pos << rect; QJsonObject PixmapjsonObject; PixmapjsonObject.insert("x", pos.x()); PixmapjsonObject.insert("y", pos.y()); PixmapjsonObject.insert("w", rect.width()); PixmapjsonObject.insert("h", rect.height()); PixmapjsonArray.append(PixmapjsonObject); } break; // case 8:{//Text // QGraphicsTextItem* pGraphicTextItem = static_cast(graphicsItem); // QPointF pos = graphicsItem->pos(); // QRectF rect = graphicsItem->boundingRect(); // qDebug() << "Text" << pos << rect; // qDebug() << pGraphicTextItem->toPlainText(); // QJsonObject TextjsonObject; // TextjsonObject.insert("x", pos.x()); // TextjsonObject.insert("y", pos.y()); // TextjsonObject.insert("w", rect.width()); // TextjsonObject.insert("h", rect.height()); // TextjsonObject.insert("word", pGraphicTextItem->toPlainText()); // TextjsonArray.append(TextjsonObject); // } // break; case 10:{ QGraphicsItemGroup* pGraphicGroupItem = static_cast(graphicsItem); QPointF pos = pGraphicGroupItem->pos(); QRectF rect = pGraphicGroupItem->boundingRect(); channelBaseInfo temp; //qDebug() << "Group" << pos << rect; QString strChannelID = pGraphicGroupItem->data(0).toString(); //qDebug() << "pGraphicGroupItem data" << strChannelID << endl; QJsonArray GroupRectjsonArray,GroupTextjsonArray; QJsonObject itemjsonObject; QList items = pGraphicGroupItem->childItems(); foreach (QGraphicsItem *item, items) { QString itemData,itemData2 = ""; for (int i = 0; i < 5; i++) { itemData = item->data(i).toString(); if(itemData == "value"){ itemData2 = item->data(i+1).toString(); //qDebug() << "item va data" << itemData << itemData2 << i << endl; break; }else if (itemData == "channel") { //qDebug() << "item ch data" << itemData << i << endl; break; }else if(itemData == "units"){ //qDebug() << "item un data" << itemData << i << endl; break; } } QGraphicsItem* graphicsItem = static_cast(item); int itemType = graphicsItem->type(); //qDebug() << "itemData2" << itemData << endl; switch (itemType) { case 3: {//Rect QPointF pos = graphicsItem->pos(); QRectF rect = graphicsItem->boundingRect(); //qDebug() << "group Rect" << pos << rect; QJsonObject RectjsonObject; RectjsonObject.insert("x", pos.x()); RectjsonObject.insert("y", pos.y()); RectjsonObject.insert("w", rect.width()); RectjsonObject.insert("h", rect.height()); GroupRectjsonArray.append(RectjsonObject); } break; case 8:{//Text QGraphicsTextItem* pGraphicTextItem = static_cast(graphicsItem); QPointF pos = graphicsItem->pos(); QRectF rect = graphicsItem->boundingRect(); QJsonObject TextjsonObject; TextjsonObject.insert("x", pos.x()); TextjsonObject.insert("y", pos.y()); TextjsonObject.insert("w", rect.width()); TextjsonObject.insert("h", rect.height()); TextjsonObject.insert("data", itemData); QColor color = pGraphicTextItem->defaultTextColor(); QRgb mRgb = qRgba(color.red(), color.green(), color.blue(), color.alpha()); TextjsonObject.insert("color",QString::number(mRgb, 16)); if(itemData2 != "") TextjsonObject.insert("static", itemData2); //qDebug() << "group Text" << pGraphicTextItem->toPlainText(); if(itemData == "units") temp.channelUnit = pGraphicTextItem->toPlainText(); else if(itemData == "channel") temp.channelName = pGraphicTextItem->toPlainText(); TextjsonObject.insert("word", pGraphicTextItem->toPlainText()); GroupTextjsonArray.append(TextjsonObject); }break; } } itemjsonObject.insert("Rect",GroupRectjsonArray); itemjsonObject.insert("Text",GroupTextjsonArray); itemjsonObject.insert("x",pos.x()); itemjsonObject.insert("y",pos.y()); itemjsonObject.insert("data",strChannelID); GroupjsonArray.append(itemjsonObject); temp.channelID = strChannelID; g_ChannelView.append(temp); }break; default: break; } } jsonObject.insert("Rect", RectjsonArray); jsonObject.insert("Line", LinejsonArray); jsonObject.insert("Pixmap", PixmapjsonArray); jsonObject.insert("Text", TextjsonArray); jsonObject.insert("Group", GroupjsonArray); QString fileName; if(ui->tabWidget->currentIndex() == 0){ fileName = QCoreApplication::applicationDirPath() + "\\config\\Graph1.json"; }else{ fileName = QCoreApplication::applicationDirPath() + "\\config\\Graph2.json"; } QJsonDocument jsonDoc; jsonDoc.setObject(jsonObject); //QString fileName = QFileDialog::getSaveFileName(this,"另存为 ","xxx.json"); //if(!fileName.isEmpty()) { qDebug() << "另存为" << fileName; QFile file(fileName); //if(!file.exists()) { file.open(QIODevice::WriteOnly); file.write(jsonDoc.toJson()); file.close(); } } } void CRealTimeForm::slotDoubleClick(QGraphicsSceneMouseEvent *event) { qDebug() << "slotDoubleClick" << event->pos() << endl; QGraphicsItem *item = m_pGraphicsScene->itemAt(event->scenePos(),QTransform()); QGraphicsItemGroup* pGraphicGroupItem = static_cast(item); qDebug() << "QGraphicsItem" << pGraphicGroupItem->childItems() << endl; } void CRealTimeForm::LoadGraphicsConfig(int type) { //读取图元的数据 m_pGraphicsScene->clear(); m_pGraphicsScene_2->clear(); QPen pen; // 定义一个画笔,设置画笔颜色和宽度 pen.setColor(QColor(0, 0, 0,0)); pen.setWidth(1); //QString fileName =QFileDialog::getOpenFileName(this,"打开文件",QDir::currentPath(), "*.json"); for (int ii = 0; ii < ui->tabWidget->count(); ii++) { QString strFileName = QString("Graph%1.json").arg(ii+1); QString fileName = QCoreApplication::applicationDirPath() + "\\config\\" + strFileName; qDebug() << "打开" << fileName ; if(!fileName.isEmpty()) { QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text); QString value = file.readAll(); file.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); // Line字段 if (jsonObject.contains(QStringLiteral("Line"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Line")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QGraphicsLineItem *pLine = new QGraphicsLineItem( 0, 0, key["w"].toDouble(), key["h"].toDouble()); pLine->setPos(key["x"].toDouble(), key["y"].toDouble()); pLine->setRotation(key["rotation"].toInt()); pLine->setZValue(100); if(type) pLine->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); if(ii == 0){ m_pGraphicsScene->addItem(pLine); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pLine); } } } } // Rect字段 if (jsonObject.contains(QStringLiteral("Rect"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Rect")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QGraphicsRectItem *pRect = new QGraphicsRectItem( 0, 0, key["w"].toDouble(), key["h"].toDouble()); pRect->setPos(key["x"].toDouble(), key["y"].toDouble()); if(type) pRect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); if(ii == 0){ m_pGraphicsScene->addItem(pRect); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pRect); } } } } // Pixmap字段 if (jsonObject.contains(QStringLiteral("Pixmap"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Pixmap")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QString name = QCoreApplication::applicationDirPath() + "\\config\\UnitParameters.json"; QFile loadFile(name); if(!loadFile.open(QIODevice::ReadOnly)) { qDebug() << "could't open projects json"; return; } QString value = loadFile.readAll(); loadFile.close(); QJsonParseError parseJsonErr; QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr); if (!(parseJsonErr.error == QJsonParseError::NoError)) { QMessageBox::about(NULL, "提示", "读取文件错误!"); return; } QJsonObject jsonObject = document.object(); if (jsonObject.contains(QStringLiteral("UnitStyle"))) { QJsonValue jsonValue = jsonObject.value(QStringLiteral("UnitStyle")); QString strContent = jsonValue["content"].toString(); QGraphicsPixmapItem* pPixmap = NULL; QLabel *gif_anim = new QLabel(); QMovie *movie = NULL; qDebug() << "strContent" << strContent << endl; if(strContent == "混流式机组"){ if(type == 1 || type == 2) pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/hunliushi.png")); else movie = new QMovie(QCoreApplication::applicationDirPath() + "/image/unit/hunliushi.gif"); }else if(strContent == "轴流定桨式机组" || strContent == "轴流转浆式机组"){ if(type == 1 || type == 2) pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/zhouliushi.png")); else movie = new QMovie(QCoreApplication::applicationDirPath() + "/image/unit/zhouliushi.gif"); //pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/zhouliushi.png")); }else if(strContent == "可逆式机组"){ if(type == 1 || type == 2) pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/kenishi.png")); else movie = new QMovie(QCoreApplication::applicationDirPath() + "/image/unit/kenishi.gif"); //pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/kenishi.png")); }else if(strContent == "灯泡式机组"){ if(type == 1 || type == 2) pPixmap = new QGraphicsPixmapItem(QPixmap(QCoreApplication::applicationDirPath() + "/image/unit/dengpaoshi.png")); else movie = new QMovie(QCoreApplication::applicationDirPath() + "/image/unit/dengpaoshi.gif"); } qDebug() << "type" << type << ii << endl; if(type){ pPixmap->setPos(key["x"].toInt(), key["y"].toInt()); // 设置可移动、可选择 if(type == 1){ pPixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); } if(ii == 0){ m_pGraphicsScene->addItem(pPixmap); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pPixmap); } }else{ gif_anim->setMovie(movie); movie->start(); QGraphicsProxyWidget *proxy = NULL; if(ii == 0){ qDebug() << "strContent111" << strContent << endl; proxy = m_pGraphicsScene->addWidget(gif_anim); }else if(ii == 1){ proxy = m_pGraphicsScene_2->addWidget(gif_anim); } proxy->setGeometry(QRectF(key["x"].toInt(),key["y"].toInt(),0,0)); } } // QGraphicsPixmapItem *pPixmap = new QGraphicsPixmapItem(QPixmap(":/image/1.png")); // pPixmap->setPos(key["x"].toInt(), key["y"].toInt()); // pPixmap->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); // if(ii == 0){ // m_pGraphicsScene->addItem(pPixmap); // }else if(ii == 1){ // m_pGraphicsScene_2->addItem(pPixmap); // } // QLabel *gif_anim = new QLabel(); // QMovie *movie = new QMovie(":/image/1.gif"); // gif_anim->setMovie(movie); // movie->start(); // QGraphicsProxyWidget *proxy = m_pGraphicsScene->addWidget(gif_anim); // QGraphicsMovieItem *pPixmap = new QGraphicsMovieItem(); // pPixmap->setPos(key["x"].toInt(), key["y"].toInt()); // QMovie movie(":/image/1.png"); // pPixmap->setMovie(&movie); // movie.start(); // m_pGraphicsScene->addItem(pPixmap); } } } // Text字段 if (jsonObject.contains(QStringLiteral("Text"))) { if(type == 1){ QGraphicsTextItem *pText = new QGraphicsTextItem("正在编辑中..."); pText->setPos(300, 530); pText->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); QFont font ( "黑体", 20, 50); font.setPixelSize(30); pText->setDefaultTextColor(QColor(255, 0, 0)); pText->setFont(font); if(ii == 0){ m_pGraphicsScene->addItem(pText); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pText); } } QJsonValue arrayValue = jsonObject.value(QStringLiteral("Text")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QGraphicsTextItem *pText = new QGraphicsTextItem(key["word"].toString()); pText->setPos(key["x"].toInt(), key["y"].toInt()); pText->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pText->setTextInteractionFlags(Qt::TextEditorInteraction); QFont font; font.setPixelSize(25); pText->setFont(font); if(ii == 0){ m_pGraphicsScene->addItem(pText); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pText); } } } } // Group字段 if (jsonObject.contains(QStringLiteral("Group"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Group")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { channelBaseInfo temp; CMyCustomGraphicsItem* pItemGroup = new CMyCustomGraphicsItem(); if(type == 1) pItemGroup->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); connect(pItemGroup, SIGNAL(doubleclick(QGraphicsSceneMouseEvent*)), this, SLOT(slotDoubleClick(QGraphicsSceneMouseEvent*))); //qDebug() << array.at(i)["x"].toDouble() << array.at(i)["y"].toDouble() << endl; QJsonObject jsonObject = array.at(i).toObject(); if (jsonObject.contains(QStringLiteral("Rect"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Rect")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QGraphicsRectItem *pRect = new QGraphicsRectItem( 0, 0, 290, 30); pRect->setPos(key["x"].toDouble(), key["y"].toDouble()); if(type == 1) pRect->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pRect->setBrush(QBrush(QColor(255, 255, 255))); pRect->setPen(pen); pItemGroup->addToGroup(pRect); } } } if (jsonObject.contains(QStringLiteral("Text"))) { QJsonValue arrayValue = jsonObject.value(QStringLiteral("Text")); if (arrayValue.isArray()) { QJsonArray array = arrayValue.toArray(); for (int i = 0; i < array.size(); i++) { QJsonValue nameArray = array.at(i); QJsonObject key = nameArray.toObject(); QGraphicsTextItem *pText = new QGraphicsTextItem(key["word"].toString()); pText->setPos(key["x"].toDouble(), key["y"].toDouble()); if(type){ pText->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); pText->setTextInteractionFlags(Qt::TextEditorInteraction); } QFont font; QFont font2 ( "黑体", 18, 50); font.setPixelSize(16); font.setFamily(QStringLiteral("黑体")); pText->setFont(font); if(key["data"].toString() == "units"){ pText->setData(1,"units"); pText->setDefaultTextColor(QColor(27, 30, 35)); temp.channelUnit = key["word"].toString(); }else if(key["data"].toString() == "channel"){ pText->setData(2,"channel"); pText->setDefaultTextColor(QColor(27, 30, 35)); temp.channelName = key["word"].toString(); }else if(key["data"].toString() == "value"){ pText->setData(3,"value"); pText->setData(4,key["static"].toString()); pText->setDefaultTextColor(QColor(31, 81, 136)); pText->setFont(font2); } pItemGroup->addToGroup(pText); } } } pItemGroup->setPos(array.at(i)["x"].toDouble() , array.at(i)["y"].toDouble()); pItemGroup->setData(0,array.at(i)["data"].toString()); if(ii == 0){ m_pGraphicsScene->addItem(pItemGroup); }else if(ii == 1){ m_pGraphicsScene_2->addItem(pItemGroup); } temp.channelID = array.at(i)["data"].toString(); g_ChannelView.append(temp); } } } } } } void CRealTimeForm::keyPressEvent(QKeyEvent *event) { //QGraphicsItem::keyPressEvent(event); qDebug() << "键盘按下"; if (event->key() == Qt::Key_Delete) { qDebug() << "press Key_Delete"; if(ui->tabWidget->currentIndex() == 0){ QList items = m_pGraphicsScene->items(); foreach (QGraphicsItem *item, items) { QGraphicsItem* graphicsItem = static_cast(item); if(graphicsItem->isSelected()){ qDebug() << "delete Item"; m_pGraphicsScene->removeItem(graphicsItem); } } }else if(ui->tabWidget->currentIndex() == 1){ QList items = m_pGraphicsScene_2->items(); foreach (QGraphicsItem *item, items) { QGraphicsItem* graphicsItem = static_cast(item); if(graphicsItem->isSelected()){ qDebug() << "delete Item"; m_pGraphicsScene_2->removeItem(graphicsItem); } } } return; } // } void CRealTimeForm::UpdateCharacteristic(QVector<_Charateristic>& m_vecCharateristic) { QString RPM = "0"; if(m_vecCharateristic.size() > 0){ for (int i = 0; i < m_vecCharateristic.size(); i++) { QList items; if(ui->tabWidget->currentIndex() == 0){ items = m_pGraphicsScene->items(); }else if(ui->tabWidget->currentIndex() == 1){ items = m_pGraphicsScene_2->items(); } foreach (QGraphicsItem *item, items) { QGraphicsItem* graphicsItem = static_cast(item); int itemType = graphicsItem->type(); switch (itemType) { case 10:{ QGraphicsItemGroup* pGraphicGroupItem = static_cast(graphicsItem); channelBaseInfo temp; QString strChannelID = pGraphicGroupItem->data(0).toString(); if(strChannelID != m_vecCharateristic[i].ChannelID) continue; QJsonObject itemjsonObject; QList items = pGraphicGroupItem->childItems(); QString itemData,itemData2 = ""; foreach (QGraphicsItem *item, items){ for (int i = 0; i < 5; i++){ itemData = item->data(i).toString(); if(itemData == "value"){ itemData2 = item->data(i+1).toString(); //qDebug() << "item va data" << itemData << itemData2 << i << endl; break; }else if (itemData == "channel"){ break; }else if(itemData == "units"){ break; } } QGraphicsItem* graphicsItem = static_cast(item); int itemType = graphicsItem->type(); switch (itemType) { case 8:{//Text QGraphicsTextItem* pGraphicTextItem = static_cast(graphicsItem); if(itemData == "value"){ //qDebug() << strChannelID << "itemData" < g_mapTriggerEvent[strChannelID].DangerOverSetpoint.toFloat() && g_mapTriggerEvent[strChannelID].DangerOver == 1)){ pGraphicTextItem->setDefaultTextColor(QColor(255, 0, 0)); flag = 1; } else{ pGraphicTextItem->setDefaultTextColor(QColor(31, 81, 136)); } } if((g_mapTriggerEvent[strChannelID].AlertOver == 1 || g_mapTriggerEvent[strChannelID].AlertUnder == 1) \ && itemData2 == g_mapTriggerEvent[strChannelID].Characteristic && flag == -1){ QString strLog = "警报" + str + "," + g_mapTriggerEvent[strChannelID].AlertUnderSetpoint + "," + g_mapTriggerEvent[strChannelID].AlertOverSetpoint; if((str.toFloat() < g_mapTriggerEvent[strChannelID].AlertUnderSetpoint.toFloat() && g_mapTriggerEvent[strChannelID].AlertUnder == 1 \ && g_mapTriggerEvent[strChannelID].DangerUnder == 1 && str.toFloat() > g_mapTriggerEvent[strChannelID].DangerUnderSetpoint.toFloat()) ||\ (str.toFloat() > g_mapTriggerEvent[strChannelID].AlertOverSetpoint.toFloat() && g_mapTriggerEvent[strChannelID].AlertOver == 1 \ && g_mapTriggerEvent[strChannelID].DangerOver == 0 ) ||\ (str.toFloat() > g_mapTriggerEvent[strChannelID].AlertOverSetpoint.toFloat() && g_mapTriggerEvent[strChannelID].AlertOver == 1 \ && g_mapTriggerEvent[strChannelID].DangerOver == 1 && str.toFloat() < g_mapTriggerEvent[strChannelID].DangerOverSetpoint.toFloat())){ pGraphicTextItem->setDefaultTextColor(QColor(255, 165, 0)); } else{ pGraphicTextItem->setDefaultTextColor(QColor(31, 81, 136)); } } sigRPM(RPM); pGraphicTextItem->setPlainText(str); } }break; } } }break; } } } if(ui->tabWidget->currentIndex() == 0){ ui->graphicsView->update(); }else if(ui->tabWidget->currentIndex() == 1){ ui->graphicsView_2->update(); } } } void CRealTimeForm::ParseCharacteristic(QJsonArray& arrayValue) { m_vecCharateristic.clear(); g_Charateristic.clear(); QVector().swap(m_vecCharateristic); QVector().swap(g_Charateristic); _Charateristic tempCharateristic; for (int i = 0; i < arrayValue.size(); i++) { int enable = arrayValue.at(i)["isEnable"].toDouble(); if(enable == 1){ tempCharateristic.Amp1x = arrayValue.at(i)["1xAmp"].toDouble(); tempCharateristic.Phase1x = arrayValue.at(i)["1xPhase"].toDouble(); tempCharateristic.ChannelID = arrayValue.at(i)["ChannelId"].toString(); tempCharateristic.ChannelName = arrayValue.at(i)["ChannelName"].toString(); tempCharateristic.CrestFactor = arrayValue.at(i)["CrestFactor"].toDouble(); tempCharateristic.DCValues = arrayValue.at(i)["DCValues"].toDouble(); tempCharateristic.DerivedPeak = arrayValue.at(i)["DerivedPeak"].toDouble(); tempCharateristic.DiagnosisPeak = arrayValue.at(i)["DiagnosisPeak"].toDouble(); tempCharateristic.DiagnosisPk2Pk = arrayValue.at(i)["DiagnosisPk2Pk"].toDouble(); tempCharateristic.DirectPK = arrayValue.at(i)["DirectPK"].toDouble(); tempCharateristic.FullScalePosition = arrayValue.at(i)["FullScalePosition"].toDouble(); tempCharateristic.Gap = arrayValue.at(i)["Gap"].toDouble(); tempCharateristic.IntegratPk2Pk2 = arrayValue.at(i)["IntegratPk2Pk/2"].toDouble(); tempCharateristic.IntegratRMS = arrayValue.at(i)["IntegratRMS"].toDouble(); tempCharateristic.MaxValues = arrayValue.at(i)["MaxValues"].toDouble(); tempCharateristic.MinValues = arrayValue.at(i)["MinValues"].toDouble(); tempCharateristic.MonitorPk2Pk = arrayValue.at(i)["MonitorPk2Pk"].toDouble(); tempCharateristic.Position = arrayValue.at(i)["Position"].toDouble(); tempCharateristic.RmsPkPk2Pk = arrayValue.at(i)["RmsPkPk2Pk"].toDouble(); tempCharateristic.SensorEngineeringUnit = arrayValue.at(i)["SensorEngineeringUnit"].toString(); tempCharateristic.SensorStatus = arrayValue.at(i)["SensorStatus"].toDouble(); tempCharateristic.ZeroScalePosition = arrayValue.at(i)["ZeroScalePosition"].toDouble(); tempCharateristic.xFullScalePosition = arrayValue.at(i)["xFullScalePosition"].toDouble(); tempCharateristic.xProcessVariableName = arrayValue.at(i)["xProcessVariableName"].toString(); tempCharateristic.xZeroScalePosition = arrayValue.at(i)["xZeroScalePosition"].toDouble(); tempCharateristic.RMSValues = arrayValue.at(i)["RMSValues"].toDouble(); tempCharateristic.speedRPM = arrayValue.at(i)["SpeedProfileSpeed"].toDouble(); tempCharateristic.ChUnitDot = arrayValue.at(i)["ChUnitDot"].toInt(); tempCharateristic.channelType = arrayValue.at(i)["ChannelType"].toString(); m_vecCharateristic.push_back(tempCharateristic); g_Charateristic.push_back(tempCharateristic); } } UpdateCharacteristic(m_vecCharateristic); } // 接受服务返回的数据 void CRealTimeForm::slotRecieve() { QByteArray arrayReady = m_pSocket->readAll(); m_arrayReady += arrayReady; // customLogMessageHandler(QtDebugMsg,m_arrayReady); // qDebug() << "arrayReady" << arrayReady << endl; // QJsonDocument doc = QJsonDocument::fromJson(arrayReady); // QJsonObject objec = doc.object(); // if(objec.contains("cmd")) // { // QJsonValue arrays_value = objec.take("cmd"); // qDebug()<<"cmd ="<(); if(objec.contains("cmd")) { QJsonValue arrays_value = objec.take("cmd"); //qDebug()<<"cmd ="<focusItem(); QGraphicsSceneEvent* mouseEvent = (QGraphicsSceneEvent*)e; qDebug() << "event->type():" << mouseEvent->type(); if (e->type() == QEvent::MouseButtonDblClick) { qDebug() << "Double Click"; // 注意这里一定要返回true,表示你要过滤该事件原本的实现 QList items = m_pGraphicsScene->items(); foreach (QGraphicsItem *item, items) { qDebug() << "Double items"; QGraphicsItem* graphicsItem = static_cast(item); if(graphicsItem->isSelected()){ qDebug() << "Double Click Item"; } } } }