2016-11-18 18:02:40 +01:00
|
|
|
#include <cmath>
|
2016-09-26 21:01:58 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QCursor>
|
|
|
|
#include <QPainter>
|
|
|
|
#include "tooltip.h"
|
|
|
|
#include "pathitem.h"
|
|
|
|
|
|
|
|
|
|
|
|
void PathItem::updateShape()
|
|
|
|
{
|
|
|
|
QPainterPathStroker s;
|
2016-12-06 01:48:26 +01:00
|
|
|
s.setWidth((_width + 1) * 1.0/scale());
|
2016-09-26 21:01:58 +02:00
|
|
|
_shape = s.createStroke(_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathItem::PathItem(QGraphicsItem *parent) : QGraphicsObject(parent)
|
|
|
|
{
|
2016-12-06 01:48:26 +01:00
|
|
|
_width = 3;
|
|
|
|
|
2016-09-26 21:01:58 +02:00
|
|
|
QBrush brush(Qt::SolidPattern);
|
2016-12-06 01:48:26 +01:00
|
|
|
_pen = QPen(brush, _width);
|
2016-09-26 21:01:58 +02:00
|
|
|
|
|
|
|
_units = Metric;
|
|
|
|
|
|
|
|
_marker = new MarkerItem(this);
|
|
|
|
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setAcceptHoverEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option);
|
|
|
|
Q_UNUSED(widget);
|
|
|
|
|
|
|
|
painter->setPen(_pen);
|
|
|
|
painter->drawPath(_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
QPen p = QPen(QBrush(Qt::red), 0);
|
|
|
|
painter->setPen(p);
|
|
|
|
painter->drawRect(boundingRect());
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::setScale(qreal scale)
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
|
2016-12-06 01:48:26 +01:00
|
|
|
_pen.setWidthF(_width * 1.0/scale);
|
2016-09-26 21:01:58 +02:00
|
|
|
QGraphicsItem::setScale(scale);
|
|
|
|
_marker->setScale(1.0/scale);
|
|
|
|
|
|
|
|
updateShape();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::setColor(const QColor &color)
|
|
|
|
{
|
|
|
|
_pen.setColor(color);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-12-06 01:48:26 +01:00
|
|
|
void PathItem::setWidth(int width)
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
|
|
|
|
_width = width;
|
|
|
|
_pen.setWidthF(_width * 1.0/scale());
|
|
|
|
|
|
|
|
updateShape();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::setStyle(Qt::PenStyle style)
|
|
|
|
{
|
|
|
|
_pen.setStyle(style);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:01:58 +02:00
|
|
|
void PathItem::setUnits(enum Units units)
|
|
|
|
{
|
|
|
|
_units = units;
|
|
|
|
}
|
|
|
|
|
2016-11-14 22:12:43 +01:00
|
|
|
QPointF PathItem::position(qreal x) const
|
|
|
|
{
|
|
|
|
int low = 0;
|
|
|
|
int high = _distance.count() - 1;
|
|
|
|
int mid = 0;
|
|
|
|
|
2016-11-15 18:08:44 +01:00
|
|
|
|
|
|
|
Q_ASSERT(_distance.count() == _path.elementCount());
|
2016-11-14 22:12:43 +01:00
|
|
|
Q_ASSERT(high > low);
|
|
|
|
Q_ASSERT(x >= _distance.at(low) && x <= _distance.at(high));
|
|
|
|
|
|
|
|
while (low <= high) {
|
|
|
|
mid = low + ((high - low) / 2);
|
|
|
|
qreal val = _distance.at(mid);
|
|
|
|
if (val > x)
|
|
|
|
high = mid - 1;
|
|
|
|
else if (val < x)
|
|
|
|
low = mid + 1;
|
|
|
|
else
|
|
|
|
return _path.elementAt(mid);
|
|
|
|
}
|
|
|
|
|
|
|
|
QLineF l;
|
|
|
|
qreal p1, p2;
|
|
|
|
if (_distance.at(mid) < x) {
|
|
|
|
l = QLineF(_path.elementAt(mid), _path.elementAt(mid+1));
|
|
|
|
p1 = _distance.at(mid); p2 = _distance.at(mid+1);
|
|
|
|
} else {
|
|
|
|
l = QLineF(_path.elementAt(mid-1), _path.elementAt(mid));
|
|
|
|
p1 = _distance.at(mid-1); p2 = _distance.at(mid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.pointAt((x - p1) / (p2 - p1));
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:01:58 +02:00
|
|
|
void PathItem::moveMarker(qreal distance)
|
|
|
|
{
|
2016-11-18 18:02:40 +01:00
|
|
|
if (distance >= _distance.first() && distance <= _distance.last()) {
|
2016-09-26 21:01:58 +02:00
|
|
|
_marker->setVisible(true);
|
2016-11-14 22:12:43 +01:00
|
|
|
_marker->setPos(position(distance));
|
2016-11-18 18:02:40 +01:00
|
|
|
} else
|
|
|
|
_marker->setVisible(false);
|
2016-09-26 21:01:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2016-12-06 01:48:26 +01:00
|
|
|
_pen.setWidthF((_width + 1) * 1.0/scale());
|
2016-09-26 21:01:58 +02:00
|
|
|
setZValue(zValue() + 1.0);
|
|
|
|
update();
|
|
|
|
|
|
|
|
emit selected(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PathItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
|
2016-12-06 01:48:26 +01:00
|
|
|
_pen.setWidthF(_width * 1.0/scale());
|
2016-09-26 21:01:58 +02:00
|
|
|
setZValue(zValue() - 1.0);
|
|
|
|
update();
|
|
|
|
|
|
|
|
emit selected(false);
|
|
|
|
}
|