├── PrinterEdit3_0 ├── GraphicsControl │ ├── Enums.h │ ├── GraphicsItems │ │ ├── graphicsellipseitem.cpp │ │ ├── graphicsellipseitem.h │ │ ├── graphicsitembase.cpp │ │ ├── graphicsitembase.h │ │ ├── graphicslineitem.cpp │ │ ├── graphicslineitem.h │ │ ├── graphicsrectitem.cpp │ │ ├── graphicsrectitem.h │ │ ├── graphicstextitem.cpp │ │ ├── graphicstextitem.h │ │ ├── qgraphicstextItemex.cpp │ │ └── qgraphicstextitemex.h │ ├── dpitool.h │ ├── sizehandle.cpp │ ├── sizehandle.h │ ├── ugraphicsscene.cpp │ ├── ugraphicsscene.h │ ├── ugraphicsview.cpp │ └── ugraphicsview.h ├── PrinterEdit3_0.pro ├── PrinterEdit3_0.pro.user ├── UWidget │ ├── rluer.cpp │ ├── rluer.h │ └── rluer.ui ├── image │ ├── .DS_Store │ ├── background1.png │ ├── background2.png │ ├── background3.png │ ├── background4.png │ ├── bold.png │ ├── bringtofront.png │ ├── circle.png │ ├── delete.png │ ├── floodfill.png │ ├── italic.png │ ├── linecolor.png │ ├── linepointer.png │ ├── ok.png │ ├── pointer.png │ ├── rectangle.png │ ├── rotation.png │ ├── save-5.png │ ├── sendtoback.png │ ├── textpointer.png │ └── underline.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui └── resources.qrc └── README.md /PrinterEdit3_0/GraphicsControl/Enums.h: -------------------------------------------------------------------------------- 1 | #ifndef ENUMS_H 2 | #define ENUMS_H 3 | 4 | namespace PrinterEdit3_0 { 5 | 6 | static const int SELECTION_HANDLE_SIZE_HALF=5; 7 | static const int SELECTION_HANDLE_SIZE = 10; 8 | static const int SELECTION_MARGIN = 10 ; 9 | 10 | 11 | enum SelectionHandleState { SelectionHandleOff, SelectionHandleInactive, SelectionHandleActive }; 12 | enum SelectMode { none, netSelect, move, size, rotate }; 13 | enum ItemShape { selection , line , rectangle , roundrect , text,text2,ellipse , poly }; 14 | 15 | } 16 | #endif // ENUMS_H 17 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsellipseitem.cpp: -------------------------------------------------------------------------------- 1 | #include "graphicsellipseitem.h" 2 | #include 3 | #include 4 | #include 5 | #include "graphicsitembase.h" 6 | 7 | 8 | GraphicsEllipseItem::GraphicsEllipseItem(const QRect &rect, QGraphicsItem *parent) 9 | :GraphicsRectItem(rect,parent) 10 | { 11 | } 12 | 13 | QPainterPath GraphicsEllipseItem::shape() const 14 | { 15 | QPainterPath path; 16 | path.addEllipse(boundingRect()); 17 | return qt_graphicsItem_shapeFromPath(path,pen()); 18 | } 19 | 20 | void GraphicsEllipseItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 21 | { 22 | //画圆形的实现 23 | QColor c = QColor(Qt::green); 24 | c.setAlpha(160); 25 | QRectF rc = rect(); 26 | qreal radius = qMax(rc.width(),rc.height()); 27 | QRadialGradient result(rc.center(),radius); 28 | result.setColorAt(0, c.light(200)); 29 | result.setColorAt(0.5, c.dark(150)); 30 | result.setColorAt(1, c); 31 | painter->setPen(pen()); 32 | painter->setBrush(result); 33 | painter->drawEllipse(rc); 34 | } 35 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsellipseitem.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICSELLIPSEITEM_H 2 | #define GRAPHICSELLIPSEITEM_H 3 | 4 | #include 5 | #include 6 | #include "graphicsrectitem.h" 7 | #include "GraphicsControl\sizehandle.h" 8 | 9 | class GraphicsEllipseItem : public GraphicsRectItem 10 | { 11 | public: 12 | GraphicsEllipseItem(const QRect & rect ,QGraphicsItem * parent); 13 | QPainterPath shape() const; 14 | protected: 15 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 16 | }; 17 | #endif // GRAPHICSELLIPSEITEM_H 18 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsitembase.cpp: -------------------------------------------------------------------------------- 1 | #include "GraphicsItemBase.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "GraphicsControl\Enums.h" 12 | #include "GraphicsControl\sizehandle.h" 13 | 14 | 15 | //返回轮廓 16 | QPainterPath GraphicsItemBase::qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen) const 17 | { 18 | //QPainterPathStroker设置宽度为1.0 19 | // We unfortunately need this hack as QPainterPathStroker will set a width of 1.0 20 | // if we pass a value of 0.0 to QPainterPathStroker::setWidth() 21 | const qreal penWidthZero = qreal(0.00000001); 22 | 23 | if (path == QPainterPath() || pen == Qt::NoPen) 24 | return path; 25 | QPainterPathStroker ps; 26 | ps.setCapStyle(pen.capStyle()); 27 | if (pen.widthF() <= 0.0) 28 | ps.setWidth(penWidthZero); 29 | else 30 | ps.setWidth(pen.widthF()); 31 | ps.setJoinStyle(pen.joinStyle()); 32 | ps.setMiterLimit(pen.miterLimit()); 33 | QPainterPath p = ps.createStroke(path); 34 | p.addPath(path); 35 | return p; 36 | } 37 | 38 | 39 | GraphicsItemBase::GraphicsItemBase(QGraphicsItem *parent) 40 | :QAbstractGraphicsShapeItem(parent) 41 | { 42 | } 43 | 44 | //设置锚点位置 45 | void GraphicsItemBase::updateGeometry() 46 | { 47 | const QRectF &geom = this->boundingRect(); 48 | const int w = SELECTION_HANDLE_SIZE; 49 | const int h = SELECTION_HANDLE_SIZE; 50 | const int wHalf = w>>1; 51 | const int hHalf = h>>1; 52 | 53 | const Handles::iterator hend = m_handles.end(); 54 | for (Handles::iterator it = m_handles.begin(); it != hend; ++it) { 55 | SizeHandleRect *hndl = *it;; 56 | switch (hndl->dir()) { 57 | case SizeHandleRect::LeftTop: 58 | hndl->setPos(geom.x() - wHalf, geom.y() - hHalf); 59 | break; 60 | case SizeHandleRect::Top: 61 | if( geom.width()>0){ 62 | hndl->setRect(0,0,geom.width()-w,h); 63 | hndl->setPos(geom.x()+ wHalf , geom.y() - hHalf ); 64 | }else{ 65 | hndl->setRect(0,0,geom.width()+w,h); 66 | hndl->setPos(geom.x()- wHalf , geom.y() - hHalf ); 67 | } 68 | break; 69 | case SizeHandleRect::RightTop: 70 | hndl->setPos(geom.x() + geom.width() - wHalf, geom.y() - hHalf); 71 | break; 72 | case SizeHandleRect::Right: 73 | if( geom.height()>0){ 74 | hndl->setRect(0,0,w, geom.height()-h); 75 | hndl->setPos(geom.x() + geom.width() - wHalf , geom.y() + hHalf ); 76 | }else{ 77 | hndl->setRect(0,0,w, geom.height()+h); 78 | hndl->setPos(geom.x() + geom.width() - wHalf , geom.y() - hHalf ); 79 | } 80 | break; 81 | case SizeHandleRect::RightBottom: 82 | hndl->setPos(geom.x() + geom.width() -wHalf, geom.y() + geom.height() - hHalf); 83 | break; 84 | case SizeHandleRect::Bottom: 85 | if( geom.width()>0){ 86 | hndl->setRect(0,0,geom.width()-w,h); 87 | hndl->setPos(geom.x()+ wHalf, geom.y() + geom.height() - hHalf ); 88 | }else{ 89 | hndl->setRect(0,0,geom.width()+w,h); 90 | hndl->setPos(geom.x()- wHalf, geom.y() + geom.height() - hHalf ); 91 | } 92 | break; 93 | case SizeHandleRect::LeftBottom: 94 | hndl->setPos(geom.x() - wHalf, geom.y() + geom.height() - hHalf); 95 | break; 96 | case SizeHandleRect::Left: 97 | if( geom.height()>0){ 98 | hndl->setRect(0,0,w, geom.height()-h); 99 | hndl->setPos(geom.x() - wHalf, geom.y() + hHalf ); 100 | } 101 | else{ 102 | hndl->setRect(0,0,w, geom.height()+h); 103 | hndl->setPos(geom.x() - wHalf, geom.y()- hHalf ); 104 | } 105 | break; 106 | case SizeHandleRect::Rotate: 107 | if( geom.height()>0) 108 | hndl->setPos(geom.center().x() - wHalf , geom.y() - hHalf -(30)); 109 | else 110 | hndl->setPos(geom.center().x() - wHalf , geom.y()+geom.height() - hHalf -(30) ); 111 | break; 112 | default: 113 | break; 114 | } 115 | } 116 | } 117 | 118 | void GraphicsItemBase::setState(SelectionHandleState st) 119 | { 120 | const Handles::iterator hend = m_handles.end(); 121 | for (Handles::iterator it = m_handles.begin(); it != hend; ++it) 122 | (*it)->setState(st); 123 | } 124 | 125 | //锚点鼠标样式交互 126 | SizeHandleRect::Direction GraphicsItemBase::hitTest(const QPointF &point) const 127 | { 128 | const Handles::const_iterator hend = m_handles.end(); 129 | for (Handles::const_iterator it = m_handles.begin(); it != hend; ++it) 130 | { 131 | if ((*it)->hitTest(point) ){ 132 | return (*it)->dir(); 133 | } 134 | } 135 | return SizeHandleRect::None; 136 | } 137 | 138 | Qt::CursorShape GraphicsItemBase::getCursor(SizeHandleRect::Direction dir, QPointF point) 139 | { 140 | const qreal geomCenterX =pos().x()+ boundingRect().center().x(); 141 | const qreal geomCenterY =pos().y()+ boundingRect().center().y(); 142 | //根据鼠标位置重新计算应返回的鼠标样式 143 | switch (dir) { 144 | case SizeHandleRect::LeftTop: 145 | case SizeHandleRect::RightTop: 146 | case SizeHandleRect::RightBottom: 147 | case SizeHandleRect::LeftBottom: 148 | if(point.x()>geomCenterX && point.y()>geomCenterY) 149 | dir=SizeHandleRect::RightBottom; 150 | else if(point.x()>geomCenterX && point.y()geomCenterY) 153 | dir=SizeHandleRect::LeftBottom; 154 | else if(point.x()screenPos()); 203 | if(selectedAction == moveAction) { 204 | setPos(0, 0); 205 | }else if(selectedAction == actAction) { 206 | this->scene()->removeItem(this); 207 | } 208 | } 209 | 210 | QVariant GraphicsItemBase::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) 211 | { 212 | //选中时 213 | if ( change == QGraphicsItem::ItemSelectedHasChanged ) { 214 | // qDebug()<<" Item Selected : " << value.toString(); 215 | setState(value.toBool() ? SelectionHandleActive : SelectionHandleOff); 216 | if(!value.toBool()){ 217 | uncheckedEvent(); 218 | } 219 | else{ 220 | selectedEvent(); 221 | } 222 | }else if ( change == QGraphicsItem::ItemRotationHasChanged ){ 223 | // qDebug()<<"Item Rotation Changed:" << value.toString(); 224 | }else if ( change == QGraphicsItem::ItemTransformOriginPointHasChanged ){ 225 | // qDebug()<<"ItemTransformOriginPointHasChanged:" << value.toPointF(); 226 | } 227 | return value; 228 | } 229 | 230 | //记录按下旋转按钮时的坐标 231 | void GraphicsItemBase::setRotateStart(QPointF point ){ 232 | m_mouseRotateStart=point; 233 | m_fLastAngle=rotation(); 234 | 235 | } 236 | void GraphicsItemBase::setRotateEnd(QPointF point) 237 | { 238 | QPointF ori = mapToScene(transformOriginPoint()); 239 | QPointF v1 = m_mouseRotateStart - ori; 240 | QPointF v2 = point - ori; 241 | //θ=atan2(v2.y,v2.x)−atan2(v1.y,v1.x) 242 | qreal angle =atan2f(v2.y(), v2.x()) - atan2f(v1.y(), v1.x()); 243 | 244 | angle =m_fLastAngle+ angle * 180 / 3.1415926; 245 | m_angle= angle; 246 | setRotation(angle); 247 | } 248 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsitembase.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICSITEMBASE_H 2 | #define GRAPHICSITEMBASE_H 3 | 4 | #include 5 | #include 6 | #include "GraphicsControl\sizehandle.h" 7 | #include "GraphicsControl\Enums.h" 8 | 9 | class GraphicsItemBase : public QAbstractGraphicsShapeItem 10 | { 11 | public: 12 | GraphicsItemBase(QGraphicsItem * parent ); 13 | void setRotateStart(QPointF point ); 14 | void setRotateEnd(QPointF point); 15 | enum {Type = UserType+1}; 16 | int type() const { return Type; } 17 | SizeHandleRect::Direction hitTest(const QPointF & point ) const; 18 | virtual void resizeTo(SizeHandleRect::Direction dir, const QPointF & point ); 19 | virtual QPointF origin () const { return QPointF(0,0); } 20 | virtual Qt::CursorShape getCursor(SizeHandleRect::Direction dir, QPointF point ); 21 | virtual QRectF rect() const { return QRectF(0,0,0,0);} 22 | virtual void multipleChoiceEvent(){} 23 | protected: 24 | virtual void uncheckedEvent(){} 25 | virtual void selectedEvent(){} 26 | virtual void updateGeometry(); 27 | void setRect(QRectF rect); 28 | void setState(SelectionHandleState st); 29 | void contextMenuEvent(QGraphicsSceneContextMenuEvent *event); 30 | QVariant itemChange(GraphicsItemChange change, const QVariant &value); 31 | typedef QVector Handles; 32 | Handles m_handles; 33 | qreal m_angle=0; 34 | qreal m_fLastAngle=0; 35 | QPointF m_mouseRotateStart; 36 | QPainterPath qt_graphicsItem_shapeFromPath(const QPainterPath &path, const QPen &pen) const; 37 | }; 38 | 39 | #endif // GRAPHICSITEMBASE_H 40 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicslineitem.cpp: -------------------------------------------------------------------------------- 1 | #include "graphicslineitem.h" 2 | #include 3 | #include 4 | #include 5 | #include "graphicsitembase.h" 6 | 7 | 8 | GraphicsLineItem::GraphicsLineItem(const QPointF &startPoint ,const QPointF &endPoint,QGraphicsItem * parent) 9 | :GraphicsItemBase(parent), 10 | m_startPoint(startPoint), 11 | m_endPoint(endPoint) 12 | { 13 | // handles创建锚点队列 14 | m_handles.reserve(SizeHandleRect::None); 15 | //枚举队列 16 | SizeHandleRect *leftTop = new SizeHandleRect(this, static_cast(SizeHandleRect::LeftTop)); 17 | m_handles.push_back(leftTop); 18 | SizeHandleRect *rightBottom = new SizeHandleRect(this, static_cast(SizeHandleRect::RightBottom)); 19 | m_handles.push_back(rightBottom); 20 | updateGeometry(); 21 | setFlag(QGraphicsItem::ItemIsMovable, true); 22 | setFlag(QGraphicsItem::ItemIsSelectable, true); 23 | setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); 24 | this->setAcceptHoverEvents(true);//接受悬停事件 25 | } 26 | QRectF GraphicsLineItem::boundingRect() const 27 | { 28 | QRectF bounding (m_startPoint.x(), m_startPoint.y(),m_endPoint.x()-m_startPoint.x(),m_endPoint.y()-m_startPoint.y()); 29 | return bounding; 30 | } 31 | 32 | QPainterPath GraphicsLineItem::shape() const 33 | { 34 | QPainterPath path; 35 | path.lineTo(QPointF(m_endPoint)); 36 | path.lineTo(QPointF(m_startPoint)); 37 | return qt_graphicsItem_shapeFromPath(path,pen()); 38 | } 39 | 40 | //设置句柄位置 41 | void GraphicsLineItem::updateGeometry(){ 42 | 43 | const QRectF &geom = this->boundingRect(); 44 | const int wHalf = SELECTION_HANDLE_SIZE>>1; 45 | const int hHalf = SELECTION_HANDLE_SIZE>>1; 46 | 47 | const Handles::iterator hend = m_handles.end(); 48 | for (Handles::iterator it = m_handles.begin(); it != hend; ++it) { 49 | SizeHandleRect *hndl = *it;; 50 | switch (hndl->dir()) { 51 | case SizeHandleRect::LeftTop: 52 | hndl->setPos(geom.x() - wHalf, geom.y()- hHalf ); 53 | break; 54 | case SizeHandleRect::RightBottom: 55 | hndl->setPos(geom.x() + geom.width() - wHalf , geom.y()+ geom.height() - hHalf ); 56 | break; 57 | default: 58 | break; 59 | } 60 | } 61 | } 62 | 63 | void GraphicsLineItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point) 64 | { 65 | GraphicsItemBase::resizeTo(dir,point); 66 | QPointF local = mapFromScene(point); 67 | switch (dir) { 68 | case SizeHandleRect::RightBottom: 69 | m_endPoint=local.toPoint(); 70 | break; 71 | case SizeHandleRect::LeftTop: 72 | m_startPoint=local.toPoint(); 73 | break; 74 | default: 75 | break; 76 | } 77 | 78 | prepareGeometryChange(); 79 | 80 | updateGeometry(); 81 | 82 | } 83 | 84 | void GraphicsLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 85 | { 86 | //画线的实现 87 | painter->setPen(pen()); 88 | painter->drawLine(m_startPoint,m_endPoint); 89 | } 90 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicslineitem.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICSLINEITEM_H 2 | #define GRAPHICSLINEITEM_H 3 | 4 | #include 5 | #include 6 | #include "graphicsrectitem.h" 7 | #include "GraphicsControl\sizehandle.h" 8 | 9 | class GraphicsLineItem: public GraphicsItemBase 10 | { 11 | public: 12 | GraphicsLineItem(const QPointF &startPoint ,const QPointF &endPoint,QGraphicsItem * parent); 13 | 14 | QRectF boundingRect() const; 15 | QPainterPath shape() const; 16 | virtual void resizeTo(SizeHandleRect::Direction dir, const QPointF & point ); 17 | virtual QRectF rect() const 18 | { 19 | QRectF bounding (m_startPoint.x(), m_startPoint.y(),m_endPoint.x()-m_startPoint.x(),m_endPoint.y()-m_startPoint.y()); 20 | return bounding; 21 | } 22 | private: 23 | QPointF m_startPoint; 24 | QPointF m_endPoint; 25 | protected: 26 | void updateGeometry(); 27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 28 | }; 29 | #endif // GRAPHICSLINEITEM_H 30 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsrectitem.cpp: -------------------------------------------------------------------------------- 1 | #include "graphicsrectitem.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "graphicsitembase.h" 12 | 13 | GraphicsRectItem::GraphicsRectItem(const QRect & rect ,QGraphicsItem *parent) 14 | :GraphicsItemBase(parent) 15 | ,m_width(rect.width()) 16 | ,m_height(rect.height()) 17 | { 18 | // handles创建锚点队列 19 | m_handles.reserve(SizeHandleRect::None); 20 | //枚举队列 21 | for (int i = SizeHandleRect::LeftTop; i <= SizeHandleRect::Rotate; ++i) { 22 | SizeHandleRect *shr = new SizeHandleRect(this, static_cast(i)); 23 | m_handles.push_back(shr); 24 | } 25 | 26 | updateGeometry(); 27 | setFlag(QGraphicsItem::ItemIsMovable, true); 28 | setFlag(QGraphicsItem::ItemIsSelectable, true); 29 | setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); 30 | m_delta =rect; 31 | this->setAcceptHoverEvents(true);//接受悬停事件 32 | } 33 | 34 | QRectF GraphicsRectItem::boundingRect() const 35 | { 36 | QRectF bounding ( QPointF(m_delta.left(), m_delta.top()),QPointF(m_delta.right(),m_delta.bottom())); 37 | return bounding; 38 | } 39 | 40 | //用来控制检测碰撞collide和鼠标点击hit响应区域,返回图形轮廓 41 | QPainterPath GraphicsRectItem::shape() const 42 | { 43 | QPainterPath path; 44 | path.addRect(boundingRect()); 45 | return qt_graphicsItem_shapeFromPath(path,pen()); 46 | } 47 | 48 | void GraphicsRectItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point) 49 | { 50 | GraphicsItemBase::resizeTo(dir,point); 51 | QPointF local = mapFromScene(point); 52 | QString dirName; 53 | m_delta= this->rect().toRect(); 54 | switch (dir) { 55 | 56 | case SizeHandleRect::Right: 57 | m_delta.setRight(local.x()); 58 | break; 59 | case SizeHandleRect::RightTop: 60 | dirName = "RightTop"; 61 | m_delta.setTopRight(local.toPoint()); 62 | break; 63 | case SizeHandleRect::RightBottom: 64 | dirName = "RightBottom"; 65 | m_delta.setBottomRight(local.toPoint()); 66 | break; 67 | case SizeHandleRect::LeftBottom: 68 | dirName = "LeftBottom"; 69 | m_delta.setBottomLeft(local.toPoint()); 70 | break; 71 | case SizeHandleRect::Bottom: 72 | dirName = "Bottom"; 73 | m_delta.setBottom(local.y()); 74 | break; 75 | case SizeHandleRect::LeftTop: 76 | dirName = "LeftTop"; 77 | m_delta.setTopLeft(local.toPoint()); 78 | break; 79 | case SizeHandleRect::Left: 80 | dirName = "Left"; 81 | m_delta.setLeft(local.x()); 82 | break; 83 | case SizeHandleRect::Top: 84 | dirName = "Top"; 85 | m_delta.setTop(local.y()); 86 | break; 87 | case SizeHandleRect::Rotate: 88 | dirName = "Rotate"; 89 | setRotateEnd(point); 90 | break; 91 | default: 92 | break; 93 | } 94 | 95 | prepareGeometryChange(); 96 | 97 | m_width = m_delta.width(); 98 | m_height = m_delta.height(); 99 | 100 | updateGeometry(); 101 | 102 | } 103 | 104 | void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 105 | { 106 | //画矩形的实现 107 | QColor c = QColor(Qt::green); 108 | // c.setAlpha(160); 109 | QLinearGradient result(rect().topLeft(), rect().topRight()); 110 | result.setColorAt(0, c.dark(150)); 111 | result.setColorAt(0.5, c.light(200)); 112 | result.setColorAt(1, c.dark(150)); 113 | painter->setPen(pen()); 114 | painter->setBrush(result); 115 | painter->drawRect(rect()); 116 | } 117 | 118 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicsrectitem.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICSRECTITEM_H 2 | #define GRAPHICSRECTITEM_H 3 | 4 | #include 5 | #include 6 | #include "GraphicsControl\sizehandle.h" 7 | #include "graphicsitembase.h" 8 | 9 | class GraphicsRectItem : public GraphicsItemBase 10 | { 11 | public: 12 | GraphicsRectItem(const QRect & rect,QGraphicsItem * parent); 13 | 14 | QRectF boundingRect() const; 15 | QPainterPath shape() const; 16 | virtual void resizeTo(SizeHandleRect::Direction dir, const QPointF & point ); 17 | virtual QRectF rect() const 18 | { 19 | QRectF bounding ( m_delta.left(), m_delta.top(),m_width,m_height); 20 | return bounding; 21 | } 22 | protected: 23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 24 | protected: 25 | qreal m_width; 26 | qreal m_height; 27 | QRect m_delta ; 28 | }; 29 | 30 | #endif // GRAPHICSRECTITEM_H 31 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicstextitem.cpp: -------------------------------------------------------------------------------- 1 | #include "graphicstextitem.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | GraphicsTextItem:: GraphicsTextItem(const QRect & rect ,QString text,QGraphicsItem * parent) 13 | :GraphicsRectItem(rect,parent) 14 | { 15 | m_Textwidget=new QGraphicsProxyWidget(this); 16 | m_text=new QGraphicsTextItemEx("hello"); 17 | m_Textwidget->setWidget(m_text); 18 | m_text->hide(); 19 | 20 | setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); 21 | } 22 | 23 | QRectF GraphicsTextItem::rect(){ 24 | return boundingRect(); 25 | } 26 | void GraphicsTextItem::multipleChoiceEvent(){ 27 | this->uncheckedEvent(); 28 | } 29 | void GraphicsTextItem::uncheckedEvent(){ 30 | m_text->hide(); 31 | } 32 | void GraphicsTextItem::resizeTo(SizeHandleRect::Direction dir, const QPointF &point){ 33 | GraphicsRectItem::resizeTo(dir, point); 34 | if(m_Textwidget->isVisible()) 35 | this->uncheckedEvent(); 36 | 37 | if(SizeHandleRect::Rotate!=dir) 38 | //重新设置内置textEdit大小 39 | m_text->resize(abs(rect().width()),abs(rect().height())); 40 | } 41 | 42 | void GraphicsTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 43 | { 44 | if(m_Textwidget!=nullptr&&!m_Textwidget->isVisible()){ 45 | qreal textItemX=rect().x(); 46 | qreal textItemY=rect().y(); 47 | if(rect().width()<0) 48 | textItemX+=rect().width(); 49 | if(rect().height()<0) 50 | textItemY+=rect().height(); 51 | if(!rect().height()||!rect().width())return; 52 | //设置textBox新坐标 53 | m_Textwidget->setPos(QPointF(textItemX,textItemY)); 54 | 55 | //多行文本绘制 56 | QFontMetrics metrics(painter->font()); 57 | QStringList texts=m_text->toPlainText().split("\n"); 58 | 59 | if(&texts ==nullptr ||texts.count()==0)return; 60 | if(m_isStretch){//启用拉伸状态时 61 | painter->save(); 62 | //找出占位最长的一行 63 | int maxItem=0; 64 | qreal maxfontWidth=0; 65 | for(int i=0;iscale(xScale,yScale); 76 | 77 | qreal x=rect().x()/xScale; 78 | qreal y=(rect().y()/yScale)-metrics.descent(); 79 | qreal fontHeight=metrics.height(); 80 | for(int i=0;idrawText(postion, texts[i]); 83 | } 84 | } 85 | else{//关闭拉伸状态时 86 | QFontInfo fInfo(painter->font()); 87 | qreal minWidth= qAbs(rect().height())/ (metrics.height()+metrics.descent())/texts.count()*fInfo.pixelSize(); 88 | QFont font=painter->font(); 89 | font.setPixelSize(static_cast(minWidth)); 90 | painter->save(); 91 | painter->setFont(font); 92 | QRectF tempRect; 93 | if(boundingRect().width()<0&& boundingRect().height()<0){ 94 | painter->scale( -1, -1 ); 95 | tempRect=QRectF(-rect().x(),-rect().y(),-rect().width(),-rect().height()); 96 | } 97 | else if(boundingRect().width()<0){ 98 | painter->scale( -1, 1 ); 99 | tempRect=QRectF(-rect().x(),rect().y(),-rect().width(),rect().height()); 100 | } 101 | else if(boundingRect().height()<0){ 102 | painter->scale( 1, -1 ); 103 | tempRect=QRectF(rect().x(),-rect().y(),rect().width(),-rect().height()); 104 | } 105 | else{ 106 | tempRect=rect(); 107 | } 108 | painter->drawText(tempRect, Qt::AlignVCenter|Qt::AlignLeft, m_text->toPlainText()); 109 | } 110 | painter->restore(); 111 | } 112 | } 113 | 114 | void GraphicsTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event){ 115 | m_Textwidget->show(); 116 | this->update(); 117 | } 118 | 119 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/graphicstextitem.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHICSTEXTITEM_H 2 | #define GRAPHICSTEXTITEM_H 3 | #include "graphicsitembase.h" 4 | #include "graphicsrectitem.h" 5 | #include 6 | #include "qgraphicstextitemex.h" 7 | #include 8 | #include 9 | class GraphicsTextItem: public GraphicsRectItem 10 | { 11 | public: 12 | void setText(QString text); 13 | GraphicsTextItem(const QRect & rect ,QString text,QGraphicsItem * parent); 14 | 15 | virtual void resizeTo(SizeHandleRect::Direction dir, const QPointF & point ); 16 | virtual QRectF rect() ; 17 | virtual void multipleChoiceEvent(); 18 | void enableStretch() { m_isStretch=true;} 19 | void unableStretch() { m_isStretch=false;} 20 | protected: 21 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 22 | void uncheckedEvent(); 23 | void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); 24 | private: 25 | QGraphicsProxyWidget *m_Textwidget; 26 | QGraphicsTextItemEx *m_text; 27 | bool m_isStretch=true;//是否开启拉伸 28 | }; 29 | 30 | #endif // GRAPHICSTEXTITEM_H 31 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/qgraphicstextItemex.cpp: -------------------------------------------------------------------------------- 1 | #include "qgraphicstextitemex.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | QGraphicsTextItemEx::QGraphicsTextItemEx(const QString &text, QWidget *parent) 9 | :QTextEdit (text,parent){ 10 | 11 | this->setStyleSheet("background-color: transparent;border:0px;"); 12 | // this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); 13 | //设置垂直滚动条样式 14 | this->verticalScrollBar()->setStyleSheet( 15 | "QScrollBar::sub-page:vertical,QScrollBar::add-page:vertical{background-color:transparent;}" /*滚动条滑块背景样式*/ 16 | "QScrollBar::add-line:vertical{background-color:transparent; border:none;}" /*滚动条下移按钮样式*/ 17 | "QScrollBar::sub-line:vertical{background-color:transparent; border:none;}" /*滚动条上移按钮样式*/ 18 | "QScrollBar::handle:vertical:hover{background-color:rgba(168,168,168,220);}" /*鼠标进入*/ 19 | "QScrollBar::handle:vertical{background-color:rgba(168,168,168,170);border-radius:7px;width:13px;}" /*滚动条滑块样式*/ 20 | "QScrollBar:vertical{margin:0px 0px 0px 0px;background-color:transparent;border:0px;width:14px;}"); /*滚动条整体样式*/ 21 | 22 | // "QScrollBar::down-arrow:vertical{border-image:url(:/image/bold.png);width:0px;height:0px;}" /*滚动条上移图标样式*/ 23 | // "QScrollBar::up-arrow:vertical{ border-image:url(:/image/bold.png);width:0px; height:0px;}" /*滚动条下移图标样式*/ 24 | } 25 | 26 | void QGraphicsTextItemEx::focusOutEvent(QFocusEvent *event){ 27 | this->hide(); 28 | } 29 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/GraphicsItems/qgraphicstextitemex.h: -------------------------------------------------------------------------------- 1 | #ifndef UTEXTITEM_H 2 | #define UTEXTITEM_H 3 | 4 | #include 5 | 6 | class QGraphicsTextItemEx: public QTextEdit 7 | { 8 | public: 9 | QGraphicsTextItemEx(const QString &text, QWidget *parent = nullptr); 10 | 11 | protected: 12 | void focusOutEvent(QFocusEvent *event); 13 | 14 | }; 15 | 16 | #endif // UTEXTITEM_H 17 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/dpitool.h: -------------------------------------------------------------------------------- 1 | #ifndef DPITOOL_H 2 | #define DPITOOL_H 3 | #include 4 | #include 5 | #include 6 | 7 | class DpiTool{ 8 | public : 9 | 10 | qreal getDPI(){ 11 | //获取dpi,dpi:一英寸所占像素长度,一英寸=2.54厘米,96dpi则是的2.54cm=96pix,所以实际1cm=当前屏幕上96/2.54≈37.8pix 12 | return QApplication::primaryScreen()->logicalDotsPerInch(); 13 | } 14 | qreal getOneMMPixel(){ 15 | //获取1毫米在屏幕上的像素数 16 | return QApplication::primaryScreen()->logicalDotsPerInch()/INCH; 17 | } 18 | qreal pixelToMM(qreal pixel){ 19 | //像素转毫米 20 | return pixel*INCH/QApplication::primaryScreen()->logicalDotsPerInch(); 21 | } 22 | 23 | private: 24 | qreal const INCH=25.4;//1英寸=25.4毫米 25 | }; 26 | 27 | #endif // DPITOOL_H 28 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/sizehandle.cpp: -------------------------------------------------------------------------------- 1 | #include "sizehandle.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | SizeHandleRect::SizeHandleRect(QGraphicsItem* parent , Direction d) 9 | :QGraphicsRectItem(0,0,SELECTION_HANDLE_SIZE,SELECTION_HANDLE_SIZE,parent) 10 | ,m_dir(d) 11 | ,m_resizable(parent) 12 | ,m_state(SelectionHandleOff) 13 | { 14 | setParentItem(parent); 15 | hide(); 16 | } 17 | 18 | void SizeHandleRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 19 | { 20 | //画锚点 21 | // c.setAlpha(200); 22 | painter->setPen(QPen(Qt::red,1,Qt::SolidLine)); 23 | 24 | if ( m_dir == Rotate ) 25 | { 26 | painter->setBrush(QBrush(Qt::blue)); 27 | painter->drawEllipse(rect()); 28 | } 29 | else if(m_dir ==Top || m_dir ==Bottom){ 30 | QPoint p1=QPoint( 0,rect().height()/2); 31 | QPoint p2=QPoint(rect().width()+p1.x(),p1.y()); 32 | painter->setPen(QPen(Qt::red,1,Qt::DashLine)); 33 | painter->drawLine(QLine(p1,p2)); 34 | } 35 | else if(m_dir ==Left || m_dir ==Right){ 36 | QPoint p1=QPoint( rect().width()/2,0); 37 | QPoint p2=QPoint( p1.x(),rect().height()-p1.y()); 38 | painter->setPen(QPen(Qt::red,1,Qt::DashLine)); 39 | painter->drawLine(QLine(p1,p2)); 40 | } 41 | else{ 42 | painter->setPen(QPen(Qt::red,1,Qt::SolidLine)); 43 | painter->drawRect(rect()); 44 | } 45 | } 46 | 47 | //设置拉伸手柄状态 48 | void SizeHandleRect::setState(SelectionHandleState st) 49 | { 50 | if (st == m_state) 51 | return; 52 | switch (st) { 53 | case SelectionHandleOff: 54 | hide(); 55 | break; 56 | case SelectionHandleInactive: 57 | case SelectionHandleActive: 58 | show(); 59 | break; 60 | } 61 | m_state = st; 62 | } 63 | 64 | //判断鼠标是否在句柄上 65 | bool SizeHandleRect::hitTest(const QPointF &point) 66 | { 67 | QPointF pt = mapFromScene(point); 68 | return rect().contains(pt); 69 | } 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/sizehandle.h: -------------------------------------------------------------------------------- 1 | #ifndef SIZEHANDLE 2 | #define SIZEHANDLE 3 | 4 | #include 5 | #include 6 | #include "Enums.h" 7 | 8 | QT_BEGIN_NAMESPACE 9 | class QFocusEvent; 10 | class QGraphicsItem; 11 | class QGraphicsScene; 12 | class QGraphicsSceneMouseEvent; 13 | QT_END_NAMESPACE 14 | 15 | 16 | using namespace PrinterEdit3_0; 17 | 18 | class SizeHandleRect :public QGraphicsRectItem 19 | { 20 | public: 21 | enum Direction { LeftTop , Top, RightTop, Right, RightBottom, Bottom, LeftBottom, Left ,Rotate, Center, None}; 22 | 23 | SizeHandleRect(QGraphicsItem* parent , Direction d); 24 | 25 | Direction dir() const { return m_dir; } 26 | void setState(SelectionHandleState st); 27 | bool hitTest( const QPointF & point ); 28 | void setScale(qreal scale); 29 | protected: 30 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); 31 | 32 | private: 33 | qreal m_scale; 34 | const Direction m_dir; 35 | QPoint m_startPos; 36 | QPoint m_curPos; 37 | QSize m_startSize; 38 | QSize m_curSize; 39 | QGraphicsItem *m_resizable; 40 | SelectionHandleState m_state; 41 | }; 42 | 43 | 44 | #endif // SIZEHANDLE 45 | 46 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/ugraphicsscene.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ugraphicsscene.h" 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "ugraphicsview.h" 8 | #include 9 | #include 10 | #include 11 | #include "GraphicsControl/GraphicsItems/graphicsrectitem.h" 12 | #include "GraphicsControl/GraphicsItems/graphicstextitem.h" 13 | #include "GraphicsControl/GraphicsItems/graphicslineitem.h" 14 | #include "GraphicsControl/GraphicsItems/graphicsellipseitem.h" 15 | #include 16 | 17 | ItemShape UGraphicsScene::c_drawShape = ItemShape::selection; 18 | SelectMode UGraphicsScene::m_selectMode=SelectMode::none; 19 | SizeHandleRect::Direction UGraphicsScene::nDragHandle = SizeHandleRect::None; 20 | 21 | UGraphicsScene::UGraphicsScene(QObject *parent) 22 | :QGraphicsScene(parent) 23 | { 24 | m_magin=new QMargins(10,10,10,10);//设置边距,单位像素 25 | installEventFilter(this); //安装事件过滤器 26 | } 27 | 28 | //获取首个视图 29 | UGraphicsView* UGraphicsScene::getFirstView(){ 30 | if(this->views().count()>0) 31 | return static_cast(this->views().first()); 32 | else 33 | throw "Scene视图是空的"; 34 | } 35 | 36 | //以毫米为单位,创建活动区域 37 | void UGraphicsScene::createArea(qreal x, qreal y, qreal w, qreal h){ 38 | this->createArea(QRectF(x,y,w,h)); 39 | } 40 | 41 | //以毫米为单位,创建活动区域 42 | void UGraphicsScene::createArea(QRectF rectF){ 43 | qreal pixel=getOneMMPixel(); 44 | qreal width=rectF.width()*pixel+m_magin->left()+m_magin->right(); 45 | qreal height=rectF.height()*pixel+m_magin->top()+m_magin->bottom(); 46 | this->setSceneRect(QRectF(rectF.x()*pixel,rectF.y()*pixel,width,height)); 47 | 48 | emit this->initSizeOverEvent(); 49 | } 50 | 51 | void UGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *event){ 52 | QGraphicsScene::wheelEvent(event); 53 | this->update(); 54 | } 55 | 56 | void UGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect){ 57 | QPainterPath path; 58 | path.addRoundedRect(this->sceneRect().adjusted(m_magin->left(),m_magin->top(),-m_magin->right(),-m_magin->bottom()), 10, 10); 59 | painter->fillPath(path, Qt::white); 60 | QGraphicsScene::drawBackground(painter,rect); 61 | } 62 | 63 | void UGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) 64 | { 65 | emit rluerDispalyItemPoint(); 66 | //create obj 67 | // qDebug() << this->selectedItems().count(); 68 | if(c_drawShape != ItemShape::selection){ 69 | this->mousePressDrawItem(event); 70 | } 71 | else{ 72 | this->mousePressSelectItem(event); 73 | } 74 | 75 | 76 | } 77 | 78 | void UGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 79 | { 80 | if(event->buttons() & Qt::LeftButton){ 81 | emit rluerDispalyItemPoint(); 82 | } 83 | 84 | this->mouseMoveSelectItem(event); 85 | 86 | } 87 | 88 | void UGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) 89 | { 90 | if(c_drawShape != ItemShape::selection) { 91 | this->mouseReleaseDrawItem(event); 92 | } 93 | else{ 94 | this->mouseReleaseSelectItem(event); 95 | } 96 | if ( this->selectedItems().count() > 1 ){ 97 | //引发多选事件 98 | multipleChoiceEvent(); 99 | } 100 | 101 | } 102 | 103 | bool UGraphicsScene::eventFilter(QObject *watched, QEvent *event){ 104 | if(event->type() == QEvent::Enter){ //当鼠标进入时 105 | if(c_drawShape != ItemShape::selection) 106 | getFirstView()->setCursor(Qt::CrossCursor); 107 | else 108 | getFirstView()->setCursor(Qt::ArrowCursor); 109 | } 110 | else if(event->type() == QEvent::Leave){//当鼠标离开时 111 | getFirstView()->setCursor(Qt::ArrowCursor); 112 | } 113 | QGraphicsScene::eventFilter(watched,event); 114 | } 115 | 116 | //mouse draw item 117 | void UGraphicsScene::mousePressDrawItem(QGraphicsSceneMouseEvent* event){ 118 | c_down = event->scenePos(); 119 | c_last = event->scenePos(); 120 | 121 | clearSelection(); 122 | item=nullptr; 123 | switch ( c_drawShape ){ 124 | case ItemShape::rectangle: 125 | item = new GraphicsRectItem(QRect(0,0,0,0),nullptr); 126 | item->setPen(Qt::NoPen); 127 | break; 128 | case ItemShape::ellipse: 129 | item = new GraphicsEllipseItem(QRect(0,0,0,0),nullptr); 130 | item->setPen(Qt::NoPen); 131 | break; 132 | case ItemShape::line: 133 | item = new GraphicsLineItem(QPointF(0,0),QPointF(0,0),nullptr); 134 | item->setPen(QPen(Qt::green,10,Qt::SolidLine)); 135 | break; 136 | case ItemShape::text: 137 | item = new GraphicsTextItem(QRect(0,0,0,0),"please input",nullptr); 138 | static_cast(item)->enableStretch(); 139 | item->setPen(Qt::NoPen); 140 | break; 141 | case ItemShape::text2: 142 | item = new GraphicsTextItem(QRect(0,0,0,0),"please input",nullptr); 143 | static_cast(item)->unableStretch(); 144 | item->setPen(Qt::NoPen); 145 | break; 146 | } 147 | if ( item == nullptr) return; 148 | item->setPos(event->scenePos()); 149 | this->addItem(item); 150 | item->setSelected(true); 151 | m_selectMode = SelectMode::size; 152 | nDragHandle = SizeHandleRect::RightBottom; 153 | } 154 | void UGraphicsScene::mouseMoveDrawItem(QGraphicsSceneMouseEvent* event){ 155 | this->mouseMoveSelectItem(event); 156 | } 157 | 158 | void UGraphicsScene::mouseReleaseDrawItem(QGraphicsSceneMouseEvent* event){ 159 | if ( event->scenePos() == c_down ){ 160 | if ( item != nullptr) 161 | this->removeItem(item); 162 | this->mousePressSelectItem(event); 163 | } 164 | this->mouseReleaseSelectItem(event); 165 | } 166 | void UGraphicsScene::multipleChoiceEvent(){ 167 | QList items = this->selectedItems(); 168 | if(items.count()>0) 169 | foreach(QGraphicsItem *item,items){ 170 | qgraphicsitem_cast(item)->multipleChoiceEvent(); 171 | } 172 | } 173 | //mouse select item 174 | void UGraphicsScene::mousePressSelectItem(QGraphicsSceneMouseEvent* event){ 175 | c_down = event->scenePos(); 176 | c_last = event->scenePos(); 177 | 178 | if (!m_hoverSizer) 179 | QGraphicsScene::mousePressEvent(event); 180 | 181 | m_selectMode = SelectMode::none ; 182 | QList items = this->selectedItems(); 183 | GraphicsItemBase *item = nullptr; 184 | 185 | if ( items.count() == 1 ) 186 | { 187 | item = qgraphicsitem_cast(items.first()); 188 | } 189 | 190 | if ( item != nullptr ){ 191 | 192 | nDragHandle = item->hitTest(event->scenePos()); 193 | if ( nDragHandle !=SizeHandleRect::None) 194 | { 195 | if(nDragHandle==SizeHandleRect::Rotate) 196 | { 197 | m_selectMode=SelectMode::rotate; 198 | //旋转时触发 199 | item->setRotateStart(event->scenePos());//更新旋转坐标 200 | } 201 | else 202 | m_selectMode = SelectMode::size; 203 | } 204 | else 205 | m_selectMode = SelectMode::move; 206 | // m_lastSize = item->boundingRect().size(); 207 | } 208 | //==============画框选框 209 | if( m_selectMode ==SelectMode::none ){ 210 | m_selectMode = SelectMode::netSelect; 211 | getFirstView()->setDragMode(QGraphicsView::RubberBandDrag);//使用橡皮筋效果,进行区域选择,可以选中一个区域内的所有图形项。 212 | 213 | } 214 | } 215 | 216 | void UGraphicsScene::mouseMoveSelectItem(QGraphicsSceneMouseEvent* event){ 217 | c_last = event->scenePos(); 218 | 219 | bool isGroup = false; 220 | QList items = this->selectedItems(); 221 | GraphicsItemBase * item = nullptr; 222 | if ( items.count() == 1 ){ 223 | 224 | item = qgraphicsitem_cast(items.first()); 225 | if ( item != nullptr ){ 226 | if ( nDragHandle != SizeHandleRect::None &&( m_selectMode == SelectMode::size || m_selectMode == SelectMode::rotate)){ 227 | // "按住拉伸ing"; 228 | item->resizeTo(nDragHandle,c_last); 229 | } 230 | else if(nDragHandle == SizeHandleRect::None && m_selectMode == ItemShape::selection ){ 231 | SizeHandleRect::Direction handl= item->hitTest(event->scenePos()); 232 | if(handl!=SizeHandleRect::None){ 233 | m_hoverSizer=true; 234 | if(c_drawShape == ItemShape::selection)//没有选择绘制图形时 235 | getFirstView()->setCursor(item->getCursor(handl,event->scenePos())); 236 | else 237 | getFirstView()->setCursor(Qt::CrossCursor); 238 | } 239 | else { 240 | m_hoverSizer=false; 241 | if(c_drawShape == ItemShape::selection)//没有选择绘制图形时 242 | getFirstView()->setCursor(Qt::ArrowCursor); 243 | else 244 | getFirstView()->setCursor(Qt::CrossCursor); 245 | } 246 | } 247 | } 248 | // QGraphicsItemGroup *item1 = qgraphicsitem_cast(items.first()); 249 | // if ( item1 != nullptr ){ 250 | // isGroup = true; 251 | // } 252 | } 253 | 254 | if ( (m_selectMode != SelectMode::size && items.count() >= 1) || isGroup ) 255 | { 256 | QGraphicsScene::mouseMoveEvent(event); 257 | } 258 | } 259 | void UGraphicsScene::mouseReleaseSelectItem(QGraphicsSceneMouseEvent *event){ 260 | if (event->scenePos() == c_down) 261 | c_drawShape = ItemShape::selection; 262 | 263 | if (m_selectMode == SelectMode::netSelect ){ 264 | getFirstView()->setDragMode(QGraphicsView::NoDrag); 265 | } 266 | m_selectMode = SelectMode::none ; 267 | nDragHandle = SizeHandleRect::None; 268 | m_hoverSizer = false; 269 | 270 | QGraphicsScene::mouseReleaseEvent(event); 271 | } 272 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/ugraphicsscene.h: -------------------------------------------------------------------------------- 1 | #ifndef UGRAPHICSSCENE_H 2 | #define UGRAPHICSSCENE_H 3 | 4 | #include 5 | #include 6 | #include "Enums.h" 7 | #include "ugraphicsview.h" 8 | #include "dpitool.h" 9 | #include 10 | #include "GraphicsControl/sizehandle.h" 11 | 12 | QT_BEGIN_NAMESPACE 13 | class QGraphicsSceneMouseEvent; 14 | class QMenu; 15 | class QPointF; 16 | class QGraphicsLineItem; 17 | class QFont; 18 | class QGraphicsTextItem; 19 | class QColor; 20 | QT_END_NAMESPACE 21 | 22 | using namespace PrinterEdit3_0; 23 | 24 | class UGraphicsScene : public QGraphicsScene,public DpiTool 25 | { 26 | Q_OBJECT 27 | signals: 28 | void initSizeOverEvent(); 29 | void rluerDispalyItemPoint(); 30 | public: 31 | explicit UGraphicsScene(QObject *parent = 0); 32 | void createArea(qreal x, qreal y, qreal w, qreal h); 33 | void createArea(QRectF rectF); 34 | QMargins* getMargin(){return m_magin;} 35 | UGraphicsView* getFirstView(); 36 | protected: 37 | void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; 38 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; 39 | void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE; 40 | bool eventFilter(QObject *watched, QEvent *event)Q_DECL_OVERRIDE; 41 | void wheelEvent(QGraphicsSceneWheelEvent *event); 42 | void drawBackground(QPainter *painter, const QRectF &rect); 43 | private: 44 | QMargins* m_magin; 45 | void multipleChoiceEvent(); 46 | private: 47 | void mousePressDrawItem(QGraphicsSceneMouseEvent* event); 48 | void mouseMoveDrawItem(QGraphicsSceneMouseEvent* event); 49 | void mouseReleaseDrawItem(QGraphicsSceneMouseEvent* event); 50 | void mousePressSelectItem(QGraphicsSceneMouseEvent* event); 51 | void mouseMoveSelectItem(QGraphicsSceneMouseEvent* event); 52 | void mouseReleaseSelectItem(QGraphicsSceneMouseEvent* event); 53 | public: 54 | QAbstractGraphicsShapeItem * item; 55 | 56 | QPointF c_down; 57 | QPointF c_last; 58 | static ItemShape c_drawShape; 59 | 60 | bool m_hoverSizer=false; 61 | 62 | static SelectMode m_selectMode; 63 | static SizeHandleRect::Direction nDragHandle ; 64 | }; 65 | 66 | #endif // UGRAPHICSSCENE_H 67 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/ugraphicsview.cpp: -------------------------------------------------------------------------------- 1 | #include "ugraphicsview.h" 2 | #include 3 | #include 4 | 5 | #define cout qDebug() << "[" <<__FILE__ <<":" <<__LINE__ << "]" 6 | 7 | 8 | UGraphicsView::UGraphicsView(QWidget *parent) : QGraphicsView (parent) 9 | { 10 | m_d_zoomDelta=0.1; // 缩放的增量 11 | m_d_scaleValue=1.0; 12 | 13 | setRenderHint(QPainter::Antialiasing);//抗锯齿 14 | //设置背景透明 15 | // this->setAttribute(Qt::WA_TranslucentBackground); 16 | setStyleSheet("background: transparent;border:0px"); 17 | 18 | } 19 | void UGraphicsView::ZoomIn() 20 | { 21 | zoom(1 + m_d_zoomDelta); 22 | } 23 | 24 | // 缩小 25 | void UGraphicsView::ZoomOut() 26 | { 27 | zoom(1 - m_d_zoomDelta); 28 | } 29 | void UGraphicsView::zoom(qreal scaleFactor) 30 | { 31 | // 防止过小或过大 32 | qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); 33 | if (factor < UGraphicsView::MIN_SCALE || factor > UGraphicsView::MAX_SCALE) 34 | return; 35 | 36 | scale(scaleFactor, scaleFactor); 37 | m_d_scaleValue *= scaleFactor; 38 | } 39 | 40 | void UGraphicsView::wheelEvent(QWheelEvent *event){ 41 | if(QApplication::keyboardModifiers() == Qt::ControlModifier) 42 | { 43 | // 滚轮的滚动量 44 | QPoint scrollAmount = event->angleDelta(); 45 | // 正值表示滚轮远离使用者(放大),负值表示朝向使用者(缩小) 46 | scrollAmount.y() > 0 ? ZoomIn() :ZoomOut(); 47 | } 48 | else{ 49 | QGraphicsView::wheelEvent(event); 50 | } 51 | } 52 | 53 | QSizeF UGraphicsView::getSize(){ 54 | return QSizeF(this->scene()->width()*m_d_scaleValue,this->scene()->height()*m_d_scaleValue); 55 | } 56 | 57 | -------------------------------------------------------------------------------- /PrinterEdit3_0/GraphicsControl/ugraphicsview.h: -------------------------------------------------------------------------------- 1 | #ifndef UGRAPHICSVIEW_H 2 | #define UGRAPHICSVIEW_H 3 | 4 | #include 5 | #include 6 | #include "dpitool.h" 7 | 8 | class UGraphicsView : public QGraphicsView,public DpiTool 9 | { 10 | Q_OBJECT 11 | public: 12 | explicit UGraphicsView(QWidget *parent = nullptr); 13 | void ZoomOut(); 14 | void ZoomIn(); 15 | QSizeF getSize(); 16 | qreal const MAX_SCALE=10; 17 | qreal const MIN_SCALE=0.1; 18 | qreal scaleValue(){ 19 | return m_d_scaleValue; 20 | }; 21 | signals: 22 | 23 | public slots: 24 | 25 | // QWidget interface 26 | protected: 27 | void wheelEvent(QWheelEvent *event); 28 | private: 29 | qreal m_d_scaleValue; // 缩放值 30 | qreal m_d_zoomDelta; // 缩放的增量 31 | void zoom(qreal scaleFactor); 32 | public: 33 | 34 | }; 35 | 36 | #endif // UGRAPHICSVIEW_H 37 | -------------------------------------------------------------------------------- /PrinterEdit3_0/PrinterEdit3_0.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2022-02-22T16:46:39 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui svg 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = PrinterEdit3_0 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which has been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | CONFIG += c++11 26 | 27 | SOURCES += \ 28 | main.cpp \ 29 | mainwindow.cpp \ 30 | GraphicsControl\ugraphicsview.cpp \ 31 | GraphicsControl\ugraphicsscene.cpp \ 32 | GraphicsControl\GraphicsItems\graphicsitembase.cpp \ 33 | GraphicsControl\sizehandle.cpp \ 34 | GraphicsControl/GraphicsItems/graphicsrectitem.cpp \ 35 | GraphicsControl/GraphicsItems/graphicsellipseitem.cpp \ 36 | UWidget/rluer.cpp \ 37 | GraphicsControl/GraphicsItems/graphicstextitem.cpp \ 38 | GraphicsControl/GraphicsItems/qgraphicstextitemex.cpp \ 39 | GraphicsControl/GraphicsItems/graphicslineitem.cpp 40 | 41 | HEADERS += \ 42 | mainwindow.h \ 43 | GraphicsControl\ugraphicsview.h \ 44 | GraphicsControl\ugraphicsscene.h \ 45 | GraphicsControl\GraphicsItems\graphicsitembase.h \ 46 | GraphicsControl\sizehandle.h \ 47 | GraphicsControl/GraphicsItems/graphicsrectitem.h \ 48 | GraphicsControl/GraphicsItems/graphicsellipseitem.h \ 49 | GraphicsControl\Enums.h \ 50 | UWidget/rluer.h \ 51 | GraphicsControl/dpitool.h \ 52 | GraphicsControl/GraphicsItems/graphicstextitem.h \ 53 | GraphicsControl/GraphicsItems/qgraphicstextitemex.h \ 54 | GraphicsControl/GraphicsItems/graphicslineitem.h 55 | 56 | FORMS += \ 57 | mainwindow.ui \ 58 | UWidget/rluer.ui 59 | 60 | # Default rules for deployment. 61 | qnx: target.path = /tmp/$${TARGET}/bin 62 | else: unix:!android: target.path = /opt/$${TARGET}/bin 63 | !isEmpty(target.path): INSTALLS += target 64 | 65 | RESOURCES += \ 66 | resources.qrc 67 | -------------------------------------------------------------------------------- /PrinterEdit3_0/PrinterEdit3_0.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {2047ff3f-7f58-4ed4-a52e-bd817466e4e9} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | -fno-delayed-template-parsing 60 | 61 | true 62 | 63 | 64 | 65 | ProjectExplorer.Project.Target.0 66 | 67 | Desktop Qt 5.12.2 MinGW 64-bit 68 | Desktop Qt 5.12.2 MinGW 64-bit 69 | qt.qt5.5122.win64_mingw73_kit 70 | 0 71 | 0 72 | 0 73 | 74 | D:/DeskTop/Leif/QtCode/day1/build-PrinterEdit3_0-Desktop_Qt_5_12_2_MinGW_64_bit-Debug 75 | 76 | 77 | true 78 | qmake 79 | 80 | QtProjectManager.QMakeBuildStep 81 | true 82 | 83 | false 84 | false 85 | false 86 | 87 | 88 | true 89 | Make 90 | 91 | Qt4ProjectManager.MakeStep 92 | 93 | false 94 | 95 | 96 | false 97 | 98 | 2 99 | Build 100 | 101 | ProjectExplorer.BuildSteps.Build 102 | 103 | 104 | 105 | true 106 | Make 107 | 108 | Qt4ProjectManager.MakeStep 109 | 110 | true 111 | clean 112 | 113 | false 114 | 115 | 1 116 | Clean 117 | 118 | ProjectExplorer.BuildSteps.Clean 119 | 120 | 2 121 | false 122 | 123 | Debug 124 | Debug 125 | Qt4ProjectManager.Qt4BuildConfiguration 126 | 2 127 | true 128 | 129 | 130 | D:/DeskTop/Leif/QtCode/day1/build-PrinterEdit3_0-Desktop_Qt_5_12_2_MinGW_64_bit-Release 131 | 132 | 133 | true 134 | qmake 135 | 136 | QtProjectManager.QMakeBuildStep 137 | false 138 | 139 | false 140 | false 141 | true 142 | 143 | 144 | true 145 | Make 146 | 147 | Qt4ProjectManager.MakeStep 148 | 149 | false 150 | 151 | 152 | false 153 | 154 | 2 155 | Build 156 | 157 | ProjectExplorer.BuildSteps.Build 158 | 159 | 160 | 161 | true 162 | Make 163 | 164 | Qt4ProjectManager.MakeStep 165 | 166 | true 167 | clean 168 | 169 | false 170 | 171 | 1 172 | Clean 173 | 174 | ProjectExplorer.BuildSteps.Clean 175 | 176 | 2 177 | false 178 | 179 | Release 180 | Release 181 | Qt4ProjectManager.Qt4BuildConfiguration 182 | 0 183 | true 184 | 185 | 186 | D:/DeskTop/Leif/QtCode/day1/build-PrinterEdit3_0-Desktop_Qt_5_12_2_MinGW_64_bit-Profile 187 | 188 | 189 | true 190 | qmake 191 | 192 | QtProjectManager.QMakeBuildStep 193 | true 194 | 195 | false 196 | true 197 | true 198 | 199 | 200 | true 201 | Make 202 | 203 | Qt4ProjectManager.MakeStep 204 | 205 | false 206 | 207 | 208 | false 209 | 210 | 2 211 | Build 212 | 213 | ProjectExplorer.BuildSteps.Build 214 | 215 | 216 | 217 | true 218 | Make 219 | 220 | Qt4ProjectManager.MakeStep 221 | 222 | true 223 | clean 224 | 225 | false 226 | 227 | 1 228 | Clean 229 | 230 | ProjectExplorer.BuildSteps.Clean 231 | 232 | 2 233 | false 234 | 235 | Profile 236 | Profile 237 | Qt4ProjectManager.Qt4BuildConfiguration 238 | 0 239 | true 240 | 241 | 3 242 | 243 | 244 | 0 245 | 部署 246 | 247 | ProjectExplorer.BuildSteps.Deploy 248 | 249 | 1 250 | Deploy Configuration 251 | 252 | ProjectExplorer.DefaultDeployConfiguration 253 | 254 | 1 255 | 256 | 257 | false 258 | false 259 | 1000 260 | 261 | true 262 | 263 | false 264 | false 265 | false 266 | false 267 | true 268 | 0.01 269 | 10 270 | true 271 | 1 272 | 25 273 | 274 | 1 275 | true 276 | false 277 | true 278 | valgrind 279 | 280 | 0 281 | 1 282 | 2 283 | 3 284 | 4 285 | 5 286 | 6 287 | 7 288 | 8 289 | 9 290 | 10 291 | 11 292 | 12 293 | 13 294 | 14 295 | 296 | 2 297 | 298 | PrinterEdit3_0 299 | PrinterEdit3_02 300 | Qt4ProjectManager.Qt4RunConfiguration:D:/DeskTop/Leif/QtCode/PrinterEditor/PrinterEdit3_0/PrinterEdit3_0.pro 301 | PrinterEdit3_0.pro 302 | 303 | 3768 304 | false 305 | true 306 | true 307 | false 308 | false 309 | true 310 | 311 | D:/DeskTop/Leif/QtCode/day1/build-PrinterEdit3_0-Desktop_Qt_5_12_2_MinGW_64_bit-Debug 312 | 313 | 1 314 | 315 | 316 | 317 | ProjectExplorer.Project.TargetCount 318 | 1 319 | 320 | 321 | ProjectExplorer.Project.Updater.FileVersion 322 | 20 323 | 324 | 325 | Version 326 | 20 327 | 328 | 329 | -------------------------------------------------------------------------------- /PrinterEdit3_0/UWidget/rluer.cpp: -------------------------------------------------------------------------------- 1 | #include "rluer.h" 2 | #include "ui_rluer.h" 3 | #include "GraphicsControl/ugraphicsscene.h" 4 | #include 5 | #include 6 | #include 7 | 8 | Rluer::Rluer(QWidget *parent) : 9 | QWidget(parent), 10 | ui(new Ui::Rluer) 11 | { 12 | ui->setupUi(this); 13 | installEventFilter(this); 14 | setMouseTracking(true); 15 | //重新设置弹簧的大小 16 | ui->topMargin->changeSize(20,m_rluerHeight); 17 | ui->leftMargin->changeSize(m_rluerHeight,20); 18 | this->layout()->invalidate(); 19 | 20 | 21 | m_scene = new UGraphicsScene(this); 22 | ui->graphicsView->setScene(m_scene); 23 | connect(m_scene,&UGraphicsScene::initSizeOverEvent, this,&Rluer::updateOffset); 24 | connect(m_scene,&UGraphicsScene::rluerDispalyItemPoint, [&](){this->update();}); 25 | m_scene->createArea(QRectF(0 , 0 , 100, 100)); 26 | connect(ui->graphicsView->horizontalScrollBar(),&QScrollBar::valueChanged,this,&Rluer::updateOffset); 27 | connect(ui->graphicsView->verticalScrollBar(),&QScrollBar::valueChanged,this,&Rluer::updateOffset); 28 | 29 | } 30 | 31 | Rluer::~Rluer() 32 | { 33 | delete ui; 34 | } 35 | 36 | void Rluer::showEvent(QShowEvent*){ 37 | updateOffset(); 38 | } 39 | 40 | void Rluer::resizeEvent(QResizeEvent*){ 41 | updateOffset(); 42 | } 43 | 44 | //更新scene的相对位置,单位mm 45 | void Rluer::updateOffset(){ 46 | setOffsetX(ui->graphicsView->pixelToMM(ui->graphicsView->mapToScene(0,0).x())); 47 | setOffsetY(ui->graphicsView->pixelToMM(ui->graphicsView->mapToScene(0,0).y())); 48 | } 49 | 50 | UGraphicsView* Rluer::getView(){ 51 | return ui->graphicsView; 52 | } 53 | 54 | void Rluer::wheelEvent(QWheelEvent *event) 55 | { 56 | if(QApplication::keyboardModifiers() == Qt::ControlModifier) 57 | { 58 | //获取scene相对于view的坐标 59 | updateOffset(); 60 | } 61 | else{ 62 | QWidget::wheelEvent(event); 63 | } 64 | 65 | } 66 | void Rluer::paintEvent(QPaintEvent *event){ 67 | 68 | QPainter* painter=new QPainter(this); 69 | drawRluer(painter); 70 | QWidget::paintEvent(event); 71 | } 72 | 73 | void Rluer::drawRluer(QPainter* painter){ 74 | qreal _scaleValue=ui->graphicsView->scaleValue(); 75 | qreal actualWidth=width(); 76 | qreal actualHeight=height(); 77 | qreal oneMMPixel=ui->graphicsView->getOneMMPixel(); 78 | qreal mm=oneMMPixel * _scaleValue;//1毫米所占像素 79 | painter->fillRect(rect(),Qt::gray); 80 | painter->setRenderHint(QPainter::Qt4CompatiblePainting);//抗锯齿 81 | painter->fillRect(QRectF(0,0,actualWidth,m_rluerHeight),QBrush(Qt::GlobalColor::white)); 82 | painter->fillRect(QRectF(0,m_rluerHeight,m_rluerHeight,actualHeight),QBrush(Qt::GlobalColor::white)); 83 | qreal rluerHeight=m_rluerHeight-1; 84 | painter->drawLine(QPointF(rluerHeight,rluerHeight),QPointF(rluerHeight,actualHeight)); 85 | painter->drawLine(QPointF(rluerHeight,rluerHeight),QPointF(actualWidth,rluerHeight)); 86 | //设置绘图有效区域 87 | QRegion region1(QRect(m_rluerHeight, 0, actualWidth, m_rluerHeight)); 88 | QRegion region2(QRect(0,m_rluerHeight,m_rluerHeight,actualHeight)); 89 | painter->setClipRegion(region1+region2); 90 | 91 | cm=_scaleValue<0.3?50:10;//缩放比例小于0.3时,改为50cm画一次10cm分界线 92 | 93 | qreal marginTop=ui->graphicsView->pixelToMM(m_scene->getMargin()->top()); 94 | qreal newOffsetY=offsetY-marginTop; 95 | int j=qRound(newOffsetY); 96 | qreal difference =(newOffsetY-j)*mm;//坐标尺是整数,所以要计算小数(毫米)所占的像素数 97 | for(qreal i=m_rluerHeight-difference;isetPen(Qt::GlobalColor::red); 102 | else 103 | painter->setPen(Qt::GlobalColor::black); 104 | painter->drawText(QPointF(0,i+12),QString::number(j)); 105 | painter->setPen(Qt::GlobalColor::black); 106 | painter->drawLine(QPointF(0,i),QPointF( m_rluerHeight,i)); 107 | } 108 | else if(j%cmHalf==0) 109 | { 110 | painter->drawLine(QPointF(m_rluerHeight-RluerHeightHalf,i),QPointF( m_rluerHeight,i)); 111 | } 112 | else 113 | { 114 | if(_scaleValue>0.5) 115 | painter->drawLine(QPointF(m_rluerHeight-RluerHeightHalfHalf,i),QPointF( m_rluerHeight,i)); 116 | } 117 | } 118 | 119 | qreal marginLeft=ui->graphicsView->pixelToMM(m_scene->getMargin()->left()); 120 | qreal newOffsetX=offsetX-marginLeft;//减去边距所占毫米数 121 | j=qRound(newOffsetX); 122 | difference=(newOffsetX-j)*mm;//坐标尺是整数,所以要计算小数所占的像素数 123 | for(double i=m_rluerHeight-difference;isetPen(Qt::GlobalColor::red); 128 | else 129 | painter->setPen(Qt::GlobalColor::black); 130 | painter->drawText(QPointF(i,15),QString::number(j)); 131 | painter->setPen(Qt::GlobalColor::black); 132 | painter->drawLine(QPointF(i,0),QPointF(i, m_rluerHeight)); 133 | } 134 | else if(j%cmHalf==0) 135 | { 136 | painter->drawLine(QPointF(i,m_rluerHeight-RluerHeightHalf),QPointF(i,m_rluerHeight)); 137 | } 138 | else 139 | { 140 | if(_scaleValue>0.5) 141 | painter->drawLine(QPointF(i,m_rluerHeight-RluerHeightHalfHalf),QPointF(i, m_rluerHeight)); 142 | } 143 | } 144 | 145 | //选中item时发生 146 | drawSelectedElement(painter); 147 | painter->end(); 148 | 149 | } 150 | void Rluer::drawSelectedElement(QPainter* painter){ 151 | QList items= m_scene->selectedItems(); 152 | if(items.count()==0){ 153 | return; 154 | } 155 | if(items.count()==1){ 156 | 157 | qreal _scaleValue=ui->graphicsView->scaleValue(); 158 | QPointF b=ui->graphicsView->mapToScene(0,0); 159 | //计算item在视场中的位置 160 | QRectF rect=items.first()->boundingRect(); 161 | qreal x1= m_rluerHeight+(items.first()->x()+rect.x()-b.x())*_scaleValue; 162 | qreal y1= m_rluerHeight+(items.first()->y()+rect.y()-b.y())*_scaleValue; 163 | qreal x2= x1+(rect.width()*_scaleValue); 164 | qreal y2= y1+(rect.height()*_scaleValue); 165 | 166 | painter->fillRect(QRectF(x1,m_rluerHeight-5,x2-x1, 5),Qt::green); 167 | painter->fillRect(QRectF(m_rluerHeight-5,y1,5,y2-y1),Qt::green); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /PrinterEdit3_0/UWidget/rluer.h: -------------------------------------------------------------------------------- 1 | #ifndef RLUER_H 2 | #define RLUER_H 3 | 4 | #include 5 | #include "GraphicsControl/ugraphicsview.h" 6 | #include "GraphicsControl/ugraphicsscene.h" 7 | 8 | namespace Ui { 9 | class Rluer; 10 | } 11 | 12 | class Rluer : public QWidget 13 | { 14 | Q_OBJECT 15 | 16 | public: 17 | explicit Rluer(QWidget *parent = nullptr); 18 | ~Rluer(); 19 | UGraphicsView* getView(); 20 | void setOffsetX(qreal value) { 21 | offsetX = value; 22 | update(); 23 | } 24 | void setOffsetY(qreal value) { 25 | offsetY = value; 26 | update(); 27 | } 28 | public slots: 29 | void updateOffset(); 30 | private: 31 | Ui::Rluer *ui; 32 | 33 | UGraphicsScene * m_scene; 34 | qreal offsetX{0}; 35 | qreal offsetY{0}; 36 | //画尺子所需变量start 37 | int cm=10; 38 | int cmHalf=5; 39 | int m_rluerHeight=60; 40 | int RluerHeightHalf=m_rluerHeight>>1; 41 | int RluerHeightHalfHalf=RluerHeightHalf>>1; 42 | //画尺子所需变量end 43 | // QWidget interface 44 | protected: 45 | void paintEvent(QPaintEvent *event); 46 | void wheelEvent(QWheelEvent *event); 47 | void resizeEvent(QResizeEvent *event); 48 | void showEvent(QShowEvent *event); 49 | private: 50 | void drawRluer(QPainter* painter); 51 | void drawSelectedElement(QPainter* painter); 52 | 53 | 54 | }; 55 | 56 | #endif // RLUER_H 57 | -------------------------------------------------------------------------------- /PrinterEdit3_0/UWidget/rluer.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Rluer 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 0 19 | 20 | 21 | 0 22 | 23 | 24 | 0 25 | 26 | 27 | 0 28 | 29 | 30 | 0 31 | 32 | 33 | 34 | 35 | Qt::Horizontal 36 | 37 | 38 | QSizePolicy::Fixed 39 | 40 | 41 | 42 | 20 43 | 20 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | Qt::Vertical 55 | 56 | 57 | QSizePolicy::Fixed 58 | 59 | 60 | 61 | 20 62 | 20 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | UGraphicsView 72 | QGraphicsView 73 |
GraphicsControl\ugraphicsview.h
74 |
75 |
76 | 77 | 78 |
79 | -------------------------------------------------------------------------------- /PrinterEdit3_0/image/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/.DS_Store -------------------------------------------------------------------------------- /PrinterEdit3_0/image/background1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/background1.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/background2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/background2.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/background3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/background3.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/background4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/background4.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/bold.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/bold.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/bringtofront.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/bringtofront.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/circle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/circle.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/delete.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/floodfill.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/floodfill.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/italic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/italic.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/linecolor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/linecolor.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/linepointer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/linepointer.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/ok.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/ok.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/pointer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/pointer.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/rectangle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/rectangle.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/rotation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/rotation.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/save-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/save-5.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/sendtoback.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/sendtoback.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/textpointer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/textpointer.png -------------------------------------------------------------------------------- /PrinterEdit3_0/image/underline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Leifxhli12138/PrinterEditor/a9e81788cecf5f72c787f03a7a0b6e592ec379af/PrinterEdit3_0/image/underline.png -------------------------------------------------------------------------------- /PrinterEdit3_0/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /PrinterEdit3_0/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | #include "GraphicsControl/ugraphicsscene.h" 4 | #include 5 | #include 6 | #include 7 | #include "GraphicsControl/GraphicsItems/graphicsitembase.h" 8 | #include 9 | #include "UWidget/rluer.h" 10 | 11 | MainWindow::MainWindow(QWidget *parent) : 12 | QMainWindow(parent), 13 | ui(new Ui::MainWindow) 14 | { 15 | ui->setupUi(this); 16 | group = new QActionGroup(this); 17 | group->addAction(ui->actionSelection); 18 | group->addAction(ui->actionEllipse); 19 | group->addAction(ui->actionRectangle); 20 | group->addAction(ui->actionText); 21 | group->addAction(ui->actionLine); 22 | group->addAction(ui->actionText2); 23 | ui->actionSelection->setChecked(true); 24 | 25 | resize(800,600); 26 | 27 | connect(ui->actionSelection,SIGNAL(triggered()),this,SLOT(addShape())); 28 | connect(ui->actionEllipse,SIGNAL(triggered()),this,SLOT(addShape())); 29 | connect(ui->actionRectangle,SIGNAL(triggered()),this,SLOT(addShape())); 30 | connect(ui->actionText,SIGNAL(triggered()),this,SLOT(addShape())); 31 | connect(ui->actionLine,SIGNAL(triggered()),this,SLOT(addShape())); 32 | connect(ui->actionText2,SIGNAL(triggered()),this,SLOT(addShape())); 33 | 34 | // foreach(QAction* a,group->actions()){ 35 | // connect(a,SIGNAL(triggered()),this,SLOT(addShape())); 36 | // } 37 | connect(&m_timer,SIGNAL(timeout()),this,SLOT(updateUI())); 38 | m_timer.start(100); 39 | } 40 | 41 | MainWindow::~MainWindow() 42 | { 43 | delete ui; 44 | } 45 | 46 | void MainWindow::addShape() 47 | { 48 | if ( sender() == ui->actionEllipse ) 49 | UGraphicsScene::c_drawShape =ItemShape::ellipse; 50 | if ( sender() == ui->actionRectangle ) 51 | UGraphicsScene::c_drawShape = ItemShape::rectangle; 52 | if ( sender() == ui->actionSelection ) 53 | UGraphicsScene::c_drawShape = ItemShape::selection; 54 | if ( sender() == ui->actionLine ) 55 | UGraphicsScene::c_drawShape = ItemShape::line; 56 | if ( sender() == ui->actionText) 57 | UGraphicsScene::c_drawShape = ItemShape::text; 58 | if ( sender() == ui->actionText2) 59 | UGraphicsScene::c_drawShape = ItemShape::text2; 60 | } 61 | 62 | void MainWindow::updateUI() 63 | { 64 | if ( UGraphicsScene::c_drawShape == ItemShape::ellipse ) 65 | ui->actionEllipse->setChecked(true); 66 | else if ( UGraphicsScene::c_drawShape == ItemShape::rectangle ) 67 | ui->actionRectangle->setChecked(true); 68 | else if (UGraphicsScene::c_drawShape == ItemShape::selection ) 69 | ui->actionSelection->setChecked(true); 70 | else if (UGraphicsScene::c_drawShape == ItemShape::text ) 71 | ui->actionText->setChecked(true); 72 | else if (UGraphicsScene::c_drawShape == ItemShape::line ) 73 | ui->actionLine->setChecked(true); 74 | else if (UGraphicsScene::c_drawShape == ItemShape::text2 ) 75 | ui->actionText2->setChecked(true); 76 | 77 | } 78 | 79 | -------------------------------------------------------------------------------- /PrinterEdit3_0/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include "GraphicsControl/ugraphicsscene.h" 8 | #include "ui_mainwindow.h" 9 | 10 | QT_BEGIN_NAMESPACE 11 | class QAction; 12 | class QActionGroup; 13 | class QMenu; 14 | class QPlainTextEdit; 15 | QT_END_NAMESPACE 16 | namespace Ui { 17 | class MainWindow; 18 | } 19 | 20 | class MainWindow : public QMainWindow 21 | { 22 | Q_OBJECT 23 | 24 | public: 25 | explicit MainWindow(QWidget *parent = 0); 26 | ~MainWindow(); 27 | 28 | public slots: 29 | void addShape() ; 30 | void updateUI(); 31 | 32 | private: 33 | Ui::MainWindow *ui; 34 | QActionGroup * group; 35 | QTimer m_timer; 36 | QString path; 37 | }; 38 | 39 | #endif // MAINWINDOW_H 40 | -------------------------------------------------------------------------------- /PrinterEdit3_0/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | TopToolBarArea 26 | 27 | 28 | false 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | true 40 | 41 | 42 | 43 | :/image/pointer.png:/image/pointer.png 44 | 45 | 46 | 1 47 | 48 | 49 | 鼠标 50 | 51 | 52 | 53 | 54 | true 55 | 56 | 57 | 58 | :/image/circle.png:/image/circle.png 59 | 60 | 61 | 2 62 | 63 | 64 | 圆形 65 | 66 | 67 | 68 | 69 | true 70 | 71 | 72 | 73 | :/image/rectangle.png:/image/rectangle.png 74 | 75 | 76 | 3 77 | 78 | 79 | 矩形 80 | 81 | 82 | 83 | 84 | true 85 | 86 | 87 | 88 | :/image/textpointer.png:/image/textpointer.png 89 | 90 | 91 | 4 92 | 93 | 94 | 常规文本 95 | 96 | 97 | 98 | 99 | true 100 | 101 | 102 | 103 | :/image/linepointer.png:/image/linepointer.png 104 | 105 | 106 | 5 107 | 108 | 109 | 线段 110 | 111 | 112 | 113 | 114 | true 115 | 116 | 117 | 118 | :/image/textpointer.png:/image/textpointer.png 119 | 120 | 121 | 6 122 | 123 | 124 | 变形文本 125 | 126 | 127 | 128 | 129 | 130 | 131 | Rluer 132 | QWidget 133 |
UWidget\rluer.h
134 | 1 135 |
136 |
137 | 138 | 139 | 140 | 141 |
142 | -------------------------------------------------------------------------------- /PrinterEdit3_0/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | image/.DS_Store 4 | image/background1.png 5 | image/background2.png 6 | image/background3.png 7 | image/background4.png 8 | image/bold.png 9 | image/bringtofront.png 10 | image/circle.png 11 | image/delete.png 12 | image/floodfill.png 13 | image/italic.png 14 | image/linecolor.png 15 | image/linepointer.png 16 | image/ok.png 17 | image/pointer.png 18 | image/rectangle.png 19 | image/rotation.png 20 | image/save-5.png 21 | image/sendtoback.png 22 | image/textpointer.png 23 | image/underline.png 24 | 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PrinterEditor 2 | QT做的打印编辑器--根据机器dpi按比例显示的真实单位--功能还在完善中 3 | ![image](https://user-images.githubusercontent.com/46430806/163780814-8192cd49-f0f1-4370-9079-bb8a656c9b38.png) 4 | --------------------------------------------------------------------------------