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);
foreach(ElementTextItem *eti, elmt->texts())
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;

View File

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

View File

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

View File

@@ -694,18 +694,6 @@ bool CustomElement::parseText(QDomElement &e, QPainter &qp) {
eti -> setFollowParentRotations(e.attribute("rotate") == "true");
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
qp.setTransform(QTransform(), false);
qp.translate(pos_x, pos_y);
@@ -762,6 +750,7 @@ ElementTextItem *CustomElement::parseInput(QDomElement &e) {
ElementTextItem *eti = new ElementTextItem(e.attribute("text"), this);
eti -> setFont(QETApp::diagramTextsFont(size));
eti -> setTagg(e.attribute("tagg", "other"));
m_element_informations.addValue(e.attribute("tagg", "other"), e.attribute("text"));
// position the text field
eti -> setOriginalPos(QPointF(pos_x, pos_y));

View File

@@ -730,21 +730,47 @@ void Element::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
}
/**
* @brief Element::SetUpSequential
* Setup the sequential value of this element
* @brief Element::setUpFormula
* 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())
{
QString formula = diagram()->project()->elementAutoNumCurrentFormula();
if (formula.isEmpty())
{
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(elementInformations()["label"].toString(), m_autoNum_seq, nc, diagram(), element_currentAutoNum);
autonum::setSequential(formula, m_autoNum_seq, nc, diagram(), element_currentAutoNum);
diagram()->project()->addElementAutoNum(element_currentAutoNum, ncc.next());
}
}
}
/**
* @brief ElementTextItem::setTaggedText
@@ -824,3 +850,26 @@ void Element::freezeNewAddedElement() {
}
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.
*/
class Element : public QetGraphicsItem {
class Element : public QetGraphicsItem
{
Q_OBJECT
// constructors, destructor
@@ -139,13 +140,16 @@ class Element : public QetGraphicsItem {
autonum::sequenceStruct sequenceStruct () const {return m_autoNum_seq;}
autonum::sequenceStruct& rSequenceStruct() {return m_autoNum_seq;}
void SetUpSequential ();
void setUpFormula(bool code_letter = true);
void setPrefix(QString);
QString getPrefix() const;
void freezeLabel();
void unfreezeLabel();
void freezeNewAddedElement();
protected:
void setUpConnectionForFormula(QString old_formula, QString new_formula);
//ATTRIBUTES
protected:
DiagramContext m_element_informations, kind_informations_;

View File

@@ -31,13 +31,11 @@
*/
MasterElement::MasterElement(const ElementsLocation &location, QGraphicsItem *qgi, int *state) :
CustomElement(location, qgi, state),
cri_ (nullptr)
m_Xref_item (nullptr)
{
link_type_ = Master;
connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext)));
connect(this, SIGNAL(xChanged()), this, SLOT(changeElementInfo()));
connect(this, SIGNAL(yChanged()), this, SLOT(changeElementInfo()));
connect(this, SIGNAL(updateLabel()), this, SLOT(changeElementInfo()));
connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());});
}
/**
@@ -62,12 +60,12 @@ void MasterElement::linkToElement(Element *elmt)
connected_elements << elmt;
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(yChanged()), cri_, SLOT(updateLabel()));
connect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel()));
cri_ -> updateLabel();
connect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel()));
connect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel()));
connect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel()));
m_Xref_item -> updateLabel();
emit linkedElementChanged();
}
}
@@ -102,11 +100,11 @@ void MasterElement::unlinkElement(Element *elmt)
elmt -> setHighlighted (false);
//update the graphics cross ref
disconnect(elmt, SIGNAL(xChanged()), cri_, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(yChanged()), cri_, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(updateLabel()), cri_, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(xChanged()), m_Xref_item, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(yChanged()), m_Xref_item, SLOT(updateLabel()));
disconnect(elmt, SIGNAL(updateLabel()), m_Xref_item, SLOT(updateLabel()));
cri_ -> updateLabel();
m_Xref_item -> updateLabel();
aboutDeleteXref();
emit linkedElementChanged();
}
@@ -124,27 +122,6 @@ void MasterElement::initLink(QETProject *project) {
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
* update label of this element
@@ -152,49 +129,33 @@ void MasterElement::changeElementInfo(){
*/
void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info)
{
const QString old_label = old_info["label"].toString();
const QString new_label = new_info["label"].toString();
QString old_formula = old_info["formula"].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 (eti && (eti->toPlainText() != newstr))
if (label.isEmpty())
{
if (new_label.isEmpty())
{
setTaggedText("label", "_", false);
setTaggedText("label", new_info["label"].toString());
}
else
{
setTaggedText("label", newstr, true);
bool visible = m_element_informations.contains("label") ? m_element_informations.keyMustShow("label") : true;
m_element_informations.addValue("label", label, visible);
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 (ElementTextItem *eti = taggedText("label"))
{
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)
{
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
if (cri_) {
cri_ -> updateLabel();
if (m_Xref_item) {
m_Xref_item -> updateLabel();
aboutDeleteXref();
}
else {
@@ -205,7 +166,7 @@ void MasterElement::updateLabel(DiagramContext old_info, DiagramContext new_info
bool must_show_location = elementInformations().keyMustShow("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
*/
bool MasterElement::aboutDeleteXref() {
if(!cri_) return true;
if(!m_Xref_item) return true;
if(!linkedElements().isEmpty()) return false;
if (cri_ -> boundingRect().isNull()) {
delete cri_;
cri_ = nullptr;
if (m_Xref_item -> boundingRect().isNull()) {
delete m_Xref_item;
m_Xref_item = nullptr;
return true;
}

View File

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

View File

@@ -34,9 +34,7 @@ SimpleElement::SimpleElement(const ElementsLocation &location, QGraphicsItem *qg
{
link_type_ = Simple;
connect(this, SIGNAL(elementInfoChange(DiagramContext, DiagramContext)), this, SLOT(updateLabel(DiagramContext, DiagramContext)));
connect(this, SIGNAL(xChanged()),this, SLOT(changeElementInfo()));
connect(this, SIGNAL(yChanged()),this, SLOT(changeElementInfo()));
connect(this, SIGNAL(updateLabel()),this,SLOT(changeElementInfo()));
connect(this, &Element::updateLabel, [this]() {this->updateLabel(this->elementInformations(), this->elementInformations());});
}
/**
@@ -57,56 +55,32 @@ void SimpleElement::initLink(QETProject *project) {
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
* update label of this element
*/
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
if (old_info["label"].toString() != label) {
if (new_info["label"].toString().isEmpty())
setTaggedText("label", "_", false);
else {
setTaggedText("label", label, true);
setUpConnectionForFormula(old_formula, new_formula);
QString label = autonum::AssignVariables::formulaToLabel(new_formula, m_autoNum_seq, diagram(), this);
if (label.isEmpty())
{
setTaggedText("label", new_info["label"].toString());
}
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 autonum formula have %id we connect the change of folio position, to keep up to date the label.
if (diagram() && diagram()->project()) {
if (old_info["label"].toString().contains("%id") && !new_info["label"].toString().contains("%id")) {
disconnect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange);
}
else if (new_info["label"].toString().contains("%id")) {
connect(diagram()->project(), &QETProject::projectDiagramsOrderChanged, this, &SimpleElement::folioIdChange);
}
}
}
if (ElementTextItem *eti = taggedText("label")) {
if (ElementTextItem *eti = taggedText("label"))
{
new_info["label"].toString().isEmpty() ? eti->setVisible(true) : eti -> setVisible(new_info.keyMustShow("label"));
}

View File

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