Files
gcs-nf/opmap/mapwidget/VirtualMarginLine.cpp
T
2022-02-11 14:10:16 +08:00

138 lines
3.1 KiB
C++

#include "VirtualMarginLine.h"
#include <math.h>
namespace mapcontrol {
VirtualMarginLine::VirtualMarginLine(VirtualMargin *from, VirtualMargin *to, MapGraphicItem *map, QColor color, bool dashed, int width ) : QGraphicsLineItem(map),
source(from), destination(to), my_map(map), myColor(color), dashed(dashed), lineWidth(width)
{
this->setLine(from->pos().x(), from->pos().y(), to->pos().x(), to->pos().y());
connect(from, SIGNAL(localPositionChanged(QPointF, VirtualMargin *)), this, SLOT(refreshLocations()));
connect(to, SIGNAL(localPositionChanged(QPointF, VirtualMargin *)), this, SLOT(refreshLocations()));
connect(from, SIGNAL(aboutToBeDeleted(VirtualMargin *)), this, SLOT(waypointdeleted()));
connect(to, SIGNAL(aboutToBeDeleted(VirtualMargin *)), this, SLOT(waypointdeleted()));
this->setZValue(10);
connect(map, SIGNAL(childSetOpacity(qreal)), this, SLOT(setOpacitySlot(qreal)));
isEdit = true;
//qDebug() << "virtual line create";
//qDebug() << this->pos();
lineWidth = 5;
}
int VirtualMarginLine::type() const
{
// Enable the use of qgraphicsitem_cast with this item.
return Type;
}
QPainterPath VirtualMarginLine::shape() const
{
QPainterPath path = QGraphicsLineItem::shape();
path.addPolygon(arrowHead);
return path;
}
void VirtualMarginLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
//qDebug() << "virtual line update";
QPen myPen = pen();
myPen.setColor(myColor);
//qreal arrowSize = 10;
painter->setPen(myPen);
/*
if(isEdit == false)
{
painter->setBrush(myColor);
}
*/
double angle = ::acos(line().dx() / line().length());
if (line().dy() >= 0) {
angle = (M_PI * 2) - angle;
}
/*
QPointF arrowP1 = line().pointAt(0.5) + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line().pointAt(0.5) + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
arrowHead.clear();
arrowHead << line().pointAt(0.5) << arrowP1 << arrowP2;
painter->drawPolygon(arrowHead);
*/
if (dashed) {
QVector<qreal> dashes;
dashes << 4 << 8;
myPen.setDashPattern(dashes);
}
myPen.setWidth(lineWidth);
painter->setPen(myPen);
painter->drawLine(line());
}
void VirtualMarginLine::setEdit(bool value)
{
isEdit = value;
}
void VirtualMarginLine::refreshLocations()
{
//qDebug() << "reflush location";
this->setLine(source->pos().x(), source->pos().y(),
destination->pos().x(), destination->pos().y());
}
void VirtualMarginLine::waypointdeleted()
{
this->deleteLater();
}
void VirtualMarginLine::WPLinedelete(VirtualMargin *from, VirtualMargin *to)
{
Q_UNUSED(from)
Q_UNUSED(to)
this->deleteLater();
}
void VirtualMarginLine::setOpacitySlot(qreal opacity)
{
setOpacity(opacity);
}
}