#include "scgridreszablerectitem.h"
#define minLength 15
const QLineF m_rectLineFs[] = {
QLineF(0.0, 0.0, 1.0, 0.0),
QLineF(1.0, 0.0, 1.0, 1.0),
QLineF(0.0, 1.0, 1.0, 1.0),
QLineF(0.0, 0.0, 0.0, 1.0)
};
const QColor m_colors[] = {
QColor("#25a061"),
QColor("#4e8df1"),
QColor("#db524a"),
QColor("#fecd52")
};
SCLineItem::SCLineItem(qreal x1, qreal y1, qreal x2, qreal y2, QGraphicsItem *parent)
{
setLine(x1,y1,x2,y2);
setParentItem(parent);
setAcceptHoverEvents(true);
setFlag(QGraphicsItem::ItemIsMovable, true);
// setFlag(QGraphicsItem::ItemIsSelectable, true);
setFlag(QGraphicsItem::ItemIsFocusable, true);
}
bool SCLineItem::isVertical() const
{
return m_isVertical;
}
bool SCLineItem::getSelected() const
{
return m_selected;
}
void SCLineItem::setSelected(bool selected)
{
m_selected = selected;
}
void SCLineItem::setDefaulColor(const QColor &defaulColor)
{
m_defaulColor = defaulColor;
}
void SCLineItem::setSelectedColor(const QColor &selectedColor)
{
m_selectedColor = selectedColor;
}
void SCLineItem::setSize(const QSizeF &size)
{
m_size = size;
}
void SCLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(Qt::red);
if(getSelected()){
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(m_selectedColor);
painter->drawLine(line());
painter->setRenderHint(QPainter::Antialiasing, false);
}
else{
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(m_defaulColor);
painter->drawLine(line());
painter->setRenderHint(QPainter::Antialiasing, false);
}
}
void SCLineItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if(m_canMove && (m_resizing || (contains(event->pos()) && isSelected()))){
if(isVertical()){
setCursor(Qt::SizeHorCursor);
}
else{
setCursor(Qt::SizeVerCursor);
}
}
else{
setCursor(Qt::ArrowCursor);
}
QGraphicsItem::hoverMoveEvent(event);
}
void SCLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(m_resizing && m_canMove){
QPointF p = event->pos();
qreal w = m_size.width();
qreal h = m_size.height();
if(isVertical()){
moveBy(-(m_point.x() - p.x()), 0);
setPos(qMax(-w/2.0, qMin(pos().x(), w/2.0)), 0);
}
else{
moveBy(0, -(m_point.y() - p.y()));
setPos(0, qMax(-h/2.0, qMin(pos().y(), h/2.0)));
}
}
else
QGraphicsItem::mouseMoveEvent(event);
}
void SCLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
static qreal z = 0.0;
setZValue(z+=1.0);
m_point = event->pos();
if(event->button() == Qt::LeftButton && contains(event->pos())){
m_resizing = true;
}
QGraphicsItem::mousePressEvent(event);
}
void SCLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton && contains(event->pos())){
m_resizing = false;
}
else
QGraphicsItem::mouseReleaseEvent(event);
}
void SCLineItem::setIsVertical(bool isVertical)
{
m_isVertical = isVertical;
}
SCGridReszableRectItem::SCGridReszableRectItem(SCGridReszableRectItem::GRID_TYPE type)
{
setPos(0,0);
initSetting(type);
}
SCGridReszableRectItem::SCGridReszableRectItem(SCGridReszableRectItem::GRID_TYPE type, int x, int y)
{
setPos(x,y);
initSetting(type);
}
SCGridReszableRectItem::SCGridReszableRectItem(SCGridReszableRectItem::GRID_TYPE type, int x, int y, qreal width, qreal height)
{
setPos(x,y);
m_size.setWidth(width);
m_size.setHeight(height);
initSetting(type);
}
SCGridReszableRectItem::~SCGridReszableRectItem()
{
}
QRectF SCGridReszableRectItem::boundingRect() const
{
return QRectF(0,0,m_size.width(), m_size.height());
}
void SCGridReszableRectItem::setSize(const QSizeF &size)
{
m_size = size;
}
void SCGridReszableRectItem::setScale(const qreal &scaleX, const qreal &scaleY)
{
m_scaleX = scaleX;
m_scaleY = scaleY;
qreal w = m_size.width();
qreal h = m_size.height();
m_size.setWidth(w * scaleX);
m_size.setHeight(h * scaleY);
}
void SCGridReszableRectItem::setItemSelected(bool selected)
{
setSelected(selected);
prepareGeometryChange();
}
void SCGridReszableRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setPen(m_defaulColor);
QRectF rectF = QRectF(1,1,m_size.width(), m_size.height());
if(option->state & QStyle::State_Selected){
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(m_selectedColor);
updateLineItems();
painter->drawRect(rectF);
painter->setRenderHint(QPainter::Antialiasing, false);
}
else{
painter->drawRect(rectF);
updateLineItems();
}
}
void SCGridReszableRectItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if(isSelected()){
m_curItemType = getCursorPosition(event->pos());
switch (m_curItemType) {
case ITEM_TYPE::TOP:
setCursor(Qt::SizeVerCursor);
break;
case ITEM_TYPE::BOTTOM:
setCursor(Qt::SizeVerCursor);
break;
case ITEM_TYPE::LEFT:
setCursor(Qt::SizeHorCursor);
break;
case ITEM_TYPE::RIGHT:
setCursor(Qt::SizeHorCursor);
break;
case ITEM_TYPE::CENTER:
setCursor(Qt::ArrowCursor);
break;
}
}
else{
setCursor(Qt::ArrowCursor);
}
QGraphicsItem::hoverMoveEvent(event);
}
void SCGridReszableRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(m_resizing){
qreal x = pos().x();
qreal y = pos().y();
qreal eventX = event->pos().x();
qreal eventY = event->pos().y();
qreal moveX = 0;
qreal moveY = 0;
qreal newW = m_size.width();
qreal newH = m_size.height();
QRectF rect = scene()->sceneRect();
switch (m_curItemType) {
case ITEM_TYPE::TOP:
moveY = qMax(-y, eventY);
newH = m_size.height() - moveY;
break;
case ITEM_TYPE::BOTTOM:
newH = qMin(eventY, rect.height() - 2 - y);
break;
case ITEM_TYPE::LEFT:
moveX = qMax(-x, eventX);
newW = m_size.width() - moveX;
break;
case ITEM_TYPE::RIGHT:
newW = qMin(eventX, rect.width() - 2 - x);
break;
case ITEM_TYPE::CENTER:
break;
}
if(newH<minLength || newW<minLength) return;
moveBy(moveX, moveY);
m_size.setWidth(newW);
m_size.setHeight(newH);
prepareGeometryChange();
}
else{
qreal moveX = 0;
qreal moveY = 0;
qreal eventX = event->pos().x();
qreal eventY = event->pos().y();
moveY = m_originPos.y() - eventY;
moveX = m_originPos.x() - eventX;
moveOutSide(-moveX, -moveY);
}
}
void SCGridReszableRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
static qreal z = 0;
setZValue(z += 1.0);
m_originPos = event->pos();
if(event->button() == Qt::LeftButton && getCursorPosition(event->pos()) != ITEM_TYPE::CENTER){
m_resizing = true;
}
else
QGraphicsItem::mousePressEvent(event);
}
void SCGridReszableRectItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton && getCursorPosition(event->pos()) != ITEM_TYPE::CENTER){
m_resizing = false;
}
else
QGraphicsItem::mousePressEvent(event);
}
QVariant SCGridReszableRectItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
if(change == ItemSelectedChange && scene()){
if(value == 1
NiklausC
- 粉丝: 39
- 资源: 8
最新资源
- 基于一款语音交互智能家居机器人全部资料+详细文档+优秀项目.zip
- CuZnAl合金焊接方法 - .pdf
- Cu异种金属冷金属过渡熔钎焊接头显微组织与性能 - .pdf
- D406A钢电子束焊接头组织及性能分析 - .pdf
- 基于语音识别的智能家居控制方案研究与设计全部资料+详细文档+优秀项目.zip
- DB21T 2700-2016 焊接绝热气瓶充装站安全技术条件.pdf
- DG1427-2001钢制压力容器产品焊接试板的力学性能检验.pdf
- DIN 928-2000 焊接方螺母 Square weld nuts.pdf
- DIN 1910-1-1983 焊接 第1部分 焊接概念焊接方法分类.pdf
- DIN 1910-2-1977 焊接 金属焊接 工艺.pdf
- DIN 1910-5-1986 焊接.金属焊接.电阻焊接.方法.pdf
- DIN 1912-1 1976 焊接.钎焊图样表示法.焊接接头.焊接坡口及焊缝的概念与名称.pdf
- DIN 1912-2 1977 焊接.钎焊图样表示法.工作位置.焊缝倾角.焊缝旋转角度.PDF
- DIN 1910-11-1979 焊接.金属焊接材料术语.pdf
- DIN 2393-1-1994 有特殊尺寸精度的精密焊接钢管 尺寸.PDF
- DIN 6700-1-2001 中文版 铁路车辆及车辆部件的焊接.第1部分基本概念,基本规则.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈