29 #include <QApplication>
33 #include <QGraphicsItem>
34 #include <QGraphicsSceneHoverEvent>
35 #include <QGraphicsSceneMouseEvent>
39 #include <QStyleOptionGraphicsItem>
43 #include "../vmisc/def.h"
44 #include "../vmisc/vmath.h"
45 #include "../vmisc/vcommonsettings.h"
46 #include "../vmisc/vabstractapplication.h"
51 #define ROTATE_RECT 60
68 QPointF apt[4] = { rectBB.topLeft(), rectBB.topRight(), rectBB.bottomLeft(), rectBB.bottomRight() };
69 QPointF ptCenter = rectBB.center();
76 double angle = qDegreesToRadians(rotation);
77 for (
int i = 0; i < 4; ++i)
79 QPointF pt = apt[i] - ptCenter;
80 qreal xPos = pt.x()*cos(angle) + pt.y()*sin(angle);
81 qreal yPos = -pt.x()*sin(angle) + pt.y()*cos(angle);
94 else if (xPos > xPos2)
102 else if (yPos > yPos2)
109 rect.setTopLeft(ptCenter + QPointF(xPos1, yPos1));
110 rect.setWidth(xPos2 - xPos1);
111 rect.setHeight(yPos2 - yPos1);
158 QColor color = QColor(
qApp->Settings()->getDefaultLabelColor());
160 painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
161 painter->setPen(QPen(color));
172 font.setBold(textLine.
bold);
173 font.setItalic(textLine.
italic);
175 QString text = textLine.
m_text;
176 QFontMetrics fm(font);
184 if (fm.horizontalAdvance(text) > width)
186 text = fm.elidedText(text, Qt::ElideMiddle, width);
189 painter->setFont(font);
190 painter->drawText(0, yPos, width, fm.height(),
static_cast<int>(textLine.
m_eAlign), text);
198 painter->setPen(QPen(Qt::black, 2, Qt::DashLine));
199 painter->drawRect(
boundingRect().adjusted(1, 1, -1, -1));
204 painter->setPen(Qt::black);
205 painter->setBrush(Qt::black);
206 painter->drawRect(
m_rectResize.adjusted(-1, -1, -1, -1));
218 painter->setPen(Qt::black);
219 painter->setBrush(Qt::black);
220 painter->drawEllipse(
227 painter->setPen(QPen(Qt::black, 3));
228 painter->setBrush(Qt::NoBrush);
253 QTransform transform = QTransform().rotate(-rotation());
254 QRectF bbRect = transform.map(parentItem()->
boundingRect()).boundingRect();
257 if (width > bbRect.width())
259 width = bbRect.width();
260 qDebug() <<
"Setting label width to parent item width" << width;
265 qDebug() <<
"Setting label width to min width" << width;
267 if (height > bbRect.height())
269 height = bbRect.height();
270 qDebug() <<
"Setting label height to parent item height" << width;
275 qDebug() <<
"Setting label height to min item height" << width;
278 prepareGeometryChange();
311 QRectF rectParent = parentItem()->boundingRect();
316 if (rectParent.contains(rectBB) ==
false)
318 if (rectParent.left() - rectBB.left() > fabs(xPos))
320 xPos = rectParent.left() - rectBB.left();
322 else if (rectBB.right() - rectParent.right() > fabs(xPos))
324 xPos = rectParent.right() - rectBB.right();
327 if (rectParent.top() - rectBB.top() > fabs(yPos))
329 yPos = rectParent.top() - rectBB.top();
331 else if (rectBB.bottom() - rectParent.bottom() > fabs(yPos))
333 yPos = rectParent.bottom() - rectBB.bottom();
389 qDebug() <<
"VTextGraphicsItem::mousePressEvent\n";
390 if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick
391 && (flags() & QGraphicsItem::ItemIsMovable))
438 qDebug() <<
" Item is Movable\n";
484 qDebug() <<
"VTextGraphicsItem::mouseMoveEvent\n";
488 const QPointF ptDiff =
event ->scenePos() -
m_start;
491 qDebug() <<
" Item is Movable\n";
494 rectBB.setTopLeft(pt);
498 if (
isContained(rectBB, rotation(), xPos, yPos) ==
false)
500 qDebug() <<
" Item is contained\n";
501 pt.setX(pt.x() + xPos);
502 pt.setY(pt.y() + yPos);
505 qDebug() <<
" Item position" << pt;
510 qDebug() <<
" Item is Resizable\n";
524 rectBB.setTopLeft(pt);
527 QTransform transform = QTransform().rotate(-rotation());
528 QPointF ptDiff2 = transform.map(ptDiff);
531 rectBB.setSize(size);
533 if (
isContained(rectBB, rotation(), xPos, yPos) ==
false)
535 size = QSizeF(size.width()+xPos, size.height()+yPos);
545 setSize(size.width(), size.height());
546 qDebug() <<
" Item size" << size.width() << size.height();
552 qDebug() <<
" Item is Rotatable\n";
561 double angle = qRadiansToDegrees(
GetAngle(event ->scenePos()) -
m_angle);
581 if (event ->button() == Qt::LeftButton)
588 double distance = fabs(event ->scenePos().x() -
m_start.x()) + fabs(event ->scenePos().y() -
m_start.y());
590 bool isShort = (distance < 2);
642 setCursor(Qt::SizeFDiagCursor);
649 VPieceItem::hoverMoveEvent(event);
659 setCursor(QCursor());
660 VPieceItem::hoverLeaveEvent(event);
682 rectBB.setTopLeft(pos());
684 if (
isContained(rectBB, rotation(), xPos, yPos) ==
false)
687 setPos(pos().x() + xPos, pos().y() + yPos);
722 setCursor(Qt::SizeAllCursor);
VPieceItem::MoveTypes m_moveType
virtual QRectF boundingRect() const Q_DECL_OVERRIDE
boundingRect returns the item bounding box
virtual double GetAngle(const QPointF &pt) const
GetAngle calculates the angle between the line, which goes from rotation center to pt and x axis.
void itemMoved(const QPointF &ptPos)
The VPieceLabelData class holds some information about a single piece like letter,...
void itemRotated(qreal angle)
void UpdateBox()
VTextGraphicsItem::UpdateBox redraws the label content.
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
VTextGraphicsItem::hoverLeaveEvent tries to restore normal mouse cursor.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
VTextGraphicsItem::mouseReleaseEvent handles left button mouse release events.
void itemResized(qreal width, int fontSize)
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
VTextGraphicsItem::mousePressEvent handles left button mouse press events.
void allUserModifications(const QPointF &pos)
int getFontSize() const
VTextGraphicsItem::getFontSize returns the currentextLiney used text base font size.
int getTextLines() const
VTextGraphicsItem::getTextLines returns the number of lines of text to show.
void userMoveAndResize(const QPointF &pos)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) Q_DECL_OVERRIDE
VTextGraphicsItem::paint redraws the item content.
void updateData(const QString &name, const VPieceLabelData &data)
VTextGraphicsItem::updateData Updates the detail label.
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event) Q_DECL_OVERRIDE
VTextGraphicsItem::hoverMoveEvent checks if cursor has to be changed.
VTextGraphicsItem(QGraphicsItem *pParent=nullptr)
VTextGraphicsItem::VTextGraphicsItem constructor.
void correctLabel()
VTextGraphicsItem::UpdateFont sets the text font size, so that the entire text will just fit into the...
void setFont(const QFont &font)
VTextGraphicsItem::SetFont sets the item font.
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event) Q_DECL_OVERRIDE
VTextGraphicsItem::mouseMoveEvent handles mouse move events.
VTextManager m_textMananger
void setSize(qreal width, qreal height)
VTextGraphicsItem::setSize Tries to set the label size to (width, height). If either of those is too ...
virtual void Update() Q_DECL_OVERRIDE
VTextGraphicsItem::Update sets the correct size and font size and redraws the label.
bool isContained(QRectF rectBB, qreal rotation, qreal &xPos, qreal &yPos) const
VTextGraphicsItem::isContained checks if the bounding box around rotated rectBB is contained in the p...
int GetSourceLinesCount() const
VTextManager::GetSourceLinesCount returns the number of input text lines.
virtual int GetSpacing() const
GetSpacing returns the vertical spacing between the lines.
const QFont & GetFont() const
GetFont returns the text base font.
const TextLine & GetSourceLine(int i) const
VTextManager::GetSourceLine returns the reference to i-th text line.
void Update(const QString &qsName, const VPieceLabelData &data)
VTextManager::Update updates the text lines with detail data.
void setFont(const QFont &font)
SetFont set the text base font.
void FitFontSize(qreal fW, qreal fH)
VTextManager::FitFontSize sets the font size just big enough, so that the text fits into rectangle of...
const QString cursorArrowOpenHand
void SetItemOverrideCursor(QGraphicsItem *item, const QString &pixmapPath, int hotX, int hotY)
const QString cursorArrowCloseHand
QRectF GetBoundingRect(QRectF rectBB, qreal rotation)
GetBoundingRect calculates the bounding box around rectBB rectangle, rotated around its center by rot...
The TextLine struct holds the information about one text line.