diff --git a/sources/diagramcontext.cpp b/sources/diagramcontext.cpp index e06eb2b38..a6f1788ba 100644 --- a/sources/diagramcontext.cpp +++ b/sources/diagramcontext.cpp @@ -74,6 +74,10 @@ bool DiagramContext::addValue(const QString &key, const QVariant &value, bool sh return(false); } +QVariant DiagramContext::value(const QString &key) const { + return m_content.value(key); +} + /** Clear the content of this diagram context. */ diff --git a/sources/diagramcontext.h b/sources/diagramcontext.h index 838fa8c79..5af5fbeaf 100644 --- a/sources/diagramcontext.h +++ b/sources/diagramcontext.h @@ -45,37 +45,39 @@ * frozenLabel -> label locked at a given time * */ -class DiagramContext { +class DiagramContext +{ public: - enum KeyOrder { - None, - Alphabetical, - DecreasingLength - }; - - QList keys(KeyOrder = None) const; - bool contains(const QString &) const; - const QVariant operator[](const QString &) const; - bool addValue(const QString &, const QVariant &, bool show = true); - void clear(); - int count(); - bool keyMustShow (const QString &) const; + enum KeyOrder { + None, + Alphabetical, + DecreasingLength + }; - bool operator==(const DiagramContext &) const; - bool operator!=(const DiagramContext &) const; - - void toXml(QDomElement &, const QString & = "property") const; - void fromXml(const QDomElement &, const QString & = "property"); - void toSettings(QSettings &, const QString &) const; - void fromSettings(QSettings &, const QString &); - - static QString validKeyRegExp(); + QList keys(KeyOrder = None) const; + bool contains(const QString &) const; + const QVariant operator[](const QString &) const; + bool addValue(const QString &, const QVariant &, bool show = true); + QVariant value(const QString &key) const; + void clear(); + int count(); + bool keyMustShow (const QString &) const; + + bool operator==(const DiagramContext &) const; + bool operator!=(const DiagramContext &) const; + + void toXml(QDomElement &, const QString & = "property") const; + void fromXml(const QDomElement &, const QString & = "property"); + void toSettings(QSettings &, const QString &) const; + void fromSettings(QSettings &, const QString &); + + static QString validKeyRegExp(); private: - static bool stringLongerThan(const QString &, const QString &); - bool keyIsAcceptable(const QString &) const; - /// Diagram context data (key/value pairs) - QHash m_content; - QHash m_content_show; + static bool stringLongerThan(const QString &, const QString &); + bool keyIsAcceptable(const QString &) const; + /// Diagram context data (key/value pairs) + QHash m_content; + QHash m_content_show; }; #endif diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index 5cec76f78..953568bfe 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -302,7 +302,7 @@ QStringList QETApp::elementInfoKeys() * @param info the key to be translated * @return */ -QString QETApp::elementTranslatedInfoKey(QString &info) +QString QETApp::elementTranslatedInfoKey(const QString &info) { if (info == "formula") return tr("formule du label"); else if (info == "label") return tr("Label"); diff --git a/sources/qetapp.h b/sources/qetapp.h index 46593c293..b59fbda40 100644 --- a/sources/qetapp.h +++ b/sources/qetapp.h @@ -72,7 +72,7 @@ class QETApp : public QETSingleApplication { static ElementsCollectionCache *collectionCache(); static QStringList elementInfoKeys(); - static QString elementTranslatedInfoKey(QString &); + static QString elementTranslatedInfoKey(const QString &); static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection(); static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection(); diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.cpp b/sources/qetgraphicsitem/dynamicelementtextitem.cpp index 84b2a9169..b66ce0ff0 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.cpp +++ b/sources/qetgraphicsitem/dynamicelementtextitem.cpp @@ -76,6 +76,14 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const QDomElement dom_text = dom_doc.createElement("text"); dom_text.appendChild(dom_doc.createTextNode(toPlainText())); root_element.appendChild(dom_text); + + //Info name + if(!m_info_name.isEmpty()) + { + QDomElement dom_info_name = dom_doc.createElement("info_name"); + dom_info_name.appendChild(dom_doc.createTextNode(m_info_name)); + root_element.appendChild(dom_info_name); + } //tagg if (!m_tagg.isEmpty()) @@ -116,12 +124,24 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt) QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("TextFrom")); m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data())); - setNoEditable(m_text_from == ElementInfo? true : false); + if(m_text_from == ElementInfo) + { + setNoEditable(true); + connect(m_parent_element, &Element::elementInfoChange, this, &DynamicElementTextItem::elementInfoChanged); + } + else { + setNoEditable(false); + } //Text QDomElement dom_text = dom_elmt.firstChildElement("text"); if (!dom_text.isNull()) setPlainText(dom_text.text()); + + //Info name + QDomElement dom_info_name = dom_elmt.firstChildElement("info_name"); + if(!dom_info_name.isNull()) + m_info_name = dom_info_name.text(); //tagg QDomElement dom_tagg = dom_elmt.firstChildElement("tagg"); @@ -159,6 +179,18 @@ void DynamicElementTextItem::setTextFrom(DynamicElementTextItem::TextFrom text_f { m_text_from = text_from; setNoEditable(m_text_from == ElementInfo? true : false); + + if(m_text_from == UserText) + { + setPlainText(m_text); + disconnect(m_parent_element, &Element::elementInfoChange, this, &DynamicElementTextItem::elementInfoChanged); + } + else if (m_text_from == ElementInfo && m_parent_element) + { + setPlainText(m_parent_element->elementInformations().value(m_info_name).toString()); + connect(m_parent_element, &Element::elementInfoChange, this, &DynamicElementTextItem::elementInfoChanged); + } + emit TextFromChanged(m_text_from); } @@ -201,6 +233,26 @@ void DynamicElementTextItem::setText(const QString &text) emit textChanged(m_text); } +/** + * @brief DynamicElementTextItem::setInfoName + * Set the information name of the parent element. + * @param info_name + */ +void DynamicElementTextItem::setInfoName(const QString &info_name) +{ + m_info_name = info_name; + + if(m_parent_element) { + setPlainText(m_parent_element->elementInformations().value(info_name).toString()); + } + + emit InfoNameChanged(info_name); +} + +QString DynamicElementTextItem::infoName() const { + return m_info_name; +} + /** * @brief DynamicElementTextItem::mouseMoveEvent * @param event @@ -253,3 +305,8 @@ void DynamicElementTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) QGraphicsTextItem::mouseReleaseEvent(event); } +void DynamicElementTextItem::elementInfoChanged() +{ + setPlainText(m_parent_element->elementInformations().value(m_info_name).toString()); +} + diff --git a/sources/qetgraphicsitem/dynamicelementtextitem.h b/sources/qetgraphicsitem/dynamicelementtextitem.h index ebfa4ce70..00ca69066 100644 --- a/sources/qetgraphicsitem/dynamicelementtextitem.h +++ b/sources/qetgraphicsitem/dynamicelementtextitem.h @@ -36,6 +36,7 @@ class DynamicElementTextItem : public DiagramTextItem Q_PROPERTY(QString tagg READ tagg WRITE setTagg NOTIFY taggChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(TextFrom textFrom READ textFrom WRITE setTextFrom NOTIFY TextFromChanged) + Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY InfoNameChanged) public: Q_ENUMS(TextFrom) @@ -50,6 +51,7 @@ class DynamicElementTextItem : public DiagramTextItem void taggChanged(QString tagg); void textChanged(QString text); void TextFromChanged(DynamicElementTextItem::TextFrom text_from); + void InfoNameChanged(QString info); public: DynamicElementTextItem(Element *parent_element); @@ -70,16 +72,21 @@ class DynamicElementTextItem : public DiagramTextItem QString text() const; void setText(const QString &text); static QString xmlTaggName() {return QString("dynamic_elmt_text");} + void setInfoName(const QString &info_name); + QString infoName() const; protected: void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; + private: + void elementInfoChanged(); + private: Element *m_parent_element = nullptr; QString m_tagg, m_text, - m_elmt_info_name; + m_info_name; DynamicElementTextItem::TextFrom m_text_from = UserText; QUuid m_uuid; }; diff --git a/sources/ui/dynamicelementtextitemeditor.cpp b/sources/ui/dynamicelementtextitemeditor.cpp index 440640ae6..429081e99 100644 --- a/sources/ui/dynamicelementtextitemeditor.cpp +++ b/sources/ui/dynamicelementtextitemeditor.cpp @@ -36,6 +36,7 @@ DynamicElementTextItemEditor::DynamicElementTextItemEditor(Element *element, QWi m_tree_view->header()->setDefaultSectionSize(150); m_tree_view->setItemDelegate(new DynamicTextItemDelegate(m_tree_view)); m_tree_view->setAlternatingRowColors(true); + m_tree_view->setEditTriggers(QAbstractItemView::CurrentChanged); ui->verticalLayout->addWidget(m_tree_view); setElement(element); } diff --git a/sources/ui/dynamicelementtextmodel.cpp b/sources/ui/dynamicelementtextmodel.cpp index b26f27698..b5527dabb 100644 --- a/sources/ui/dynamicelementtextmodel.cpp +++ b/sources/ui/dynamicelementtextmodel.cpp @@ -24,6 +24,8 @@ #include #include #include "QPropertyUndoCommand/qpropertyundocommand.h" +#include "qetapp.h" +#include "element.h" DynamicElementTextModel::DynamicElementTextModel(QObject *parent) : QStandardItemModel(parent) @@ -85,9 +87,10 @@ void DynamicElementTextModel::addText(DynamicElementTextItem *deti) QStandardItem *info = new QStandardItem(tr("Information")); info->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); - QStandardItem *infoa = new QStandardItem(deti->toPlainText()); + QStandardItem *infoa = new QStandardItem(QETApp::elementTranslatedInfoKey(deti->infoName())); infoa->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable); - infoa->setData(DynamicElementTextModel::infoText, Qt::UserRole+1); + infoa->setData(DynamicElementTextModel::infoText, Qt::UserRole+1); //Use to know the edited thing + infoa->setData(deti->infoName(), Qt::UserRole+2); //Use to know to element info name qsi_list.clear(); qsi_list << info << infoa; @@ -218,9 +221,18 @@ QUndoCommand *DynamicElementTextModel::undoForEditedText(DynamicElementTextItem else if ((from == tr("Information de l'élément")) && (deti->textFrom() != DynamicElementTextItem::ElementInfo)) new QPropertyUndoCommand(deti, "textFrom", QVariant(deti->textFrom()), QVariant(DynamicElementTextItem::ElementInfo), undo); - QString text = text_qsi->child(0,0)->child(0,1)->data(Qt::DisplayRole).toString(); - if (text != deti->text()) - new QPropertyUndoCommand(deti, "text", QVariant(deti->text()), QVariant(text), undo); + if(from == tr("Texte utilisateur")) + { + QString text = text_qsi->child(0,0)->child(0,1)->data(Qt::DisplayRole).toString(); + if (text != deti->text()) + new QPropertyUndoCommand(deti, "text", QVariant(deti->text()), QVariant(text), undo); + } + else if (from == tr("Information de l'élément")) + { + QString info_name = text_qsi->child(0,0)->child(1,1)->data(Qt::UserRole+2).toString(); + if(info_name != deti->infoName()) + new QPropertyUndoCommand(deti, "infoName", QVariant(deti->infoName()), QVariant(info_name), undo); + } int fs = text_qsi->child(1,1)->data(Qt::EditRole).toInt(); if (fs != deti->fontSize()) @@ -281,6 +293,11 @@ void DynamicElementTextModel::dataEdited(QStandardItem *qsi) QString text = qsi->data(Qt::DisplayRole).toString(); m_texts_list.value(deti)->setData(text, Qt::DisplayRole); } + else if (qsi->data().toInt() == infoText && deti->ParentElement()) + { + QString info = qsi->data(Qt::UserRole+2).toString(); + m_texts_list.value(deti)->setData(deti->ParentElement()->elementInformations().value(info), Qt::DisplayRole); + } } /** @@ -374,9 +391,36 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti qcb->addItem(tr("Information de l'élément")); return qcb; } + case DynamicElementTextModel::infoText: + { + const DynamicElementTextModel *detm = static_cast(index.model()); + QStandardItem *qsi = detm->itemFromIndex(index); + + if(!qsi) + break; + + DynamicElementTextItem *deti = detm->textFromIndex(index); + if(!deti) + break; + + //We use a QMap because the keys of the map are sorted, then no matter the curent local, + //the value of the combo box are always alphabetically sorted + QMap info_map; + for(QString str : availableInfo(deti)) { + info_map.insert(QETApp::elementTranslatedInfoKey(str), str); + } + + QComboBox *qcb = new QComboBox(parent); + qcb->setObjectName("info_text"); + for (QString key : info_map.keys()) { + qcb->addItem(key, info_map.value(key)); + } + return qcb; + } case DynamicElementTextModel::color: { QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value()); + cd->setObjectName("color_dialog"); return cd; } } @@ -386,22 +430,61 @@ QWidget *DynamicTextItemDelegate::createEditor(QWidget *parent, const QStyleOpti void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { if (index.isValid()) - { - if (QStandardItemModel *qsim = dynamic_cast(model)) + { + if(editor->objectName() == "color_dialog") { - QStandardItem *qsi = qsim->itemFromIndex(index); - if(qsi) + if (QStandardItemModel *qsim = dynamic_cast(model)) { - if(QColorDialog *cd = dynamic_cast (editor)) + if(QStandardItem *qsi = qsim->itemFromIndex(index)) { + QColorDialog *cd = static_cast (editor); qsi->setData(cd->selectedColor(), Qt::EditRole); qsi->setData(cd->selectedColor(), Qt::ForegroundRole); return; } + + } + } + else if (editor->objectName() == "info_text") + { + if (QStandardItemModel *qsim = dynamic_cast(model)) + { + if(QStandardItem *qsi = qsim->itemFromIndex(index)) + { + QComboBox *cb = static_cast(editor); + qsi->setData(cb->currentText(), Qt::DisplayRole); + qsi->setData(cb->currentData(), Qt::UserRole+2); + return; + } + } - } } QStyledItemDelegate::setModelData(editor, model, index); } + +/** + * @brief DynamicTextItemDelegate::availableInfo + * @param deti + * @return A list of available info of element + */ +QStringList DynamicTextItemDelegate::availableInfo(DynamicElementTextItem *deti) const +{ + QStringList qstrl; + Element *elmt = deti->ParentElement(); + if(!elmt) + return qstrl; + + QStringList info_list = QETApp::elementInfoKeys(); + info_list.removeAll("formula"); //No need to have formula + DiagramContext dc = elmt->elementInformations(); + + for(QString info : info_list) + { + if(dc.contains(info)) + qstrl << info; + } + + return qstrl; +} diff --git a/sources/ui/dynamicelementtextmodel.h b/sources/ui/dynamicelementtextmodel.h index 27a088ef8..c9fe5d783 100644 --- a/sources/ui/dynamicelementtextmodel.h +++ b/sources/ui/dynamicelementtextmodel.h @@ -71,6 +71,9 @@ class DynamicTextItemDelegate : public QStyledItemDelegate QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + + private: + QStringList availableInfo(DynamicElementTextItem *deti) const; }; #endif // DYNAMICELEMENTTEXTMODEL_H