Tried QGraphicsTextItem::boundingBox approach

--HG--
branch : feature
This commit is contained in:
BojanKverh 2016-06-24 17:57:08 +02:00
parent 6fbffd2834
commit caa6b425b9
6 changed files with 118 additions and 92 deletions

View File

@ -1,7 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<pattern>
<!--Valentina pattern format.-->
<version>0.3.1</version>
<version>0.2.4</version>
<unit>cm</unit>
<author/>
<description/>
@ -26,14 +26,14 @@
<point type="alongLine" typeLine="hair" name="M" id="15" firstPoint="6" secondPoint="11" mx="0.132292" lineColor="black" my="0.264583" length="1.15"/>
<spline point4="12" type="simple" kAsm1="1" kAsm2="0.951923" angle1="180" angle2="352.272" id="19" kCurve="1" color="black" point1="14"/>
<spline point4="13" type="simple" kAsm1="1.03" kAsm2="0.99" angle1="0" angle2="184" id="20" kCurve="1" color="black" point1="15"/>
<point type="cutSpline" name="N" id="21" spline="19" mx="0.132292" my="0.264583" length="Spl_L_J/3"/>
<point type="cutSpline" name="P" id="25" spline="19" mx="0.0124795" my="0.504208" length="Spl_L_J*0.66"/>
<point type="cutSpline" name="p1" id="28" spline="23" mx="-0.439814" my="-1.99663" length="(Spl_L_J - (waist_circ/4))/2"/>
<point type="cutSpline" name="p2" id="31" spline="27" mx="-0.762067" my="-2.16296" length="(Spl_L_J - (waist_circ/4))/2"/>
<point type="cutSpline" name="R" id="42" spline="20" mx="0.132292" my="0.264583" length="Spl_M_K/3"/>
<point type="cutSpline" name="T" id="45" spline="20" mx="-0.378769" my="0.328469" length="Spl_M_K*0.66"/>
<point type="cutSpline" name="p5" id="48" spline="44" mx="-0.37877" my="-2.40734" length="(Spl_M_K - waist_circ/4)/2"/>
<point type="cutSpline" name="p6" id="51" spline="47" mx="-0.442653" my="-2.22685" length="(Spl_M_K - waist_circ/4)/2"/>
<point type="cutSpline" name="N" id="21" spline="19" color="black" mx="0.132292" my="0.264583" length="Spl_L_J/3"/>
<point type="cutSpline" name="P" id="25" spline="19" color="black" mx="0.0124795" my="0.504208" length="Spl_L_J*0.66"/>
<point type="cutSpline" name="p1" id="28" spline="23" color="black" mx="-0.439814" my="-1.99663" length="(Spl_L_J - (waist_circ/4))/2"/>
<point type="cutSpline" name="p2" id="31" spline="27" color="black" mx="-0.762067" my="-2.16296" length="(Spl_L_J - (waist_circ/4))/2"/>
<point type="cutSpline" name="R" id="42" spline="20" color="black" mx="0.132292" my="0.264583" length="Spl_M_K/3"/>
<point type="cutSpline" name="T" id="45" spline="20" color="black" mx="-0.378769" my="0.328469" length="Spl_M_K*0.66"/>
<point type="cutSpline" name="p5" id="48" spline="44" color="black" mx="-0.37877" my="-2.40734" length="(Spl_M_K - waist_circ/4)/2"/>
<point type="cutSpline" name="p6" id="51" spline="47" color="black" mx="-0.442653" my="-2.22685" length="(Spl_M_K - waist_circ/4)/2"/>
<point type="alongLine" typeLine="hair" name="p9" id="60" firstPoint="3" secondPoint="9" mx="0.132292" lineColor="black" my="0.264583" length="Line_C_G - 3"/>
<spline point4="9" type="simple" kAsm1="1.36147" kAsm2="0.520526" angle1="291.592" angle2="91.5128" id="73" kCurve="1" color="black" point1="13"/>
<point type="alongLine" typeLine="hair" name="A1" id="74" firstPoint="3" secondPoint="9" mx="0.132292" lineColor="black" my="0.264583" length="Line_C_G/2"/>

View File

@ -29,7 +29,7 @@
#ifndef VPATTERNPIECEDATA_H
#define VPATTERNPIECEDATA_H
#define MIN_FONT_SIZE 24
#define MIN_FONT_SIZE 12
#include <QList>
#include <QString>

View File

@ -29,11 +29,16 @@
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QTextDocument>
#include <QDebug>
#include "vtextgraphicsitem.h"
#define RESIZE_SQUARE 30
#define MIN_W 120
#define MIN_H 60
#define MIN_FONT_SIZE 12
#define MAX_FONT_SIZE 36
//---------------------------------------------------------------------------------------------------------------------
VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
@ -42,7 +47,12 @@ VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
m_eMode = mNormal;
m_iMinW = 100;
m_rectBoundingBox.setTopLeft(QPointF(0, 0));
setTextWidth(MIN_W);
m_rectBoundingBox.setWidth(MIN_W);
m_iMinH = MIN_H;
m_rectBoundingBox.setHeight(m_iMinH);
setZValue(2);
}
//---------------------------------------------------------------------------------------------------------------------
@ -67,13 +77,6 @@ void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
}
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetMinimalWidth(int iW)
{
m_iMinW = iW;
setTextWidth(m_iMinW);
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::Reset()
{
@ -82,11 +85,21 @@ void VTextGraphicsItem::Reset()
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetHTML(const QString& qsHTML)
QRectF VTextGraphicsItem::boundingRect() const
{ return m_rectBoundingBox; }
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::SetHTML(const QString& qsHtml)
{
QGraphicsTextItem::setHtml(qsHTML);
m_iMinFontSize = font().pixelSize();
m_iMinH = boundingRect().height();
QGraphicsTextItem::setHtml(qsHtml);
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();
}
//---------------------------------------------------------------------------------------------------------------------
@ -98,13 +111,11 @@ void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
if (m_rectResize.contains(pME->pos()) == true)
{
m_eMode = mResize;
m_iCurrentH = boundingRect().height();
}
else
{
m_eMode = mMove;
}
qDebug() << "MOUSEPRESS" << m_rectResize << pME->pos() << int(m_eMode);
Update();
m_ptStart = pME->pos();
}
@ -122,29 +133,21 @@ void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *pME)
else if (m_eMode == mResize)
{
prepareGeometryChange();
int iNewW = textWidth() + pME->pos().x() - m_ptStart.x();
m_iCurrentH += pME->pos().y() - m_ptStart.y();
if (iNewW >= m_iMinW)
{
setTextWidth(iNewW);
m_ptStart.setX(pME->pos().x());
Update();
}
qreal fW = m_rectBoundingBox.width() + pME->pos().x() - m_ptStart.x();
qreal fH = m_rectBoundingBox.height() + pME->pos().y() - m_ptStart.y();
if (m_iCurrentH >= m_iMinH)
if (fW >= MIN_W)
{
int iFontSize = m_iMinFontSize*m_iCurrentH/m_iMinH;
if (iFontSize > m_iMinFontSize*2)
m_rectBoundingBox.setWidth(fW);
setTextWidth(fW);
}
if (fH >= m_iMinH)
{
// prevent too big letters
return;
}
QFont fnt = font();
fnt.setPixelSize(iFontSize);
setFont(fnt);
m_ptStart.setY(pME->pos().y());
Update();
m_rectBoundingBox.setHeight(fH);
}
m_ptStart = pME->pos();
UpdateFont();
}
}
@ -183,10 +186,34 @@ void VTextGraphicsItem::SetResizeArea()
}
//---------------------------------------------------------------------------------------------------------------------
void VTextGraphicsItem::setHtml(const QString &qsHtml)
void VTextGraphicsItem::UpdateFont()
{
Q_UNUSED(qsHtml);
// prevent using this method! Use SetHTML instead!
QScopedPointer<QTextDocument> pDoc(document()->clone());
pDoc->setTextWidth(m_rectBoundingBox.width());
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;
fnt.setPixelSize(iFS);
pDoc->setDefaultFont(fnt);
sz = pDoc->size();
}
while ((sz.width() >= m_rectBoundingBox.width() || sz.height() >= m_rectBoundingBox.height()) && iFS > MIN_FONT_SIZE)
{
--iFS;
fnt.setPixelSize(iFS);
pDoc->setDefaultFont(fnt);
sz = pDoc->size();
}
fnt.setPixelSize(iFS);
setFont(fnt);
Update();
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -48,10 +48,10 @@ public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void SetMinimalWidth(int iW);
void Reset();
void SetHTML(const QString& qsHTML);
QRectF boundingRect() const;
void SetHTML(const QString& qsHtml);
signals:
void SignalMoved(QPointF ptPos);
@ -64,16 +64,15 @@ protected:
void Update();
void SetResizeArea();
void setHtml(const QString& qsHtml);
void UpdateFont();
private:
int m_iMinW;
Mode m_eMode;
QPointF m_ptStart;
QRectF m_rectResize;
int m_iMinFontSize;
int m_iMinH;
int m_iCurrentH;
QRectF m_rectBoundingBox;
};
#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::SignalResized, this, &VToolDetail::SaveResize);
UpdateLabel();
}
//---------------------------------------------------------------------------------------------------------------------
@ -302,7 +303,6 @@ void VToolDetail::FullUpdateFromGuiOk(int result)
{
if (result == QDialog::Accepted)
{
qDebug() << "FullUpdate" << qApp->getUndoStack()->count();
SCASSERT(dialog != nullptr);
DialogDetail *dialogTool = qobject_cast<DialogDetail*>(dialog);
SCASSERT(dialogTool != nullptr);
@ -313,7 +313,7 @@ void VToolDetail::FullUpdateFromGuiOk(int result)
SaveDetailOptions *saveCommand = new SaveDetailOptions(oldDet, newDet, doc, id, this->scene());
connect(saveCommand, &SaveDetailOptions::NeedLiteParsing, doc, &VAbstractPattern::LiteParseTree);
qApp->getUndoStack()->push(saveCommand);
qDebug() << "FullUpdate finished" << qApp->getUndoStack()->count();
UpdateLabel();
}
delete dialog;
dialog = nullptr;
@ -629,6 +629,41 @@ void VToolDetail::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief UpdateLabel updates the text label, making it just big enough for the text to fit it
*/
void VToolDetail::UpdateLabel()
{
const VDetail detail = VAbstractTool::data.GetDetail(id);
const VPatternPieceData& data = detail.GetPatternPieceData();
if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0)
{
QString qsText = "Cut %1 on %2%3";
QStringList qslPlace;
qslPlace << "" << " on Fold";
QFont fnt = qApp->font();
fnt.setPixelSize(data.GetFontSize());
dataLabel->setFont(fnt);
QString qsHTML = "<b><font size=\"+4\"><center>" + data.GetLetter() + "</center></font></b><br/>";
qsHTML += "<font size=\"+2\" align=\"center\">" + data.GetName() + "</font><br/>";
for (int i = 0; i < data.GetMCPCount(); ++i)
{
MaterialCutPlacement mcp = data.GetMCP(i);
QString qsLine = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]);
qsHTML += qsLine + "<br/>";
}
dataLabel->SetHTML(qsHTML);
dataLabel->setPos(data.GetPos());
dataLabel->show();
}
else
{
dataLabel->hide();
}
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief SaveMove saves the move operation to the undo stack
@ -752,42 +787,6 @@ void VToolDetail::RefreshGeometry()
}
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
const VPatternPieceData& data = detail.GetPatternPieceData();
if (data.GetLetter().isEmpty() == false || data.GetName().isEmpty() == false || data.GetMCPCount() > 0)
{
QString qsText = "Cut %1 on %2%3";
QStringList qslPlace;
qslPlace << "" << " on Fold";
QFont fnt = qApp->font();
fnt.setPixelSize(data.GetFontSize());
QFontMetrics fm(fnt);
dataLabel->setFont(fnt);
int iMinW = 200;
QString qsHTML = "<b><font size=\"+4\">" + data.GetLetter() + "</font></b><br/>";
qsHTML += "<font size=\"+2\">" + data.GetName() + "</font><br/>";
for (int i = 0; i < data.GetMCPCount(); ++i)
{
MaterialCutPlacement mcp = data.GetMCP(i);
QString qsLine = qsText.arg(mcp.m_iCutNumber).arg(mcp.m_qsMaterialUserDef).arg(qslPlace[int(mcp.m_ePlacement)]);
if (fm.width(qsLine) > iMinW)
iMinW = fm.width(qsLine);
qsHTML += qsLine + "<br/>";
}
// also add some offset
dataLabel->SetMinimalWidth(iMinW + 10);
if (data.GetLabelWidth() > iMinW)
{
dataLabel->setTextWidth(data.GetLabelWidth());
}
dataLabel->SetHTML(qsHTML);
dataLabel->setPos(data.GetPos());
dataLabel->show();
}
else
{
dataLabel->hide();
}
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -104,6 +104,7 @@ protected:
virtual void contextMenuEvent ( QGraphicsSceneContextMenuEvent * event ) Q_DECL_OVERRIDE;
virtual void keyReleaseEvent(QKeyEvent * event) Q_DECL_OVERRIDE;
virtual void SetVisualization() Q_DECL_OVERRIDE {}
virtual void UpdateLabel();
protected slots:
virtual void SaveMove(QPointF ptPos);