59 { |
62 { |
60 if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
63 if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
61 { |
64 { |
62 QPainterPath path = m_currPath->path(); |
65 QPainterPath path = m_currPath->path(); |
63 |
66 |
64 if(mouseEvent->modifiers() & Qt::ControlModifier) |
67 switch (m_pathType) |
65 { |
68 { |
66 int c = path.elementCount(); |
69 case Polyline: |
67 QPointF pos = mouseEvent->scenePos(); |
70 if(mouseEvent->modifiers() & Qt::ControlModifier) |
68 path.setElementPositionAt(c - 1, pos.x(), pos.y()); |
71 { |
69 |
72 int c = path.elementCount(); |
70 } |
73 QPointF pos = mouseEvent->scenePos(); |
71 else |
74 path.setElementPositionAt(c - 1, pos.x(), pos.y()); |
72 { |
75 |
73 path.lineTo(mouseEvent->scenePos()); |
76 } |
74 paths.first().points.append(mouseEvent->scenePos().toPoint()); |
77 else |
75 } |
78 { |
|
79 path.lineTo(mouseEvent->scenePos()); |
|
80 paths.first().points.append(mouseEvent->scenePos().toPoint()); |
|
81 } |
|
82 break; |
|
83 case Rectangle: { |
|
84 path = QPainterPath(); |
|
85 QPointF p1 = paths.first().initialPoint; |
|
86 QPointF p2 = mouseEvent->scenePos(); |
|
87 path.moveTo(p1); |
|
88 path.lineTo(p1.x(), p2.y()); |
|
89 path.lineTo(p2); |
|
90 path.lineTo(p2.x(), p1.y()); |
|
91 path.lineTo(p1); |
|
92 break; |
|
93 } |
|
94 case Ellipse: { |
|
95 path = QPainterPath(); |
|
96 QList<QPointF> points = makeEllipse(paths.first().initialPoint, mouseEvent->scenePos()); |
|
97 path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points))); |
|
98 break; |
|
99 } |
|
100 } |
|
101 |
76 m_currPath->setPath(path); |
102 m_currPath->setPath(path); |
77 |
103 |
78 emit pathChanged(); |
104 emit pathChanged(); |
79 } |
105 } |
80 |
106 |
94 path.lineTo(mouseEvent->scenePos()); |
120 path.lineTo(mouseEvent->scenePos()); |
95 |
121 |
96 PathParams params; |
122 PathParams params; |
97 params.width = serializePenWidth(m_pen.width()); |
123 params.width = serializePenWidth(m_pen.width()); |
98 params.erasing = m_isErasing; |
124 params.erasing = m_isErasing; |
99 params.points = QList<QPoint>() << mouseEvent->scenePos().toPoint(); |
125 params.initialPoint = mouseEvent->scenePos().toPoint(); |
|
126 params.points = QList<QPoint>() << params.initialPoint; |
100 paths.prepend(params); |
127 paths.prepend(params); |
101 m_currPath->setPath(path); |
128 m_currPath->setPath(path); |
102 |
129 |
103 emit pathChanged(); |
130 emit pathChanged(); |
104 } |
131 } |
105 |
132 |
106 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
133 void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
107 { |
134 { |
108 if (m_currPath) |
135 if (m_currPath) |
109 { |
136 { |
110 QPainterPath path = m_currPath->path(); |
137 switch (m_pathType) |
111 path.lineTo(mouseEvent->scenePos()); |
138 { |
112 paths.first().points.append(mouseEvent->scenePos().toPoint()); |
139 case Polyline: { |
113 m_currPath->setPath(path); |
140 QPainterPath path = m_currPath->path(); |
|
141 path.lineTo(mouseEvent->scenePos()); |
|
142 paths.first().points.append(mouseEvent->scenePos().toPoint()); |
|
143 m_currPath->setPath(path); |
|
144 break; |
|
145 } |
|
146 case Rectangle: { |
|
147 QPoint p1 = paths.first().initialPoint; |
|
148 QPoint p2 = mouseEvent->scenePos().toPoint(); |
|
149 QList<QPoint> rpoints; |
|
150 rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1; |
|
151 paths.first().points = rpoints; |
|
152 break; |
|
153 } |
|
154 case Ellipse: |
|
155 QPoint p1 = paths.first().initialPoint; |
|
156 QPoint p2 = mouseEvent->scenePos().toPoint(); |
|
157 QList<QPointF> points = makeEllipse(p1, p2); |
|
158 QList<QPoint> epoints; |
|
159 foreach(const QPointF & p, points) |
|
160 epoints.append(p.toPoint()); |
|
161 paths.first().points = epoints; |
|
162 break; |
|
163 } |
114 |
164 |
115 simplifyLast(); |
165 simplifyLast(); |
116 |
166 |
117 m_currPath = 0; |
167 m_currPath = 0; |
118 } |
168 } |
381 |
431 |
382 int DrawMapScene::deserializePenWidth(quint8 width) |
432 int DrawMapScene::deserializePenWidth(quint8 width) |
383 { |
433 { |
384 return width * 10 + 6; |
434 return width * 10 + 6; |
385 } |
435 } |
|
436 |
|
437 void DrawMapScene::setPathType(PathType pathType) |
|
438 { |
|
439 m_pathType = pathType; |
|
440 } |
|
441 |
|
442 QList<QPointF> DrawMapScene::makeEllipse(const QPointF ¢er, const QPointF &corner) |
|
443 { |
|
444 QList<QPointF> l; |
|
445 qreal r = (center - corner).manhattanLength(); |
|
446 qreal rx = qAbs(center.x() - corner.x()); |
|
447 qreal ry = qAbs(center.y() - corner.y()); |
|
448 |
|
449 if(r < 4) |
|
450 { |
|
451 l.append(center); |
|
452 } else |
|
453 { |
|
454 qreal angleDelta = 12 / r; |
|
455 for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta) |
|
456 l.append(center + QPointF(rx * cos(angle), ry * sin(angle))); |
|
457 l.append(l.first()); |
|
458 } |
|
459 |
|
460 return l; |
|
461 } |