Rotation of the labels. There are a couple of bugs still to fix

--HG--
branch : feature
This commit is contained in:
BojanKverh 2016-06-30 18:04:25 +02:00
parent d5a3289ef7
commit 23a31b1fa9
2 changed files with 113 additions and 27 deletions

View File

@ -29,12 +29,13 @@
#include <QPainter> #include <QPainter>
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
#include <QTextDocument> #include <QTransform>
#include <QDebug> #include <QDebug>
#include "vtextgraphicsitem.h" #include "vtextgraphicsitem.h"
#define RESIZE_SQUARE 30 #define RESIZE_SQUARE 30
#define ROTATE_CIRCLE 20
#define MIN_W 120 #define MIN_W 120
#define MIN_H 60 #define MIN_H 60
#define MIN_FONT_SIZE 12 #define MIN_FONT_SIZE 12
@ -48,6 +49,7 @@ VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsSelectable, true);
m_eMode = mNormal; m_eMode = mNormal;
m_bReleased = false;
m_rectBoundingBox.setTopLeft(QPointF(0, 0)); m_rectBoundingBox.setTopLeft(QPointF(0, 0));
m_iMinH = MIN_H; m_iMinH = MIN_H;
SetSize(MIN_W, m_iMinH); SetSize(MIN_W, m_iMinH);
@ -93,6 +95,8 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
painter->setPen(QPen(Qt::black, 2, Qt::DashLine)); painter->setPen(QPen(Qt::black, 2, Qt::DashLine));
painter->drawRect(boundingRect().adjusted(1, 1, -1, -1)); painter->drawRect(boundingRect().adjusted(1, 1, -1, -1));
if (m_eMode != mRotate)
{
painter->setPen(Qt::black); painter->setPen(Qt::black);
painter->setBrush(Qt::black); painter->setBrush(Qt::black);
painter->drawRect(m_rectResize); painter->drawRect(m_rectResize);
@ -103,12 +107,26 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
painter->drawLine(0, m_rectBoundingBox.height(), m_rectBoundingBox.width(), 0); painter->drawLine(0, m_rectBoundingBox.height(), m_rectBoundingBox.width(), 0);
} }
} }
else
{
painter->setPen(Qt::black);
painter->setBrush(Qt::black);
painter->drawEllipse(
m_rectBoundingBox.width()/2,
m_rectBoundingBox.height()/2,
ROTATE_CIRCLE,
ROTATE_CIRCLE
);
}
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Reset() void VTextGraphicsItem::Reset()
{ {
return;
m_eMode = mNormal; m_eMode = mNormal;
m_bReleased = false;
Update(); Update();
setZValue(2); setZValue(2);
} }
@ -165,9 +183,15 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
{ {
if (pME->button() == Qt::LeftButton) if (pME->button() == Qt::LeftButton)
{ {
m_ptStart = pME->pos(); qDebug() << "PRESS" << m_eMode;
m_ptStartPos = pos();
m_ptStart = pME->scenePos();
m_szStart = m_rectBoundingBox.size(); m_szStart = m_rectBoundingBox.size();
if (m_rectResize.contains(m_ptStart) == true) m_dAngle = GetAngle(pME->scenePos());
m_dRotation = rotation();
if (m_eMode != mRotate)
{
if (m_rectResize.contains(pME->pos()) == true)
{ {
m_eMode = mResize; m_eMode = mResize;
} }
@ -175,18 +199,21 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
{ {
m_eMode = mMove; m_eMode = mMove;
} }
}
setZValue(3); setZValue(3);
UpdateBox(); UpdateBox();
qDebug() << "PRESS finished" << m_eMode;
} }
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME) void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
{ {
QPointF ptDiff = pME->pos() - m_ptStart; QPointF ptDiff = pME->scenePos() - m_ptStart;
if (m_eMode == mMove) if (m_eMode == mMove)
{ {
moveBy(ptDiff.x(), ptDiff.y()); //moveBy(ptDiff.x(), ptDiff.y());
setPos(m_ptStartPos + ptDiff);
UpdateBox(); UpdateBox();
} }
else if (m_eMode == mResize) else if (m_eMode == mResize)
@ -195,6 +222,17 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
Update(); Update();
emit SignalShrink(); emit SignalShrink();
} }
else if (m_eMode == mRotate)
{
if (fabs(m_dAngle) < 0.01)
{
m_dAngle = GetAngle(pME->scenePos());
return;
}
double dAng = 180*(GetAngle(pME->scenePos()) - m_dAngle)/M_PI;
setRotation(m_dRotation + dAng);
Update();
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -202,7 +240,22 @@ void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
{ {
if (pME->button() == Qt::LeftButton) if (pME->button() == Qt::LeftButton)
{ {
if (m_eMode == mMove) double dDist = fabs(pME->scenePos().x() - m_ptStart.x()) + fabs(pME->scenePos().y() - m_ptStart.y());
bool bShort = (dDist < 2);
qDebug() << "RELEASE" << m_eMode << dDist;
if (m_eMode == mMove || m_eMode == mResize)
{ // when released in mMove or mResize mode
if (bShort == true)
{
if (m_bReleased == true)
{
m_eMode = mRotate;
UpdateBox();
}
}
else if (m_eMode == mMove)
{ {
emit SignalMoved(pos()); emit SignalMoved(pos());
UpdateBox(); UpdateBox();
@ -212,7 +265,21 @@ void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
emit SignalResized(m_rectBoundingBox.width(), m_font.pixelSize()); emit SignalResized(m_rectBoundingBox.width(), m_font.pixelSize());
Update(); Update();
} }
m_eMode = mActivated; }
else
{ // when released in mRotate mode
if (bShort == true)
{
m_eMode = mMove;
UpdateBox();
}
else
{
emit SignalRotated(rotation());
UpdateBox();
}
}
m_bReleased = true;
} }
} }
@ -291,3 +358,15 @@ QStringList VTextGraphicsItem::SplitString(const QString &qs, qreal fW, const QF
qslLines << qsCurrent; qslLines << qsCurrent;
return qslLines; return qslLines;
} }
//---------------------------------------------------------------------------------------------------------------------
double VTextGraphicsItem::GetAngle(QPointF pt) const
{
double dX = pt.x() - m_ptStart.x();
double dY = pt.y() - m_ptStart.y();
if (fabs(dX) < 1 && fabs(dY) < 1)
return 0;
else
return atan2(dY, dX);
}

View File

@ -49,9 +49,9 @@ class VTextGraphicsItem : public QGraphicsObject
enum Mode { enum Mode {
mNormal, mNormal,
mActivated,
mMove, mMove,
mResize mResize,
mRotate
}; };
public: public:
@ -78,15 +78,22 @@ protected:
bool IsBigEnough(qreal fW, qreal fH, int iFontSize); bool IsBigEnough(qreal fW, qreal fH, int iFontSize);
QStringList SplitString(const QString& qs, qreal fW, const QFontMetrics& fm); QStringList SplitString(const QString& qs, qreal fW, const QFontMetrics& fm);
double GetAngle(QPointF pt) const;
signals: signals:
void SignalMoved(QPointF ptPos); void SignalMoved(QPointF ptPos);
void SignalResized(qreal iTW, int iFontSize); void SignalResized(qreal iTW, int iFontSize);
void SignalRotated(qreal dAng);
void SignalShrink(); void SignalShrink();
private: private:
Mode m_eMode; Mode m_eMode;
bool m_bReleased;
QPointF m_ptStartPos;
QPointF m_ptStart; QPointF m_ptStart;
QSizeF m_szStart; QSizeF m_szStart;
double m_dRotation;
double m_dAngle;
QRectF m_rectResize; QRectF m_rectResize;
int m_iMinH; int m_iMinH;
QRectF m_rectBoundingBox; QRectF m_rectBoundingBox;