7#include "resizerectangle.h"
10#include <QSGFlatColorMaterial>
12#include <QSGGeometryNode>
14ResizeRectangle::ResizeRectangle(
QQuickItem *parent)
18 setFlag(ItemHasContents);
21void ResizeRectangle::componentComplete()
25 m_handleComponent =
new QQmlComponent(engine,
QUrl(QStringLiteral(
"qrc:/BasicResizeHandle.qml")));
28 qDebug() << handleItem;
32 m_handleBottomLeft->setResizeCorner(ResizeHandle::BottomLeft);
33 m_handleBottomLeft->
setX(m_insideX - 5);
34 m_handleBottomLeft->
setY(m_insideY + m_insideHeight - 5);
35 m_handleBottomLeft->setRectangle(
this);
41 m_handleBottomRight->setResizeCorner(ResizeHandle::BottomRight);
42 m_handleBottomRight->
setX(m_insideX + m_insideWidth - 5);
43 m_handleBottomRight->
setY(m_insideY + m_insideHeight - 5);
44 m_handleBottomRight->setRectangle(
this);
50 m_handleTopLeft->setResizeCorner(ResizeHandle::TopLeft);
51 m_handleTopLeft->
setX(m_insideX - 5);
52 m_handleTopLeft->
setY(m_insideY - 5);
53 m_handleTopLeft->setRectangle(
this);
59 m_handleTopRight->setResizeCorner(ResizeHandle::TopRight);
60 m_handleTopRight->
setX(m_insideX + m_insideWidth - 5);
61 m_handleTopRight->
setY(m_insideY - 5);
62 m_handleTopRight->setRectangle(
this);
65void ResizeRectangle::updateHandles()
68 m_handleTopRight->
setX(m_insideX + m_insideWidth - 5);
69 m_handleTopRight->
setY(m_insideY - 5);
70 m_handleTopLeft->
setX(m_insideX - 5);
71 m_handleTopLeft->
setY(m_insideY - 5);
72 m_handleBottomRight->
setX(m_insideX + m_insideWidth - 5);
73 m_handleBottomRight->
setY(m_insideY + m_insideHeight - 5);
74 m_handleBottomLeft->
setX(m_insideX - 5);
75 m_handleBottomLeft->
setY(m_insideY + m_insideHeight - 5);
79qreal ResizeRectangle::insideX()
const
84void ResizeRectangle::setInsideX(qreal x)
86 x = qBound(0.0,
x, this->
width() - m_insideWidth);
96qreal ResizeRectangle::insideY()
const
101void ResizeRectangle::setInsideY(qreal y)
103 y = qBound(0.0,
y, this->
height() - m_insideHeight);
104 if (m_insideY ==
y) {
113qreal ResizeRectangle::insideWidth()
const
115 return m_insideWidth;
118void ResizeRectangle::setInsideWidth(qreal width)
121 if (m_insideWidth ==
width) {
124 m_insideWidth =
width;
126 Q_EMIT insideWidthChanged();
130qreal ResizeRectangle::insideHeight()
const
132 return m_insideHeight;
135void ResizeRectangle::setInsideHeight(qreal height)
138 if (m_insideHeight ==
height) {
143 Q_EMIT insideHeightChanged();
147QSGNode *ResizeRectangle::updatePaintNode(
QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
152 const int vertexCount = 12;
153 const int indexCount = 8 * 3;
169 geometry->
allocate(vertexCount, indexCount);
175 points[2].
set(m_insideX, 0);
178 points[4].
set(m_insideX + m_insideWidth, 0);
179 points[5].
set(m_insideX + m_insideWidth,
height());
183 points[8].
set(m_insideX, m_insideY);
184 points[9].
set(m_insideX + m_insideWidth, m_insideY);
185 points[10].
set(m_insideX + m_insideWidth, m_insideY + m_insideHeight);
186 points[11].
set(m_insideX, m_insideY + m_insideHeight);
218 indices[18 + 1] = 11;
219 indices[18 + 2] = 10;
223 indices[21 + 2] = 10;
231void ResizeRectangle::mouseReleaseEvent(
QMouseEvent *event)
233 m_mouseClickedOnRectangle =
false;
237void ResizeRectangle::mousePressEvent(
QMouseEvent *event)
239 m_mouseDownPosition =
event->pos();
240 m_mouseDownGeometry =
QPointF(m_insideX, m_insideY);
241 if (m_mouseDownPosition.
x() >= m_insideX && m_mouseDownPosition.
x() <= m_insideX + m_insideWidth && m_mouseDownPosition.
y() >= m_insideY
242 && m_mouseDownPosition.
y() <= m_insideY + m_insideHeight) {
243 m_mouseClickedOnRectangle =
true;
248void ResizeRectangle::mouseMoveEvent(
QMouseEvent *event)
250 if (m_mouseClickedOnRectangle) {
251 const QPointF difference = m_mouseDownPosition -
event->pos();
252 const qreal
x = m_mouseDownGeometry.
x() - difference.
x();
253 const qreal
y = m_mouseDownGeometry.
y() - difference.
y();
259void ResizeRectangle::mouseDoubleClickEvent(
QMouseEvent *event)
265#include "moc_resizerectangle.cpp"
void acceptSize()
Double click event signal.
T qobject_cast(QObject *object)
void setParent(QObject *parent)
virtual QObject * create(QQmlContext *context)
virtual void componentComplete() override
bool isComponentComplete() const const
void setParentItem(QQuickItem *parent)
void setGeometry(QSGGeometry *geometry)
void setColor(const QColor &color)
void set(float x, float y)
void allocate(int vertexCount, int indexCount)
const AttributeSet & defaultAttributes_Point2D()
quint16 * indexDataAsUShort()
void markIndexDataDirty()
void markVertexDataDirty()
void setDrawingMode(unsigned int mode)
void setIndexDataPattern(DataPattern p)
Point2D * vertexDataAsPoint2D()
void setMaterial(QSGMaterial *material)
void markDirty(DirtyState bits)
void setFlag(Flag f, bool enabled)