qt/LedOK/LoQClass/loqgraphicsobject.cpp

579 lines
16 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include "loqgraphicsobject.h"
#include "QCoreApplication"
LoQGraphicsObject::LoQGraphicsObject(InteractiveType type, QGraphicsItem *parent) :
QGraphicsObject(parent),
m_interactiveType(type),
m_movable(false),
m_handleLen(10),
m_rLimit(INVALID_RECT)
{
setGeometry(0, 0, 100, 100);
init();
}
LoQGraphicsObject::LoQGraphicsObject(QRectF rect, InteractiveType type, QGraphicsItem *parent) :
QGraphicsObject(parent),
m_interactiveType(type),
m_movable(false),
m_handleLen(10),
m_rLimit(INVALID_RECT)
{
setGeometry(rect);
init();
}
LoQGraphicsObject::LoQGraphicsObject(qreal x, qreal y, qreal w, qreal h, InteractiveType type, QGraphicsItem *parent) :
QGraphicsObject(parent),
m_interactiveType(type),
m_movable(false),
m_handleLen(10),
m_rLimit(INVALID_RECT)
{
setGeometry(x, y, w, h);
init();
}
void LoQGraphicsObject::setInteractiveType(InteractiveType type)
{
m_interactiveType = type;
GraphicsItemFlags flag = flags();
if(Dynamic == m_interactiveType) {
flag |= ItemIsMovable;
flag |= ItemIsSelectable;
flag &= ~ItemIsFocusable;
} else {
flag &= ~ItemIsMovable;
flag &= ~ItemIsSelectable;
flag &= ~ItemIsFocusable;
}
setFlags(flag);
}
void LoQGraphicsObject::init()
{
setInteractiveType(m_interactiveType);
m_handlePen.setBrush(QBrush(QColor::fromRgb(0, 255, 0)));
m_handlePen.setWidth(1);
QVector<qreal> dashes;
qreal space = 2;
qreal solid = 2;
dashes << solid << space << solid <<space;
m_borderPen.setBrush(QBrush(QColor::fromRgb(0, 255, 0)));
m_borderPen.setDashPattern(dashes);
m_borderPen.setWidth(1);
}
QRectF LoQGraphicsObject::boundingRect() const
{
qreal x, y, w, h;
QRectF r = rect();
x = r.x() - m_handleLen;
y = r.y() - m_handleLen;
w = r.width() + m_handleLen * 2;
h = r.height() + m_handleLen * 2;
return QRectF(x, y, w, h);
}
//绘制选中和未选中的区域边框
void LoQGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
painter->save();
//绘制边框
QPen pen; // creates a default pen
// pen.setStyle(Qt::DashDotLine);
// QLinearGradient svGradient(0, 0, 2, 0);
// svGradient.setColorAt(1, QColor("#00ff00"));
// svGradient.setColorAt(0, QColor("#ffffff"));
// QBrush myBrush(svGradient);
QString qexeFullPath = QCoreApplication::applicationDirPath();
QString qtBordImage=qexeFullPath+"/m/"+"M8_8.bmp";
QPixmap aa(qtBordImage);
int iBordWidth=aa.height();
pen.setWidth(iBordWidth);
QBrush myBrush(aa);
//myBrush.setTexture(aa);
pen.setBrush(myBrush);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
QPainterPath path;
path.moveTo(iBordWidth/2,iBordWidth/2);
path.lineTo(rect().right()-iBordWidth/2,iBordWidth/2);
path.lineTo(rect().right()-iBordWidth/2,rect().bottom()-iBordWidth/2);
path.lineTo(iBordWidth/2,rect().bottom()-iBordWidth/2);
path.lineTo(iBordWidth/2,iBordWidth/2);
painter->setPen(pen);
painter->drawPath(path);
QMatrix matrix2;
matrix2.rotate(90);
QBrush myBrush2;
QPixmap bb=aa.transformed(matrix2, Qt::SmoothTransformation);
myBrush2.setTexture(bb);
QPen pen2;
pen2.setBrush(myBrush2);
QPainterPath path2;
path2.moveTo(rect().right()-iBordWidth/2,iBordWidth/2);
path2.lineTo(rect().right()-iBordWidth/2,rect().bottom()-iBordWidth/2);
painter->setPen(pen2);
painter->drawPath(path2);
//QMatrix matrix3;
//matrix3.rotate(0);
// QBrush myBrush3;
// QPainterPath path3;
//// myBrush3.setTexture(aa);
//// pen.setBrush(myBrush);
//// painter->setPen(pen);
// path3.moveTo(rect().right()-iBordWidth/2,rect().bottom()-iBordWidth/2);
// path3.lineTo(iBordWidth/2,rect().bottom()-iBordWidth/2);
// painter->drawPath(path3);
// QMatrix matrix4;
// matrix4.rotate(270);
// QBrush myBrush4;
// QPainterPath path4;
// myBrush4.setTexture(aa.transformed(matrix4, Qt::SmoothTransformation));
// pen.setBrush(myBrush4);
// path4.moveTo(iBordWidth/2,rect().bottom()-iBordWidth/2);
// path4.lineTo(iBordWidth/2,iBordWidth/2);
// painter->drawPath(path4);
if(isSelected()) {
QPainterPath pRect;
pRect.addRect(rect());
m_borderPen.setBrush(QBrush(QColor::fromRgb(0, 255, 0)));
painter->setPen(m_borderPen);
painter->drawPath(pRect);
QPainterPath pHandle;
pHandle.addRect(m_rLT);
pHandle.addRect(m_rT);
pHandle.addRect(m_rRT);
pHandle.addRect(m_rL);
pHandle.addRect(m_rR);
pHandle.addRect(m_rLB);
pHandle.addRect(m_rB);
pHandle.addRect(m_rRB);
painter->setPen(m_handlePen);
painter->drawPath(pHandle);
}
else {
m_borderPen.setBrush(QBrush(QColor::fromRgb(0, 125, 0)));
QPainterPath pRect;
pRect.addRect(rect());
painter->setPen(m_borderPen);
painter->drawPath(pRect);
}
//磁条吸附时两化吸附的边
QPen CitiePen=QPen(QColor::fromRgb(0, 255, 0),1,Qt::SolidLine);
painter->setPen(CitiePen);
//if(bMousePress)
{
if (bLeftCitie) {
painter->setPen(CitiePen);
//painter->drawLine(QPointF(rect().x(),rect().y()-100),QPointF(rect().x(),rect().bottom()+100));
painter->drawLine(QPointF(rect().topLeft()),QPointF(rect().bottomLeft()));
}
if (bTopCitie) {
painter->setPen(CitiePen);
painter->drawLine(QPointF(rect().topLeft()),QPointF(rect().topRight()));
}
if (bRightCitie) {
painter->setPen(CitiePen);
painter->drawLine(QPointF(rect().topRight()),QPointF(rect().bottomRight()));
}
if (bBottomCitie) {
painter->setPen(CitiePen);
painter->drawLine(QPointF(rect().bottomLeft()),QPointF(rect().bottomRight()));
}
}
// QPen(const QBrush &brush,
// qreal width,
// Qt::PenStyle style = Qt::SolidLine,
// Qt::PenCapStyle cap = Qt::SquareCap,
// Qt::PenJoinStyle join = Qt::BevelJoin);
// QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
//painter->drawLine(QPointF(rect().bottomLeft()),QPointF(rect().bottomRight()));
painter->restore();
}
void LoQGraphicsObject::setBrightBianLeft(bool b)
{
//if(bMousePress)
// {
// bLeftCitie=b;
// }
// else {
// bLeftCitie=false;
// }
bLeftCitie=b;
updateGeometry();
}
void LoQGraphicsObject::setBrightBianRight(bool b)
{
// if(bMousePress)
// {
// bRightCitie=b;
// }
// else {
// bRightCitie=false;
// }
bRightCitie=b;
updateGeometry();
}
void LoQGraphicsObject::setBrightBianTop(bool b)
{
// if(bMousePress)
// {
// bTopCitie=b;
// }
// else {
// bTopCitie=false;
// }
bTopCitie=b;
updateGeometry();
}
void LoQGraphicsObject::setBrightBianbottom(bool b)
{
// if(bMousePress)
// {
// bBottomCitie=b;
// }
// else {
// bBottomCitie=false;
// }
bBottomCitie=b;
updateGeometry();
}
void LoQGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// return;
if(false == m_movable) return;
qreal cx, cy, cw, ch;
qreal mx, my, mw, mh;
QPointF ePos = pointToParent(event->pos());
QPointF absoluteOfs = ePos - m_pOrg;
mx = m_rOrg.x();
my = m_rOrg.y();
mw = m_w;
mh = m_h;
if(m_hDir != NONE) {
prepareGeometryChange();
switch(m_hDir) {
case LT:
cx = m_rOrg.x() + absoluteOfs.x();
cy = m_rOrg.y() + absoluteOfs.y();
if(m_rOrg.right() - cx < m_handleLen) cx = m_rOrg.right() - m_handleLen;
if(m_rOrg.bottom() - cy < m_handleLen) cy = m_rOrg.bottom() - m_handleLen;
mx = cx;
my = cy;
if(m_rLimit != INVALID_RECT) {
if(mx < 0) mx = 0;
if(my < 0) my = 0;
}
mw = m_rOrg.right() - mx;
mh = m_rOrg.bottom() - my;
break;
case T:
cy = m_rOrg.y() + absoluteOfs.y();
if(m_rOrg.bottom() - cy < m_handleLen) cy = m_rOrg.bottom() - m_handleLen;
my = cy;
if(m_rLimit != INVALID_RECT) {
if(my < 0) my = 0;
}
mh = m_rOrg.bottom() - my;
break;
case RT:
cy = m_rOrg.y() + absoluteOfs.y();
cw = m_rOrg.width() + absoluteOfs.x();
if(m_rOrg.bottom() - cy < m_handleLen) cy = m_rOrg.bottom() - m_handleLen;
if(cw < m_handleLen) cw = m_handleLen;
my = cy;
mw = cw;
if(m_rLimit != INVALID_RECT) {
if(my < 0) my = 0;
if(mx + mw > m_rLimit.width()) mw = m_rLimit.width() - mx;
}
mh = m_rOrg.bottom() - my;
break;
case L:
cx = m_rOrg.x() + absoluteOfs.x();
if(m_rOrg.right() - cx < m_handleLen) cx = m_rOrg.right() - m_handleLen;
mx = cx;
if(m_rLimit != INVALID_RECT) {
if(mx < 0) mx = 0;
}
mw = m_rOrg.right() - mx;
break;
case R:
cw = m_rOrg.width() + absoluteOfs.x();
if(cw < m_handleLen) cw = m_handleLen;
mw = cw;
if(m_rLimit != INVALID_RECT) {
if(mx + mw > m_rLimit.width()) mw = m_rLimit.width() - mx;
}
break;
case LB:
cx = m_rOrg.x() + absoluteOfs.x();
ch = m_rOrg.height() + absoluteOfs.y();
if(m_rOrg.right() - cx < m_handleLen) cx = m_rOrg.right() - m_handleLen;
if(ch < m_handleLen) ch = m_handleLen;
mx = cx;
if(m_rLimit != INVALID_RECT) {
if(mx < 0) mx = 0;
}
mw = m_rOrg.right() - mx;
mh = ch;
if(m_rLimit != INVALID_RECT) {
if(my + mh > m_rLimit.height()) mh = m_rLimit.height() - my;
}
break;
case B:
ch = m_rOrg.height() + absoluteOfs.y();
if(ch < m_handleLen) ch = m_handleLen;
mh = ch;
if(m_rLimit != INVALID_RECT) {
if(my + mh > m_rLimit.height()) mh = m_rLimit.height() - my;
}
break;
case RB:
cw = m_rOrg.width() + absoluteOfs.x();
ch = m_rOrg.height() + absoluteOfs.y();
if(cw < m_handleLen) cw = m_handleLen;
if(ch < m_handleLen) ch = m_handleLen;
mw = cw;
mh = ch;
if(m_rLimit != INVALID_RECT) {
if(mx + mw > m_rLimit.width()) mw = m_rLimit.width() - mx;
if(my + mh > m_rLimit.height()) mh = m_rLimit.height() - my;
}
break;
default:
break;
}
setPos(mx, my);
m_w = mw;
m_h = mh;
adjustHandle();
updateGeometry(m_rOrg);
rectChanged(rect());
} else {
mx = m_rOrg.x() + absoluteOfs.x();
my = m_rOrg.y() + absoluteOfs.y();
if(m_rLimit != INVALID_RECT) {
if(mx < 0) mx = 0;
if(my < 0) my = 0;
if(mx + mw > m_rLimit.width()) mx = m_rLimit.width() - mw;
if(my + mh > m_rLimit.height()) my = m_rLimit.height() - mh;
}
setPos(mx, my);
}
geometryChanged(geometry());
if(bMousePress)
{
emit sigCiTie(true,m_hDir);
}
}
void LoQGraphicsObject::setRLimit(const QRectF &r)
{
if(m_rLimit != INVALID_RECT) {
qreal mx, my, mw, mh;
qreal scale_w = r.width() / m_rLimit.width();
qreal scale_h = r.height() / m_rLimit.height();
mx = std::round(x() * scale_w);
my = std::round(y() * scale_h);
mw = std::round(m_w * scale_w);
mh = std::round(m_h * scale_h);
setPos(mx, my);
m_w = mw;
m_h = mh;
adjustHandle();
geometryChanged(geometry());
}
m_rLimit = r;
}
void LoQGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(Qt::LeftButton == event->button()) {
m_hDir = handleDir(event->pos());
m_pOrg = pointToParent(event->pos());
m_rOrg = geometry();
m_movable = true;
bMousePress=true;
}
QGraphicsItem::mousePressEvent(event);
}
void LoQGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
bMousePress=false;
if(Qt::LeftButton == event->button()) {
m_hDir = NONE;
m_movable = false;
bLeftCitie=false;
bRightCitie=false;
bTopCitie=false;
bBottomCitie=false;
m_keyPressId++;
emit sigCiTie(false,m_keyPressId);
}
QGraphicsItem::mouseReleaseEvent(event);
}
void LoQGraphicsObject::setGeometry(const QRectF &r)
{
if(r != geometry()) {
prepareGeometryChange();
setPos(r.x(), r.y());
m_w = r.width();
m_h = r.height();
adjustHandle();
emit rectChanged(rect());
}
}
void LoQGraphicsObject::updateGeometry()
{
QRectF r = geometry();
qreal x, y, w, h;
x = r.x() - m_handleLen;
y = r.y() - m_handleLen;
w = r.width() + m_handleLen * 2;
h = r.height() + m_handleLen * 2;
r = QRectF(x, y, w, h);
if(nullptr != scene()) {
scene()->update(r);
}
emit requestUpdate(r);
}
void LoQGraphicsObject::updateGeometry(const QRectF &gC, const QRectF &gL)
{
qreal x, y, w, h;
QRectF gCur, gLast;
x = gC.x() - m_handleLen;
y = gC.y() - m_handleLen;
w = gC.width() + m_handleLen * 2;
h = gC.height() + m_handleLen * 2;
gCur = QRectF(x, y, w, h);
x = gL.x() - m_handleLen;
y = gL.y() - m_handleLen;
w = gL.width() + m_handleLen * 2;
h = gL.height() + m_handleLen * 2;
gLast = QRectF(x, y, w, h);
qreal l = (gCur.left() < gLast.left()) ? gCur.left() : gLast.left();
qreal t = (gCur.top() < gLast.top()) ? gCur.top() : gLast.top();
qreal r = (gCur.right() > gLast.right()) ? gCur.right() : gLast.right();
qreal b = (gCur.bottom() > gLast.bottom()) ? gCur.bottom() : gLast.bottom();
QRectF gFlash = QRectF(QPointF(l, t), QPointF(r, b));
if(nullptr != scene()) {
scene()->update(gFlash);
}
emit requestUpdate(gFlash);
}
QPointF LoQGraphicsObject::pointToParent(const QPointF &p)
{
qreal px, py;
px = p.x() + x();
py = p.y() + y();
return QPointF(px, py);
}
LoQGraphicsObject::HANDLE_DIR LoQGraphicsObject::handleDir(const QPointF &p)
{
HANDLE_DIR hDir = NONE;
if(isSelected()) {
if(m_rLT.contains(p)) hDir = LT;
else if(m_rT.contains(p)) hDir = T;
else if(m_rRT.contains(p)) hDir = RT;
else if(m_rL.contains(p)) hDir = L;
else if(m_rR.contains(p)) hDir = R;
else if(m_rLB.contains(p)) hDir = LB;
else if(m_rB.contains(p)) hDir = B;
else if(m_rRB.contains(p)) hDir = RB;
}
return hDir;
}
//拖拽的虚线矩形
void LoQGraphicsObject::adjustHandle()
{
const QRectF &r = rect();
//左上角
m_rLT = QRectF(r.left() - m_handleLen/2,
r.top() - m_handleLen/2,
m_handleLen, m_handleLen);
//上中
m_rT = QRectF(r.center().x() - m_handleLen / 2,
r.top() - m_handleLen/2,
m_handleLen, m_handleLen);
//右上角
m_rRT = QRectF(r.right()-m_handleLen/2,
r.top() - m_handleLen/2,
m_handleLen, m_handleLen);
//左中
m_rL = QRectF(r.left() - m_handleLen/2,
r.center().y() - m_handleLen / 2,
m_handleLen, m_handleLen);
//右中
m_rR = QRectF(r.right()-m_handleLen/2,
r.center().y() - m_handleLen / 2,
m_handleLen, m_handleLen);
//左下角
m_rLB = QRectF(r.left() - m_handleLen/2,
r.bottom()-m_handleLen / 2,
m_handleLen, m_handleLen);
//中下
m_rB = QRectF(r.center().x() - m_handleLen / 2,
r.bottom()-m_handleLen / 2,
m_handleLen, m_handleLen);
//右下角
m_rRB = QRectF(r.right()-m_handleLen / 2,
r.bottom()-m_handleLen / 2,
m_handleLen, m_handleLen);
}