#include "waypointbutton.h" namespace mapcontrol { WayPointButton::WayPointButton(MapGraphicItem *parent) { this->setAcceptHoverEvents(true); this->setFlag(QGraphicsItem::ItemIgnoresParentOpacity,true); this->setFlag(QGraphicsItem::ItemIsMovable, true); this->setFlag(QGraphicsItem::ItemIgnoresTransformations, true); this->setFlag(QGraphicsItem::ItemIsSelectable, false); this->setAcceptedMouseButtons(Qt::LeftButton); connect(parent, SIGNAL(childRefreshPosition()), this, SLOT(RefreshPos())); connect(parent, SIGNAL(childSetOpacity(qreal)), this, SLOT(setOpacitySlot(qreal))); this->setZValue(8);//在幕布上面 myColor = QColor("#98FB98"); position.setX(0); position.setY(0); position.setWidth(100); position.setHeight(100); RefreshPos(); } QRectF WayPointButton::boundingRect() const //鼠标可以操作的大小 { return QRectF(0,0,position.width(),position.height()); } void WayPointButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(option); Q_UNUSED(widget); painter->setRenderHint(QPainter::Antialiasing, true); QPen ePen; ePen.setWidth(2); QFont font; font.setPixelSize(15);//不随着屏幕分辨率改变而改变 font.setFamily("黑体");//非衬线 painter->setFont(font); //画底部 painter->save(); ePen.setColor(Qt::darkYellow); painter->setPen(ePen); painter->setBrush(myColor); painter->setOpacity(opacity); painter->drawRoundedRect(0,0,this->boundingRect().width(),this->boundingRect().height(),10,10); painter->restore(); if(img.size() > 0) { painter->save(); QRectF target(0, 0, 80.0, 80.0); QRectF source(0.0, 0.0, 64.0, 64.0); QImage image(img); painter->drawImage(target, image, source); painter->restore(); } painter->save(); ePen.setColor(QColor("#000000")); painter->setPen(ePen); painter->drawText(0,0,this->boundingRect().width(),this->boundingRect().height(),Qt::AlignCenter,text); painter->restore(); } void WayPointButton::RefreshPos() { this->setPos(position.x(), position.y()); } void WayPointButton::setOpacitySlot(qreal opacity) { setOpacity(opacity); } void WayPointButton::setPosition(int x,int y) { position.setX(x); position.setY(y); RefreshPos(); update(); } void WayPointButton::resize(int width,int height) { position.setWidth(width); position.setHeight(height); RefreshPos(); update(); } void WayPointButton::setGeometry(QRect rect) { position.setX(rect.x()); position.setY(rect.y()); position.setWidth(rect.width()); position.setHeight(rect.height()); update(); } void WayPointButton::setGeometry(int x,int y,int w,int h) { position.setX(x); position.setY(y); position.setWidth(w); position.setHeight(h); update(); } void WayPointButton::setText(QString t) { text = t; update(); } QString WayPointButton::Text(void) { return text; } void WayPointButton::setHighLight(bool flag) { highlight = flag; if(highlight) { opacity = 1; } else { opacity = 0.5; } } void WayPointButton::mousePressEvent(QGraphicsSceneMouseEvent *event) { event->accept(); if (event->button() == Qt::LeftButton) { opacity = 1; emit press(); } RefreshPos(); update(); } void WayPointButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { event->accept(); if (event->button() == Qt::LeftButton) { opacity = 0.8; emit release(); emit clicked(); } RefreshPos(); update(); } void WayPointButton::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { event->accept(); if (event->button() == Qt::LeftButton) { emit doubleclicked(); } } void WayPointButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { event->accept(); if (isDragging) { } } void WayPointButton::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { event->accept(); opacity = 0.8; update(); } void WayPointButton::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { event->accept(); opacity = 0.5; update(); } void WayPointButton::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { event->accept(); } }