#include
"CustomItem.h"
#include
#include
#include
CustomItem::CustomItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
setAcceptedMouseButtons(Qt::LeftButton
| Qt::RightButton
| Qt::MiddleButton);
setFlag(QQuickItem::ItemHasContents);
}
QString CustomItem::name()
const
{
return
m_name;
}
void
CustomItem::setName(
const
QString &name)
{
m_name = name;
}
QColor CustomItem::color()
const
{
return
m_color;
}
void
CustomItem::setColor(
const
QColor &color)
{
m_color = color;
}
void
CustomItem::paint(QPainter *painter)
{
// pen & brush
QPen pen(m_color,
10
);
QLinearGradient gradient;
gradient.setStart(
0
,
50
);
gradient.setFinalStop(
200
,
50
);
gradient.setColorAt(
0
, Qt::blue);
gradient.setColorAt(
0
.
2
, Qt::green);
gradient.setColorAt(
0
.
4
, Qt::red);
gradient.setColorAt(
0
.
6
, Qt::yellow);
gradient.setColorAt(
1
, Qt::cyan);
painter->setPen(pen);
painter->setBrush(gradient);
painter->setRenderHints(QPainter::Antialiasing,
true
);
// Unclosed shape
static
const
QPointF points[
3
] =
{
QPointF(
10
.
0
,
100
.
0
),
QPointF(
20
.
0
,
10
.
0
),
QPointF(
200
.
0
,
50
.
0
),
};
painter->save();
pen.setJoinStyle(Qt::MiterJoin);
// MiterJoin, BevelJoin, RoundJoin
pen.setCapStyle(Qt::RoundCap);
// FlatCap, SquareCap, RoundCap
pen.setStyle(Qt::DashLine);
painter->drawPolyline(points,
3
);
painter->restore();
// Closed shape
QPainterPath path;
path.addEllipse(QRectF(
10
.
0
,
10
.
0
, width() -
10
.
0
, height() -
10
.
0
));
painter->drawPath(path);
/* 三角形
QPainterPath path;
path.moveTo(width() / 2, 0);
path.lineTo(width(), height());
path.lineTo(0, height());
path.lineTo(width() / 2, 0);
painter->fillPath(path, m_color);
*/
}
bool
CustomItem::contains(
const
QPointF &point)
const
{
// Unclosed shape
static
const
QPointF points[
3
] =
{
QPointF(
10
.
0
,
100
.
0
),
QPointF(
20
.
0
,
10
.
0
),
QPointF(
200
.
0
,
50
.
0
),
};
QPainterPath path;
path.moveTo(points[
0
]);
path.lineTo(points[
1
]);
path.lineTo(points[
2
]);
QPainterPathStroker stroker;
stroker.setWidth(
10
);
stroker.setJoinStyle(Qt::MiterJoin);
stroker.setCapStyle(Qt::RoundCap);
stroker.setDashPattern(Qt::SolidLine);
return
stroker.createStroke(path).contains(point);
// Close shape
QPainterPath path;
path.addEllipse(QRectF(
10
.
0
,
10
.
0
, width() -
10
.
0
, height() -
10
.
0
));
QPainterPathStroker stroker;
stroker.setWidth(
10
);
return
path.contains(point) || stroker.createStroke(path).contains(point);
/* 三角形
QPainterPath path;
path.moveTo(width() / 2, 0);
path.lineTo(width(), height());
path.lineTo(0, height());
path.lineTo(width() / 2, 0);
QPainterPathStroker stroker;
stroker.setWidth(10);
return return path.contains(point) || stroker.createStroke(path).contains(point);
*/
}
void
CustomItem::mousePressEvent(QMouseEvent *event)
{
qDebug() <<
"CustomItem::mousePressEvent"
;
QQuickPaintedItem::mousePressEvent(event);
}
void
CustomItem::mouseMoveEvent(QMouseEvent *event)
{
qDebug() <<
"CustomItem::mouseMoveEvent"
;
QQuickPaintedItem::mouseMoveEvent(event);
}
void
CustomItem::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() <<
"CustomItem::mouseReleaseEvent"
;
QQuickPaintedItem::mouseReleaseEvent(event);
}