1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| #ifndef JRPAINTER_H #define JRPAINTER_H #include <QQuickPaintedItem> #include <QVector> #include <QPointF> #include <QImage>
class JRPainter : public QQuickPaintedItem { Q_OBJECT
public: JRPainter(QQuickItem *parent = 0);
void paint(QPainter *painter);
void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);
Q_INVOKABLE void init(); Q_INVOKABLE void stop() { qDebug() << "Gemini::stop() called"; } enum shape { Line = 1,Ellipse,Rect,Triangle }; Q_ENUM(shape) Q_INVOKABLE void setShape(shape); Q_INVOKABLE void setPenWidth(int w); Q_INVOKABLE void setPenColor(QColor c); signals:
public slots:
private: void drawShape(QImage &theImage);
private: QVector<QPointF> point; QImage m_bgImage; QImage m_tempImage; bool isDrawing; QPointF m_nowPoint; QPointF m_lastPoint; JRPainter::shape type; int penWidth; QColor penColor; };
#endif
#include "jrpainter.h" #include <QPainter> #include <QImage>
JRPainter::JRPainter(QQuickItem *parent): isDrawing(false), type(Line), penWidth(4), penColor(QColor(255,0,0)) { setAcceptedMouseButtons(Qt::LeftButton); }
void JRPainter::setShape(JRPainter::shape t) { type = t; }
void JRPainter::setPenWidth(int w) { penWidth = w; }
void JRPainter::setPenColor(QColor c) { penColor = c; }
void JRPainter::paint(QPainter *painter) { if(isDrawing){ painter->drawImage(0,0,m_tempImage); }else{ painter->drawImage(0,0,m_bgImage); } } void JRPainter::init() { QImage img(":/res/background/Farm.svg"); m_bgImage = img; }
void JRPainter::mousePressEvent(QMouseEvent *event) { m_lastPoint = event->localPos(); isDrawing = true; }
void JRPainter::mouseMoveEvent(QMouseEvent *event) { m_nowPoint = event->localPos(); m_tempImage = m_bgImage; if(type == Line){ drawShape(m_bgImage); }else{ drawShape(m_tempImage); } }
void JRPainter::mouseReleaseEvent(QMouseEvent *event) { isDrawing = false; if (type != Line) { drawShape(m_bgImage); } }
void JRPainter::drawShape(QImage &theImage) { QPainter painter(&theImage); QPen apen; apen.setWidth(penWidth); apen.setColor(penColor); painter.setPen(apen); painter.setRenderHint(QPainter::Antialiasing, true); int x1, y1, x2, y2; x1 = m_lastPoint.x(); y1 = m_lastPoint.y(); x2 = m_nowPoint.x(); y2 = m_nowPoint.y();
switch (type) { case JRPainter::Line: { painter.drawLine(m_lastPoint,m_nowPoint); m_lastPoint = m_nowPoint; break; } case JRPainter::Ellipse: { painter.drawEllipse(x1,y1,x2-x1,y2-y1); break; } case JRPainter::Rect: { painter.drawRect(x1, y1, x2 - x1, y2 - y1); break; } case JRPainter::Triangle: { int top, buttom, left, right; top = (y1 < y2) ? y1 : y2; buttom = (y1 > y2) ? y1 : y2; left = (x1 < x2) ? x1 : x2; right = (x1 > x2) ? x1 : x2; if (y1 < y2) { QPoint points[3] = { QPoint(left,buttom), QPoint(right,buttom), QPoint((right + left) / 2,top) }; painter.drawPolygon(points, 3); } else { QPoint points[3] = { QPoint(left,top), QPoint(right,top), QPoint((left + right) / 2,buttom) }; painter.drawPolygon(points, 3); } break; } } update(); }
|