178 lines
5.3 KiB
C++
178 lines
5.3 KiB
C++
#ifndef REALTIMEFORM_H
|
||
#define REALTIMEFORM_H
|
||
|
||
#include <QWidget>
|
||
#include <QGraphicsScene>
|
||
#include <QGraphicsView>
|
||
#include <QGraphicsItem>
|
||
#include <QGraphicsRectItem>
|
||
#include <QGraphicsLineItem>
|
||
#include <QGraphicsTextItem>
|
||
#include <QGraphicsProxyWidget>
|
||
#include <QKeyEvent>
|
||
#include <AddChannel.h>
|
||
#include "sqlitedb.h"
|
||
#include <QTimer> //定时器类
|
||
#include <QAbstractSocket>
|
||
#include <QTcpSocket>
|
||
|
||
|
||
|
||
class NetMgr;
|
||
|
||
namespace Ui {
|
||
class CRealTimeForm;
|
||
}
|
||
// 1 创建新的类 MyItem2 继承 QGraphicsPathItem,重新实现点击、悬浮等函数
|
||
|
||
#include <QGraphicsSceneEvent>
|
||
#include <QPen>
|
||
|
||
// 点击该控件时,调用该函数
|
||
typedef void (*ClickHandle)(int type,void *other);
|
||
|
||
class MyItem2 : public QGraphicsPathItem
|
||
{
|
||
|
||
public:
|
||
explicit MyItem2(QGraphicsItem *parent = nullptr);
|
||
|
||
protected:
|
||
// 重新实现这些函数
|
||
void keyPressEvent(QKeyEvent *event);
|
||
void mousePressEvent(QGraphicsSceneMouseEvent *event);
|
||
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
|
||
void hoverLeaveEvent (QGraphicsSceneHoverEvent * event);
|
||
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
|
||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
|
||
public:
|
||
|
||
void setID(int ID);
|
||
void setClicKHandle(ClickHandle click,void * other);
|
||
QPainterPath path;
|
||
private:
|
||
int ID;
|
||
ClickHandle click;
|
||
void * other;
|
||
QPen pen;
|
||
|
||
};
|
||
class MyGraphicsView : public QGraphicsView
|
||
{
|
||
public:
|
||
MyGraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr) : QGraphicsView(scene, parent) {}
|
||
|
||
protected:
|
||
void mousePressEvent(QMouseEvent *event) override
|
||
{
|
||
if (event->button() == Qt::LeftButton) {
|
||
QPointF scenePos = mapToScene(event->pos());
|
||
|
||
// 查找点击位置的 QGraphicsItem
|
||
QGraphicsItem *item = scene()->itemAt(scenePos, transform());
|
||
|
||
if (item) {
|
||
// 检查该项目是否是一个 QGraphicsItemGroup
|
||
QGraphicsItemGroup *group = dynamic_cast<QGraphicsItemGroup*>(item);
|
||
if (group) {
|
||
qDebug() << "QGraphicsItemGroup clicked";
|
||
|
||
// 在组内项目上改变颜色,表示被选中
|
||
for (QGraphicsItem *child : group->childItems()) {
|
||
QGraphicsRectItem *rectItem = dynamic_cast<QGraphicsRectItem*>(child);
|
||
if (rectItem) {
|
||
rectItem->setBrush(Qt::blue); // 将颜色设置为蓝色
|
||
}
|
||
}
|
||
} else {
|
||
qDebug() << "Other QGraphicsItem clicked";
|
||
}
|
||
} else {
|
||
qDebug() << "No item clicked";
|
||
}
|
||
}
|
||
|
||
// 调用基类的鼠标按下事件处理器
|
||
QGraphicsView::mousePressEvent(event);
|
||
}
|
||
};
|
||
|
||
class CRealTimeForm : public QWidget //, public MyItem2
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit CRealTimeForm(QWidget *parent = nullptr);
|
||
~CRealTimeForm();
|
||
|
||
private slots:
|
||
void on_graphView_customContextMenuRequested(const QPoint &pos);
|
||
|
||
void onTaskBoxContextMenuEvent();
|
||
void slotDoubleClick(QGraphicsSceneMouseEvent *event);
|
||
void on_addchannel(channelBaseInfo channelbaseInfo,QString str);
|
||
//网络请求数据响应
|
||
void slotNetMgr(QString sAddr,const QVariant& msg);
|
||
public slots:
|
||
// 接受服务器的数据槽函数
|
||
void slotRecieve();
|
||
void disConnect();
|
||
// 连接状态变化的时候触发
|
||
//void slotStateChanged(QAbstractSocket::SocketState state);
|
||
signals:
|
||
void sigWCStatus(QString&);
|
||
void sigDOStatus(QJsonArray&,QJsonArray&);
|
||
void sigRPM(QString&);
|
||
|
||
protected:
|
||
void keyPressEvent(QKeyEvent *event);
|
||
void mouseDoubleClickEvent(QMouseEvent* e);
|
||
|
||
private:
|
||
Ui::CRealTimeForm *ui;
|
||
QGraphicsScene* m_pGraphicsScene ;
|
||
QGraphicsScene* m_pGraphicsScene_2 ;
|
||
QAction *pActionVerticalLine ;
|
||
QAction *pActionHorizontalLine ;
|
||
QAction *pActionRect ;
|
||
QAction *pActionPixmap ;
|
||
QAction *pActionText ;
|
||
QAction *pActionRect2 ;
|
||
QAction *pActionEdit ;
|
||
QAction *pActionSave ;
|
||
QAction *pActionAlignmentX ;
|
||
QAction *pActionAlignmentY ;
|
||
int id1; //定时器1的唯一标示
|
||
int m_RealtimeLog;
|
||
int m_MachineStatus;//1 运行 0 停机
|
||
QByteArray m_arrayReady;
|
||
|
||
QMap<QString,QMap<int,QGraphicsItem*>> mapGrapics;
|
||
|
||
void InitChannelInfo();
|
||
void SetCurDrawType(QString strType);
|
||
void SetEdit();
|
||
void SetAlignmentX();
|
||
void SetAlignmentY();
|
||
void LoadGraphicsConfig(int type);//0 正常,1 编辑 2 停机
|
||
void SaveGraphicsConfig();
|
||
void UpdateCharacteristic(QVector<_Charateristic>& m_vecCharateristic);
|
||
void ParseCharacteristic(QJsonArray& arrayValue);
|
||
|
||
QTcpSocket *m_pSocket; // 客户端Socket对象
|
||
QString m_strServerIp; // 服务端IP
|
||
QList<QString> m_strReciveList; // 接受回来的数据放到链表里面
|
||
bool m_bConnected; // 连接的标志
|
||
int m_nConnectWaitTimes; // 连接的等待时间
|
||
int m_nServerPort; // 服务端端口号
|
||
int m_EditMode;
|
||
|
||
NetMgr *m_pNetMgr; //HTTP消息类对象
|
||
QVector<_Charateristic> m_vecCharateristic;
|
||
QString m_RPM;
|
||
//重写定时器的事件 虚函数 子类重写父类的虚函数
|
||
virtual void timerEvent(QTimerEvent *);
|
||
};
|
||
|
||
#endif // REALTIMEFORM_H
|