Element now use the pattern and variable of formula to create the label, both stored in diagram context (instead of the label with a pattern and formula, to create the final label)

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4795 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-12-01 09:09:50 +00:00
parent a495675b71
commit c811b02516
10 changed files with 122 additions and 166 deletions

View File

@@ -1016,15 +1016,6 @@ void Diagram::addItem(QGraphicsItem *item)
Element *elmt = static_cast<Element*>(item); Element *elmt = static_cast<Element*>(item);
foreach(ElementTextItem *eti, elmt->texts()) foreach(ElementTextItem *eti, elmt->texts())
connect (eti, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged); connect (eti, &ElementTextItem::diagramTextChanged, this, &Diagram::diagramTextChanged);
Element::kind linkType = elmt->linkType();
if ((linkType == Element::Simple) ||
(linkType == Element::Master) ||
(linkType == Element::Slave) ||
(linkType == Element::Terminale)) {
CustomElement *celmt = static_cast<CustomElement*>(item);
celmt->parseLabels();
}
elmt->updateLabel();
} }
break; break;

View File

@@ -189,7 +189,7 @@ bool DiagramEventAddElement::buildElement()
//Everything is good //Everything is good
return true; return true;
} }
#include "elementtextitem.h"
/** /**
* @brief DiagramEventAddElement::addElement * @brief DiagramEventAddElement::addElement
* Add an element at the current pos en current rotation, * Add an element at the current pos en current rotation,
@@ -237,8 +237,9 @@ void DiagramEventAddElement::addElement()
conductor->setFreezeLabel(true); conductor->setFreezeLabel(true);
} }
}; };
m_diagram -> undoStack().push(undo_object); m_diagram -> undoStack().push(undo_object);
element->SetUpSequential(); element->setUpFormula();
element->freezeNewAddedElement();
element->updateLabel(); element->updateLabel();
element->freezeNewAddedElement();
} }

View File

@@ -113,7 +113,6 @@ void CommentItem::updateLabel()
QString location = m_element -> elementInformations()["location"].toString(); QString location = m_element -> elementInformations()["location"].toString();
QPainterPath m_shape_path_ = QPainterPath();
prepareGeometryChange(); prepareGeometryChange();
m_bounding_rect = QRectF(); m_bounding_rect = QRectF();

View File

@@ -694,18 +694,6 @@ bool CustomElement::parseText(QDomElement &e, QPainter &qp) {
eti -> setFollowParentRotations(e.attribute("rotate") == "true"); eti -> setFollowParentRotations(e.attribute("rotate") == "true");
list_texts_ << eti; list_texts_ << eti;
if (e.attribute("tagg")=="label") {
DiagramContext &dc = this->rElementInformations();
dc.addValue("label", e.attribute("text"));
this->setElementInformations(dc);
this->setTaggedText("label", e.attribute("text"));
}
else if (e.attribute("tagg")=="function") {
DiagramContext &dc = this->rElementInformations();
dc.addValue("function", e.attribute("text"));
this->setElementInformations(dc);
}
// Se positionne aux coordonnees indiquees dans la description du texte // Se positionne aux coordonnees indiquees dans la description du texte
qp.setTransform(QTransform(), false); qp.setTransform(QTransform(), false);
qp.translate(pos_x, pos_y); qp.translate(pos_x, pos_y);
@@ -762,18 +750,19 @@ ElementTextItem *CustomElement::parseInput(QDomElement &e) {
ElementTextItem *eti = new ElementTextItem(e.attribute("text"), this); ElementTextItem *eti = new ElementTextItem(e.attribute("text"), this);
eti -> setFont(QETApp::diagramTextsFont(size)); eti -> setFont(QETApp::diagramTextsFont(size));
eti -> setTagg(e.attribute("tagg", "other")); eti -> setTagg(e.attribute("tagg", "other"));
m_element_informations.addValue(e.attribute("tagg", "other"), e.attribute("text"));
// position the text field // position the text field
eti -> setOriginalPos(QPointF(pos_x, pos_y)); eti -> setOriginalPos(QPointF(pos_x, pos_y));
eti -> setPos(pos_x, pos_y); eti -> setPos(pos_x, pos_y);
// rotation of the text field // rotation of the text field
qreal original_rotation_angle = 0.0; qreal original_rotation_angle = 0.0;
QET::attributeIsAReal(e, "rotation", &original_rotation_angle); QET::attributeIsAReal(e, "rotation", &original_rotation_angle);
eti -> setOriginalRotationAngle(original_rotation_angle); eti -> setOriginalRotationAngle(original_rotation_angle);
eti -> setRotationAngle(original_rotation_angle); eti -> setRotationAngle(original_rotation_angle);
// behavior when the parent element is rotated // behavior when the parent element is rotated
eti -> setFollowParentRotations(e.attribute("rotate") == "true"); eti -> setFollowParentRotations(e.attribute("rotate") == "true");
list_texts_ << eti; list_texts_ << eti;

View File

@@ -730,19 +730,45 @@ void Element::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
} }
/** /**
* @brief Element::SetUpSequential * @brief Element::setUpFormula
* Setup the sequential value of this element * Set up the formula used to create the label of this element
* @param : if true set tagged text to code letter (ex K for coil) with condition :
* formula is empty, text tagged "label" is emptty or "_";
*/ */
void Element::SetUpSequential() void Element::setUpFormula(bool code_letter)
{ {
if (linkType() == Element::Slave || linkType() & Element::AllReport)
return;
if (diagram()) if (diagram())
{ {
QString element_currentAutoNum = diagram()->project()->elementCurrentAutoNum(); QString formula = diagram()->project()->elementAutoNumCurrentFormula();
NumerotationContext nc = diagram()->project()->elementAutoNum(element_currentAutoNum);
NumerotationContextCommands ncc (nc);
autonum::setSequential(elementInformations()["label"].toString(), m_autoNum_seq, nc, diagram(), element_currentAutoNum); if (formula.isEmpty())
diagram()->project()->addElementAutoNum(element_currentAutoNum, ncc.next()); {
if (code_letter && !m_prefix.isEmpty())
{
if (ElementTextItem *eti = taggedText("label"))
{
QString text = eti->toPlainText();
if (text.isEmpty() || text == "_")
{
m_element_informations.addValue("formula", "%prefix");
}
}
}
}
else
{
m_element_informations.addValue("formula", formula);
QString element_currentAutoNum = diagram()->project()->elementCurrentAutoNum();
NumerotationContext nc = diagram()->project()->elementAutoNum(element_currentAutoNum);
NumerotationContextCommands ncc (nc);
autonum::setSequential(formula, m_autoNum_seq, nc, diagram(), element_currentAutoNum);
diagram()->project()->addElementAutoNum(element_currentAutoNum, ncc.next());
}
} }
} }
@@ -824,3 +850,26 @@ void Element::freezeNewAddedElement() {
} }
else return; else return;
} }
/**
* @brief Element::setUpConnectionForFormula
* setup connection according to the variable of formula
* @param old_formula
* @param new_formula
*/
void Element::setUpConnectionForFormula(QString old_formula, QString new_formula)
{
if (diagram() && (old_formula.contains("%f") || old_formula.contains("%id")))
disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &Element::updateLabel);
if (old_formula.contains("%l"))
disconnect(this, &Element::yChanged, this, &Element::updateLabel);
if (old_formula.contains("%c"))
disconnect(this, &Element::xChanged, this, &Element::updateLabel);
if (diagram() && (new_formula.contains("%f") || new_formula.contains("%id")))
connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &Element::updateLabel);
if (new_formula.contains("%l"))
connect(this, &Element::yChanged, this, &Element::updateLabel);
if (new_formula.contains("%c"))
connect(this, &Element::xChanged, this, &Element::updateLabel);
}

View File

@@ -32,7 +32,8 @@ class NumerotationContext;
/** /**
This is the base class for electrical elements. This is the base class for electrical elements.
*/ */
class Element : public QetGraphicsItem { class Element : public QetGraphicsItem
{
Q_OBJECT Q_OBJECT
// constructors, destructor // constructors, destructor
@@ -139,14 +140,17 @@ class Element : public QetGraphicsItem {
autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;} autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;}
autonum::sequenceStruct& rSequenceStruct() {return m_autoNum_seq;} autonum::sequenceStruct& rSequenceStruct() {return m_autoNum_seq;}
void SetUpSequential (); void setUpFormula(bool code_letter = true);
void setPrefix(QString); void setPrefix(QString);
QString getPrefix() const; QString getPrefix() const;
void freezeLabel(); void freezeLabel();
void unfreezeLabel(); void unfreezeLabel();
void freezeNewAddedElement(); void freezeNewAddedElement();
//ATTRIBUTES protected:
void setUpConnectionForFormula(QString old_formula, QString new_formula);
//ATTRIBUTES
protected: protected:
DiagramContext m_element_informations, kind_informations_; DiagramContext m_element_informations, kind_informations_;
autonum::sequenceStruct m_autoNum_seq; autonum::sequenceStruct m_autoNum_seq;

View File

@@ -31,13 +31,11 @@
*/ */
MasterElement::MasterElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) : MasterElement::MasterElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) :
CustomElement(location, qgi, state), CustomElement(location, qgi, state),
cri_ (nullptr) m_Xref_item (nullptr)
{ {
link_type_ = Master; link_type_ = Master;
connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext))); connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext)));
connect(this, SIGNAL(xChanged()), this, SLOT(changeElementInfo())); connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());});
connect(this, SIGNAL(yChanged()), this, SLOT(changeElementInfo()));
connect(this, SIGNAL(updateLabel()), this, SLOT(changeElementInfo()));
} }
/** /**
@@ -62,12 +60,12 @@ void MasterElement::linkToElement(Element *elmt)
connected_elements << elmt; connected_elements << elmt;
elmt->linkToElement(this); elmt->linkToElement(this);
if (!cri_) cri_ = new CrossRefItem(this); //create cross ref item if not yet if (!m_Xref_item) m_Xref_item = new CrossRefItem(this); //create cross ref item if not yet
connect(elmt, SIGNAL(xChanged()), cri_, SLOT(updateLabel())); connect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel()));
connect(elmt, SIGNAL(yChanged()), cri_, SLOT(updateLabel())); connect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel()));
connect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel())); connect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel()));
cri_ -> updateLabel(); m_Xref_item -> updateLabel();
emit linkedElementChanged(); emit linkedElementChanged();
} }
} }
@@ -102,11 +100,11 @@ void MasterElement::unlinkElement(Element *elmt)
elmt -> setHighlighted (false); elmt -> setHighlighted (false);
//update the graphics cross ref //update the graphics cross ref
disconnect(elmt, SIGNAL(xChanged()), cri_, SLOT(updateLabel())); disconnect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(yChanged()), cri_, SLOT(updateLabel())); disconnect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel())); disconnect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel()));
cri_ -> updateLabel(); m_Xref_item -> updateLabel();
aboutDeleteXref(); aboutDeleteXref();
emit linkedElementChanged(); emit linkedElementChanged();
} }
@@ -124,27 +122,6 @@ void MasterElement::initLink(QETProject *project) {
updateLabel(DiagramContext(), elementInformations()); updateLabel(DiagramContext(), elementInformations());
} }
/**
* @brief MasterElement::folioIdChange
* Used to update the label of this item when the folio id change
*/
void MasterElement::folioIdChange() {
DiagramContext dc =elementInformations();
setTaggedText("label", autonum::AssignVariables::formulaToLabel(dc["label"].toString(), m_autoNum_seq, diagram(), this), true);
}
/**
* @brief MasterElement::changeElementInfo()
* Update label if it contains %c, %l, %f or %F variables
*/
void MasterElement::changeElementInfo(){
QString temp_label = this->elementInformations()["label"].toString();
if (temp_label.contains("\%")) {
if (this->diagram()!=NULL)
this->updateLabel(this->elementInformations(),this->elementInformations());
}
}
/** /**
* @brief MasterElement::updateLabel * @brief MasterElement::updateLabel
* update label of this element * update label of this element
@@ -152,49 +129,33 @@ void MasterElement::changeElementInfo(){
*/ */
void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info) void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info)
{ {
const QString old_label = old_info["label"].toString(); QString old_formula = old_info["formula"].toString();
const QString new_label = new_info["label"].toString(); QString new_formula = new_info["formula"].toString();
QString newstr = autonum::AssignVariables::formulaToLabel(new_label, m_autoNum_seq, diagram(), this); setUpConnectionForFormula(old_formula, new_formula);
ElementTextItem *eti = taggedText("label"); QString label = autonum::AssignVariables::formulaToLabel(new_formula, m_autoNum_seq, diagram(), this);
//Label of element if (label.isEmpty())
if (eti && (eti->toPlainText() != newstr))
{ {
if (new_label.isEmpty()) setTaggedText("label", new_info["label"].toString());
{ }
setTaggedText("label", "_", false); else
} {
else bool visible = m_element_informations.contains("label") ? m_element_informations.keyMustShow("label") : true;
{ m_element_informations.addValue("label", label, visible);
setTaggedText("label", newstr, true); setTaggedText("label", label);
}
//If autonum formula have %id %f or %F (because %F can itself contain %id or %f),
//we connect the change of folio position, to keep up to date the label.
if (diagram() && diagram()->project())
{
if (old_label.contains(QRegularExpression("%id|%f|%F")) && !new_label.contains(QRegularExpression("%id|%f|%F")))
{
disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &MasterElement::folioIdChange);
}
else if (new_label.contains(QRegularExpression("%id|%f|%F")))
{
connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &MasterElement::folioIdChange);
}
}
} }
if (eti) if (ElementTextItem *eti = taggedText("label"))
{ {
new_label.isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label")); new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label"));
} }
//Delete or update the xref //Delete or update the xref
if (cri_) { if (m_Xref_item) {
cri_ -> updateLabel(); m_Xref_item -> updateLabel();
aboutDeleteXref(); aboutDeleteXref();
} }
else { else {
@@ -205,7 +166,7 @@ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info
bool must_show_location = elementInformations().keyMustShow("location"); bool must_show_location = elementInformations().keyMustShow("location");
if (! (comment.isEmpty() || !must_show_comment) || !(location.isEmpty() || !must_show_location)) { if (! (comment.isEmpty() || !must_show_comment) || !(location.isEmpty() || !must_show_location)) {
cri_ = new CrossRefItem(this); m_Xref_item = new CrossRefItem(this);
} }
} }
} }
@@ -221,12 +182,12 @@ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info
* @return * @return
*/ */
bool MasterElement::aboutDeleteXref() { bool MasterElement::aboutDeleteXref() {
if(!cri_) return true; if(!m_Xref_item) return true;
if(!linkedElements().isEmpty()) return false; if(!linkedElements().isEmpty()) return false;
if (cri_ -> boundingRect().isNull()) { if (m_Xref_item -> boundingRect().isNull()) {
delete cri_; delete m_Xref_item;
cri_ = nullptr; m_Xref_item = nullptr;
return true; return true;
} }

View File

@@ -40,21 +40,15 @@ class MasterElement : public CustomElement
virtual void unlinkAllElements (); virtual void unlinkAllElements ();
virtual void unlinkElement (Element *elmt); virtual void unlinkElement (Element *elmt);
virtual void initLink (QETProject *project); virtual void initLink (QETProject *project);
signals:
private:
void folioIdChange();
public slots: public slots:
void updateLabel(DiagramContext old_info, DiagramContext new_info); void updateLabel(DiagramContext old_info, DiagramContext new_info);
void changeElementInfo();
private: private:
bool aboutDeleteXref (); bool aboutDeleteXref ();
private: private:
CrossRefItem *cri_; CrossRefItem *m_Xref_item;
}; };
#endif // MASTERELEMENT_H #endif // MASTERELEMENT_H

View File

@@ -34,9 +34,7 @@ SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qg
{ {
link_type_ = Simple; link_type_ = Simple;
connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext))); connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext)));
connect(this, SIGNAL(xChanged()),this, SLOT(changeElementInfo())); connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());});
connect(this, SIGNAL(yChanged()),this, SLOT(changeElementInfo()));
connect(this, SIGNAL(updateLabel()),this,SLOT(changeElementInfo()));
} }
/** /**
@@ -57,60 +55,36 @@ void SimpleElement::initLink(QETProject *project) {
updateLabel(DiagramContext(), elementInformations()); updateLabel(DiagramContext(), elementInformations());
} }
/**
* @brief SimpleElement::folioIdChange
* Use to update the label of this item when the foio id change
*/
void SimpleElement::folioIdChange()
{
DiagramContext dc =elementInformations();
setTaggedText("label", autonum::AssignVariables::formulaToLabel(dc["label"].toString(), m_autoNum_seq, diagram(), this));
}
/**
* @brief SimpleElement::changeElementInfo()
* Update label if it contains %c, %l, %f or %F variables
*/
void SimpleElement::changeElementInfo(){
QString temp_label = this->elementInformations()["label"].toString();
if (temp_label.contains("\%")) {
if (this->diagram()!=NULL)
this->updateLabel(this->elementInformations(),this->elementInformations());
}
}
/** /**
* @brief SimpleElement::updateLabel * @brief SimpleElement::updateLabel
* update label of this element * update label of this element
*/ */
void SimpleElement::updateLabel(DiagramContext old_info, DiagramContext new_info) void SimpleElement::updateLabel(DiagramContext old_info, DiagramContext new_info)
{ {
QString label = autonum::AssignVariables::formulaToLabel(new_info["label"].toString(), m_autoNum_seq, diagram(), this); QString old_formula = old_info["formula"].toString();
QString new_formula = new_info["formula"].toString();
//Label of element setUpConnectionForFormula(old_formula, new_formula);
if (old_info["label"].toString() != label) {
if (new_info["label"].toString().isEmpty())
setTaggedText("label", "_", false);
else {
setTaggedText("label", label, true);
}
//If autonum formula have %id we connect the change of folio position, to keep up to date the label. QString label = autonum::AssignVariables::formulaToLabel(new_formula, m_autoNum_seq, diagram(), this);
if (diagram() && diagram()->project()) {
if (old_info["label"].toString().contains("%id") && !new_info["label"].toString().contains("%id")) { if (label.isEmpty())
disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange); {
} setTaggedText("label", new_info["label"].toString());
else if (new_info["label"].toString().contains("%id")) {
connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange);
}
} }
else
{
bool visible = m_element_informations.contains("label") ? m_element_informations.keyMustShow("label") : true;
m_element_informations.addValue("label", label, visible);
setTaggedText("label", label);
} }
if (ElementTextItem *eti = taggedText("label")) { if (ElementTextItem *eti = taggedText("label"))
{
new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label")); new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label"));
} }
//Comment and Location of element //Comment and Location of element
QString comment = new_info["comment"].toString(); QString comment = new_info["comment"].toString();
bool must_show = new_info.keyMustShow("comment"); bool must_show = new_info.keyMustShow("comment");
QString location = new_info["location"].toString(); QString location = new_info["location"].toString();

View File

@@ -37,14 +37,8 @@ class SimpleElement : public CustomElement {
virtual void initLink(QETProject *project); virtual void initLink(QETProject *project);
signals:
private:
void folioIdChange();
public slots: public slots:
void updateLabel(DiagramContext old_info, DiagramContext new_info); void updateLabel(DiagramContext old_info, DiagramContext new_info);
void changeElementInfo();
private: private:
CommentItem *m_comment_item; CommentItem *m_comment_item;