Skip to content

Commit 540b263

Browse files
devmilpauldotknopf
authored andcommitted
Introduces QQuickPaintedItem
First support for some basic draw operations
1 parent b4cf9f8 commit 540b263

File tree

9 files changed

+228
-1876
lines changed

9 files changed

+228
-1876
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
QmlNet.pro.user.*
3+
*.user

QmlNet.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ HEADERS += $$PWD/QmlNet.h \
1616

1717
include (QmlNet/types/types.pri)
1818
include (QmlNet/qml/qml.pri)
19+
include (QmlNet/controls/controls.pri)
1920

2021
SOURCES += \
2122
$$PWD/QmlNet.cpp \

QmlNet.pro.user

Lines changed: 0 additions & 605 deletions
This file was deleted.

QmlNet.user

Lines changed: 0 additions & 333 deletions
This file was deleted.

QmlNet/QmlNet.pro.user

Lines changed: 0 additions & 605 deletions
This file was deleted.

QmlNet/QmlNet.user

Lines changed: 0 additions & 333 deletions
This file was deleted.
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef QMLNETPAINTEDITEM_H
2+
#define QMLNETPAINTEDITEM_H
3+
4+
#include <QtQuick/QQuickPaintedItem>
5+
#include <QObject>
6+
#include <vector>
7+
8+
9+
class QmlNetPaintedItem : public QQuickPaintedItem
10+
{
11+
Q_OBJECT
12+
Q_PROPERTY(QObject* paintHandler READ paintHandler WRITE setPaintHandler)
13+
QML_ELEMENT
14+
public:
15+
QmlNetPaintedItem(QQuickItem *parent = nullptr);
16+
17+
QObject* paintHandler() const;
18+
void setPaintHandler(QObject* paintHandler);
19+
20+
void paint(QPainter *painter) override;
21+
22+
void beginRecordPaintActions();
23+
void endRecordPaintActions();
24+
25+
//Record api
26+
void setPen(QString color);
27+
void resetPen();
28+
void setBrush(QString color);
29+
void resetBrush();
30+
void setFont(QString fontFamilyName, bool isBold, bool isItalic, bool isUnderline, int pxSize);
31+
void drawText(int x, int y, QString text);
32+
void drawRect(int x, int y, int width, int height);
33+
void fillRect(int x, int y, int width, int height, QString color);
34+
void fillRect(int x, int y, int width, int height);
35+
36+
private:
37+
bool m_isRecording = false;
38+
std::vector<std::function<void(QPainter*)>> m_paintActions;
39+
std::vector<std::function<void(QPainter*)>> m_recordedPaintActions;
40+
QObject* m_paintHandler = nullptr;
41+
42+
QMutex m_paintActionMutex;
43+
44+
void checkRecordingAndAdd(std::function<void(QPainter*)> action);
45+
};
46+
47+
#endif // QMLNETPAINTEDITEM_H

QmlNet/controls/controls.pri

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONFIG += qmltypes
2+
QML_IMPORT_NAME = Qml.Net
3+
QML_IMPORT_MAJOR_VERSION = 1
4+
5+
HEADERS += \
6+
$$PWD/QmlNetPaintedItem.h
7+
8+
SOURCES += \
9+
$$PWD/QmlNetPaintedItem.cpp
10+
11+
INCLUDEPATH += \
12+
$$PWD

0 commit comments

Comments
 (0)