#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