#ifndef DrawFrac_H #define DrawFrac_H #include #include #include #include #include #include class QMyCustomPlot; class QCustomPlot; class QCPItemCurve; class QCPItemTracer; class QCPItemLine; class QCPCurve; class QCPItemText; static const int iFracType2 = 15; typedef struct tagPOINTF { float x; float y; } POINTF; typedef struct { int iCode; QString csName; int iType; // 1:正弦曲线 2:连线 3:封闭区域 4:直线 QColor crColor; int nLineWidth; int bDraw; } FRAC_DEF_Crack; typedef struct { float DEP; float AorX; float XETAorH; float W; float DIPorS; float DIR; float TDIP; float TDIR; float ID; float NUM; POINTF point[16]; } FRAC_TABLE_Crack; typedef struct { float DEP; float AorX; float XETAorH; float W; float DIPorS; float DIR; float ID; } FRAC_TABLE_OLD_Crack; class DraggableCrackItem; class CPickFrac : public QObject { Q_OBJECT public: CPickFrac(QMyCustomPlot *widget, QString strSlfName, QString csCurve, int iMyWidth); virtual ~CPickFrac(); void setDraggingEnabled(bool enabled); QList getAllItems() const { return m_items; } bool saveToFile(); bool createNewCrack(double depth, QString type); void setShowHide(QString names, int nameShow, int angleShow, int digitCapacity); public: QMyCustomPlot *m_myCustomPlot; int m_iMyWidth = 0; QList m_FracDef; bool m_bTypeDraw[iFracType2]; void ReadFracDef(); void ReadData(QString strSlfName, QString csCurve); void drawOne(FRAC_TABLE_Crack frac, int iType, int nLineWidth, QColor crColor, QString csName); protected: bool eventFilter(QObject *watched, QEvent *event) override; private slots: void onRemoveCrackItem(DraggableCrackItem *item); private: QList m_items; bool m_draggingEnabled = true; QString m_currentSlfName; QString m_currentTableName; }; // ========== 可拖拽裂缝项 ========== class DraggableCrackItem : public QObject { Q_OBJECT public: enum Type { TypeA_Sine, TypeB_Polyline, TypeC_Closed }; DraggableCrackItem(QCustomPlot *plot, Type type, const QColor &color, int lineWidth); ~DraggableCrackItem(); void setSineData(double depth, double amplitude, double phase, double xScale, double width); void setPolylineData(const QVector &points, double scaleX = 1.0, double flipY = -1.0); void setClosedData(const QVector &points, double scaleX = 1.0, double flipY = -1.0); void setVisible(bool visible); void deactivate(); bool eventFilter(QObject *obj, QEvent *event) override; // 保存接口 Type getType() const { return m_type; } void setCrackCode(int code) { m_crackCode = code; } int getCrackCode() const { return m_crackCode; } double getDepthForSort() const; void getCurrentFracData(FRAC_TABLE_Crack &data) const; // 编辑模式 void startEditing(); void finishEditing(); // 控制所有拖拽点的可见性 void setDragPointsVisible(bool visible); // 获取当前激活的item(静态) static DraggableCrackItem* getActiveItem() { return s_activeItem; } void cleanupFromPlot(); // 立即从画布移除所有图形项 void setShow(bool flag); QString csName; float DIPorS; float DIR; void setLabelInfo(const QString &name, double angle = -1.0, double angle2 = -1.0, int digitCapacity = 2); // angle为-1时不显示角度 void updateLabelPosition(); QCPItemText *m_labelText = nullptr; signals: void dataChanged(); void removeMe(DraggableCrackItem* item); private: QCustomPlot *m_plot; Type m_type; QColor m_color; int m_lineWidth; int m_crackCode = 0; double m_originalXETAorH = 0.0; double m_originalAorX = 0.0; // 模式A QCPItemCurve *m_curve = nullptr; QCPItemTracer *m_tracer1 = nullptr, *m_tracer2 = nullptr; double m_orig_x1, m_orig_x2, m_orig_y1, m_orig_y2; double m_orig_startX, m_orig_endX, m_orig_startDirX, m_orig_startDirY, m_orig_endDirX, m_orig_endDirY; double m_offsetXA = 0.0, m_offsetYA = 0.0; double m_depth = 0.0, m_endX = 0.0; enum DragStateA { IdleA, DraggingPoint1, DraggingPoint2, DraggingCurveA } m_dragStateA = IdleA; QPointF m_lastDragPixelA; // 模式B struct LineItem { QCPItemLine *line; QCPItemTracer *startTracer, *endTracer; QPointF startOrig, endOrig; }; QList m_lines; double m_offsetXB = 0.0, m_offsetYB = 0.0; bool m_isAddingLine = false; QPointF m_tempPoint; enum DragStateB { IdleB, DraggingStartPoint, DraggingEndPoint, DraggingLineOverall } m_dragStateB = IdleB; QPointF m_bDragStart; int m_draggedLineIndex = -1; // 模式C QCPCurve *m_curveC = nullptr; QVector m_pointsC; QVector m_labelsC; double m_offsetXC = 0.0, m_offsetYC = 0.0; bool m_cFinished = false, m_cDragging = false, m_draggingPoint = false; int m_draggedPointIndex = -1; QPointF m_cDragStart; // 编辑模式标志 bool m_editingMode = false; // 删除安全标志 bool m_pendingDelete = false; // 辅助函数 void updateCurveFromTargets(); void updateCurvePosition(); void updateTracers(); void setTracerHighlight(QCPItemTracer *tracer, bool highlight); void updateLineEndpoints(LineItem &item); void updateLinesPosition(); void clearLines(); void updatePolylineC(bool closed); void clearPolylineC(); static QPointer s_activeItem; }; #endif