diff --git a/CMakeLists.txt b/CMakeLists.txt index dcf230d..4ca4c7b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,15 +53,19 @@ set(H_HEADER_FILES include/util/baseSelector.h include/util/creatingSelector.h include/util/movingSelector.h + include/util/subMovingSelector.h include/util/rotationSelector.h include/util/scalingSelector.h include/util/editingSelector.h include/util/connectingSelector.h + include/util/linkMovingSelector.h include/util/selectorManager.h include/graphicsDataModel/fixedPortsModel.h include/graphicsItem/itemControlHandle.h + include/graphicsItem/handleRect.h + include/graphicsItem/handleText.h include/graphicsItem/itemPort.h include/graphicsItem/graphicsBaseItem.h include/graphicsItem/graphicsRectItem.h @@ -119,15 +123,19 @@ set(CPP_SOURCE_FILES source/util/baseSelector.cpp source/util/creatingSelector.cpp source/util/movingSelector.cpp + source/util/subMovingSelector.cpp source/util/rotationSelector.cpp source/util/scalingSelector.cpp source/util/editingSelector.cpp source/util/connectingSelector.cpp + source/util/linkMovingSelector.cpp source/util/selectorManager.cpp source/graphicsDataModel/fixedPortsModel.cpp source/graphicsItem/itemControlHandle.cpp + source/graphicsItem/handleRect.cpp + source/graphicsItem/handleText.cpp source/graphicsItem/itemPort.cpp source/graphicsItem/graphicsBaseItem.cpp source/graphicsItem/graphicsRectItem.cpp diff --git a/CMakeLists.txt.user.180d0b4 b/CMakeLists.txt.user.180d0b4 new file mode 100644 index 0000000..f06aac5 --- /dev/null +++ b/CMakeLists.txt.user.180d0b4 @@ -0,0 +1,315 @@ + + + + + + EnvironmentId + {180d0b44-822c-4522-af92-2d8bdefcfe6f} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + 0 + false + true + false + 2 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + false + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 14 + true + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 6.7.2 MinGW 64-bit + Desktop Qt 6.7.2 MinGW 64-bit + qt.qt6.672.win64_mingw_kit + 0 + 0 + 0 + + Debug + 2 + false + + -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_BUILD_TYPE:STRING=Debug +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake + 0 + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug + + + + + all + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + CMakeProjectManager.CMakeBuildConfiguration + + + Release + 2 + false + + -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_BUILD_TYPE:STRING=Release +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Release + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + CMakeProjectManager.CMakeBuildConfiguration + + + Debug + 2 + false + + -DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_BUILD_TYPE:STRING=Debug +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake + D:\work\project\DiagramDesigner\DiagramDesigner + 0 + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug2 + CMakeProjectManager.CMakeBuildConfiguration + + 3 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + 0 + true + + 2 + + false + -e cpu-cycles --call-graph "dwarf,4096" -F 250 + DiagramDesigner + CMakeProjectManager.CMakeRunConfiguration.DiagramDesigner + DiagramDesigner + false + true + true + true + D:/work/project/DiagramDesigner/DiagramDesigner/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/x64/bin + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/CMakeLists.txt.user.f86a295 b/CMakeLists.txt.user.f86a295 new file mode 100644 index 0000000..0994d91 --- /dev/null +++ b/CMakeLists.txt.user.f86a295 @@ -0,0 +1,315 @@ + + + + + + EnvironmentId + {f86a2956-ea1a-464e-91b2-1ab824389029} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + 0 + false + true + false + 2 + true + true + 0 + 8 + true + false + 1 + true + true + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + false + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 14 + true + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop Qt 6.7.2 MinGW 64-bit + Desktop Qt 6.7.2 MinGW 64-bit + qt.qt6.672.win64_mingw_kit + 0 + 0 + 0 + + Debug + 2 + false + + -DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_BUILD_TYPE:STRING=Debug +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} + 0 + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug + + + + + all + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + 构建 + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug + CMakeProjectManager.CMakeBuildConfiguration + + + Release + 2 + false + + -DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_BUILD_TYPE:STRING=Release +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Release + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Release + CMakeProjectManager.CMakeBuildConfiguration + + + Debug + 2 + false + + -DCMAKE_GENERATOR:STRING=Ninja +-DCMAKE_PROJECT_INCLUDE_BEFORE:FILEPATH=%{BuildConfig:BuildDirectory:NativeFilePath}/.qtc/package-manager/auto-setup.cmake +-DCMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C} +-DCMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX} +-DCMAKE_CXX_FLAGS_INIT:STRING=%{Qt:QML_DEBUG_FLAG} +-DQT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable} +-DCMAKE_BUILD_TYPE:STRING=Debug +-DCMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx} + D:\work\project\DiagramDesigner\DiagramDesigner + 0 + D:\work\project\DiagramDesigner\DiagramDesigner\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug + + + + + all + + false + + true + CMakeProjectManager.MakeStep + + 1 + 构建 + 构建 + ProjectExplorer.BuildSteps.Build + + + + + + clean + + false + + true + CMakeProjectManager.MakeStep + + 1 + 清除 + 清除 + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Debug2 + CMakeProjectManager.CMakeBuildConfiguration + + 3 + + + 0 + 部署 + 部署 + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + 0 + true + + 2 + + false + -e cpu-cycles --call-graph "dwarf,4096" -F 250 + DiagramDesigner + CMakeProjectManager.CMakeRunConfiguration.DiagramDesigner + DiagramDesigner + false + true + true + true + D:/work/project/DiagramDesigner/DiagramDesigner/build/Desktop_Qt_6_7_2_MinGW_64_bit-Debug/x64/bin + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/include/global.h b/include/global.h index ea69cc9..ac89082 100644 --- a/include/global.h +++ b/include/global.h @@ -6,8 +6,8 @@ #include #include -const double g_dGriaphicsScene_Width = 600; -const double g_dGriaphicsScene_Height = 450; +const double g_dGriaphicsScene_Width = 800; +const double g_dGriaphicsScene_Height = 600; //Q_NAMESPACE enum GraphicsItemType @@ -22,7 +22,7 @@ enum GraphicsItemType GIT_bus = QGraphicsItem::UserType + 50, GIT_itemRect = QGraphicsItem::UserType + 51, GIT_itemTri = QGraphicsItem::UserType + 52, - GIT_connectLine = QGraphicsItem::UserType + 53 + GIT_link= QGraphicsItem::UserType + 53 }; //Q_ENUM_NS(GraphicsItemType) diff --git a/include/graphicsItem/electricConnectLineItem.h b/include/graphicsItem/electricConnectLineItem.h index ba61ac5..0a1ff42 100644 --- a/include/graphicsItem/electricConnectLineItem.h +++ b/include/graphicsItem/electricConnectLineItem.h @@ -9,9 +9,11 @@ struct Connection int nSrcNodeId; int nSrcPort; HandleType srcType; + PortPos srcPos; int nDestNodeId; int nDestPort; HandleType destType; + PortPos destPos; Connection() { @@ -19,19 +21,23 @@ struct Connection nSrcNodeId = -1; nSrcPort = -1; srcType = T_none; + srcPos = P_top; nDestNodeId = -1; nDestPort = -1; destType = T_none; + destPos = P_top; } - Connection(int nSNI,int nSP,HandleType sT,int nDNI,int nDP,HandleType dT) + Connection(int nSNI,int nSP,HandleType sT,PortPos sPOS,int nDNI,int nDP,HandleType dT,PortPos dPOS) { nSrcNodeId = nSNI; nSrcPort = nSP; srcType = sT; + srcPos = sPOS; nDestNodeId = nDNI; nDestPort = nDP; destType = dT; + destPos = dPOS; } bool operator==(Connection& obj) { @@ -45,9 +51,11 @@ struct Connection nSrcNodeId = obj.nSrcNodeId; nSrcPort = obj.nSrcPort; srcType = obj.srcType; + srcPos = obj.srcPos; nDestNodeId = obj.nDestNodeId; nDestPort = obj.nDestPort; destType = obj.destType; + destPos = obj.destPos; return *this; } }; @@ -58,23 +66,30 @@ public: ElectricConnectLineItem(QGraphicsItem *parent = 0); virtual ~ElectricConnectLineItem(); - void setStartPoint(const QPointF& p){m_pStart = p;} - void setEndPoint(const QPointF& p){m_pEnd = p;} + void setStartPoint(const QPointF& p); + void setEndPoint(const QPointF& p); QPainterPath getPoints(void) const { return m_points; } + void moveLine(QPointF); //鼠标点击拖动 void calculatePath(); bool addConnection(); + void resetCurLine(){_curLine = QPoint();} void updateConnection(int callerId,QPointF pos); //外部调用的更新函数,id为调用者id void setConnection(Connection con){m_connectState = con;} protected: - virtual QPainterPath shape(); - virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*); + virtual QPainterPath shape() const override; + virtual QRectF boundingRect() const override; + virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override; private: - Connection m_connectState; - QPainterPath m_points; - QPointF m_pStart; - QPointF m_pEnd; + Connection m_connectState; + QPainterPath m_points; + QPainterPath m_pointsBoundingRect; //包裹点的矩形集合 + QList m_lstPoints; + QPoint _curLine; //参数1用点序号表示的当前线段起始点,参数2表示线段方向 + + //QPointF m_pStart; + //QPointF m_pEnd; }; #endif diff --git a/include/graphicsItem/graphicsBaseItem.h b/include/graphicsItem/graphicsBaseItem.h index 51a7ba2..0482ca0 100644 --- a/include/graphicsItem/graphicsBaseItem.h +++ b/include/graphicsItem/graphicsBaseItem.h @@ -9,7 +9,7 @@ #include #include #include - +//#include "graphicsItem/itemPort.h" enum ShapeType { @@ -199,6 +199,21 @@ public: } } } + virtual void setHandleIfShow(int tag,bool bVisible) //是否参与显示判断 + { + foreach (int key, m_vecHanle.keys()) + { + ItemControlHandle* pHandle = m_vecHanle.value(key); + if(pHandle) + { + if(pHandle->getTag() == tag) + { + pHandle->setIfShow(bVisible); + break; + } + } + } + } virtual void setFunctionHandleIfShow(bool bVisible) //是否参与显示判断 { foreach (int key, m_vecHanle.keys()) @@ -398,6 +413,7 @@ public: virtual void syncRotationDataFromParent(const double&) {} virtual double getSyncRotationDataFromParent() {return m_dSyncRotationByParent;} virtual void setTouched(bool b){m_touched = b;} + virtual void setLastPoint(QPointF p) {m_lastPoint = p;} protected: ShapeType m_type; QPen m_pen; @@ -412,7 +428,7 @@ protected: QGraphicsPathItem* m_pOperationCopy; //图元移动和旋转时的操作副本 QPointF m_movingIniPos; //移动副本开始移动初始 - + QPointF m_lastPoint; //鼠标上次点击位置 QMap m_vecHanle; }; @@ -451,6 +467,7 @@ public: virtual void setLastPort(int n){_lastPort = n;} virtual int getLastPort() const {return _lastPort;} virtual ItemPort* getPortPtr(int) const; + virtual ItemControlHandle* getHandlePtr(int) const; virtual QMap& getPorts() {return m_mapPort;} protected: virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange, const QVariant&); diff --git a/include/graphicsItem/handleRect.h b/include/graphicsItem/handleRect.h new file mode 100644 index 0000000..0bc6787 --- /dev/null +++ b/include/graphicsItem/handleRect.h @@ -0,0 +1,19 @@ +#ifndef HANDLERECT_H +#define HANDLERECT_H + +#include "graphicsItem/itemControlHandle.h" + +class HandleRect : public ItemControlHandle +{ + Q_OBJECT +public: + HandleRect(QGraphicsItem *parent); + virtual ~HandleRect(); +protected: + virtual void hoverEnterEvent(QGraphicsSceneHoverEvent*) override; + virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override; + virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override; + virtual QRectF boundingRect() const override; +}; + +#endif diff --git a/include/graphicsItem/handleText.h b/include/graphicsItem/handleText.h new file mode 100644 index 0000000..a2ed54d --- /dev/null +++ b/include/graphicsItem/handleText.h @@ -0,0 +1,33 @@ +#ifndef HANDLETEXT_H +#define HANDLETEXT_H + +#include +#include "graphicsItem/itemControlHandle.h" + +int const TEXT_WIDTH = 30; +int const TEXT_HEIGHT = 15; + +class QGraphicsProxyWidget; + +class HandleText : public ItemControlHandle +{ + Q_OBJECT +public: + HandleText(QGraphicsItem *parent); + virtual ~HandleText(); + + void setText(QString); + void creatEditor(); //创建editor编辑文本 + void setEditable(bool b){_editable = b;} +protected: + virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override; + virtual QRectF boundingRect() const override; +private: + QString _text; + QFont _font; + QRectF _boundingRect; + QGraphicsProxyWidget* _proxy; + bool _editable; +}; + +#endif diff --git a/include/graphicsItem/itemControlHandle.h b/include/graphicsItem/itemControlHandle.h index aaf5be8..8379b97 100644 --- a/include/graphicsItem/itemControlHandle.h +++ b/include/graphicsItem/itemControlHandle.h @@ -9,6 +9,7 @@ enum HandleType T_resize, //调整大小 T_rotate, //旋转 T_editShape, //编辑形状 + T_text, //文本 T_lineIn, //入线口 T_lineOut, //出线口 T_lineInOut //双端线 @@ -30,6 +31,7 @@ enum HandleTag H_rotate_rightBottom, H_rotate_leftBottom, //12 H_edit, + H_textCaption = 40, //标题文本 H_connect = 50 //连接操作点从50开始,前面预留 }; @@ -43,7 +45,9 @@ enum PortPos const int HNDLE_SIZE = 8; -class ItemControlHandle : public QObject,public QGraphicsRectItem +class GraphicsBaseItem; + +class ItemControlHandle : public QObject,public QGraphicsItem { Q_OBJECT @@ -52,30 +56,33 @@ public: virtual ~ItemControlHandle(); public: + virtual int getSize(); + virtual void move(double, double); + void setType(HandleType ht) { m_type = ht; } HandleType getType() { return m_type; } void setTag(int ht) { m_tag = ht; } int getTag() { return m_tag; } - int getSize(); - void move(double, double); - void setEnable(bool b){m_enable = b;} bool enable(){return m_enable;} void setIfShow(bool b){m_ifShow = b;} bool ifShow(){return m_ifShow;} + + GraphicsBaseItem* getParentPtr() const {return _parent;} + void setParent(GraphicsBaseItem* p) {_parent = p;} protected: virtual void hoverEnterEvent(QGraphicsSceneHoverEvent*) override; virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent*) override; virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override; - protected: HandleType m_type; int m_tag; bool m_enable; bool m_ifShow; + GraphicsBaseItem* _parent; }; #endif diff --git a/include/graphicsItem/itemPort.h b/include/graphicsItem/itemPort.h index dfd9a66..eed5b09 100644 --- a/include/graphicsItem/itemPort.h +++ b/include/graphicsItem/itemPort.h @@ -1,11 +1,11 @@ #ifndef ITEMPORT_H #define ITEMPORT_H -#include "graphicsItem/itemControlHandle.h" +#include "graphicsItem/handleRect.h" class ElectricConnectLineItem; -class ItemPort : public ItemControlHandle +class ItemPort : public HandleRect { Q_OBJECT diff --git a/include/util/baseSelector.h b/include/util/baseSelector.h index 47237eb..06947b7 100644 --- a/include/util/baseSelector.h +++ b/include/util/baseSelector.h @@ -21,6 +21,8 @@ enum SelectorType ST_scaling, //缩放 ST_rotation, //旋转 ST_connecting, //连接 + ST_subMoving, //子对象移动 + ST_linkMoving, //连接线移动 }; enum OperationMode @@ -32,6 +34,8 @@ enum OperationMode OM_scale, //缩放 OM_rotate, //旋转 OM_connect, //连接 + OM_subMove,//子对象移动 + OM_linkMove, //连接线移动 }; class GraphicsBaseItem; diff --git a/include/util/linkMovingSelector.h b/include/util/linkMovingSelector.h new file mode 100644 index 0000000..2bcf67c --- /dev/null +++ b/include/util/linkMovingSelector.h @@ -0,0 +1,30 @@ +/** + *\file linkMovingSelector.h + * + *\brief 实现连接线移动的selector,与移动movingSelector作区分 + * + *\author by + */ + +#ifndef LINKMOVINGSELECTOR_H +#define LINKMOVINGSELECTOR_H + +#include "baseSelector.h" + +class ElectricConnectLineItem; + +class LinkMovingSelector : public BaseSelector +{ + Q_OBJECT +public: + explicit LinkMovingSelector(QObject *parent = 0); + virtual ~LinkMovingSelector(); +public: + void mousePressEvent(QGraphicsSceneMouseEvent*, DesignerScene*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*, DesignerScene*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*, DesignerScene*); +private: + ElectricConnectLineItem* m_pMovingLine; +}; + +#endif diff --git a/include/util/subMovingSelector.h b/include/util/subMovingSelector.h new file mode 100644 index 0000000..9b1eadd --- /dev/null +++ b/include/util/subMovingSelector.h @@ -0,0 +1,28 @@ +/** + *\file subMovingSelector.h + * + *\brief 实现子类图元移动的selector,与移动movingSelector作区分 + * + *\author by + */ + +#ifndef SUBMOVINGSELECTOR_H +#define SUBMOVINGSELECTOR_H + +#include "baseSelector.h" + +class SubMovingSelector : public BaseSelector +{ + Q_OBJECT +public: + explicit SubMovingSelector(QObject *parent = 0); + virtual ~SubMovingSelector(); +public: + void mousePressEvent(QGraphicsSceneMouseEvent*, DesignerScene*); + void mouseMoveEvent(QGraphicsSceneMouseEvent*, DesignerScene*); + void mouseReleaseEvent(QGraphicsSceneMouseEvent*, DesignerScene*); +private: + GraphicsBaseItem* m_pParentItem; +}; + +#endif diff --git a/source/designerScene.cpp b/source/designerScene.cpp index ecba920..d128d61 100644 --- a/source/designerScene.cpp +++ b/source/designerScene.cpp @@ -109,7 +109,7 @@ void DesignerScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* mouseEvent) update(); } else - QGraphicsScene::mouseReleaseEvent(mouseEvent); + QGraphicsScene::mouseDoubleClickEvent(mouseEvent); } void DesignerScene::keyPressEvent(QKeyEvent* event) diff --git a/source/graphicsItem/electricConnectLineItem.cpp b/source/graphicsItem/electricConnectLineItem.cpp index d05acbc..2cf814b 100644 --- a/source/graphicsItem/electricConnectLineItem.cpp +++ b/source/graphicsItem/electricConnectLineItem.cpp @@ -1,52 +1,384 @@ #include "graphicsItem/electricConnectLineItem.h" #include #include +#include ElectricConnectLineItem::ElectricConnectLineItem(QGraphicsItem *parent) : GraphicsBaseItem(parent) { + m_boundingRect = QRectF(); m_pen = QPen(Qt::black); m_brush = QBrush(Qt::NoBrush); setHandleVisible(false); setFunctionHandleIfShow(false); setFunctionHandleEnaable(false); + m_lstPoints.push_back(QPointF()); //起点 + m_lstPoints.push_back(QPointF()); //终点 + _curLine = QPoint(); } ElectricConnectLineItem::~ElectricConnectLineItem() { } -QPainterPath ElectricConnectLineItem::shape() +void ElectricConnectLineItem::setStartPoint(const QPointF& p) +{ + int n = m_lstPoints.size(); + if(n) + { + if(n >2) + { + if(m_lstPoints[0].x() == m_lstPoints[1].x()) //相邻点在垂直方向,水平移动,否则垂直移动 + { + m_lstPoints[1].setX(p.x()); + } + else + { + m_lstPoints[1].setY(p.y()); + } + } + m_lstPoints[0] = p; + } +} +void ElectricConnectLineItem::setEndPoint(const QPointF& p) +{ + int n = m_lstPoints.size(); + if(n) + { + if(n >2) + { + if(m_lstPoints[n-1].x() == m_lstPoints[n-2].x()) //相邻点在垂直方向,水平移动,否则垂直移动 + { + m_lstPoints[n-2].setX(p.x()); + } + else + { + m_lstPoints[n-2].setY(p.y()); + } + } + m_lstPoints[n-1] = p; + } +} + +QPainterPath ElectricConnectLineItem::shape() const { QPainterPath path; - path.addPath(m_points); - path.closeSubpath(); //将路径闭合 + //path.addPath(m_points); + path.addPath(m_pointsBoundingRect); return path; } +QRectF ElectricConnectLineItem::boundingRect() const +{ + return m_boundingRect; +} + void ElectricConnectLineItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { - painter->setPen(m_pen); + if (option->state & QStyle::State_Selected) + { + painter->setPen(Qt::red); + } + else + { + painter->setPen(m_pen); + } + painter->setBrush(m_brush); painter->drawPath(m_points); } +void ElectricConnectLineItem::moveLine(QPointF pos) +{ + QPointF delta = pos - m_lastPoint; + if(_curLine.isNull()) + { + if(m_lstPoints.size() > 1) + { + int n = m_lstPoints.size(); + for(int i = 0;i < m_lstPoints.size() -1;++i) + { + QPointF p1 = m_lstPoints[i]; + QPointF p2 = m_lstPoints[i+1]; + + if(p1.x() == p2.x() && abs(p1.x() - pos.x()) < 2) //在相同竖线 + { + if(p1.y() > p2.y()) + { + if(pos.y() > p2.y() && pos.y() < p1.y()) //在竖线上 + { + + if(i == 0 ) //起点是连接点,创建复制点 + { + m_lstPoints.prepend(p1); + m_lstPoints.prepend(p1); + + i+=2; + if(m_lstPoints.size() < 6) //补齐后端 + { + m_lstPoints.append(m_lstPoints.last()); + } + } + if(i+1 == n-1) //终点是连接点,创建复制点 + { + m_lstPoints.append(p2); + m_lstPoints.append(p2); + + m_lstPoints[i+1] = p2; + if(m_lstPoints.size() < 6) //补齐前端,移动后至少6个点 + { + m_lstPoints.prepend(m_lstPoints.first()); + i+=1; + } + } + //qDebug() << 1 <<" "< p1.y()) //在竖线上 + { + + if(i == 0) //起点是连接点,创建复制点 + { + m_lstPoints.prepend(p1); + m_lstPoints.prepend(p1); + i+=2; + if(m_lstPoints.size() < 6) //补齐后端 + { + m_lstPoints.append(m_lstPoints.last()); + } + } + if(i+1 == n-1) //终点是连接点,创建复制点 + { + m_lstPoints.append(p2); + m_lstPoints.append(p2); + + m_lstPoints[i+1] = p2; + if(m_lstPoints.size() < 6) //补齐前端 + { + m_lstPoints.prepend(m_lstPoints.first()); + i+=1; + } + } + //qDebug() << 2 <<" "< p2.x()) + { + if(pos.x() > p2.x() && pos.x() < p1.x()) + { + + if(i == 0) //起点是连接点,创建复制点 + { + m_lstPoints.prepend(p1); + m_lstPoints.prepend(p1); + + i+=2; + if(m_lstPoints.size() < 6) //补齐后端 + { + m_lstPoints.append(m_lstPoints.last()); + } + } + if(i+1 == n-1) //终点是连接点,创建复制点 + { + m_lstPoints.append(p2); + m_lstPoints.append(p2); + + if(m_lstPoints.size() < 6) //补齐前端,移动后至少6个点 + { + m_lstPoints.prepend(m_lstPoints.first()); + i+=1; + } + } + //qDebug() << 3 <<" "< p1.x()) + { + + if(i == 0) //起点是连接点,创建复制点 + { + m_lstPoints.prepend(p1); + m_lstPoints.prepend(p1); + + i+=2; + m_lstPoints[i] = p1; + if(m_lstPoints.size() < 6) //补齐后端 + { + m_lstPoints.append(m_lstPoints.last()); + } + } + if(i+1 == n-1) //终点是连接点,创建复制点 + { + m_lstPoints.append(p2); + m_lstPoints.append(p2); + + if(m_lstPoints.size() < 6) //补齐前端 + { + m_lstPoints.prepend(m_lstPoints.first()); + i+=1; + } + } + //qDebug() << 4 <<" "< 3) + { + + if(_curLine.y() == 1) //竖线 + { + if(n == 0) //起点是原点 + { + QPointF p(0,delta.y()); //原点不动 + m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只竖直移动 + m_lstPoints[n +2] = m_lstPoints[n +2] + p; //第三个点只竖直移动 + } + else if(n == 1) + { + QPointF p(delta.x(),0); + m_lstPoints[n] += p; //第一个点只水平移动 + m_lstPoints[n +1] += p; //第二个点只水平移动 + } + else if(n == m_lstPoints.size()-2) //起点是倒数第二个点 + { + QPointF p(0,delta.y()); + m_lstPoints[n] += p; //起点只上下移动 + m_lstPoints[n-1] += p; //上个点只竖直移动 + } + else if(n == m_lstPoints.size()-3) //起点是倒数第三个点 + { + QPointF py(0,delta.y()); + QPointF px(delta.x(),0); + m_lstPoints[n-1] += py; //上个点只垂直移动 + m_lstPoints[n] += delta; //起点任意动 + m_lstPoints[n+1] += px; //第二个点只水平移动 + } + else + { + m_lstPoints[n -1].setY(m_lstPoints[n ].y()); + m_lstPoints[n] += delta; + m_lstPoints[n +1] += delta; + m_lstPoints[n +2].setY(m_lstPoints[n +1].y()); + } + } + else //横线 + { + if(n == 0) //起点是原点 + { + QPointF p(delta.x(),0); //原点不动 + m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只水平移动 + m_lstPoints[n +2] = m_lstPoints[n +2] + p; //第三个点只水平移动 + } + else if(n == 1) + { + QPointF p(0,delta.y()); + m_lstPoints[n] = m_lstPoints[n] + p; //第一个点只竖直移动 + m_lstPoints[n +1] = m_lstPoints[n +1] + p; //第二个点只竖直移动 + } + else if(n == m_lstPoints.size()-2) //起点是倒数第二个点 + { + QPointF p(delta.x(),0); + m_lstPoints[n] += p; //起点水平移动 + m_lstPoints[n-1] += p; //上个点只水平移动 + } + else if(n == m_lstPoints.size()-3) //起点是倒数第三个点 + { + QPointF py(0,delta.y()); + QPointF px(delta.x(),0); + m_lstPoints[n-1] += px; //上个点只水平移动 + m_lstPoints[n] += delta; //起点任意动 + m_lstPoints[n+1] += py; //第二个点只上下移动 + } + else + { + m_lstPoints[n -1].setX(m_lstPoints[n].x()); + m_lstPoints[n] += delta; + m_lstPoints[n +1] += delta; + m_lstPoints[n +2].setX(m_lstPoints[n +1].x()); + } + } + } + + calculatePath(); + } + + m_lastPoint = pos; +} void ElectricConnectLineItem::calculatePath() { + int n = m_lstPoints.size(); + /*for(int i = 0;i < n-1;++i) + { + if(m_lstPoints[i] == m_lstPoints[i+1]) + { + if(i == 0) //头重复去除一个 + { + m_lstPoints.removeAt(i+1); + } + else if((i+1 == n-1)) //尾重复去除一个 + { + m_lstPoints.removeAt(i+1); + } + else + { + m_lstPoints.removeAt(i+1); + m_lstPoints.removeAt(i); + } + qDebug()<<"reset"; + resetCurLine(); + break; + } + }*/ prepareGeometryChange(); m_points.clear(); + m_pointsBoundingRect.clear(); - m_points.moveTo(m_pStart); - m_points.lineTo(m_pEnd.x(),m_pStart.y()); - m_points.lineTo(m_pEnd); + if(m_lstPoints.size() == 2 && (m_lstPoints.first().x() != m_lstPoints.last().x()) && (m_lstPoints.first().y() != m_lstPoints.last().y())) + { + if(m_lstPoints.first().y() < m_lstPoints.last().y()) + m_lstPoints.insert(1,QPointF(m_lstPoints.first().x(),m_lstPoints.last().y())); + else + m_lstPoints.insert(1,QPointF(m_lstPoints.last().x(),m_lstPoints.first().y())); + } + m_points.moveTo(m_lstPoints.first()); + QPointF pLast = m_lstPoints.first(); + for(int i = 1;i @@ -13,19 +15,27 @@ GraphicsBaseItem::GraphicsBaseItem(QGraphicsItem *parent) //m_vecHanle.reserve(H_left); for(int i = H_leftTop; i <= H_left; i++) { - ItemControlHandle* pHandle = new ItemControlHandle(this); + ItemControlHandle* pHandle = new HandleRect(this); pHandle->setType(T_resize); pHandle->setTag(i); m_vecHanle.insert(i-1,pHandle); } for(int i = H_rotate_leftTop; i <= H_rotate_leftBottom; i++) { - ItemControlHandle* pHandle = new ItemControlHandle(this); + ItemControlHandle* pHandle = new HandleRect(this); pHandle->setType(T_rotate); pHandle->setTag(i); m_vecHanle.insert(i-1,pHandle); } + HandleText* pHandle = new HandleText(this); + pHandle->setType(T_text); + pHandle->setTag(H_textCaption); + pHandle->setText(QString::number(123456)); + pHandle->setPos(30,-20); + pHandle->setParent(this); + m_vecHanle.insert(H_textCaption,pHandle); + setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); @@ -163,3 +173,14 @@ ItemPort* GraphicsBaseItem::getPortPtr(int n) const } return nullptr; } + +ItemControlHandle* GraphicsBaseItem::getHandlePtr(int n) const +{ + for(auto iter:m_vecHanle) + { + int tag = iter->getTag(); + if(tag == n) + return iter; + } + return nullptr; +} diff --git a/source/graphicsItem/graphicsItemGroup.cpp b/source/graphicsItem/graphicsItemGroup.cpp index b09c2e4..72cbfec 100644 --- a/source/graphicsItem/graphicsItemGroup.cpp +++ b/source/graphicsItem/graphicsItemGroup.cpp @@ -1,4 +1,5 @@ #include "graphicsItem/graphicsItemGroup.h" +#include "graphicsItem/handleRect.h" #include #include #include @@ -17,14 +18,14 @@ GraphicsItemGroup::GraphicsItemGroup(QGraphicsItem *parent) //m_vecHanle.reserve(H_left); for(int i = H_leftTop; i <= H_left; i++) { - ItemControlHandle* pHandle = new ItemControlHandle(this); + ItemControlHandle* pHandle = new HandleRect(this); pHandle->setType(T_resize); pHandle->setTag(i); m_vecHanle.insert(i-1,pHandle); } for(int i = H_rotate_leftTop; i <= H_rotate_leftBottom; i++) { - ItemControlHandle* pHandle = new ItemControlHandle(this); + ItemControlHandle* pHandle = new HandleRect(this); pHandle->setType(T_rotate); pHandle->setTag(i); m_vecHanle.insert(i-1,pHandle); diff --git a/source/graphicsItem/graphicsPolygonItem.cpp b/source/graphicsItem/graphicsPolygonItem.cpp index f8538bb..d5c2719 100644 --- a/source/graphicsItem/graphicsPolygonItem.cpp +++ b/source/graphicsItem/graphicsPolygonItem.cpp @@ -1,6 +1,6 @@ #include "graphicsItem/graphicsPolygonItem.h" -#include "graphicsItem/itemControlHandle.h" - +//#include "graphicsItem/itemControlHandle.h" +#include "graphicsItem/handleRect.h" #include #include @@ -138,7 +138,7 @@ void GraphicPolygonItem::addPoint(const QPointF& point) m_points.append(mapFromScene(point)); int nCount = m_points.count(); //每个顶点都可以编辑,所以一个顶点需要编辑handle - ItemControlHandle* pHandle = new ItemControlHandle(this); + ItemControlHandle* pHandle = new HandleRect(this); pHandle->setType(T_editShape); pHandle->setTag(H_edit + nCount - 1); m_vecHanle.insert(H_edit + nCount - 1,pHandle); diff --git a/source/graphicsItem/graphicsRectItem.cpp b/source/graphicsItem/graphicsRectItem.cpp index 5121cea..04b341f 100644 --- a/source/graphicsItem/graphicsRectItem.cpp +++ b/source/graphicsItem/graphicsRectItem.cpp @@ -1,6 +1,6 @@ #include "graphicsItem/graphicsRectItem.h" -#include "graphicsItem/itemControlHandle.h" - +//#include "graphicsItem/itemControlHandle.h" +#include "graphicsItem/handleRect.h" #include #include @@ -17,12 +17,12 @@ GraphicsRectItem::GraphicsRectItem(const QRect &rect, bool isRound, QGraphicsIte if (m_bIsRound) //圆角矩形添加两个圆角大小控制点 { //横轴X控制点 - ItemControlHandle* pHandle1 = new ItemControlHandle(this); + ItemControlHandle* pHandle1 = new HandleRect(this); pHandle1->setType(T_editShape); pHandle1->setTag(H_edit); m_vecHanle.insert(H_edit,pHandle1); //纵轴Y控制点 - ItemControlHandle* pHandle2 = new ItemControlHandle(this); + ItemControlHandle* pHandle2 = new HandleRect(this); pHandle2->setType(T_editShape); pHandle2->setTag(H_edit+1); m_vecHanle.insert(H_edit+1,pHandle2); diff --git a/source/graphicsItem/handleRect.cpp b/source/graphicsItem/handleRect.cpp new file mode 100644 index 0000000..1f803af --- /dev/null +++ b/source/graphicsItem/handleRect.cpp @@ -0,0 +1,69 @@ +#include "graphicsItem/handleRect.h" +#include + +HandleRect::HandleRect(QGraphicsItem *parent) + :ItemControlHandle(parent) +{ + +} + +HandleRect::~HandleRect() +{ +} + +QRectF HandleRect::boundingRect() const +{ + // 设置边界矩形,这里我们返回一个从(10,10)开始,宽度为100,高度为50的矩形 + return QRectF(-HNDLE_SIZE / 2, + -HNDLE_SIZE / 2, + HNDLE_SIZE, + HNDLE_SIZE); +} + +void HandleRect::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) + + painter->setPen(Qt::SolidLine); + painter->setRenderHint(QPainter::Antialiasing, true); + + if(m_type==T_resize) + { + painter->setBrush(Qt::white); + painter->drawRect(boundingRect()); + } + else if(m_type==T_rotate) + { + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } + else if(m_type==T_editShape) + { + painter->setBrush(Qt::green); + painter->drawEllipse(boundingRect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); + } + else if(m_type==T_lineIn) + { + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::green); + painter->drawEllipse(boundingRect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); + } + else if(m_type==T_lineOut) + { + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::red); + painter->drawEllipse(boundingRect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); + } +} + +void HandleRect::hoverEnterEvent(QGraphicsSceneHoverEvent* event) +{ + ItemControlHandle::hoverEnterEvent(event); +} + +void HandleRect::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) +{ + ItemControlHandle::hoverLeaveEvent(event); +} diff --git a/source/graphicsItem/handleText.cpp b/source/graphicsItem/handleText.cpp new file mode 100644 index 0000000..bd67df2 --- /dev/null +++ b/source/graphicsItem/handleText.cpp @@ -0,0 +1,68 @@ +#include "graphicsItem/handleText.h" +#include +#include +#include + +HandleText::HandleText(QGraphicsItem *parent) + : ItemControlHandle(parent) + ,_font(QFont()) + ,_proxy(nullptr) +{ + _editable = true; + _boundingRect = QRectF(-TEXT_WIDTH*0.5,-TEXT_WIDTH*0.5,TEXT_WIDTH,TEXT_HEIGHT); +} + +HandleText::~HandleText() +{ +} + +void HandleText::setText(QString str) +{ + _text = str; + QFontMetrics fontMetrics(_font); + _boundingRect = fontMetrics.boundingRect(_text); + int w = _boundingRect.width(); + int h = _boundingRect.height(); + _boundingRect.moveTo(QPointF(-w*0.5,-h*0.5)); +} + +void HandleText::creatEditor() +{ + if(!_editable) + return; + if(!_proxy) + _proxy = new QGraphicsProxyWidget(this); + _proxy->setVisible(true); + QLineEdit *editor = new QLineEdit(); + editor->setText(_text); + editor->resize(boundingRect().size().toSize()*1.5); + _proxy->setWidget(editor); + + // 将文本编辑的更改连接到文本项 + connect(editor, &QLineEdit::textChanged, this, [this, editor]() { + //_text = editor->text(); + setText(editor->text()); + }); + + // 完成编辑后,删除QTextEdit + connect(editor, &QLineEdit::editingFinished, this, [this, editor]() { + //_text = editor->text(); + setText(editor->text()); + _proxy->setVisible(false); + }); +} + +void HandleText::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + painter->setPen(Qt::blue); + painter->setBrush(Qt::blue); + painter->setRenderHint(QPainter::TextAntialiasing, true); + int w = _boundingRect.width(); + int h = _boundingRect.height(); + painter->drawText(QPointF(-w*0.5,h*0.5),_text); +} + +QRectF HandleText::boundingRect() const +{ + return _boundingRect; +} diff --git a/source/graphicsItem/itemControlHandle.cpp b/source/graphicsItem/itemControlHandle.cpp index 99bead1..f5f83ec 100644 --- a/source/graphicsItem/itemControlHandle.cpp +++ b/source/graphicsItem/itemControlHandle.cpp @@ -1,11 +1,10 @@ #include "graphicsItem/itemControlHandle.h" +#include "graphicsItem/graphicsBaseItem.h" #include ItemControlHandle::ItemControlHandle(QGraphicsItem *parent) - : QGraphicsRectItem(-HNDLE_SIZE / 2, - -HNDLE_SIZE / 2, - HNDLE_SIZE, - HNDLE_SIZE, parent) + : QGraphicsItem(parent) + ,_parent(nullptr) { m_type = T_resize; m_tag = H_none; @@ -21,48 +20,16 @@ void ItemControlHandle::paint(QPainter* painter, const QStyleOptionGraphicsItem* { Q_UNUSED(option) Q_UNUSED(widget) - - painter->setPen(Qt::SolidLine); - painter->setRenderHint(QPainter::Antialiasing, true); - - if(m_type==T_resize) - { - painter->setBrush(Qt::white); - painter->drawRect(rect()); - } - else if(m_type==T_rotate) - { - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::NoBrush); - painter->drawRect(rect()); - } - else if(m_type==T_editShape) - { - painter->setBrush(Qt::green); - painter->drawEllipse(rect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); - } - else if(m_type==T_lineIn) - { - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::green); - painter->drawEllipse(rect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); - } - else if(m_type==T_lineOut) - { - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::red); - painter->drawEllipse(rect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); - } } void ItemControlHandle::hoverEnterEvent(QGraphicsSceneHoverEvent* event) { - QGraphicsRectItem::hoverEnterEvent(event); + QGraphicsItem::hoverEnterEvent(event); } void ItemControlHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) { - QGraphicsRectItem::hoverLeaveEvent(event); + QGraphicsItem::hoverLeaveEvent(event); } int ItemControlHandle::getSize() diff --git a/source/graphicsItem/itemPort.cpp b/source/graphicsItem/itemPort.cpp index a67d4f8..d3e92a2 100644 --- a/source/graphicsItem/itemPort.cpp +++ b/source/graphicsItem/itemPort.cpp @@ -1,8 +1,9 @@ #include #include "graphicsItem/itemPort.h" +#include "graphicsItem/handleRect.h" ItemPort::ItemPort(QGraphicsItem *parent) - : ItemControlHandle(parent) + : HandleRect(parent) { _pos = P_top; _ptr = nullptr; @@ -21,7 +22,7 @@ void ItemPort::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, painter->setRenderHint(QPainter::Antialiasing, true); QPolygonF Triangle; - QRectF rec = rect(); + QRectF rec = boundingRect(); qreal left = rec.left(); qreal right = rec.right(); qreal width = rec.width(); @@ -58,20 +59,6 @@ void ItemPort::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, } else if(m_type == T_lineInOut) { - painter->drawEllipse(rect().center(), HNDLE_SIZE / 4, HNDLE_SIZE / 4); + painter->drawEllipse(boundingRect().center(), HNDLE_SIZE / 4, HNDLE_SIZE / 4); } - - /*if(m_type==T_lineIn) - { - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::green); - painter->drawEllipse(rect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); - } - else if(m_type==T_lineOut) - { - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::red); - painter->drawEllipse(rect().center(), HNDLE_SIZE / 2, HNDLE_SIZE / 2); - }*/ - //ItemControlHandle::paint(painter,option,widget); } diff --git a/source/util/baseSelector.cpp b/source/util/baseSelector.cpp index 3630c22..e9f0a07 100644 --- a/source/util/baseSelector.cpp +++ b/source/util/baseSelector.cpp @@ -1,8 +1,11 @@ #include "util/baseSelector.h" #include #include -#include #include +#include "graphicsItem/graphicsBaseItem.h" +#include "graphicsItem/handleText.h" + + QPointF BaseSelector::ms_ptMouseDown(0.0,0.0); QPointF BaseSelector::ms_ptMouseLast(0.0,0.0); @@ -39,48 +42,58 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen QList items = scene->selectedItems(); if (items.count() == 1) //只有一个选中 { - AbstractShape* item = qgraphicsitem_cast(items.first()); + GraphicsBaseItem* item = qgraphicsitem_cast(items.first()); if(item) { - //需要增加当前是否点击在控制点的判断函数 - ms_nDragHandle = item->collidesWithHandle(event->scenePos()); - if(ms_nDragHandle != H_none && ms_nDragHandle <= H_left) //在缩放控制点上 + GraphicsItemType tpe = item->getItemType(); + if(tpe == GIT_link) //对象是连接线 { - m_opMode = OM_scale; - emit setWorkingSelector(ST_scaling); - } - else if(ms_nDragHandle >= H_rotate_leftTop && ms_nDragHandle <= H_rotate_leftBottom) //在旋转控制点上 - { - m_opMode = OM_rotate; - //计算夹角 - QPointF originPoint = item->mapToScene(item->boundingRect().center()); - double dLengthY = ms_ptMouseLast.y() - originPoint.y(); - double dLengthX = ms_ptMouseLast.x() - originPoint.x(); - ms_dAngleMouseDownToItem = atan2(dLengthY, dLengthX) * 180 / M_PI; - // if(atan2(dLengthY, dLengthX) < 0) - // ms_dAngleMouseDownToItem += 360.0; - //创建副本 - item->createOperationCopy(); - emit setWorkingSelector(ST_rotation); - } - else if(ms_nDragHandle > H_rotate_leftBottom && ms_nDragHandle < H_connect) //编辑控制点上 - { - m_opMode = OM_edit; - setCursor(scene, Qt::ClosedHandCursor); - emit setWorkingSelector(ST_editing); - } - else if(ms_nDragHandle >= H_connect ) //连接控制点 - { - m_opMode = OM_connect; - //setCursor(scene, Qt::ClosedHandCursor); + m_opMode = OM_linkMove; setCursor(scene, Qt::ArrowCursor); - emit setWorkingSelector(ST_connecting); + emit setWorkingSelector(ST_linkMoving); } else { - m_opMode = OM_move; - setCursor(scene, Qt::ClosedHandCursor); - emit setWorkingSelector(ST_moving); + //需要增加当前是否点击在控制点的判断函数 + ms_nDragHandle = item->collidesWithHandle(event->scenePos()); + if(ms_nDragHandle != H_none && ms_nDragHandle <= H_left) //在缩放控制点上 + { + m_opMode = OM_scale; + emit setWorkingSelector(ST_scaling); + } + else if(ms_nDragHandle >= H_rotate_leftTop && ms_nDragHandle <= H_rotate_leftBottom) //在旋转控制点上 + { + m_opMode = OM_rotate; + //计算夹角 + QPointF originPoint = item->mapToScene(item->boundingRect().center()); + double dLengthY = ms_ptMouseLast.y() - originPoint.y(); + double dLengthX = ms_ptMouseLast.x() - originPoint.x(); + ms_dAngleMouseDownToItem = atan2(dLengthY, dLengthX) * 180 / M_PI; + // if(atan2(dLengthY, dLengthX) < 0) + // ms_dAngleMouseDownToItem += 360.0; + //创建副本 + item->createOperationCopy(); + emit setWorkingSelector(ST_rotation); + } + else if(ms_nDragHandle > H_rotate_leftBottom && ms_nDragHandle < H_textCaption) //编辑控制点上 + { + m_opMode = OM_edit; + setCursor(scene, Qt::ClosedHandCursor); + emit setWorkingSelector(ST_editing); + } + else if(ms_nDragHandle >= H_connect ) //连接控制点 + { + m_opMode = OM_connect; + //setCursor(scene, Qt::ClosedHandCursor); + setCursor(scene, Qt::ArrowCursor); + emit setWorkingSelector(ST_connecting); + } + else + { + m_opMode = OM_move; + setCursor(scene, Qt::ClosedHandCursor); + emit setWorkingSelector(ST_moving); + } } } } @@ -90,13 +103,36 @@ void BaseSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScen emit setWorkingSelector(ST_moving); setCursor(scene, Qt::ClosedHandCursor); } + else if(items.count() == 0) //单独移动子类 + { + QList items = scene->items(ms_ptMouseLast); + if (items.count() == 1) + { + ItemControlHandle* pHandle = qgraphicsitem_cast(items.first()); + if(pHandle) + { + //GraphicsBaseItem* item = pHandle->getParentPtr(); + ms_nDragHandle = pHandle->getTag(); + //ms_nDragHandle = item->collidesWithHandle(event->scenePos()); + if(ms_nDragHandle >= H_textCaption && ms_nDragHandle < H_connect) //移动文本 + { + m_opMode = OM_subMove; + setCursor(scene, Qt::ClosedHandCursor); + emit setWorkingSelector(ST_subMoving); + } + } + } + } if(m_opMode == OM_move) //可以多个选中同时移动 { for(int n = 0; n < items.size(); n++) { //创建副本 - AbstractShape* item = qgraphicsitem_cast(items.at(n)); + GraphicsBaseItem* item = qgraphicsitem_cast(items.at(n)); + GraphicsItemType tpe = item->getItemType(); + if(tpe == GIT_link) + continue; item->createOperationCopy(); } } @@ -303,8 +339,30 @@ void BaseSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerSc void BaseSelector::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) { - Q_UNUSED(event) - Q_UNUSED(scene) + QList items = scene->selectedItems(); + if(items.count() == 0) + { + QList items = scene->items(ms_ptMouseLast); + if (items.count() == 1) + { + ItemControlHandle* pHandle = qgraphicsitem_cast(items.first()); + if(pHandle) + { + ms_nDragHandle = pHandle->getTag(); + //ms_nDragHandle = item->collidesWithHandle(event->scenePos()); + if(ms_nDragHandle >= H_textCaption && ms_nDragHandle < H_connect) //移动文本 + { + HandleText* pText = qgraphicsitem_cast(pHandle); + if(pText) + { + //m_opMode = OM_subMove; + //emit setWorkingSelector(ST_subMoving); + pText->creatEditor(); + } + } + } + } + } } void BaseSelector::setCursor(DesignerScene *scene, const QCursor &cursor) diff --git a/source/util/connectingSelector.cpp b/source/util/connectingSelector.cpp index 980abfe..78e14ad 100644 --- a/source/util/connectingSelector.cpp +++ b/source/util/connectingSelector.cpp @@ -90,13 +90,14 @@ void ConnectingSelector::createConnectLline(GraphicsBaseItem* connectingItem,Gra { ElectricConnectLineItem* pItem = new ElectricConnectLineItem(); pItem->setItemId(++_Id); - pItem->setItemType(GIT_connectLine); + pItem->setItemType(GIT_link); scene->addItem(pItem); ItemPort* ptSrc = connectingItem->getPortPtr(ms_nDragHandle); int srcId = connectingItem->itemId(); int srcPort = ptSrc->getTag(); HandleType srcType = ptSrc->getType(); + PortPos srcPos = ptSrc->portPos(); pItem->setStartPoint(ptSrc->scenePos()); ptSrc->setConnect(pItem); @@ -122,9 +123,10 @@ void ConnectingSelector::createConnectLline(GraphicsBaseItem* connectingItem,Gra int destId = touchedItem->itemId(); int destPort = ptDest->getTag(); HandleType destType = ptDest->getType(); + PortPos destPos = ptDest->portPos(); pItem->calculatePath(); - pItem->setConnection(Connection(srcId,srcPort,srcType,destId,destPort,destType)); + pItem->setConnection(Connection(srcId,srcPort,srcType,srcPos,destId,destPort,destType,destPos)); ptDest->setConnect(pItem); m_graphicsItem[sceneName()].insert(QString::number(pItem->itemId()),pItem); //插入连接线到总表 @@ -142,14 +144,20 @@ void ConnectingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, Designe GraphicsBaseItem* item = qgraphicsitem_cast(items.first()); if(item) { - if(ms_nDragHandle != H_none) + if(ms_nDragHandle >= H_connect) { - item->setState(S_prepareConnect); - QPointF start = item->mapFromScene(ms_ptMouseDown); - QPointF end = item->mapFromScene(ms_ptMouseLast); - item->setBeginConnectPos(start); - item->setEndConnectPos(end); - m_pConnectingItem = item; + ItemPort* pt = item->getPortPtr(ms_nDragHandle); + if(pt) + { + if(pt->connected()) //只判断未连接的port + return; + item->setState(S_prepareConnect); + QPointF start = item->mapFromScene(ms_ptMouseDown); + QPointF end = item->mapFromScene(ms_ptMouseLast); + item->setBeginConnectPos(start); + item->setEndConnectPos(end); + m_pConnectingItem = item; + } } } } diff --git a/source/util/creatingSelector.cpp b/source/util/creatingSelector.cpp index 1efdf9b..01f2921 100644 --- a/source/util/creatingSelector.cpp +++ b/source/util/creatingSelector.cpp @@ -68,7 +68,7 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer case GIT_itemRect: { m_creatingMethod = CM_click; - m_pCreatingItem = new ElectricSvgItemRect(QRect(-20, -20, 40, 40)); + m_pCreatingItem = new ElectricSvgItemRect(QRect(-15, -15, 30, 30)); m_pCreatingItem->setItemId(++_Id); m_pCreatingItem->setItemType(GIT_itemRect); m_pCreatingItem->editShape(ms_nDragHandle, ms_ptMouseLast); @@ -78,7 +78,7 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer case GIT_itemTri: { m_creatingMethod = CM_click; - m_pCreatingItem = new ElectricSvgItemTriangle(QRect(-20, -20, 40, 40)); + m_pCreatingItem = new ElectricSvgItemTriangle(QRect(-15, -15, 30, 30)); m_pCreatingItem->setItemId(++_Id); m_pCreatingItem->setItemType(GIT_itemTri); m_pCreatingItem->editShape(ms_nDragHandle, ms_ptMouseLast); diff --git a/source/util/linkMovingSelector.cpp b/source/util/linkMovingSelector.cpp new file mode 100644 index 0000000..607ca3d --- /dev/null +++ b/source/util/linkMovingSelector.cpp @@ -0,0 +1,62 @@ +#include "util/linkMovingSelector.h" +#include +#include +#include "graphicsItem/electricConnectLineItem.h" + +LinkMovingSelector::LinkMovingSelector(QObject *parent) + : BaseSelector(parent) + ,m_pMovingLine(nullptr) +{ + m_type = ST_linkMoving; +} +LinkMovingSelector::~LinkMovingSelector() +{ + +} + +void LinkMovingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + ms_ptMouseLast = event->scenePos(); + + //QList items = scene->selectedItems(); +} + +void LinkMovingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + ms_ptMouseLast = event->scenePos(); + + if(m_pMovingLine == nullptr) + { + QList items = scene->selectedItems(); + if (items.count() == 1) //只有一个选中 + { + ElectricConnectLineItem* item = qgraphicsitem_cast(items.first()); + if(item) + { + m_pMovingLine = item; + item->setLastPoint(ms_ptMouseLast); + } + } + } + else + { + if(m_pMovingLine) + m_pMovingLine->moveLine(ms_ptMouseLast); + } + +} + +void LinkMovingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + if(m_pMovingLine) + { + m_pMovingLine->resetCurLine(); + qDebug()<<"reset"; + } + m_pMovingLine = nullptr; + ms_nDragHandle = H_none; + setCursor(scene, Qt::ArrowCursor); + scene->callParentEvent(event); + emit setWorkingSelector(ST_base); +} + diff --git a/source/util/movingSelector.cpp b/source/util/movingSelector.cpp index bc4c16d..47ef7d2 100644 --- a/source/util/movingSelector.cpp +++ b/source/util/movingSelector.cpp @@ -59,7 +59,7 @@ void MovingSelector::updateConnectLine(QList lst) auto item = dynamic_cast(iter); if(item) { - if(item->getItemType() != GIT_connectLine) + if(item->getItemType() != GIT_link) { int nId = item->itemId(); for(auto pPort:item->getPorts()) diff --git a/source/util/selectorManager.cpp b/source/util/selectorManager.cpp index fcd6299..aa96a6d 100644 --- a/source/util/selectorManager.cpp +++ b/source/util/selectorManager.cpp @@ -1,6 +1,8 @@ #include "util/selectorManager.h" #include "util/creatingSelector.h" #include "util/movingSelector.h" +#include "util/subMovingSelector.h" +#include "util/linkMovingSelector.h" #include "util/rotationSelector.h" #include "util/scalingSelector.h" #include "util/editingSelector.h" @@ -21,6 +23,12 @@ SelectorManager::SelectorManager(QObject *parent) MovingSelector* movingSelector = new MovingSelector(this); connect(movingSelector, SIGNAL(setWorkingSelector(SelectorType)), this, SLOT(onSignal_setWorkingSelector(SelectorType))); m_vecSelectors.push_back(movingSelector); + SubMovingSelector* subMovSelector = new SubMovingSelector(this); + connect(subMovSelector, SIGNAL(setWorkingSelector(SelectorType)), this, SLOT(onSignal_setWorkingSelector(SelectorType))); + m_vecSelectors.push_back(subMovSelector); + LinkMovingSelector* linkMovSelector = new LinkMovingSelector(this); + connect(linkMovSelector, SIGNAL(setWorkingSelector(SelectorType)), this, SLOT(onSignal_setWorkingSelector(SelectorType))); + m_vecSelectors.push_back(linkMovSelector); RotationSelector* rotationSelector = new RotationSelector(this); connect(rotationSelector, SIGNAL(setWorkingSelector(SelectorType)), this, SLOT(onSignal_setWorkingSelector(SelectorType))); m_vecSelectors.push_back(rotationSelector); diff --git a/source/util/subMovingSelector.cpp b/source/util/subMovingSelector.cpp new file mode 100644 index 0000000..1df162d --- /dev/null +++ b/source/util/subMovingSelector.cpp @@ -0,0 +1,73 @@ +#include "util/SubMovingSelector.h" +#include +#include +#include "graphicsItem/graphicsBaseItem.h" +#include "graphicsItem/handleText.h" + +SubMovingSelector::SubMovingSelector(QObject *parent) + : BaseSelector(parent) + ,m_pParentItem(nullptr) +{ + m_type = ST_subMoving; +} +SubMovingSelector::~SubMovingSelector() +{ + +} + +void SubMovingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + ms_ptMouseLast = event->scenePos(); + + QList items = scene->selectedItems(); +} + +void SubMovingSelector::mouseMoveEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + ms_ptMouseLast = event->scenePos(); + if(m_pParentItem == nullptr) + { + QList items = scene->items(ms_ptMouseLast); + if (items.count() == 1) + { + ItemControlHandle* pHandle = qgraphicsitem_cast(items.first()); + if(pHandle) + { + GraphicsBaseItem* item = pHandle->getParentPtr(); + if(item) + { + if(ms_nDragHandle >= H_textCaption && ms_nDragHandle < H_connect) //移动文本 + { + pHandle->setPos(item->mapFromScene(ms_ptMouseLast)); + m_pParentItem = item; + } + } + } + } + } + else + { + if(m_pParentItem) + { + if(ms_nDragHandle >= H_textCaption && ms_nDragHandle < H_connect) //文本 + { + HandleText* pt = dynamic_cast(m_pParentItem->getHandlePtr(ms_nDragHandle)); + if(pt) + { + pt->setPos(m_pParentItem->mapFromScene(ms_ptMouseLast)); + } + } + } + } + +} + +void SubMovingSelector::mouseReleaseEvent(QGraphicsSceneMouseEvent* event, DesignerScene* scene) +{ + m_pParentItem = nullptr; + ms_nDragHandle = H_none; + setCursor(scene, Qt::ArrowCursor); + scene->callParentEvent(event); + emit setWorkingSelector(ST_base); +} +