Dynamic element text item : Add a new option "width" for define the width of the text.

If the text is wider than the defined width, the text is broken into multiple line.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5220 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2018-01-26 18:49:38 +00:00
parent 3e48da73b0
commit 31fcec10ff
6 changed files with 96 additions and 14 deletions

View File

@@ -52,6 +52,12 @@ DynamicElementTextItem::DynamicElementTextItem(Element *parent_element) :
}
});
//Option when text is displayed in multiple line
QTextOption option = document()->defaultTextOption();
option.setAlignment(Qt::AlignHCenter);
option.setWrapMode(QTextOption::WordWrap);
document()->setDefaultTextOption(option);
}
DynamicElementTextItem::~DynamicElementTextItem()
@@ -86,6 +92,7 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
root_element.setAttribute("font_size", font().pointSize());
root_element.setAttribute("uuid", m_uuid.toString());
root_element.setAttribute("frame", m_frame? "true" : "false");
root_element.setAttribute("text_width", QString::number(m_text_width));
QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from));
@@ -139,6 +146,7 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
//Text from
QMetaEnum me = textFromMetaEnum();
@@ -603,6 +611,7 @@ void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphics
if (m_frame)
{
painter->save();
painter->setFont(QETApp::diagramTextsFont(fontSize()));
//Adjust the thickness according to the font size,
@@ -620,17 +629,24 @@ void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphics
painter->setPen(pen);
painter->setRenderHint(QPainter::Antialiasing);
//Get the bounding rectangle of the text
QRectF text_bounding = painter->boundingRect(boundingRect(), toPlainText());
//Center text_bounding in the bounding rect of this
text_bounding.moveTop((boundingRect().height()-text_bounding.height())/2);
text_bounding.moveLeft((boundingRect().width() - text_bounding.width())/2);
//adjust only for better visual
text_bounding.adjust(-2,0,2,0);
//Get the bounding rectangle of the text
QSizeF size = document()->size();
size.setWidth(document()->idealWidth());
//Remove the margin. Size is exactly the bounding rect of the text
size.rheight() -= document()->documentMargin()*2;
size.rwidth() -= document()->documentMargin()*2;
//Add a little margin only for a better visual;
size.rheight() += 2;
size.rwidth() += 2;
//The pos of the rect
QPointF pos = boundingRect().center();
pos.rx() -= size.width()/2;
pos.ry() -= size.height()/2;
//Adjust the rounding of the rectangle according to the size of the font
qreal ro = (qreal)fontSize()/3;
painter->drawRoundedRect(text_bounding, ro, ro);
painter->drawRoundedRect(QRectF(pos, size), ro, ro);
painter->restore();
}
@@ -1262,6 +1278,20 @@ void DynamicElementTextItem::updateXref()
void DynamicElementTextItem::setPlainText(const QString &text)
{
DiagramTextItem::setPlainText(text);
//User define a text width
if(m_text_width > 0)
{
if(document()->size().width() > m_text_width)
{
document()->setTextWidth(m_text_width);
if(document()->size().width() > m_text_width)
{
document()->setTextWidth(document()->idealWidth());
}
}
}
if(m_Xref_item)
m_Xref_item->autoPos();
else if(m_slave_Xref_item)
@@ -1273,3 +1303,10 @@ void DynamicElementTextItem::setPlainText(const QString &text)
}
}
void DynamicElementTextItem::setTextWidth(qreal width)
{
this->document()->setTextWidth(width);
m_text_width = width;
emit textWidthChanged(width);
}

View File

@@ -1,4 +1,4 @@
/*
/*
Copyright 2006-2017 The QElectroTech Team
This file is part of QElectroTech.
@@ -47,6 +47,7 @@ class DynamicElementTextItem : public DiagramTextItem
Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
public:
Q_ENUMS(TextFrom)
@@ -65,6 +66,7 @@ class DynamicElementTextItem : public DiagramTextItem
void compositeTextChanged(QString text);
void frameChanged(bool frame);
void plainTextChanged();
void textWidthChanged(qreal width);
public:
DynamicElementTextItem(Element *parent_element);
@@ -97,6 +99,7 @@ class DynamicElementTextItem : public DiagramTextItem
QUuid uuid() const;
void updateXref();
void setPlainText(const QString &text);
void setTextWidth(qreal width);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
@@ -148,6 +151,7 @@ class DynamicElementTextItem : public DiagramTextItem
m_first_scene_change = true;
CrossRefItem *m_Xref_item = nullptr;
QGraphicsTextItem *m_slave_Xref_item = nullptr;
qreal m_text_width = -1;
};
#endif // DYNAMICELEMENTTEXTITEM_H

View File

@@ -665,6 +665,7 @@ bool Element::fromXml(QDomElement &e, QHash<int, Terminal *> &table_id_adr, bool
comment_text->setInfoName("comment");
comment_text->setFontSize(6);
comment_text->setFrame(true);
comment_text->setTextWidth(70);
comment_text->setPos(deti->x(), deti->y()+10); //+10 is arbitrary, comment_text must be below deti
addDynamicTextItem(comment_text);
}
@@ -676,6 +677,7 @@ bool Element::fromXml(QDomElement &e, QHash<int, Terminal *> &table_id_adr, bool
location_text->setTextFrom(DynamicElementTextItem::ElementInfo);
location_text->setInfoName("location");
location_text->setFontSize(6);
location_text->setTextWidth(70);
location_text->setPos(deti->x(), deti->y()+20); //+20 is arbitrary, location_text must be below deti and comment
addDynamicTextItem(location_text);
}

View File

@@ -75,6 +75,7 @@ void ElementTextItemGroup::addToGroup(QGraphicsItem *item)
connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::plainTextChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::textWidthChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateXref);
connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref);
@@ -105,6 +106,7 @@ void ElementTextItemGroup::removeFromGroup(QGraphicsItem *item)
disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::plainTextChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::textWidthChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::textFromChanged, this, &ElementTextItemGroup::updateXref);
disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref);