[qet] qet/qet: [5010] Dynamic element text item can display information of parent element |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/qet Archives
]
Revision: 5010
Author: blacksun
Date: 2017-08-06 12:18:33 +0200 (Sun, 06 Aug 2017)
Log Message:
-----------
Dynamic element text item can display information of parent element
Modified Paths:
--------------
trunk/sources/diagramcontext.cpp
trunk/sources/diagramcontext.h
trunk/sources/qetapp.cpp
trunk/sources/qetapp.h
trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
trunk/sources/ui/dynamicelementtextitemeditor.cpp
trunk/sources/ui/dynamicelementtextmodel.cpp
trunk/sources/ui/dynamicelementtextmodel.h
Modified: trunk/sources/diagramcontext.cpp
===================================================================
--- trunk/sources/diagramcontext.cpp 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/diagramcontext.cpp 2017-08-06 10:18:33 UTC (rev 5010)
@@ -74,6 +74,10 @@
return(false);
}
+QVariant DiagramContext::value(const QString &key) const {
+ return m_content.value(key);
+}
+
/**
Clear the content of this diagram context.
*/
Modified: trunk/sources/diagramcontext.h
===================================================================
--- trunk/sources/diagramcontext.h 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/diagramcontext.h 2017-08-06 10:18:33 UTC (rev 5010)
@@ -45,37 +45,39 @@
* frozenLabel -> label locked at a given time
*
*/
-class DiagramContext {
+class DiagramContext
+{
public:
- enum KeyOrder {
- None,
- Alphabetical,
- DecreasingLength
- };
-
- QList<QString> 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;
+ QList<QString> 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();
- 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<QString, QVariant> m_content;
- QHash<QString, bool> m_content_show;
+ static bool stringLongerThan(const QString &, const QString &);
+ bool keyIsAcceptable(const QString &) const;
+ /// Diagram context data (key/value pairs)
+ QHash<QString, QVariant> m_content;
+ QHash<QString, bool> m_content_show;
};
#endif
Modified: trunk/sources/qetapp.cpp
===================================================================
--- trunk/sources/qetapp.cpp 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/qetapp.cpp 2017-08-06 10:18:33 UTC (rev 5010)
@@ -302,7 +302,7 @@
* @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");
Modified: trunk/sources/qetapp.h
===================================================================
--- trunk/sources/qetapp.h 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/qetapp.h 2017-08-06 10:18:33 UTC (rev 5010)
@@ -72,7 +72,7 @@
static ElementsCollectionCache *collectionCache();
static QStringList elementInfoKeys();
- static QString elementTranslatedInfoKey(QString &);
+ static QString elementTranslatedInfoKey(const QString &);
static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection();
static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection();
Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.cpp 2017-08-06 10:18:33 UTC (rev 5010)
@@ -76,6 +76,14 @@
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 @@
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 @@
{
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);
}
@@ -202,6 +234,26 @@
}
/**
+ * @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 @@
QGraphicsTextItem::mouseReleaseEvent(event);
}
+void DynamicElementTextItem::elementInfoChanged()
+{
+ setPlainText(m_parent_element->elementInformations().value(m_info_name).toString());
+}
+
Modified: trunk/sources/qetgraphicsitem/dynamicelementtextitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/dynamicelementtextitem.h 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/qetgraphicsitem/dynamicelementtextitem.h 2017-08-06 10:18:33 UTC (rev 5010)
@@ -36,6 +36,7 @@
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 @@
void taggChanged(QString tagg);
void textChanged(QString text);
void TextFromChanged(DynamicElementTextItem::TextFrom text_from);
+ void InfoNameChanged(QString info);
public:
DynamicElementTextItem(Element *parent_element);
@@ -70,6 +72,8 @@
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;
@@ -76,10 +80,13 @@
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;
};
Modified: trunk/sources/ui/dynamicelementtextitemeditor.cpp
===================================================================
--- trunk/sources/ui/dynamicelementtextitemeditor.cpp 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/ui/dynamicelementtextitemeditor.cpp 2017-08-06 10:18:33 UTC (rev 5010)
@@ -36,6 +36,7 @@
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);
}
Modified: trunk/sources/ui/dynamicelementtextmodel.cpp
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.cpp 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/ui/dynamicelementtextmodel.cpp 2017-08-06 10:18:33 UTC (rev 5010)
@@ -24,6 +24,8 @@
#include <QComboBox>
#include <QUndoCommand>
#include "QPropertyUndoCommand/qpropertyundocommand.h"
+#include "qetapp.h"
+#include "element.h"
DynamicElementTextModel::DynamicElementTextModel(QObject *parent) :
QStandardItemModel(parent)
@@ -85,9 +87,10 @@
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 @@
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 @@
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 @@
qcb->addItem(tr("Information de l'élément"));
return qcb;
}
+ case DynamicElementTextModel::infoText:
+ {
+ const DynamicElementTextModel *detm = static_cast<const DynamicElementTextModel *>(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 <QString, QString> 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<QColor>());
+ cd->setObjectName("color_dialog");
return cd;
}
}
@@ -386,22 +430,61 @@
void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.isValid())
- {
- if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
+ {
+ if(editor->objectName() == "color_dialog")
{
- QStandardItem *qsi = qsim->itemFromIndex(index);
- if(qsi)
+ if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
{
- if(QColorDialog *cd = dynamic_cast<QColorDialog *> (editor))
+ if(QStandardItem *qsi = qsim->itemFromIndex(index))
{
+ QColorDialog *cd = static_cast<QColorDialog *> (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<QStandardItemModel *>(model))
+ {
+ if(QStandardItem *qsi = qsim->itemFromIndex(index))
+ {
+ QComboBox *cb = static_cast<QComboBox *>(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;
+}
Modified: trunk/sources/ui/dynamicelementtextmodel.h
===================================================================
--- trunk/sources/ui/dynamicelementtextmodel.h 2017-08-05 02:10:01 UTC (rev 5009)
+++ trunk/sources/ui/dynamicelementtextmodel.h 2017-08-06 10:18:33 UTC (rev 5010)
@@ -71,6 +71,9 @@
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