Reimplemented QTextGraphicsItem class

--HG--
branch : feature
This commit is contained in:
BojanKverh 2016-06-25 17:19:44 +02:00
parent 33e1a12a67
commit 21debaf249
4 changed files with 228 additions and 138 deletions

View File

@ -38,20 +38,19 @@
#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
#define MAX_FONT_SIZE 36 #define MAX_FONT_SIZE 48
#define SPACING 2
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent) VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
: QGraphicsTextItem(pParent) : QGraphicsObject(pParent)
{ {
setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsSelectable, true);
m_eMode = mNormal; m_eMode = mNormal;
m_rectBoundingBox.setTopLeft(QPointF(0, 0)); m_rectBoundingBox.setTopLeft(QPointF(0, 0));
setTextWidth(MIN_W);
m_rectBoundingBox.setWidth(MIN_W);
m_iMinH = MIN_H; m_iMinH = MIN_H;
m_rectBoundingBox.setHeight(m_iMinH); Resize(MIN_W, m_iMinH);
setZValue(2); setZValue(2);
} }
@ -59,11 +58,33 @@ VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
VTextGraphicsItem::~VTextGraphicsItem() VTextGraphicsItem::~VTextGraphicsItem()
{} {}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetFont(const QFont& fnt)
{
m_font = fnt;
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{ {
painter->fillRect(option->rect, QColor(251, 251, 175)); painter->fillRect(option->rect, QColor(251, 251, 175));
QGraphicsTextItem::paint(painter, option, widget);
// draw text lines
int iY = 0;
int iH;
painter->setPen(Qt::black);
QFont fnt = m_font;
for (int i = 0; i < m_liOutput.count(); ++i)
{
const TextLine& tl = m_liOutput.at(i);
iH = tl.m_iHeight;
fnt.setPixelSize(m_font.pixelSize() + tl.m_iFontSize);
fnt.setWeight(tl.m_eFontWeight);
painter->setFont(fnt);
painter->drawText(0, iY, boundingRect().width(), iH, tl.m_eAlign, tl.m_qsText);
iY += iH + SPACING;
}
if (m_eMode != mNormal) if (m_eMode != mNormal)
{ {
@ -72,149 +93,175 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
painter->setPen(Qt::black); painter->setPen(Qt::black);
painter->setBrush(Qt::black); painter->setBrush(Qt::black);
SetResizeArea();
painter->drawRect(m_rectResize); painter->drawRect(m_rectResize);
if (m_eMode == mResize)
{
painter->drawLine(0, 0, m_rectBoundingBox.width(), m_rectBoundingBox.height());
painter->drawLine(0, m_rectBoundingBox.height(), m_rectBoundingBox.width(), 0);
}
} }
} }
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::AddLine(const TextLine& tl)
{
m_liLines << tl;
while (IsBigEnough(MIN_W, m_iMinH, MIN_FONT_SIZE) == false)
{
m_iMinH += 5;
}
if (m_rectBoundingBox.height() < m_iMinH)
{
Resize(m_rectBoundingBox.width(), m_iMinH);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Clear()
{
m_liLines.clear();
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Reset() void VTextGraphicsItem::Reset()
{ {
m_eMode = mNormal; m_eMode = mNormal;
Update(); UpdateFont();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QRectF VTextGraphicsItem::boundingRect() const QRectF VTextGraphicsItem::boundingRect() const
{ return m_rectBoundingBox; }
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetHTML(const QString& qsHtml)
{ {
QGraphicsTextItem::setHtml(qsHtml); return m_rectBoundingBox;
m_rectBoundingBox.setHeight(document()->size().height());
// calculate the minimal height
QScopedPointer<QTextDocument> pDoc(document()->clone());
QFont fnt = font();
fnt.setPixelSize(MIN_FONT_SIZE);
pDoc->setDefaultFont(fnt);
m_iMinH = pDoc->size().height();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME) void VTextGraphicsItem::Resize(qreal fW, qreal fH)
{ {
if ((pME->button() & Qt::LeftButton) > 0) // don't allow resize under specific size
{ if (fW < MIN_W || fH < m_iMinH)
SetResizeArea(); return;
if (m_rectResize.contains(pME->pos()) == true)
{
m_eMode = mResize;
}
else
{
m_eMode = mMove;
}
Update();
m_ptStart = pME->pos();
}
}
//--------------------------------------------------------------------------------------------------------------------- m_rectBoundingBox.setWidth(fW);
void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *pME) m_rectBoundingBox.setHeight(fH);
{ m_rectResize.setTopLeft(QPointF(fW - RESIZE_SQUARE, fH - RESIZE_SQUARE));
if (m_eMode == mMove)
{
m_eMode = mMove;
QPointF ptRel = pME->pos() - m_ptStart;
moveBy(ptRel.x(), ptRel.y());
}
else if (m_eMode == mResize)
{
prepareGeometryChange();
qreal fW = m_rectBoundingBox.width() + pME->pos().x() - m_ptStart.x();
qreal fH = m_rectBoundingBox.height() + pME->pos().y() - m_ptStart.y();
if (fW >= MIN_W)
{
m_rectBoundingBox.setWidth(fW);
setTextWidth(fW);
}
if (fH >= m_iMinH)
{
m_rectBoundingBox.setHeight(fH);
}
m_ptStart = pME->pos();
UpdateFont();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *pME)
{
if ((pME->button() & Qt::LeftButton) > 0)
{
if (m_eMode == mMove)
{
m_eMode = mActivated;
emit SignalMoved(pos());
}
else if (m_eMode == mResize)
{
m_eMode = mActivated;
emit SignalResized(textWidth(), font().pixelSize());
}
Update();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Update()
{
update(boundingRect());
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetResizeArea()
{
m_rectResize.setLeft(boundingRect().right() - RESIZE_SQUARE);
m_rectResize.setTop(boundingRect().bottom() - RESIZE_SQUARE);
m_rectResize.setWidth(RESIZE_SQUARE); m_rectResize.setWidth(RESIZE_SQUARE);
m_rectResize.setHeight(RESIZE_SQUARE); m_rectResize.setHeight(RESIZE_SQUARE);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::UpdateFont() void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
{ {
QScopedPointer<QTextDocument> pDoc(document()->clone()); m_ptStart = pME->pos();
pDoc->setTextWidth(m_rectBoundingBox.width()); m_szStart = m_rectBoundingBox.size();
if (m_rectResize.contains(m_ptStart) == true)
QFont fnt = font();
int iFS = fnt.pixelSize();
QSizeF sz = pDoc->size();
while (sz.width() < m_rectBoundingBox.width() && sz.height() < m_rectBoundingBox.height() && iFS < MAX_FONT_SIZE)
{ {
++iFS; m_eMode = mResize;
fnt.setPixelSize(iFS);
pDoc->setDefaultFont(fnt);
sz = pDoc->size();
} }
else
while ((sz.width() >= m_rectBoundingBox.width() || sz.height() >= m_rectBoundingBox.height()) && iFS > MIN_FONT_SIZE)
{ {
--iFS; m_eMode = mMove;
fnt.setPixelSize(iFS);
pDoc->setDefaultFont(fnt);
sz = pDoc->size();
} }
fnt.setPixelSize(iFS);
setFont(fnt);
Update(); Update();
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
{
QPointF ptDiff = pME->pos() - m_ptStart;
if (m_eMode == mMove)
{
moveBy(ptDiff.x(), ptDiff.y());
Update();
}
else if (m_eMode == mResize)
{
Resize(m_szStart.width() + ptDiff.x(), m_szStart.height() + ptDiff.y());
UpdateFont();
emit SignalShrink();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
{
m_eMode = mActivated;
Update();
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Update()
{
update(m_rectBoundingBox);
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::UpdateFont()
{
int iFS = m_font.pixelSize();
// increase the font size until the bounding rect is not big enough
while (iFS < MAX_FONT_SIZE && IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == true)
{
++iFS;
}
// decrease the font size until the bounding rect is big enough
while (iFS >= MIN_FONT_SIZE && IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == false)
{
--iFS;
}
m_font.setPixelSize(iFS);
Update();
}
//---------------------------------------------------------------------------------------------------------------------
bool VTextGraphicsItem::IsBigEnough(qreal fW, qreal fH, int iFontSize)
{
m_liOutput.clear();
QFont fnt = m_font;
int iY = 0;
for (int i = 0; i < m_liLines.count(); ++i)
{
const TextLine& tl = m_liLines.at(i);
TextLine tlOut = tl;
fnt.setPixelSize(iFontSize + tl.m_iFontSize);
QFontMetrics fm(fnt);
tlOut.m_iHeight = fm.height();
QStringList qslLines = SplitString(tlOut.m_qsText, fW, fm);
for (int i = 0; i < qslLines.count(); ++i)
{
tlOut.m_qsText = qslLines[i];
m_liOutput << tlOut;
iY += tlOut.m_iHeight + SPACING;
}
}
return iY < fH;
}
//---------------------------------------------------------------------------------------------------------------------
QStringList VTextGraphicsItem::SplitString(const QString &qs, qreal fW, const QFontMetrics &fm)
{
QRegExp reg("\\s+");
QStringList qslWords = qs.split(reg);
QStringList qslLines;
QString qsCurrent;
for (int i = 0; i < qslWords.count(); ++i)
{
if (qsCurrent.length() > 0)
{
qsCurrent += " ";
}
if (fm.width(qsCurrent + qslWords[i]) > fW)
{
qslLines << qsCurrent;
qsCurrent = qslWords[i];
}
else
{
qsCurrent += qslWords[i];
}
}
qslLines << qsCurrent;
return qslLines;
}

View File

@ -29,9 +29,20 @@
#ifndef VTEXTGRAPHICSITEM_H #ifndef VTEXTGRAPHICSITEM_H
#define VTEXTGRAPHICSITEM_H #define VTEXTGRAPHICSITEM_H
#include <QGraphicsTextItem> #include <QGraphicsObject>
#include <QFont>
#include <QList>
class VTextGraphicsItem : public QGraphicsTextItem struct TextLine
{
QString m_qsText;
int m_iFontSize; // 0 means default
QFont::Weight m_eFontWeight;
Qt::Alignment m_eAlign;
int m_iHeight;
};
class VTextGraphicsItem : public QGraphicsObject
{ {
Q_OBJECT Q_OBJECT
@ -46,33 +57,40 @@ public:
VTextGraphicsItem(QGraphicsItem* pParent = 0); VTextGraphicsItem(QGraphicsItem* pParent = 0);
~VTextGraphicsItem(); ~VTextGraphicsItem();
void SetFont(const QFont& fnt);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void Reset(); void Reset();
QRectF boundingRect() const; QRectF boundingRect() const;
void SetHTML(const QString& qsHtml); void AddLine(const TextLine& tl);
void Clear();
signals:
void SignalMoved(QPointF ptPos);
void SignalResized(qreal dTW, int iFontSize);
protected: protected:
void Resize(qreal fW, qreal fH);
void mousePressEvent(QGraphicsSceneMouseEvent* pME); void mousePressEvent(QGraphicsSceneMouseEvent* pME);
void mouseMoveEvent(QGraphicsSceneMouseEvent* pME); void mouseMoveEvent(QGraphicsSceneMouseEvent* pME);
void mouseReleaseEvent(QGraphicsSceneMouseEvent* pME); void mouseReleaseEvent(QGraphicsSceneMouseEvent* pME);
void Update(); void Update();
void SetResizeArea();
void UpdateFont(); void UpdateFont();
bool IsBigEnough(qreal fW, qreal fH, int iFontSize);
QStringList SplitString(const QString& qs, qreal fW, const QFontMetrics& fm);
signals:
void SignalMoved(QPointF ptPos);
void SignalResized(qreal iTW, int iFontSize);
void SignalShrink();
private: private:
Mode m_eMode; Mode m_eMode;
QPointF m_ptStart; QPointF m_ptStart;
QSizeF m_szStart;
QRectF m_rectResize; QRectF m_rectResize;
int m_iMinH; int m_iMinH;
QRectF m_rectBoundingBox; QRectF m_rectBoundingBox;
QFont m_font;
QList<TextLine> m_liLines;
QList<TextLine> m_liOutput;
}; };
#endif // VTEXTGRAPHICSITEM_H #endif // VTEXTGRAPHICSITEM_H

View File

@ -130,6 +130,7 @@ VToolDetail::VToolDetail(VAbstractPattern *doc, VContainer *data, const quint32
connect(dataLabel, &VTextGraphicsItem::SignalMoved, this, &VToolDetail::SaveMove); connect(dataLabel, &VTextGraphicsItem::SignalMoved, this, &VToolDetail::SaveMove);
connect(dataLabel, &VTextGraphicsItem::SignalResized, this, &VToolDetail::SaveResize); connect(dataLabel, &VTextGraphicsItem::SignalResized, this, &VToolDetail::SaveResize);
connect(dataLabel, &VTextGraphicsItem::SignalShrink, this, &VToolDetail::UpdateAll);
UpdateLabel(); UpdateLabel();
} }
@ -640,22 +641,38 @@ void VToolDetail::UpdateLabel()
const VPatternPieceData& data = detail.GetPatternPieceData(); const VPatternPieceData& data = detail.GetPatternPieceData();
if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0) if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0)
{ {
QString qsText = "Cut %1 on %2%3"; QString qsText = "Cut %1 of %2%3";
QStringList qslPlace; QStringList qslPlace;
qslPlace << "" << " on Fold"; qslPlace << "" << " on Fold";
QFont fnt = qApp->font(); QFont fnt = qApp->font();
fnt.setPixelSize(data.GetFontSize()); fnt.setPixelSize(data.GetFontSize());
dataLabel->setFont(fnt); dataLabel->SetFont(fnt);
QString qsHTML = "<b><font size=\"+4\"><center>" + data.GetLetter() + "</center></font></b><br/>"; dataLabel->Clear();
qsHTML += "<font size=\"+2\" align=\"center\">" + data.GetName() + "</font><br/>"; TextLine tl;
for (int i = 0; i < data.GetMCPCount(); ++i) // letter
{ tl.m_qsText = data.GetLetter();
tl.m_eAlign = Qt::AlignCenter;
tl.m_eFontWeight = QFont::Bold;
tl.m_iFontSize = 4;
dataLabel->AddLine(tl);
tl.m_qsText = data.GetName();
tl.m_eAlign = Qt::AlignCenter;
tl.m_eFontWeight = QFont::DemiBold;
tl.m_iFontSize = 2;
dataLabel->AddLine(tl);
// MCP
tl.m_eAlign = Qt::AlignLeft | Qt::AlignVCenter;
tl.m_eFontWeight = QFont::Normal;
tl.m_iFontSize = 0;
for (int i = 0; i < data.GetMCPCount(); ++i) {
MaterialCutPlacement mcp = data.GetMCP(i); MaterialCutPlacement mcp = data.GetMCP(i);
QString qsLine = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]); tl.m_qsText = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]);
qsHTML += qsLine + "<br/>"; dataLabel->AddLine(tl);
} }
dataLabel->SetHTML(qsHTML);
dataLabel->setPos(data.GetPos()); dataLabel->setPos(data.GetPos());
dataLabel->Reset();
dataLabel->show(); dataLabel->show();
} }
else else
@ -848,3 +865,10 @@ void VToolDetail::ResetChild()
{ {
dataLabel->Reset(); dataLabel->Reset();
} }
//---------------------------------------------------------------------------------------------------------------------
void VToolDetail::UpdateAll()
{
sceneDetails->update();
update();
}

View File

@ -92,6 +92,7 @@ public slots:
virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE; virtual void AllowHover(bool enabled) Q_DECL_OVERRIDE;
virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE; virtual void AllowSelecting(bool enabled) Q_DECL_OVERRIDE;
virtual void ResetChild(); virtual void ResetChild();
virtual void UpdateAll();
protected: protected:
virtual void AddToFile () Q_DECL_OVERRIDE; virtual void AddToFile () Q_DECL_OVERRIDE;
virtual void RefreshDataInFile() Q_DECL_OVERRIDE; virtual void RefreshDataInFile() Q_DECL_OVERRIDE;