Fix moving label according to restrictions.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-03-13 15:54:38 +02:00
parent 65b3c6d3aa
commit 94ec43d85e
2 changed files with 122 additions and 39 deletions

View File

@ -403,6 +403,12 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
{ {
if (pME->button() == Qt::LeftButton && pME->type() != QEvent::GraphicsSceneMouseDoubleClick) if (pME->button() == Qt::LeftButton && pME->type() != QEvent::GraphicsSceneMouseDoubleClick)
{ {
if (m_moveType == NotMovable)
{
pME->ignore();
return;
}
// record the parameters of the mouse press. Specially record the position // record the parameters of the mouse press. Specially record the position
// of the press as the origin for the following operations // of the press as the origin for the following operations
m_ptStartPos = pos(); m_ptStartPos = pos();
@ -414,45 +420,66 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
// in rotation mode, do not do any changes here, because user might want to // in rotation mode, do not do any changes here, because user might want to
// rotate the label more. // rotate the label more.
if (m_moveType & IsRotatable) if ((m_moveType & AllModifications ) == AllModifications)
{ {
if (m_eMode != mRotate) AllUserModifications(pME->pos());
setZValue(ACTIVE_Z);
Update();
}
else if (m_moveType & IsRotatable)
{
if (m_moveType & IsResizable)
{
AllUserModifications(pME->pos());
}
else if (m_moveType & IsMovable)
{
UserRotateAndMove();
}
else
{ {
m_eMode = mRotate; m_eMode = mRotate;
SetOverrideCursor(cursorArrowCloseHand, 1, 1); SetOverrideCursor(cursorArrowCloseHand, 1, 1);
} }
setZValue(ACTIVE_Z);
Update();
}
else if (m_moveType & IsResizable)
{
if (m_moveType & IsRotatable)
{
AllUserModifications(pME->pos());
}
else if (m_moveType & IsMovable)
{
UserMoveAndResize(pME->pos());
}
setZValue(ACTIVE_Z);
Update();
}
else if (m_moveType & IsMovable)
{
if (m_moveType & IsRotatable)
{
UserRotateAndMove();
}
else if (m_moveType & IsResizable)
{
UserMoveAndResize(pME->pos());
}
else else
{ {
m_eMode = mMove;
SetOverrideCursor(cursorArrowCloseHand, 1, 1); SetOverrideCursor(cursorArrowCloseHand, 1, 1);
} }
setZValue(ACTIVE_Z); setZValue(ACTIVE_Z);
Update(); Update();
} }
else // All modifications else
{ {
if (m_eMode != mRotate) pME->ignore();
{ return;
// if user pressed the button inside the resize square, switch to resize mode
if (m_rectResize.contains(pME->pos()) == true)
{
m_eMode = mResize;
SetOverrideCursor(Qt::SizeFDiagCursor);
}
else
{
// if user pressed the button outside the resize square, switch to move mode
m_eMode = mMove;
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
}
}
else
{
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
}
// raise the label and redraw it
setZValue(ACTIVE_Z);
UpdateBox();
} }
} }
} }
@ -468,7 +495,7 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
qreal dY; qreal dY;
QRectF rectBB; QRectF rectBB;
const QPointF ptDiff = pME->scenePos() - m_ptStart; const QPointF ptDiff = pME->scenePos() - m_ptStart;
if (m_eMode == mMove) if (m_eMode == mMove && m_moveType & IsMovable)
{ {
// in move mode move the label along the mouse move from the origin // in move mode move the label along the mouse move from the origin
QPointF pt = m_ptStartPos + ptDiff; QPointF pt = m_ptStartPos + ptDiff;
@ -484,10 +511,21 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
setPos(pt); setPos(pt);
UpdateBox(); UpdateBox();
} }
else if (m_eMode == mResize) else if (m_eMode == mResize && m_moveType & IsResizable)
{ {
// in resize mode, resize the label along the mouse move from the origin // in resize mode, resize the label along the mouse move from the origin
QPointF pt = m_ptStartPos; QPointF pt;
// if (m_moveType & IsMovable)
// {
pt = m_ptStartPos;
// }
// else
// {
// pt = m_ptRotCenter - QRectF(0, 0, m_szStart.width() + ptDiff.x(),
// m_szStart.height() + ptDiff.y()).center();
// }
rectBB.setTopLeft(pt); rectBB.setTopLeft(pt);
QSizeF sz(m_szStart.width() + ptDiff.x(), m_szStart.height() + ptDiff.y()); QSizeF sz(m_szStart.width() + ptDiff.x(), m_szStart.height() + ptDiff.y());
rectBB.setSize(sz); rectBB.setSize(sz);
@ -496,12 +534,19 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
{ {
sz = QSizeF(sz.width()+dX, sz.height()+dY); sz = QSizeF(sz.width()+dX, sz.height()+dY);
} }
// else
// {
// if (not (m_moveType & IsMovable))
// {
// setPos(pt);
// }
// }
SetSize(sz.width(), sz.height()); SetSize(sz.width(), sz.height());
Update(); Update();
emit SignalShrink(); emit SignalShrink();
} }
else if (m_eMode == mRotate) else if (m_eMode == mRotate && m_moveType & IsRotatable)
{ {
// if the angle from the original position is small (0.5 degrees), just remeber the new angle // if the angle from the original position is small (0.5 degrees), just remeber the new angle
// new angle will be the starting angle for rotation // new angle will be the starting angle for rotation
@ -551,18 +596,18 @@ void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
// but if user did some operation (move/resize), emit the proper signal and update the label // but if user did some operation (move/resize), emit the proper signal and update the label
if (bShort == true) if (bShort == true)
{ {
if (m_bReleased == true) if (m_bReleased == true && m_moveType & IsRotatable)
{ {
m_eMode = mRotate; m_eMode = mRotate;
UpdateBox(); UpdateBox();
} }
} }
else if (m_eMode == mMove) else if (m_eMode == mMove && m_moveType & IsMovable)
{ {
emit SignalMoved(pos()); emit SignalMoved(pos());
UpdateBox(); UpdateBox();
} }
else else if (m_moveType & IsResizable)
{ {
emit SignalResized(m_rectBoundingBox.width(), m_tm.GetFont().pixelSize()); emit SignalResized(m_rectBoundingBox.width(), m_tm.GetFont().pixelSize());
Update(); Update();
@ -572,18 +617,14 @@ void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
{ // in rotate mode, if user did just press/release, switch to move mode { // in rotate mode, if user did just press/release, switch to move mode
if (bShort == true) if (bShort == true)
{ {
if (not (m_moveType & IsRotatable)) m_eMode = mMove;
{
m_eMode = mMove;
}
UpdateBox();
} }
else else if (m_moveType & IsRotatable)
{ {
// if user rotated the item, emit proper signal and update the label // if user rotated the item, emit proper signal and update the label
emit SignalRotated(rotation()); emit SignalRotated(rotation());
UpdateBox();
} }
UpdateBox();
} }
m_bReleased = true; m_bReleased = true;
} }
@ -649,3 +690,41 @@ void VTextGraphicsItem::CorrectLabel()
m_tm.FitFontSize(m_rectBoundingBox.width(), m_rectBoundingBox.height()); m_tm.FitFontSize(m_rectBoundingBox.width(), m_rectBoundingBox.height());
UpdateBox(); UpdateBox();
} }
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::AllUserModifications(const QPointF &pos)
{
if (m_eMode != mRotate)
{
UserMoveAndResize(pos);
}
else
{
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::UserRotateAndMove()
{
if (m_eMode != mRotate)
{
m_eMode = mMove;
}
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::UserMoveAndResize(const QPointF &pos)
{
if (m_rectResize.contains(pos) == true)
{
m_eMode = mResize;
SetOverrideCursor(Qt::SizeFDiagCursor);
}
else
{
m_eMode = mMove; // block later if need
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
}
}

View File

@ -92,6 +92,10 @@ private:
double m_dAngle; double m_dAngle;
QRectF m_rectResize; QRectF m_rectResize;
VTextManager m_tm; VTextManager m_tm;
void AllUserModifications(const QPointF &pos);
void UserRotateAndMove();
void UserMoveAndResize(const QPointF &pos);
}; };
#endif // VTEXTGRAPHICSITEM_H #endif // VTEXTGRAPHICSITEM_H