分类: C/C++
2013-03-21 10:31:51
polyline.h
#ifndef POLYLINE_H
#define POLYLINE_H
#include
#include "diagramitem.h"
#include "public.h"
QT_BEGIN_NAMESPACE
class QGraphicsPolygonItem;
class QGraphicsLineItem;
class QGraphicsScene;
class QRectF;
class QGraphicsSceneMouseEvent;
class QPainterPath;
QT_END_NAMESPACE
class Polyline
{
public:
~Polyline();
Polyline(DiagramItem *startItem, DiagramItem *endItem,
QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
vector<QGraphicsLineItem*> vectorLines;
private:
void updatePosition();
struct polyPoints myPoints;
void getPoint();
void getNewPoint();
DiagramItem *myStartItem;
DiagramItem *myEndItem;
};
#endif // POLYLINE_H
polyline.cpp
#include "polyline.h"
#include
Polyline::~Polyline()
{
QGraphicsLineItem *item;
vector<QGraphicsLineItem*>::iterator lineItem;
for(lineItem = vectorLines.begin(); lineItem != vectorLines.end(); lineItem++) {
item = (QGraphicsLineItem*)*lineItem;
delete item;
}
vectorLines.clear();
}
Polyline::Polyline(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent, QGraphicsScene *scene)
{
myStartItem = startItem;
myEndItem = endItem;
getPoint();
QGraphicsLineItem *item = new QGraphicsLineItem(myPoints.top_x, myPoints.top_y,
myPoints.top_x, myPoints.mid_y,
parent, scene);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
item->setZValue(-1);
vectorLines.push_back(item);
item = new QGraphicsLineItem(myPoints.top_x, myPoints.mid_y,
myPoints.buttom_x, myPoints.mid_y,
parent, scene);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
item->setZValue(-1);
vectorLines.push_back(item);
item = new QGraphicsLineItem(myPoints.buttom_x, myPoints.mid_y,
myPoints.buttom_x, myPoints.buttom_y,
parent, scene);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setPen(QPen(Qt::black, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
item->setZValue(-1);
vectorLines.push_back(item);
}
void Polyline::updatePosition()
{
getNewPoint();
QLineF line(myPoints.top_x, myPoints.top_y,
myPoints.top_x, myPoints.mid_y);
vectorLines.at(0)->setLine(line);
QLineF line1(myPoints.top_x, myPoints.mid_y,
myPoints.buttom_x, myPoints.mid_y);
vectorLines.at(1)->setLine(line1);
QLineF line2(myPoints.buttom_x, myPoints.mid_y,
myPoints.buttom_x, myPoints.buttom_y);
vectorLines.at(2)->setLine(line2);
}
void Polyline::getPoint()
{
float w ;
float h ;
w = myStartItem->pixmap().width();
h = myStartItem->pixmap().height();
myPoints.top_x = myStartItem->point()->rx() + w / 2;
myPoints.top_y = myStartItem->point()->ry() + h / 2;
myPoints.buttom_x = myEndItem->point()->rx() + w / 2;
myPoints.buttom_y = myEndItem->point()->ry() + h / 2;
myPoints.mid_x = (myPoints.buttom_x - myPoints.top_x) / 2 + myPoints.top_x;
myPoints.mid_y = (myPoints.buttom_y - myPoints.top_y) / 2 + myPoints.top_y;
}
void Polyline::getNewPoint()
{
float w ;
float h ;
w = myStartItem->pixmap().width();
h = myStartItem->pixmap().height();
QPointF point1 = this->vectorLines.at(0)->mapFromItem(myStartItem, 0, 0);
QPointF point2 = this->vectorLines.at(0)->mapFromItem(myEndItem, 0, 0);
myPoints.top_x = point1.x() + w / 2;
myPoints.top_y = point1.y() + h / 2;;
myPoints.buttom_x = point2.x() + w / 2;
myPoints.buttom_y = point2.y() + h / 2;;
myPoints.mid_x = (myPoints.buttom_x - myPoints.top_x) / 2 + myPoints.top_x;
myPoints.mid_y = (myPoints.buttom_y - myPoints.top_y) / 2 + myPoints.top_y;
}
备注:系统CU博客系统提供的代码插入标签太难用了,效果很难看。