mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-03-10 18:39:59 +01:00
Element editor : dynamic text field can be set with element info as source of text.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5260 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -54,6 +54,11 @@ PartDynamicTextField::PartDynamicTextField(QETElementEditor *editor, QGraphicsIt
|
||||
setTextFrom(DynamicElementTextItem::UserText);
|
||||
setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsMovable);
|
||||
|
||||
//Option when text is displayed in multiple line
|
||||
QTextOption option = document()->defaultTextOption();
|
||||
option.setAlignment(Qt::AlignHCenter);
|
||||
option.setWrapMode(QTextOption::WordWrap);
|
||||
document()->setDefaultTextOption(option);
|
||||
}
|
||||
|
||||
QString PartDynamicTextField::name() const
|
||||
@@ -106,6 +111,7 @@ const QDomElement PartDynamicTextField::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 = DynamicElementTextItem::textFromMetaEnum();
|
||||
@@ -130,14 +136,6 @@ const QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const
|
||||
dom_comp_text.appendChild(dom_doc.createTextNode(m_composite_text));
|
||||
root_element.appendChild(dom_comp_text);
|
||||
}
|
||||
|
||||
//tagg
|
||||
if (!m_tagg.isEmpty())
|
||||
{
|
||||
QDomElement dom_tagg = dom_doc.createElement("tagg");
|
||||
dom_tagg.appendChild(dom_doc.createTextNode(m_tagg));
|
||||
root_element.appendChild(dom_tagg);
|
||||
}
|
||||
|
||||
//Color
|
||||
if(color() != QColor(Qt::black))
|
||||
@@ -167,7 +165,8 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt)
|
||||
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()));
|
||||
m_frame = dom_elmt.attribute("frame", "false") == "true"? true : false;
|
||||
setFrame(dom_elmt.attribute("frame", "false") == "true"? true : false);
|
||||
setTextWidth(dom_elmt.attribute("text_width", QString::number(-1)).toDouble());
|
||||
|
||||
QMetaEnum me = DynamicElementTextItem::textFromMetaEnum();
|
||||
m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data()));
|
||||
@@ -189,11 +188,6 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt)
|
||||
QDomElement dom_comp_text = dom_elmt.firstChildElement("composite_text");
|
||||
if(!dom_comp_text.isNull())
|
||||
m_composite_text = dom_comp_text.text();
|
||||
|
||||
//tagg
|
||||
QDomElement dom_tagg = dom_elmt.firstChildElement("tagg");
|
||||
if (!dom_tagg.isNull())
|
||||
m_tagg = dom_tagg.text();
|
||||
|
||||
//Color
|
||||
QDomElement dom_color = dom_elmt.firstChildElement("color");
|
||||
@@ -249,25 +243,6 @@ void PartDynamicTextField::setTextFrom(DynamicElementTextItem::TextFrom text_fro
|
||||
emit textFromChanged(m_text_from);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartDynamicTextField::tagg
|
||||
* @return the tagg of this text
|
||||
*/
|
||||
QString PartDynamicTextField::tagg() const {
|
||||
return m_tagg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartDynamicTextField::setTagg
|
||||
* set the taggof this text
|
||||
* @param tagg
|
||||
*/
|
||||
void PartDynamicTextField::setTagg(const QString &tagg)
|
||||
{
|
||||
m_tagg = tagg;
|
||||
emit taggChanged(m_tagg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartDynamicTextField::text
|
||||
* @return the text of this text
|
||||
@@ -364,6 +339,36 @@ bool PartDynamicTextField::frame() const
|
||||
return m_frame;
|
||||
}
|
||||
|
||||
void PartDynamicTextField::setTextWidth(qreal width)
|
||||
{
|
||||
this->document()->setTextWidth(width);
|
||||
|
||||
//Adjust the width, to ideal width if needed
|
||||
if(width > 0 && document()->size().width() > width)
|
||||
document()->setTextWidth(document()->idealWidth());
|
||||
|
||||
m_text_width = document()->textWidth();
|
||||
emit textWidthChanged(m_text_width);
|
||||
}
|
||||
|
||||
void PartDynamicTextField::setPlainText(const QString &text)
|
||||
{
|
||||
QGraphicsTextItem::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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartDynamicTextField::mouseMoveEvent
|
||||
* @param event
|
||||
@@ -417,7 +422,16 @@ void PartDynamicTextField::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
QVariant PartDynamicTextField::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
if (change == QGraphicsItem::ItemPositionHasChanged || change == QGraphicsItem::ItemSceneHasChanged)
|
||||
{
|
||||
updateCurrentPartEditor();
|
||||
if(change == QGraphicsItem::ItemSceneHasChanged &&
|
||||
m_first_add &&
|
||||
elementScene() != nullptr)
|
||||
{
|
||||
connect(elementScene(), &ElementScene::elementInfoChanged, this, &PartDynamicTextField::elementInfoChanged);
|
||||
m_first_add = false;
|
||||
}
|
||||
}
|
||||
else if ((change == QGraphicsItem::ItemSelectedHasChanged) && (value.toBool() == true))
|
||||
updateCurrentPartEditor();
|
||||
|
||||
@@ -441,25 +455,45 @@ void PartDynamicTextField::paint(QPainter *painter, const QStyleOptionGraphicsIt
|
||||
if(w > 2.5)
|
||||
w = 2.5;
|
||||
}
|
||||
|
||||
|
||||
QPen pen;
|
||||
pen.setColor(color());
|
||||
pen.setWidthF(w);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartDynamicTextField::elementInfoChanged
|
||||
* Used to up to date this text field, when the element information (see elementScene) changed
|
||||
*/
|
||||
void PartDynamicTextField::elementInfoChanged()
|
||||
{
|
||||
if(m_text_from == DynamicElementTextItem::ElementInfo)
|
||||
{
|
||||
DiagramContext dc = elementScene()->elementInformation();
|
||||
setPlainText(dc.value(m_info_name).toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString tagg READ tagg WRITE setTagg NOTIFY taggChanged)
|
||||
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
|
||||
Q_PROPERTY(DynamicElementTextItem::TextFrom textFrom READ textFrom WRITE setTextFrom NOTIFY textFromChanged)
|
||||
Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
|
||||
@@ -41,6 +40,7 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
|
||||
Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
|
||||
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
|
||||
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
|
||||
|
||||
public:
|
||||
static bool canImportFromTextField(const QDomElement &dom_element);
|
||||
@@ -59,6 +59,7 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
void colorChanged(QColor color);
|
||||
void fontSizeChanged(int size);
|
||||
void frameChanged(bool frame);
|
||||
void textWidthChanged(qreal width);
|
||||
|
||||
public:
|
||||
PartDynamicTextField(QETElementEditor *editor, QGraphicsItem *parent = nullptr);
|
||||
@@ -68,6 +69,7 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
|
||||
QString name() const override;
|
||||
QString xmlName() const override;
|
||||
static QString xmlTaggName() {return QString("dynamic_text");}
|
||||
bool isUseless() const override {return false;}
|
||||
QRectF sceneGeometricRect() const override {return sceneBoundingRect();}
|
||||
void startUserTransformation(const QRectF &initial_selection_rect) override;
|
||||
@@ -79,8 +81,6 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
|
||||
DynamicElementTextItem::TextFrom textFrom() const;
|
||||
void setTextFrom (DynamicElementTextItem::TextFrom text_from);
|
||||
QString tagg() const;
|
||||
void setTagg(const QString &tagg);
|
||||
QString text() const;
|
||||
void setText(const QString &text);
|
||||
void setInfoName(const QString &info_name);
|
||||
@@ -93,6 +93,8 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
int fontSize()const;
|
||||
void setFrame(bool frame);
|
||||
bool frame() const;
|
||||
void setTextWidth(qreal width);
|
||||
void setPlainText(const QString &text);
|
||||
|
||||
protected:
|
||||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
@@ -101,18 +103,20 @@ class PartDynamicTextField : public QGraphicsTextItem, public CustomElementPart
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
|
||||
private:
|
||||
void elementInfoChanged();
|
||||
|
||||
private:
|
||||
QPointF m_origine_pos,
|
||||
m_saved_point;
|
||||
QString m_tagg,
|
||||
m_text,
|
||||
QString m_text,
|
||||
m_info_name,
|
||||
m_composite_text;
|
||||
|
||||
DynamicElementTextItem::TextFrom m_text_from = DynamicElementTextItem::UserText;
|
||||
QUuid m_uuid;
|
||||
|
||||
bool m_frame = false;
|
||||
bool m_frame = false,
|
||||
m_first_add = true;
|
||||
qreal m_text_width = -1;
|
||||
};
|
||||
|
||||
#endif // PARTDYNAMICTEXTFIELD_H
|
||||
|
||||
Reference in New Issue
Block a user