diff --git a/sources/qetgraphicsitem/diagramtextitem.cpp b/sources/qetgraphicsitem/diagramtextitem.cpp index 584b0f205..2deb0888c 100644 --- a/sources/qetgraphicsitem/diagramtextitem.cpp +++ b/sources/qetgraphicsitem/diagramtextitem.cpp @@ -184,6 +184,30 @@ Qt::Alignment DiagramTextItem::alignment() const return m_alignment; } +/** + * @brief DiagramTextItem::frameRect + * @return the rect used to draw a frame around this text + */ +QRectF DiagramTextItem::frameRect() const +{ + //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; + + return QRectF(pos, size); +} + /** * @brief DiagramTextItem::paint * Draw this text field. This method draw the text by calling QGraphicsTextItem::paint. diff --git a/sources/qetgraphicsitem/diagramtextitem.h b/sources/qetgraphicsitem/diagramtextitem.h index 53c471b8c..e405095e1 100644 --- a/sources/qetgraphicsitem/diagramtextitem.h +++ b/sources/qetgraphicsitem/diagramtextitem.h @@ -76,6 +76,8 @@ class DiagramTextItem : public QGraphicsTextItem void setAlignment(const Qt::Alignment &alignment); Qt::Alignment alignment() const; bool m_block_alignment = false; + + QRectF frameRect() const; protected: void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override; diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index c2f287055..68268a1e5 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -635,7 +635,6 @@ 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, @@ -653,24 +652,9 @@ void DynamicElementTextItem::paint(QPainter *painter, const QStyleOptionGraphics painter->setPen(pen); painter->setRenderHint(QPainter::Antialiasing); - //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(QRectF(pos, size), ro, ro); + painter->drawRoundedRect(frameRect(), ro, ro); painter->restore(); } diff --git a/sources/qetgraphicsitem/elementtextitemgroup.cpp b/sources/qetgraphicsitem/elementtextitemgroup.cpp index 43519d6c5..0a07cc700 100644 --- a/sources/qetgraphicsitem/elementtextitemgroup.cpp +++ b/sources/qetgraphicsitem/elementtextitemgroup.cpp @@ -298,6 +298,17 @@ void ElementTextItemGroup::setHoldToBottomPage(bool hold) emit holdToBottomPageChanged(hold); } +void ElementTextItemGroup::setFrame(const bool frame) +{ + m_frame = frame; + update(); + emit frameChanged(m_frame); +} + +bool ElementTextItemGroup::frame() const { + return m_frame; +} + /** * @brief ElementTextItemGroup::texts * @return Every texts in this group @@ -356,6 +367,7 @@ QDomElement ElementTextItemGroup::toXml(QDomDocument &dom_document) const dom_element.setAttribute("rotation", this->rotation()); dom_element.setAttribute("vertical_adjustment", m_vertical_adjustment); + dom_element.setAttribute("frame", m_frame? "true" : "false"); dom_element.setAttribute("hold_to_bottom_page", m_hold_to_bottom_of_page == true ? "true" : "false"); @@ -392,6 +404,7 @@ void ElementTextItemGroup::fromXml(QDomElement &dom_element) setRotation(dom_element.attribute("rotation", QString::number(0)).toDouble()); setVerticalAdjustment(dom_element.attribute("vertical_adjustment").toInt()); + setFrame(dom_element.attribute("frame", "false") == "true"? true : false); QString hold = dom_element.attribute("hold_to_bottom_page", "false"); setHoldToBottomPage(hold == "true" ? true : false); @@ -437,6 +450,36 @@ void ElementTextItemGroup::paint(QPainter *painter, const QStyleOptionGraphicsIt painter->restore(); } + if(m_frame) + { + int font_size = 1; + QRectF rect; + for(DynamicElementTextItem *deti : this->texts()) + { + font_size = std::max(font_size, deti->fontSize()); + rect = rect.united(mapFromItem(deti, deti->frameRect()).boundingRect()); + } + + //Adjust the thickness according to the font size + qreal w=0.3; + if (font_size >= 5) + { + w = (qreal)font_size*0.1; + if(w > 2.5) + w = 2.5; + } + + painter->save(); + QPen pen; + pen.setWidthF(w); + painter->setPen(pen); + painter->setRenderHint(QPainter::Antialiasing); + + //Adjust the rounding of the rectangle according to the size of the font + qreal ro = (qreal)font_size/3; + painter->drawRoundedRect(rect, ro, ro); + painter->restore(); + } } /** diff --git a/sources/qetgraphicsitem/elementtextitemgroup.h b/sources/qetgraphicsitem/elementtextitemgroup.h index 17e3f728d..46698d11c 100644 --- a/sources/qetgraphicsitem/elementtextitemgroup.h +++ b/sources/qetgraphicsitem/elementtextitemgroup.h @@ -42,6 +42,7 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(bool holdToBottomPage READ holdToBottomPage WRITE setHoldToBottomPage NOTIFY holdToBottomPageChanged) + Q_PROPERTY(bool frame READ frame WRITE setFrame NOTIFY frameChanged) public: signals: @@ -52,6 +53,7 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup void holdToBottomPageChanged(bool); void xChanged(); void yChanged(); + void frameChanged(bool frame); public: ElementTextItemGroup(const QString &name, Element *parent); @@ -69,6 +71,8 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup QString name() const {return m_name;} void setHoldToBottomPage(bool hold); bool holdToBottomPage() const {return m_hold_to_bottom_of_page;} + void setFrame(const bool frame); + bool frame() const; QList texts() const; Diagram *diagram() const; Element *parentElement() const; @@ -102,7 +106,8 @@ class ElementTextItemGroup : public QObject, public QGraphicsItemGroup QString m_name; bool m_first_move = true, m_hold_to_bottom_of_page = false, - m_block_alignment_update = false; + m_block_alignment_update = false, + m_frame = false; QPointF m_initial_position; int m_vertical_adjustment = 0; CrossRefItem *m_Xref_item = nullptr; diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index 0d253fbc3..279b23a44 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -53,7 +53,8 @@ int x_grp_row = 1; int y_grp_row = 2; int rot_grp_row = 3; int adjust_grp_row = 4; -int hold_to_bottom_grp_row = 5; +int frame_grp_row = 5; +int hold_to_bottom_grp_row = 6; DynamicElementTextModel::DynamicElementTextModel(Element *element, QObject *parent) : QStandardItemModel(parent), @@ -566,19 +567,23 @@ QUndoCommand *DynamicElementTextModel::undoForEditedGroup(ElementTextItemGroup * else if((alignment == tr("Centre")) && (group->alignment() != Qt::AlignVCenter)) new QPropertyUndoCommand(group, "alignment", QVariant(group->alignment()), QVariant(Qt::AlignVCenter), undo); - QPointF pos(group_qsi->child(x_grp_row,1)->data(Qt::EditRole).toDouble(), - group_qsi->child(y_grp_row,1)->data(Qt::EditRole).toDouble()); - if(group->pos() != pos) - { - QPropertyUndoCommand *qpuc = new QPropertyUndoCommand(group, "pos", QVariant(group->pos()), QVariant(pos), undo); - qpuc->setAnimated(true, false); - } - qreal rotation = group_qsi->child(rot_grp_row,1)->data(Qt::EditRole).toDouble(); - if(group->rotation() != rotation) + if (group_qsi->child(hold_to_bottom_grp_row, 1)->checkState() == Qt::Unchecked) { - QPropertyUndoCommand *qpuc = new QPropertyUndoCommand(group, "rotation", QVariant(group->rotation()), QVariant(rotation), undo); - qpuc->setAnimated(true, false); + QPointF pos(group_qsi->child(x_grp_row,1)->data(Qt::EditRole).toDouble(), + group_qsi->child(y_grp_row,1)->data(Qt::EditRole).toDouble()); + if(group->pos() != pos) + { + QPropertyUndoCommand *qpuc = new QPropertyUndoCommand(group, "pos", QVariant(group->pos()), QVariant(pos), undo); + qpuc->setAnimated(true, false); + } + + qreal rotation = group_qsi->child(rot_grp_row,1)->data(Qt::EditRole).toDouble(); + if(group->rotation() != rotation) + { + QPropertyUndoCommand *qpuc = new QPropertyUndoCommand(group, "rotation", QVariant(group->rotation()), QVariant(rotation), undo); + qpuc->setAnimated(true, false); + } } int v_adjustment = group_qsi->child(adjust_grp_row,1)->data(Qt::EditRole).toInt(); @@ -592,8 +597,11 @@ QUndoCommand *DynamicElementTextModel::undoForEditedGroup(ElementTextItemGroup * bool hold_to_bottom = group_qsi->child(hold_to_bottom_grp_row, 1)->checkState() == Qt::Checked? true : false; if(group->holdToBottomPage() != hold_to_bottom) new QPropertyUndoCommand(group, "holdToBottomPage", QVariant(group->holdToBottomPage()), QVariant(hold_to_bottom), undo); - + bool frame_ = group_qsi->child(frame_grp_row, 1)->checkState() == Qt::Checked? true : false; + if(group->frame() != frame_) + new QPropertyUndoCommand(group, "frame", QVariant(group->frame()), QVariant(frame_), undo); + return undo; } @@ -689,6 +697,20 @@ void DynamicElementTextModel::addGroup(ElementTextItemGroup *group) qsi_list << v_adj << v_adj_a; grp->appendRow(qsi_list); + //Frame + QStandardItem *frame_ = new QStandardItem(tr("Cadre")); + frame_->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); + + QStandardItem *frame_a = new QStandardItem; + frame_a->setCheckable(true); + frame_a->setCheckState(group->frame()? Qt::Checked : Qt::Unchecked); + frame_a->setData(DynamicElementTextModel::grpFrame, Qt::UserRole+1); + frame_a->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable); + qsi_list.clear(); + qsi_list << frame_ << frame_a; + grp->appendRow(qsi_list); + + //Hold to the bottom of the page QStandardItem *hold_bottom = new QStandardItem(tr("Maintenir en bas de page")); hold_bottom->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable); @@ -1233,6 +1255,7 @@ void DynamicElementTextModel::setConnection(ElementTextItemGroup *group, bool se connection_list << connect(group, &ElementTextItemGroup::holdToBottomPageChanged, [group, this]() {this->updateDataFromGroup(group, grpHoldBottom);}); connection_list << connect(group, &ElementTextItemGroup::xChanged, [group, this]() {this->updateDataFromGroup(group, grpPos);}); connection_list << connect(group, &ElementTextItemGroup::yChanged, [group, this]() {this->updateDataFromGroup(group, grpPos);}); + connection_list << connect(group, &ElementTextItemGroup::frameChanged, [group, this]() {this->updateDataFromGroup(group, grpFrame);}); m_hash_group_connect.insert(group, connection_list); } @@ -1373,6 +1396,9 @@ void DynamicElementTextModel::updateDataFromGroup(ElementTextItemGroup *group, D enableGroupRotationAndPos(group); break; } + case grpFrame: + qsi->child(frame_grp_row, 1)->setCheckState(group->frame()? Qt::Checked : Qt::Unchecked); + break; default:break; } diff --git a/sources/ui/dynamicelementtextmodel.h b/sources/ui/dynamicelementtextmodel.h index 379834953..87a6e1503 100644 --- a/sources/ui/dynamicelementtextmodel.h +++ b/sources/ui/dynamicelementtextmodel.h @@ -54,7 +54,8 @@ class DynamicElementTextModel : public QStandardItemModel grpRotation, grpVAdjust, grpName, - grpHoldBottom + grpHoldBottom, + grpFrame }; DynamicElementTextModel(Element *element, QObject *parent = nullptr);