diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 6aa607302..66de5e222 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -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); +} + diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.h b/sources/qetgraphicsitem/dynamicelementtextitem.h index f6b72238e..d35122a62 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.h +++ b/sources/qetgraphicsitem/dynamicelementtextitem.h @@ -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 diff --git a/sources/qetgraphicsitem/element.cpp b/sources/qetgraphicsitem/element.cpp index baae8d875..f1295617d 100644 --- a/sources/qetgraphicsitem/element.cpp +++ b/sources/qetgraphicsitem/element.cpp @@ -665,6 +665,7 @@ bool Element::fromXml(QDomElement &e, QHash &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 &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); } diff --git a/sources/qetgraphicsitem/elementtextitemgroup.cpp b/sources/qetgraphicsitem/elementtextitemgroup.cpp index aff771c6e..53b7cc876 100644 --- a/sources/qetgraphicsitem/elementtextitemgroup.cpp +++ b/sources/qetgraphicsitem/elementtextitemgroup.cpp @@ -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); diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index e41ce7b4f..a2f94c807 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -41,9 +41,10 @@ int compo_txt_row = 3; int size_txt_row = 4; int color_txt_row = 5; int frame_txt_row = 6; -int x_txt_row = 7; -int y_txt_row = 8; -int rot_txt_row = 9; +int width_txt_row = 7; +int x_txt_row = 8; +int y_txt_row = 9; +int rot_txt_row = 10; int align_grp_row = 0; int rot_grp_row = 1; @@ -220,6 +221,19 @@ QList DynamicElementTextModel::itemsForText(DynamicElementTextI qsi_list.clear(); 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); if(deti->parentGroup() == nullptr) @@ -457,6 +471,13 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem 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 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::frameChanged, [deti,this](){this->updateDataFromText(deti, frame);}); 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);}); 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); break; } + case textWidth: + { + qsi->child(width_txt_row,1)->setData(deti->textWidth(), Qt::EditRole); + break; + } } m_block_dataChanged = false; @@ -1314,6 +1341,15 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti sb->setSuffix(" °"); 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: { 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. //then the change is apply in live 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); diff --git a/sources/ui/dynamicelementtextmodel.h b/sources/ui/dynamicelementtextmodel.h index 0c64b8e56..e2805bfea 100644 --- a/sources/ui/dynamicelementtextmodel.h +++ b/sources/ui/dynamicelementtextmodel.h @@ -46,7 +46,8 @@ class DynamicElementTextModel : public QStandardItemModel color, pos, frame, - rotation, + rotation, + textWidth, grp_alignment, grp_rotation, grp_v_adjust,