mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-09-13 20:23:04 +02:00
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:
parent
31f36b8625
commit
6e2ed873bb
@ -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.
|
||||
*/
|
||||
|
@ -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;
|
||||
|
||||
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<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();
|
||||
|
||||
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
|
||||
|
@ -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");
|
||||
|
@ -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();
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 @@ 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<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 @@ 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<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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user