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() DynamicElementTextItem::~DynamicElementTextItem()
@@ -86,6 +92,7 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
root_element.setAttribute("font_size", font().pointSize()); root_element.setAttribute("font_size", font().pointSize());
root_element.setAttribute("uuid", m_uuid.toString()); root_element.setAttribute("uuid", m_uuid.toString());
root_element.setAttribute("frame", m_frame? "true" : "false"); root_element.setAttribute("frame", m_frame? "true" : "false");
root_element.setAttribute("text_width", QString::number(m_text_width));
QMetaEnum me = textFromMetaEnum(); QMetaEnum me = textFromMetaEnum();
root_element.setAttribute("text_from", me.valueToKey(m_text_from)); 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())); setFont(QETApp::diagramTextsFont(dom_elmt.attribute("font_size", QString::number(9)).toInt()));
m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString())); m_uuid = QUuid(dom_elmt.attribute("uuid", QUuid::createUuid().toString()));
setFrame(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());
//Text from //Text from
QMetaEnum me = textFromMetaEnum(); QMetaEnum me = textFromMetaEnum();
@@ -603,6 +611,7 @@ void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphics
if (m_frame) if (m_frame)
{ {
painter->save(); painter->save();
painter->setFont(QETApp::diagramTextsFont(fontSize())); painter->setFont(QETApp::diagramTextsFont(fontSize()));
//Adjust the thickness according to the font size, //Adjust the thickness according to the font size,
@@ -621,16 +630,23 @@ void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphics
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
//Get the bounding rectangle of the text //Get the bounding rectangle of the text
QRectF text_bounding = painter->boundingRect(boundingRect(), toPlainText()); QSizeF size = document()->size();
//Center text_bounding in the bounding rect of this size.setWidth(document()->idealWidth());
text_bounding.moveTop((boundingRect().height()-text_bounding.height())/2); //Remove the margin. Size is exactly the bounding rect of the text
text_bounding.moveLeft((boundingRect().width() - text_bounding.width())/2); size.rheight() -= document()->documentMargin()*2;
//adjust only for better visual size.rwidth() -= document()->documentMargin()*2;
text_bounding.adjust(-2,0,2,0); //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 //Adjust the rounding of the rectangle according to the size of the font
qreal ro = (qreal)fontSize()/3; qreal ro = (qreal)fontSize()/3;
painter->drawRoundedRect(text_bounding, ro, ro); painter->drawRoundedRect(QRectF(pos, size), ro, ro);
painter->restore(); painter->restore();
} }
@@ -1262,6 +1278,20 @@ void DynamicElementTextItem::updateXref()
void DynamicElementTextItem::setPlainText(const QString &text) void DynamicElementTextItem::setPlainText(const QString &text)
{ {
DiagramTextItem::setPlainText(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) if(m_Xref_item)
m_Xref_item->autoPos(); m_Xref_item->autoPos();
else if(m_slave_Xref_item) 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 Copyright 2006-2017 The QElectroTech Team
This file is part of QElectroTech. 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 infoName READ infoName WRITE setInfoName NOTIFY infoNameChanged)
Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged) Q_PROPERTY(QString compositeText READ compositeText WRITE setCompositeText NOTIFY compositeTextChanged)
Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged) Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged)
Q_PROPERTY(qreal textWidth READ textWidth WRITE setTextWidth NOTIFY textWidthChanged)
public: public:
Q_ENUMS(TextFrom) Q_ENUMS(TextFrom)
@@ -65,6 +66,7 @@ class DynamicElementTextItem : public DiagramTextItem
void compositeTextChanged(QString text); void compositeTextChanged(QString text);
void frameChanged(bool frame); void frameChanged(bool frame);
void plainTextChanged(); void plainTextChanged();
void textWidthChanged(qreal width);
public: public:
DynamicElementTextItem(Element *parent_element); DynamicElementTextItem(Element *parent_element);
@@ -97,6 +99,7 @@ class DynamicElementTextItem : public DiagramTextItem
QUuid uuid() const; QUuid uuid() const;
void updateXref(); void updateXref();
void setPlainText(const QString &text); void setPlainText(const QString &text);
void setTextWidth(qreal width);
protected: protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override; void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
@@ -148,6 +151,7 @@ class DynamicElementTextItem : public DiagramTextItem
m_first_scene_change = true; m_first_scene_change = true;
CrossRefItem *m_Xref_item = nullptr; CrossRefItem *m_Xref_item = nullptr;
QGraphicsTextItem *m_slave_Xref_item = nullptr; QGraphicsTextItem *m_slave_Xref_item = nullptr;
qreal m_text_width = -1;
}; };
#endif // DYNAMICELEMENTTEXTITEM_H #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->setInfoName("comment");
comment_text->setFontSize(6); comment_text->setFontSize(6);
comment_text->setFrame(true); 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 comment_text->setPos(deti->x(), deti->y()+10); //+10 is arbitrary, comment_text must be below deti
addDynamicTextItem(comment_text); 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->setTextFrom(DynamicElementTextItem::ElementInfo);
location_text->setInfoName("location"); location_text->setInfoName("location");
location_text->setFontSize(6); 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 location_text->setPos(deti->x(), deti->y()+20); //+20 is arbitrary, location_text must be below deti and comment
addDynamicTextItem(location_text); addDynamicTextItem(location_text);
} }

View File

@@ -75,6 +75,7 @@ void ElementTextItemGroup::addToGroup(QGraphicsItem *item)
connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment); connect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment); connect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
connect(deti, &DynamicElementTextItem::plainTextChanged, 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::textFromChanged, this, &ElementTextItemGroup::updateXref);
connect(deti, &DynamicElementTextItem::infoNameChanged, 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::infoNameChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment); disconnect(deti, &DynamicElementTextItem::compositeTextChanged, this, &ElementTextItemGroup::updateAlignment);
disconnect(deti, &DynamicElementTextItem::plainTextChanged, 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::textFromChanged, this, &ElementTextItemGroup::updateXref);
disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref); disconnect(deti, &DynamicElementTextItem::infoNameChanged, this, &ElementTextItemGroup::updateXref);

View File

@@ -41,9 +41,10 @@ int compo_txt_row = 3;
int size_txt_row = 4; int size_txt_row = 4;
int color_txt_row = 5; int color_txt_row = 5;
int frame_txt_row = 6; int frame_txt_row = 6;
int x_txt_row = 7; int width_txt_row = 7;
int y_txt_row = 8; int x_txt_row = 8;
int rot_txt_row = 9; int y_txt_row = 9;
int rot_txt_row = 10;
int align_grp_row = 0; int align_grp_row = 0;
int rot_grp_row = 1; int rot_grp_row = 1;
@@ -220,6 +221,19 @@ QList<QStandardItem *> DynamicElementTextModel::itemsForText(DynamicElementTextI
qsi_list.clear(); qsi_list.clear();
qsi_list << frame << frame_a; qsi_list << frame << frame_a;
qsi->appendRow(qsi_list);
//Width
QStandardItem *width = new QStandardItem(tr("Largeur"));
width->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
QStandardItem *width_a = new QStandardItem;
width_a->setData(deti->textWidth(), Qt::EditRole);
width_a->setData(DynamicElementTextModel::textWidth, Qt::UserRole+1);
width_a->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
qsi_list.clear();
qsi_list << width << width_a;
qsi->appendRow(qsi_list); qsi->appendRow(qsi_list);
if(deti->parentGroup() == nullptr) if(deti->parentGroup() == nullptr)
@@ -457,6 +471,13 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem
quc->setText(tr("Modifier le cadre d'un texte d'élément")); quc->setText(tr("Modifier le cadre d'un texte d'élément"));
} }
qreal text_width = text_qsi->child(width_txt_row, 1)->data(Qt::EditRole).toDouble();
if(text_width != deti->textWidth())
{
QUndoCommand *quc = new QPropertyUndoCommand(deti, "textWidth", QVariant(deti->textWidth()), QVariant(text_width), undo);
quc->setText(tr("Modifier la largeur d'un texte d'élément"));
}
//When text is in a group, they're isn't item for position of the text //When text is in a group, they're isn't item for position of the text
if(text_qsi->child(x_txt_row,1) && text_qsi->child(y_txt_row,1)) if(text_qsi->child(x_txt_row,1) && text_qsi->child(y_txt_row,1))
{ {
@@ -1054,6 +1075,7 @@ void DynamicElementTextModel::setConnection(DynamicElementTextItem *deti, bool s
connection_list << connect(deti, &DynamicElementTextItem::yChanged, [deti,this](){this->updateDataFromText(deti, pos);}); connection_list << connect(deti, &DynamicElementTextItem::yChanged, [deti,this](){this->updateDataFromText(deti, pos);});
connection_list << connect(deti, &DynamicElementTextItem::frameChanged, [deti,this](){this->updateDataFromText(deti, frame);}); connection_list << connect(deti, &DynamicElementTextItem::frameChanged, [deti,this](){this->updateDataFromText(deti, frame);});
connection_list << connect(deti, &DynamicElementTextItem::rotationChanged, [deti,this](){this->updateDataFromText(deti, rotation);}); connection_list << connect(deti, &DynamicElementTextItem::rotationChanged, [deti,this](){this->updateDataFromText(deti, rotation);});
connection_list << connect(deti, &DynamicElementTextItem::textWidthChanged,[deti,this](){this->updateDataFromText(deti, textWidth);});
connection_list << connect(deti, &DynamicElementTextItem::compositeTextChanged, [deti, this]() {this->updateDataFromText(deti, compositeText);}); connection_list << connect(deti, &DynamicElementTextItem::compositeTextChanged, [deti, this]() {this->updateDataFromText(deti, compositeText);});
m_hash_text_connect.insert(deti, connection_list); m_hash_text_connect.insert(deti, connection_list);
@@ -1177,6 +1199,11 @@ void DynamicElementTextModel::updateDataFromText(DynamicElementTextItem *deti, V
qsi->child(rot_txt_row,1)->setData(deti->rotation(), Qt::EditRole); qsi->child(rot_txt_row,1)->setData(deti->rotation(), Qt::EditRole);
break; break;
} }
case textWidth:
{
qsi->child(width_txt_row,1)->setData(deti->textWidth(), Qt::EditRole);
break;
}
} }
m_block_dataChanged = false; m_block_dataChanged = false;
@@ -1314,6 +1341,15 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti
sb->setSuffix(" °"); sb->setSuffix(" °");
return sb; return sb;
} }
case DynamicElementTextModel::textWidth:
{
QSpinBox *sb = new QSpinBox(parent);
sb->setObjectName("width_spinbox");
sb->setRange(-1, 500);
sb->setFrame(false);
sb->setSuffix(" px");
return sb;
}
case DynamicElementTextModel::grp_alignment: case DynamicElementTextModel::grp_alignment:
{ {
QComboBox *qcb = new QComboBox(parent); QComboBox *qcb = new QComboBox(parent);
@@ -1431,7 +1467,7 @@ bool DynamicTextItemDelegate::eventFilter(QObject *object, QEvent *event)
//With this hack the value is commited each time the value change without the need to validate. //With this hack the value is commited each time the value change without the need to validate.
//then the change is apply in live //then the change is apply in live
if(object->objectName() == "pos_dialog" || object->objectName() == "font_size" || object->objectName() == "rot_spinbox" || \ if(object->objectName() == "pos_dialog" || object->objectName() == "font_size" || object->objectName() == "rot_spinbox" || \
object->objectName() == "group_rotation" || object->objectName() == "group_v_adjustment") object->objectName() == "group_rotation" || object->objectName() == "group_v_adjustment" || object->objectName() == "width_spinbox")
{ {
object->event(event); object->event(event);

View File

@@ -47,6 +47,7 @@ class DynamicElementTextModel : public QStandardItemModel
pos, pos,
frame, frame,
rotation, rotation,
textWidth,
grp_alignment, grp_alignment,
grp_rotation, grp_rotation,
grp_v_adjust, grp_v_adjust,