Dynamic element text item can display information of parent element

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5010 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2017-08-06 10:18:33 +00:00
parent 31f36b8625
commit 6e2ed873bb
9 changed files with 200 additions and 43 deletions

View File

@@ -74,6 +74,10 @@ bool DiagramContext::addValue(const QString &key, const QVariant &value, bool sh
return(false); return(false);
} }
QVariant DiagramContext::value(const QString &key) const {
return m_content.value(key);
}
/** /**
Clear the content of this diagram context. Clear the content of this diagram context.
*/ */

View File

@@ -45,37 +45,39 @@
* frozenLabel -> label locked at a given time * frozenLabel -> label locked at a given time
* *
*/ */
class DiagramContext { class DiagramContext
{
public: public:
enum KeyOrder { enum KeyOrder {
None, None,
Alphabetical, Alphabetical,
DecreasingLength DecreasingLength
}; };
QList<QString> keys(KeyOrder = None) const; QList<QString> keys(KeyOrder = None) const;
bool contains(const QString &) const; bool contains(const QString &) const;
const QVariant operator[](const QString &) const; const QVariant operator[](const QString &) const;
bool addValue(const QString &, const QVariant &, bool show = true); bool addValue(const QString &, const QVariant &, bool show = true);
void clear(); QVariant value(const QString &key) const;
int count(); void clear();
bool keyMustShow (const QString &) const; int count();
bool keyMustShow (const QString &) const;
bool operator==(const DiagramContext &) const; bool operator==(const DiagramContext &) const;
bool operator!=(const DiagramContext &) const; bool operator!=(const DiagramContext &) const;
void toXml(QDomElement &, const QString & = "property") const; void toXml(QDomElement &, const QString & = "property") const;
void fromXml(const QDomElement &, const QString & = "property"); void fromXml(const QDomElement &, const QString & = "property");
void toSettings(QSettings &, const QString &) const; void toSettings(QSettings &, const QString &) const;
void fromSettings(QSettings &, const QString &); void fromSettings(QSettings &, const QString &);
static QString validKeyRegExp(); static QString validKeyRegExp();
private: private:
static bool stringLongerThan(const QString &, const QString &); static bool stringLongerThan(const QString &, const QString &);
bool keyIsAcceptable(const QString &) const; bool keyIsAcceptable(const QString &) const;
/// Diagram context data (key/value pairs) /// Diagram context data (key/value pairs)
QHash<QString, QVariant> m_content; QHash<QString, QVariant> m_content;
QHash<QString, bool> m_content_show; QHash<QString, bool> m_content_show;
}; };
#endif #endif

View File

@@ -302,7 +302,7 @@ QStringList QETApp::elementInfoKeys()
* @param info the key to be translated * @param info the key to be translated
* @return * @return
*/ */
QString QETApp::elementTranslatedInfoKey(QString &info) QString QETApp::elementTranslatedInfoKey(const QString &info)
{ {
if (info == "formula") return tr("formule du label"); if (info == "formula") return tr("formule du label");
else if (info == "label") return tr("Label"); else if (info == "label") return tr("Label");

View File

@@ -72,7 +72,7 @@ class QETApp : public QETSingleApplication {
static ElementsCollectionCache *collectionCache(); static ElementsCollectionCache *collectionCache();
static QStringList elementInfoKeys(); static QStringList elementInfoKeys();
static QString elementTranslatedInfoKey(QString &); static QString elementTranslatedInfoKey(const QString &);
static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection(); static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection();
static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection(); static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection();

View File

@@ -77,6 +77,14 @@ QDomElement DynamicElementTextItem::toXml(QDomDocument &dom_doc) const
dom_text.appendChild(dom_doc.createTextNode(toPlainText())); dom_text.appendChild(dom_doc.createTextNode(toPlainText()));
root_element.appendChild(dom_text); 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 //tagg
if (!m_tagg.isEmpty()) if (!m_tagg.isEmpty())
{ {
@@ -116,13 +124,25 @@ void DynamicElementTextItem::fromXml(const QDomElement &dom_elmt)
QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("TextFrom")); QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("TextFrom"));
m_text_from = DynamicElementTextItem::TextFrom(me.keyToValue(dom_elmt.attribute("text_from").toStdString().data())); 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 //Text
QDomElement dom_text = dom_elmt.firstChildElement("text"); QDomElement dom_text = dom_elmt.firstChildElement("text");
if (!dom_text.isNull()) if (!dom_text.isNull())
setPlainText(dom_text.text()); 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 //tagg
QDomElement dom_tagg = dom_elmt.firstChildElement("tagg"); QDomElement dom_tagg = dom_elmt.firstChildElement("tagg");
if (!dom_tagg.isNull()) if (!dom_tagg.isNull())
@@ -159,6 +179,18 @@ void DynamicElementTextItem::setTextFrom(DynamicElementTextItem::TextFrom text_f
{ {
m_text_from = text_from; m_text_from = text_from;
setNoEditable(m_text_from == ElementInfo? true : false); 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); emit TextFromChanged(m_text_from);
} }
@@ -201,6 +233,26 @@ void DynamicElementTextItem::setText(const QString &text)
emit textChanged(m_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 * @brief DynamicElementTextItem::mouseMoveEvent
* @param event * @param event
@@ -253,3 +305,8 @@ void DynamicElementTextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QGraphicsTextItem::mouseReleaseEvent(event); QGraphicsTextItem::mouseReleaseEvent(event);
} }
void DynamicElementTextItem::elementInfoChanged()
{
setPlainText(m_parent_element->elementInformations().value(m_info_name).toString());
}

View File

@@ -36,6 +36,7 @@ class DynamicElementTextItem : public DiagramTextItem
Q_PROPERTY(QString tagg READ tagg WRITE setTagg NOTIFY taggChanged) Q_PROPERTY(QString tagg READ tagg WRITE setTagg NOTIFY taggChanged)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(TextFrom textFrom READ textFrom WRITE setTextFrom NOTIFY TextFromChanged) Q_PROPERTY(TextFrom textFrom READ textFrom WRITE setTextFrom NOTIFY TextFromChanged)
Q_PROPERTY(QString infoName READ infoName WRITE setInfoName NOTIFY InfoNameChanged)
public: public:
Q_ENUMS(TextFrom) Q_ENUMS(TextFrom)
@@ -50,6 +51,7 @@ class DynamicElementTextItem : public DiagramTextItem
void taggChanged(QString tagg); void taggChanged(QString tagg);
void textChanged(QString text); void textChanged(QString text);
void TextFromChanged(DynamicElementTextItem::TextFrom text_from); void TextFromChanged(DynamicElementTextItem::TextFrom text_from);
void InfoNameChanged(QString info);
public: public:
DynamicElementTextItem(Element *parent_element); DynamicElementTextItem(Element *parent_element);
@@ -70,16 +72,21 @@ class DynamicElementTextItem : public DiagramTextItem
QString text() const; QString text() const;
void setText(const QString &text); void setText(const QString &text);
static QString xmlTaggName() {return QString("dynamic_elmt_text");} static QString xmlTaggName() {return QString("dynamic_elmt_text");}
void setInfoName(const QString &info_name);
QString infoName() const;
protected: protected:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
private:
void elementInfoChanged();
private: private:
Element *m_parent_element = nullptr; Element *m_parent_element = nullptr;
QString m_tagg, QString m_tagg,
m_text, m_text,
m_elmt_info_name; m_info_name;
DynamicElementTextItem::TextFrom m_text_from = UserText; DynamicElementTextItem::TextFrom m_text_from = UserText;
QUuid m_uuid; QUuid m_uuid;
}; };

View File

@@ -36,6 +36,7 @@ DynamicElementTextItemEditor::DynamicElementTextItemEditor(Element *element, QWi
m_tree_view->header()->setDefaultSectionSize(150); m_tree_view->header()->setDefaultSectionSize(150);
m_tree_view->setItemDelegate(new DynamicTextItemDelegate(m_tree_view)); m_tree_view->setItemDelegate(new DynamicTextItemDelegate(m_tree_view));
m_tree_view->setAlternatingRowColors(true); m_tree_view->setAlternatingRowColors(true);
m_tree_view->setEditTriggers(QAbstractItemView::CurrentChanged);
ui->verticalLayout->addWidget(m_tree_view); ui->verticalLayout->addWidget(m_tree_view);
setElement(element); setElement(element);
} }

View File

@@ -24,6 +24,8 @@
#include <QComboBox> #include <QComboBox>
#include <QUndoCommand> #include <QUndoCommand>
#include "QPropertyUndoCommand/qpropertyundocommand.h" #include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "qetapp.h"
#include "element.h"
DynamicElementTextModel::DynamicElementTextModel(QObject *parent) : DynamicElementTextModel::DynamicElementTextModel(QObject *parent) :
QStandardItemModel(parent) QStandardItemModel(parent)
@@ -85,9 +87,10 @@ void DynamicElementTextModel::addText(DynamicElementTextItem *deti)
QStandardItem *info = new QStandardItem(tr("Information")); QStandardItem *info = new QStandardItem(tr("Information"));
info->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); 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->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.clear();
qsi_list << info << infoa; 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)) else if ((from == tr("Information de l'élément")) && (deti->textFrom() != DynamicElementTextItem::ElementInfo))
new QPropertyUndoCommand(deti, "textFrom", QVariant(deti->textFrom()), QVariant(DynamicElementTextItem::ElementInfo), undo); 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(from == tr("Texte utilisateur"))
if (text != deti->text()) {
new QPropertyUndoCommand(deti, "text", QVariant(deti->text()), QVariant(text), 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);
}
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(); int fs = text_qsi->child(1,1)->data(Qt::EditRole).toInt();
if (fs != deti->fontSize()) if (fs != deti->fontSize())
@@ -281,6 +293,11 @@ void DynamicElementTextModel::dataEdited(QStandardItem *qsi)
QString text = qsi->data(Qt::DisplayRole).toString(); QString text = qsi->data(Qt::DisplayRole).toString();
m_texts_list.value(deti)->setData(text, Qt::DisplayRole); 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")); qcb->addItem(tr("Information de l'élément"));
return qcb; 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: case DynamicElementTextModel::color:
{ {
QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>()); QColorDialog *cd = new QColorDialog(index.data(Qt::EditRole).value<QColor>());
cd->setObjectName("color_dialog");
return cd; return cd;
} }
} }
@@ -387,21 +431,60 @@ void DynamicTextItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *
{ {
if (index.isValid()) if (index.isValid())
{ {
if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model)) if(editor->objectName() == "color_dialog")
{ {
QStandardItem *qsi = qsim->itemFromIndex(index); if (QStandardItemModel *qsim = dynamic_cast<QStandardItemModel *>(model))
if(qsi)
{ {
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::EditRole);
qsi->setData(cd->selectedColor(), Qt::ForegroundRole); qsi->setData(cd->selectedColor(), Qt::ForegroundRole);
return; 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); 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;
}

View File

@@ -71,6 +71,9 @@ class DynamicTextItemDelegate : public QStyledItemDelegate
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, 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 #endif // DYNAMICELEMENTTEXTMODEL_H