void WGraphicsPolygonItem::paint(WPainter* painter) { WPainterProxy proxy(painter); painter->setPen(data()->pen); painter->setBrush(data()->brush); painter->drawPolygon(data()->polygon); //绘制多边形 if (isSelected()) //多边形被选中 { painter->setPen(CP_PEN); painter->setBrush(CP_BRUSH); //绘制控制点 for (unsigned i=0; i<_cp.size(); ++i) { painter->drawEllipse(WWorldRectF(_cp[i].x() - CP_RADIUS, _cp[i].y() - CP_RADIUS, CP_RADIUS * 2, CP_RADIUS * 2)); painter->drawEllipse(WWorldRectF(data()->polygon[i].x() - CP_RADIUS, data()->polygon[i].y() - CP_RADIUS, CP_RADIUS * 2, CP_RADIUS * 2)); } if (_dragging) //多边形被拖动,绘制拖动后的多边形 { WPen dragPen = data()->pen; WBrush dragBrush = data()->brush; dragPen.setColor(dragPen.color().lighter(2.0)); dragBrush.setColor(dragBrush.color().lighter(2.0)); painter->setPen(dragPen); painter->setBrush(dragBrush); painter->drawPolygon(draggedPolygon(data()->polygon, scene()->dragStartPos(), scene()->attachedPoint(scene()->mousePos()), _select_flag)); } } }
QPen transform(const WPen& pen) { return QPen(QBrush(transform(pen.color())), pen.width(), static_cast<Qt::PenStyle>(pen.style())); }
void WGraphicsRectItem::paint(WPainter* painter) { WPainterProxy proxy(painter); painter->setRenderHint(Ws::Antialiasing, false); painter->setRenderHint(Ws::TextAntialiasing, false); painter->setWorldTransform(getRotateMatrix(), true); //设置旋转矩阵 painter->setBrush(brush()); painter->setPen(pen()); painter->drawRect(data()->rect); //绘制矩形 if (isSelected()) //矩形被选中,绘制所有的控制点 { painter->drawLine(WWorldLineF(_rotate_pt, WWorldPointF(_rotate_pt.x(), data()->rect.top()))); painter->setPen(CP_PEN); painter->setBrush(CP_BRUSH); for (unsigned i=0; i<array_size(_cp); ++i) { if (i & 1) { painter->drawRect(WWorldRectF(_cp[i].x() - CP_RADIUS, _cp[i].y() - CP_RADIUS, CP_RADIUS * 2, CP_RADIUS * 2)); } else { painter->drawEllipse(WWorldRectF(_cp[i].x() - CP_RADIUS, _cp[i].y() - CP_RADIUS, CP_RADIUS * 2, CP_RADIUS * 2)); } } painter->setBrush(ROTATE_BRUSH); painter->drawEllipse(WWorldRectF(_rotate_pt.x() - CP_RADIUS, _rotate_pt.y() - CP_RADIUS, CP_RADIUS * 2, CP_RADIUS * 2)); } if (_dragging) //矩形在拖动,绘制正在拖动的矩形 { WWorldPointF pos = scene()->attachedPoint(scene()->mousePos()); WPen dragPen = data()->pen; WBrush dragBrush = data()->brush; dragPen.setColor(dragPen.color().lighter(2.0)); dragBrush.setColor(dragBrush.color().lighter(2.0)); painter->setPen(dragPen); painter->setBrush(dragBrush); if (_select_flag != SF_ROTATE) { if (_select_flag != SF_CONTENT || isMovable()) { WWorldPointF offset = pos - scene()->dragStartPos(); const int (&f)[4] = ADJUST_TABLE[bit(_select_flag)]; painter->drawRect(data()->rect.adjusted(offset.x() * f[0], offset.y() * f[1], offset.x() * f[2], offset.y() * f[3])); } } else { painter->setWorldTransform(getRotateMatrix_1(), true); WWorldPointF c = data()->rect.center(); double ag = WWorldLineF(c, pos).angle() - 90; painter->setWorldTransform(WMatrix().translate(-c.x(), -c.y()) * WMatrix().rotate(ag + data()->angle) * WMatrix().translate(c.x(), c.y())); painter->drawRect(data()->rect); } } }