Dynamic element text item : add new feature -> alignment

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5353 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2018-05-11 18:14:41 +00:00
parent bf12d337c9
commit 609fcc351f
10 changed files with 480 additions and 6 deletions

View File

@@ -171,6 +171,17 @@ QColor DiagramTextItem::color() const {
return defaultTextColor();
}
void DiagramTextItem::setAlignment(const Qt::Alignment &alignment)
{
m_alignment = alignment;
emit alignmentChanged(alignment);
}
Qt::Alignment DiagramTextItem::alignment() const
{
return m_alignment;
}
/**
* @brief DiagramTextItem::paint
* Draw this text field. This method draw the text by calling QGraphicsTextItem::paint.
@@ -328,6 +339,46 @@ void DiagramTextItem::applyRotation(const qreal &angle) {
setRotation(QET::correctAngle(rotation()+angle));
}
/**
* @brief DiagramTextItem::prepareAlignment
* Call this function before changing the bounding rect of this text.
*/
void DiagramTextItem::prepareAlignment()
{
m_alignment_rect = mapToParent(boundingRect()).boundingRect();
}
/**
* @brief DiagramTextItem::finishAlignment
* Call this function after changing the bouding rect of this text
* to set the position of this text according the alignment property.
*/
void DiagramTextItem::finishAlignment()
{
if(m_block_alignment)
return;
QPointF pos = this->pos();
if(m_alignment &Qt::AlignRight)
pos.setX(m_alignment_rect.right() - boundingRect().width());
else if(m_alignment &Qt::AlignHCenter)
{
qreal x = m_alignment_rect.x() + (m_alignment_rect.width()/2);
pos.setX(x - boundingRect().width()/2);
}
if(m_alignment &Qt::AlignBottom)
pos.setY(m_alignment_rect.bottom() - boundingRect().height());
else if(m_alignment &Qt::AlignVCenter)
{
qreal y = m_alignment_rect.y() + (m_alignment_rect.height()/2);
pos.setY(y - boundingRect().height()/2);
}
setPos(pos);
}
/**
* @brief Edit the text with HtmlEditor
*/

View File

@@ -35,10 +35,14 @@ class DiagramTextItem : public QGraphicsTextItem
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
signals:
void fontSizeChanged(int size);
void colorChanged(QColor color);
void alignmentChanged(Qt::Alignment alignment);
void diagramTextChanged(DiagramTextItem *, const QString &, const QString &);
void textEdited(const QString &old_str, const QString &new_str);
public:
DiagramTextItem(QGraphicsItem * = nullptr);
@@ -68,6 +72,10 @@ class DiagramTextItem : public QGraphicsTextItem
QColor color() const;
void setNoEditable(bool e = true) {m_no_editable = e;}
void setAlignment(const Qt::Alignment &alignment);
Qt::Alignment alignment() const;
bool m_block_alignment = false;
protected:
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override;
@@ -84,10 +92,9 @@ class DiagramTextItem : public QGraphicsTextItem
void hoverMoveEvent(QGraphicsSceneHoverEvent *) override;
virtual void applyRotation(const qreal &);
void prepareAlignment();
void finishAlignment();
signals:
void diagramTextChanged(DiagramTextItem *, const QString &, const QString &);
void textEdited(const QString &old_str, const QString &new_str);
protected:
bool m_mouse_hover = false,
@@ -98,5 +105,9 @@ class DiagramTextItem : public QGraphicsTextItem
m_previous_text;
QPointF m_mouse_to_origin_movement;
private:
QRectF m_alignment_rect;
Qt::Alignment m_alignment = (Qt::AlignTop | Qt::AlignLeft);
};
#endif

View File

@@ -97,6 +97,22 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from));
me = QMetaEnum::fromType<Qt::Alignment>();
if(this->alignment() &Qt::AlignRight)
root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignRight));
else if(this->alignment() &Qt::AlignLeft)
root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignLeft));
else if(this->alignment() &Qt::AlignHCenter)
root_element.setAttribute("Halignment", me.valueToKey(Qt::AlignHCenter));
if(this->alignment() &Qt::AlignBottom)
root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom));
else if(this->alignment() & Qt::AlignTop)
root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignTop));
else if(this->alignment() &Qt::AlignVCenter)
root_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter));
QDomElement dom_text = dom_doc.createElement("text");
dom_text.appendChild(dom_doc.createTextNode(toPlainText()));
root_element.appendChild(dom_text);
@@ -140,8 +156,6 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
return;
}
QGraphicsTextItem::setPos(dom_elmt.attribute("x", QString::number(0)).toDouble(),
dom_elmt.attribute("y", QString::number(0)).toDouble());
QGraphicsTextItem::setRotation(dom_elmt.attribute("rotation", QString::number(0)).toDouble());
setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
@@ -151,6 +165,12 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
//Text from
QMetaEnum me = textFromMetaEnum();
setTextFrom(DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data())));
me = QMetaEnum::fromType<Qt::Alignment>();
if(dom_elmt.hasAttribute("Halignment"))
setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Halignment").toStdString().data())));
if(dom_elmt.hasAttribute(("Valignment")))
setAlignment(Qt::Alignment(me.keyToValue(dom_elmt.attribute("Valignment").toStdString().data())) | this->alignment());
//Text
QDomElement dom_text = dom_elmt.firstChildElement("text");
@@ -174,6 +194,9 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
//Force the update of the displayed text
setTextFrom(m_text_from);
QGraphicsTextItem::setPos(dom_elmt.attribute("x", QString::number(0)).toDouble(),
dom_elmt.attribute("y", QString::number(0)).toDouble());
}
/**
@@ -1291,6 +1314,8 @@ void DynamicElementTextItem::updateXref()
void DynamicElementTextItem::setPlainText(const QString &text)
{
prepareAlignment();
DiagramTextItem::setPlainText(text);
//User define a text width
@@ -1306,6 +1331,8 @@ void DynamicElementTextItem::setPlainText(const QString &text)
}
}
finishAlignment();
if(m_Xref_item)
m_Xref_item->autoPos();
else if(m_slave_Xref_item)
@@ -1323,4 +1350,3 @@ void DynamicElementTextItem::setTextWidth(qreal width)
m_text_width = width;
emit textWidthChanged(width);
}

View File

@@ -130,6 +130,7 @@ class DynamicElementTextItem : public DiagramTextItem
void conductorPropertiesChanged();
QString reportReplacedCompositeText() const;
void zoomToLinkedElement();
private:
QPointer <Element> m_parent_element,

View File

@@ -551,7 +551,14 @@ bool Element::fromXml(QDomElement &e, QHash<int, Terminal *> &table_id_adr, bool
!m_element_informations.value("label").toString().isEmpty())
dc.addValue("label", m_element_informations.value("label"));
//We must to block the update of the alignment when load the information
//otherwise the pos of the text will not be the same as it was at save time.
for(DynamicElementTextItem *deti : m_dynamic_text_list)
deti->m_block_alignment = true;
setElementInformations(dc);
for(DynamicElementTextItem *deti : m_dynamic_text_list)
deti->m_block_alignment = false;
/**
During the devel of the version 0.7, the "old text" was replaced by the dynamic element text item.