|
| 1 | +#include "QmlNetPaintedItem.h" |
| 2 | +#include <QPainter> |
| 3 | + |
| 4 | +QmlNetPaintedItem::QmlNetPaintedItem(QQuickItem *parent) |
| 5 | + : QQuickPaintedItem(parent) |
| 6 | +{ |
| 7 | +} |
| 8 | + |
| 9 | +QObject* QmlNetPaintedItem::paintHandler() const { |
| 10 | + return m_paintHandler; |
| 11 | +} |
| 12 | + |
| 13 | +void QmlNetPaintedItem::setPaintHandler(QObject* paintHandler) { |
| 14 | + m_paintHandler = paintHandler; |
| 15 | + if(m_paintHandler != nullptr) { |
| 16 | + QMetaObject::invokeMethod(m_paintHandler, "__setPaintedItem", Q_ARG(QVariant, QVariant::fromValue((int64_t)this))); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +void QmlNetPaintedItem::beginRecordPaintActions() { |
| 22 | + m_isRecording = true; |
| 23 | + m_recordedPaintActions.clear(); |
| 24 | +} |
| 25 | + |
| 26 | +void QmlNetPaintedItem::endRecordPaintActions() { |
| 27 | + m_isRecording = false; |
| 28 | + m_paintActionMutex.lock(); |
| 29 | + m_paintActions = m_recordedPaintActions; |
| 30 | + m_paintActionMutex.unlock(); |
| 31 | + update(); |
| 32 | +} |
| 33 | + |
| 34 | +void QmlNetPaintedItem::setPen(QString colorString) { |
| 35 | + checkRecordingAndAdd([colorString](QPainter* p) { |
| 36 | + QColor color(colorString); |
| 37 | + QPen pen(color); |
| 38 | + p->setPen(color); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +void QmlNetPaintedItem::resetPen() { |
| 43 | + checkRecordingAndAdd([](QPainter* p) { |
| 44 | + p->setPen(QPen()); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +void QmlNetPaintedItem::setBrush(QString colorString) { |
| 49 | + checkRecordingAndAdd([colorString](QPainter* p) { |
| 50 | + QColor color(colorString); |
| 51 | + QBrush brush(color); |
| 52 | + p->setBrush(brush); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +void QmlNetPaintedItem::resetBrush() { |
| 57 | + checkRecordingAndAdd([](QPainter* p) { |
| 58 | + p->setBrush(QBrush()); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +void QmlNetPaintedItem::setFont(QString fontFamilyName, bool isBold, bool isItalic, bool isUnderline, int pxSize) { |
| 63 | + checkRecordingAndAdd([fontFamilyName, isBold, isItalic, isUnderline, pxSize](QPainter* p) { |
| 64 | + auto font = QFont(fontFamilyName); |
| 65 | + font.setBold(isBold); |
| 66 | + font.setItalic(isItalic); |
| 67 | + font.setUnderline(isUnderline); |
| 68 | + font.setPixelSize(pxSize); |
| 69 | + p->setFont(font); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +void QmlNetPaintedItem::drawText(int x, int y, QString text) { |
| 74 | + checkRecordingAndAdd([x,y,text](QPainter* p) { |
| 75 | + auto vp = p->viewport(); |
| 76 | + p->drawText(vp.x() + x, vp.y() + y, text); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +void QmlNetPaintedItem::drawRect(int x, int y, int width, int height) { |
| 81 | + checkRecordingAndAdd([x,y,width, height](QPainter* p) { |
| 82 | + p->drawRect(x, y, width, height); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +void QmlNetPaintedItem::fillRect(int x, int y, int width, int height, QString colorString) { |
| 87 | + checkRecordingAndAdd([x,y,width, height, colorString](QPainter* p) { |
| 88 | + QColor color(colorString); |
| 89 | + QBrush brush(color); |
| 90 | + p->fillRect(x, y, width, height, brush); |
| 91 | + }); |
| 92 | +} |
| 93 | + |
| 94 | +void QmlNetPaintedItem::fillRect(int x, int y, int width, int height) { |
| 95 | + checkRecordingAndAdd([x,y,width, height](QPainter* p) { |
| 96 | + p->fillRect(x, y, width, height, p->brush()); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +void QmlNetPaintedItem::checkRecordingAndAdd(std::function<void(QPainter*)> action) { |
| 101 | + if(!m_isRecording) { |
| 102 | + //TODO: throw |
| 103 | + } |
| 104 | + m_recordedPaintActions.push_back(action); |
| 105 | +} |
| 106 | + |
| 107 | +void QmlNetPaintedItem::paint(QPainter *painter) |
| 108 | +{ |
| 109 | + std::vector<std::function<void(QPainter*)>> paintActionsCopy; |
| 110 | + m_paintActionMutex.lock(); |
| 111 | + paintActionsCopy = m_paintActions; |
| 112 | + m_paintActionMutex.unlock(); |
| 113 | + |
| 114 | + for(auto pa : paintActionsCopy) { |
| 115 | + pa(painter); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +extern "C" { |
| 120 | + |
| 121 | +Q_DECL_EXPORT void qqmlnetpainteditem_beginRecordPaintActions(QmlNetPaintedItem* paintedItem) { |
| 122 | + paintedItem->beginRecordPaintActions(); |
| 123 | +} |
| 124 | + |
| 125 | +Q_DECL_EXPORT void qqmlnetpainteditem_endRecordPaintActions(QmlNetPaintedItem* paintedItem) { |
| 126 | + paintedItem->endRecordPaintActions(); |
| 127 | +} |
| 128 | + |
| 129 | +Q_DECL_EXPORT void qqmlnetpainteditem_setPen(QmlNetPaintedItem* paintedItem, QChar* colorString) { |
| 130 | + paintedItem->setPen(QString(colorString)); |
| 131 | +} |
| 132 | + |
| 133 | +Q_DECL_EXPORT void qqmlnetpainteditem_resetPen(QmlNetPaintedItem* paintedItem) { |
| 134 | + paintedItem->resetPen(); |
| 135 | +} |
| 136 | + |
| 137 | +Q_DECL_EXPORT void qqmlnetpainteditem_setBrush(QmlNetPaintedItem* paintedItem, QChar* colorString) { |
| 138 | + paintedItem->setBrush(QString(colorString)); |
| 139 | +} |
| 140 | + |
| 141 | +Q_DECL_EXPORT void qqmlnetpainteditem_resetBrush(QmlNetPaintedItem* paintedItem) { |
| 142 | + paintedItem->resetBrush(); |
| 143 | +} |
| 144 | + |
| 145 | +Q_DECL_EXPORT void qqmlnetpainteditem_setFont(QmlNetPaintedItem* paintedItem, QChar* fontFamilyName, bool isBold, bool isItalic, bool isUnderline, int pxSize) { |
| 146 | + paintedItem->setFont(QString(fontFamilyName), isBold, isItalic, isUnderline, pxSize); |
| 147 | +} |
| 148 | + |
| 149 | +Q_DECL_EXPORT void qqmlnetpainteditem_drawText(QmlNetPaintedItem* paintedItem, int x, int y, QChar* text) { |
| 150 | + paintedItem->drawText(x, y, QString(text)); |
| 151 | +} |
| 152 | + |
| 153 | +Q_DECL_EXPORT void qqmlnetpainteditem_drawRect(QmlNetPaintedItem* paintedItem, int x, int y, int width, int height) { |
| 154 | + paintedItem->drawRect(x, y, width, height); |
| 155 | +} |
| 156 | + |
| 157 | +Q_DECL_EXPORT void qqmlnetpainteditem_fillRectColor(QmlNetPaintedItem* paintedItem, int x, int y, int width, int height, QChar* color) { |
| 158 | + paintedItem->fillRect(x, y, width, height, QString(color)); |
| 159 | +} |
| 160 | + |
| 161 | +Q_DECL_EXPORT void qqmlnetpainteditem_fillRect(QmlNetPaintedItem* paintedItem, int x, int y, int width, int height) { |
| 162 | + paintedItem->fillRect(x, y, width, height); |
| 163 | +} |
| 164 | + |
| 165 | +} |
0 commit comments