diff --git a/sources/properties/xrefproperties.cpp b/sources/properties/xrefproperties.cpp index 8a80b27f6..c730ff502 100644 --- a/sources/properties/xrefproperties.cpp +++ b/sources/properties/xrefproperties.cpp @@ -35,6 +35,8 @@ void XRefProperties::toSettings(QSettings &settings, const QString prefix) const settings.setValue(prefix + "showpowerctc", m_show_power_ctc); QString display = m_display == Cross? "cross" : "contacts"; settings.setValue(prefix + "displayhas", display); + settings.setValue(prefix + "powerprefix", m_prefix.value("power")); + settings.setValue(prefix + "delayprefix", m_prefix.value("delay")); } /** @@ -47,6 +49,8 @@ void XRefProperties::fromSettings(const QSettings &settings, const QString prefi m_show_power_ctc = settings.value(prefix + "showpowerctc", false).toBool(); QString display = settings.value(prefix + "displayhas", "cross").toString(); display == "cross"? m_display = Cross : m_display = Contacts; + m_prefix.insert("power", settings.value(prefix + "powerprefix").toString()); + m_prefix.insert("delay", settings.value(prefix + "delayprefix").toString()); } /** @@ -58,6 +62,8 @@ void XRefProperties::toXml(QDomElement &xml_element) const { xml_element.setAttribute("showpowerctc", m_show_power_ctc? "true" : "fasle"); QString display = m_display == Cross? "cross" : "contacts"; xml_element.setAttribute("displayhas", display); + xml_element.setAttribute("powerprefix", m_prefix.value("power")); + xml_element.setAttribute("delayprefix", m_prefix.value("delay")); } /** @@ -69,11 +75,14 @@ void XRefProperties::fromXml(const QDomElement &xml_element) { m_show_power_ctc = xml_element.attribute("showpowerctc") == "true"; QString display = xml_element.attribute("displayhas", "cross"); display == "cross"? m_display = Cross : m_display = Contacts; + m_prefix.insert("power", xml_element.attribute("powerprefix")); + m_prefix.insert("delay", xml_element.attribute("delayprefix")); } bool XRefProperties::operator ==(const XRefProperties &xrp) const{ return (m_show_power_ctc == xrp.m_show_power_ctc && - m_display == xrp.m_display); + m_display == xrp.m_display && + m_prefix == xrp.m_prefix); } bool XRefProperties::operator !=(const XRefProperties &xrp) const { diff --git a/sources/properties/xrefproperties.h b/sources/properties/xrefproperties.h index aa9e64690..d8408e24e 100644 --- a/sources/properties/xrefproperties.h +++ b/sources/properties/xrefproperties.h @@ -48,9 +48,13 @@ class XRefProperties : public PropertiesInterface void setDisplayHas (const DisplayHas dh) {m_display = dh;} DisplayHas displayHas () const {return m_display;} + void setPrefix (const QString &key, const QString &value) {m_prefix.insert(key, value);} + QString prefix (const QString &key) const {return m_prefix.value(key);} + private: bool m_show_power_ctc; DisplayHas m_display; + QHash m_prefix; }; #endif // XREFPROPERTIES_H diff --git a/sources/qetgraphicsitem/crossrefitem.cpp b/sources/qetgraphicsitem/crossrefitem.cpp index 2c9a29ce7..c9df4c461 100644 --- a/sources/qetgraphicsitem/crossrefitem.cpp +++ b/sources/qetgraphicsitem/crossrefitem.cpp @@ -22,8 +22,6 @@ //define the height of the header. #define header 5 -//define the widht of the cross -#define crossWidth 50 /** * @brief CrossRefItem::CrossRefItem @@ -71,7 +69,25 @@ QPainterPath CrossRefItem::shape() const{ return m_shape_path; } -void CrossRefItem::setProperties(XRefProperties xrp) { +/** + * @brief CrossRefItem::elementPositionText + * @param elmt + * @return the string corresponding to the position of @elmt in the diagram. + * if @add_prefix is true, prefix (for power and delay contact) is added to the poistion text. + */ +QString CrossRefItem::elementPositionText(const Element *elmt, const bool &add_prefix) const{ + QString txt; + txt += QString::number(elmt->diagram()->folioIndex() + 1); + txt += "-"; + txt += elmt->diagram()->convertPosition(elmt -> scenePos()).toString(); + if (add_prefix) { + if (elmt->kindInformations()["type"].toString() == "power") txt.prepend(m_properties.prefix("power")); + else if (elmt->kindInformations()["type"].toString().contains("delay")) txt.prepend(m_properties.prefix("delay")); + } + return txt; +} + +void CrossRefItem::setProperties(const XRefProperties &xrp) { if (m_properties != xrp) { m_properties = xrp; updateLabel(); @@ -128,7 +144,8 @@ void CrossRefItem::autoPos() { point.setY(border.height() - m_element->diagram()->border_and_titleblock.titleBlockHeight() - boundingRect().height()); } - point.setX(point.x() - crossWidth/2); + qreal offset = m_bounding_rect.topLeft().x() < 0 ? m_bounding_rect.topLeft().x() : 0; + point.setX(point.x() - m_bounding_rect.width()/2 - offset); setPos(point); } @@ -177,6 +194,52 @@ void CrossRefItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { QGraphicsObject::mouseReleaseEvent(e); } +/** + * @brief CrossRefItem::buildHeaderContact + * Draw the QPicture of m_hdr_no_ctc and m_hdr_nc_ctc + */ +void CrossRefItem::buildHeaderContact() { + if (!m_hdr_no_ctc.isNull() && !m_hdr_nc_ctc.isNull()) return; + + //init the painter + QPainter qp; + QPen pen_; + pen_.setWidthF(0.2); + + //draw the NO contact + if (m_hdr_no_ctc.isNull()) { + qp.begin(&m_hdr_no_ctc); + qp.setPen(pen_); + qp.drawLine(0, 3, 5, 3); + QPointF p1[3] = { + QPointF(5, 0), + QPointF(10, 3), + QPointF(15, 3), + }; + qp.drawPolyline(p1,3); + qp.end(); + } + + //draw the NC contact + if (m_hdr_nc_ctc.isNull()) { + qp.begin(&m_hdr_nc_ctc); + qp.setPen(pen_); + QPointF p2[3] = { + QPointF(0, 3), + QPointF(5, 3), + QPointF(5, 0) + }; + qp.drawPolyline(p2,3); + QPointF p3[3] = { + QPointF(4, 0), + QPointF(10, 3), + QPointF(15, 3), + }; + qp.drawPolyline(p3,3); + qp.end(); + } +} + /** * @brief CrossRefItem::setUpCrossBoundingRect * Get the numbers of slaves elements linked to this parent element, @@ -185,30 +248,55 @@ void CrossRefItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { */ void CrossRefItem::setUpCrossBoundingRect() { //this is the default size of cross ref item - QRectF default_bounding(0, 0, crossWidth, 40); + //add 2 to header for better visual + QRectF default_bounding(0, 0, 40, header+2); //No need to calcul if nothing is linked if (!m_element->isFree()) { + /*Set up a Qpainter with the same parametres + than the QPainter used for draw the text inside the cross, + for calculate the size of each text + ===== + We can also use the QPainter used for draw the text inside the cross + and call the method "boundingrect", for know the size of text, + but the QrectF returned isn't good (bug)*/ + QPainter qp; + QPicture pict; + qp.begin(&pict); + QPen pen_; + pen_.setWidthF(0.2); + qp.setPen(pen_); + qp.setFont(QETApp::diagramTextsFont(5)); + QList NO_list; QList NC_list; //find each no and nc of connected element to m_element + //and define the size of default_bounding according to the connected elements + bool was_ajusted = false; + qreal no_height = 0, nc_height = 0; foreach (Element *elmt, m_element->linkedElements()) { + QRectF r; + qp.drawText(r, Qt::AlignCenter, elementPositionText(elmt, true), &r); + if (r.width() > default_bounding.width()/2) { + default_bounding.setWidth(r.width()*2); + was_ajusted = true; + } + QString state = elmt->kindInformations()["state"].toString(); - if (state == "NO") NO_list << elmt; - else if (state == "NC") NC_list << elmt; - } - - int i =0; - if (NO_list.count()>4 || NC_list.count()>4) { - i = NO_list.count() > NC_list.count()? - NO_list.count() : NC_list.count(); - - //increase the height of bounding rect, - //according to the number of slave item less 4. - i-=4; - default_bounding.setHeight(default_bounding.height() + (i*8)); + if (state == "NO") { + NO_list << elmt; + no_height += r.height(); + } + else if (state == "NC") { + NC_list << elmt; + nc_height += r.height(); + } } + if (was_ajusted) default_bounding.setWidth (default_bounding.width()+5); //adjust only for better visual + no_height > nc_height? default_bounding.setHeight (default_bounding.height() + no_height) : + default_bounding.setHeight (default_bounding.height() + nc_height); + qp.end(); } m_shape_path.addRect(default_bounding); m_bounding_rect = default_bounding; @@ -226,41 +314,14 @@ void CrossRefItem::drawHasCross(QPainter &painter) { //draw the cross QRectF br = boundingRect(); painter.drawLine(br.width()/2, 0, br.width()/2, br.height()); //vertical line - painter.drawLine(br.width()/2-(crossWidth/2), header, br.width()/2+(crossWidth/2), header); //horizontal line + painter.drawLine(0, header, br.width(), header); //horizontal line - //draw the symbolic NO - qreal xoffset = br.width()/2 - 25; - painter.drawLine(xoffset+5, 3, xoffset+10, 3); - QPointF p1[3] = { - QPointF(xoffset+10, 0), - QPointF(xoffset+15, 3), - QPointF(xoffset+20, 3), - }; - painter.drawPolyline(p1,3); - - //draw the symbolic NC - xoffset = br.width()/2; - QPointF p2[3] = { - QPointF(xoffset+5, 3), - QPointF(xoffset+10, 3), - QPointF(xoffset+10, 0) - }; - painter.drawPolyline(p2,3); - QPointF p3[3] = { - QPointF(xoffset+9, 0), - QPointF(xoffset+15, 3), - QPointF(xoffset+20, 3), - }; - painter.drawPolyline(p3,3); - - ///keep this code for possible next feature - ///choice to use symbolic or text. - //draw the header - /*qp.setFont(QETApp::diagramTextsFont(7)); - QRectF header_rect (0,0,30,10); - qp.drawText(header_rect, Qt::AlignCenter, "NO"); - header_rect.setRect(30, 0, 30, 10); - qp.drawText(header_rect, Qt::AlignCenter, "NC");*/ + //Add the symbolic contacts + buildHeaderContact(); + QPointF p((m_bounding_rect.width()/4) - (m_hdr_no_ctc.width()/2), 0); + painter.drawPicture (p, m_hdr_no_ctc); + p.setX((m_bounding_rect.width() * 3/4) - (m_hdr_nc_ctc.width()/2)); + painter.drawPicture (p, m_hdr_nc_ctc); //and fill it fillCrossRef(painter); @@ -294,15 +355,11 @@ void CrossRefItem::drawHasContacts(QPainter &painter) { else if (type == "delayOn") option += DelayOn; else if (type == "delayOff") option += DelayOff; - QString contact_str; - contact_str += QString::number(elmt->diagram()->folioIndex() + 1); - contact_str += "-"; - contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString(); - drawContact(painter, option, contact_str); + drawContact(painter, option, elementPositionText(elmt)); } } - QRectF br(0,0,crossWidth, m_drawed_contacts*10+4); + QRectF br(0, 0, 50, m_drawed_contacts*10+4); m_bounding_rect = br; m_shape_path.addRect(br); painter.restore(); @@ -416,30 +473,27 @@ void CrossRefItem::fillCrossRef(QPainter &painter) { //fill the NO foreach (Element *elmt, NO_list) { ++i; - contact_str += QString::number(elmt->diagram()->folioIndex() + 1); - contact_str += "-"; - contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString(); + contact_str += elementPositionText(elmt, true); if(NO_list.size() > i) contact_str += "\n"; } - QRectF rect_(middle_cross - (crossWidth/2), + QRectF rect_(0, header, middle_cross, - (m_bounding_rect.height()-header)); + m_bounding_rect.height()-header); painter.drawText(rect_, Qt::AlignTop | Qt::AlignLeft, contact_str); //fill the NC contact_str.clear(); i = 0; foreach (Element *elmt, NC_list) { - contact_str += QString::number(elmt->diagram()->folioIndex() + 1); - contact_str += "-"; - contact_str += elmt->diagram()->convertPosition(elmt -> scenePos()).toString(); + ++i; + contact_str += elementPositionText(elmt, true); if (NC_list.size() > i) contact_str += "\n"; } rect_.setRect(middle_cross, header, - crossWidth/2, - (m_bounding_rect.height()-header)); + middle_cross, + m_bounding_rect.height()-header); painter.drawText(rect_, Qt::AlignTop | Qt::AlignRight, contact_str); } @@ -475,7 +529,6 @@ void CrossRefItem::AddExtraInfo(QPainter &painter) { * must to be show or not */ void CrossRefItem::checkMustShow() { - //We always show Xref when is displayed has contact if (m_properties.displayHas() == XRefProperties::Contacts) { this->show(); diff --git a/sources/qetgraphicsitem/crossrefitem.h b/sources/qetgraphicsitem/crossrefitem.h index 5c15f8ae4..69502e76e 100644 --- a/sources/qetgraphicsitem/crossrefitem.h +++ b/sources/qetgraphicsitem/crossrefitem.h @@ -50,15 +50,16 @@ class CrossRefItem : public QGraphicsObject DelayOff = 16 }; - QRectF boundingRect() const; - virtual QPainterPath shape() const; + QRectF boundingRect () const; + virtual QPainterPath shape () const; + QString elementPositionText (const Element *elmt, const bool &add_prefix = false) const; signals: public slots: - void setProperties (XRefProperties xrp); - void updateLabel(); - void autoPos(); + void setProperties (const XRefProperties &xrp); + void updateLabel (); + void autoPos (); protected: virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); @@ -66,19 +67,20 @@ class CrossRefItem : public QGraphicsObject virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *e); private: - void setUpCrossBoundingRect(); - void drawHasCross (QPainter &painter); - void drawHasContacts (QPainter &painter); - void drawContact (QPainter &painter, int flags, QString str = QString()); - void fillCrossRef (QPainter &painter); - void AddExtraInfo (QPainter &painter); - void checkMustShow (); + void buildHeaderContact (); + void setUpCrossBoundingRect (); + void drawHasCross (QPainter &painter); + void drawHasContacts (QPainter &painter); + void drawContact (QPainter &painter, int flags, QString str = QString()); + void fillCrossRef (QPainter &painter); + void AddExtraInfo (QPainter &painter); + void checkMustShow (); //Attributes private: Element *m_element; //element to display the cross reference QRectF m_bounding_rect; - QPicture m_drawing; + QPicture m_drawing, m_hdr_no_ctc, m_hdr_nc_ctc; QPainterPath m_shape_path; XRefProperties m_properties; int m_drawed_contacts; diff --git a/sources/ui/xrefpropertieswidget.cpp b/sources/ui/xrefpropertieswidget.cpp index b4ac5cf68..6e661bc9f 100644 --- a/sources/ui/xrefpropertieswidget.cpp +++ b/sources/ui/xrefpropertieswidget.cpp @@ -31,7 +31,7 @@ XRefPropertiesWidget::XRefPropertiesWidget(XRefProperties properties, QWidget *p m_properties(properties) { ui->setupUi(this); - connect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_show_power_cb, SLOT(setEnabled(bool))); + connect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_cross_properties_gb, SLOT(setEnabled(bool))); updateDisplay(); } @@ -41,7 +41,7 @@ XRefPropertiesWidget::XRefPropertiesWidget(XRefProperties properties, QWidget *p */ XRefPropertiesWidget::~XRefPropertiesWidget() { - disconnect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_show_power_cb, SLOT(setEnabled(bool))); + disconnect(ui->m_display_has_cross_rb, SIGNAL(toggled(bool)), ui->m_cross_properties_gb, SLOT(setEnabled(bool))); delete ui; } @@ -63,6 +63,8 @@ XRefProperties XRefPropertiesWidget::properties() { if (ui->m_display_has_cross_rb->isChecked()) m_properties.setDisplayHas(XRefProperties::Cross); else if (ui->m_display_has_contacts_rb->isChecked()) m_properties.setDisplayHas(XRefProperties::Contacts); m_properties.setShowPowerContac(ui->m_show_power_cb->isChecked()); + m_properties.setPrefix("power", ui->m_power_prefix_le->text()); + m_properties.setPrefix("delay", ui->m_delay_prefix_le->text()); return m_properties; } @@ -77,9 +79,9 @@ void XRefPropertiesWidget::setReadOnly(bool ro) { ui->m_display_has_contacts_rb->setDisabled(ro); if (m_properties.displayHas() != XRefProperties::Cross) - ui->m_show_power_cb->setDisabled(true); + ui->m_cross_properties_gb->setDisabled(true); else - ui->m_show_power_cb->setDisabled(ro); + ui->m_cross_properties_gb->setDisabled(ro); } /** @@ -96,5 +98,7 @@ void XRefPropertiesWidget::updateDisplay() { } ui->m_show_power_cb->setChecked(m_properties.showPowerContact()); - ui->m_show_power_cb->setDisabled(!ui->m_display_has_cross_rb->isChecked()); + ui->m_power_prefix_le->setText(m_properties.prefix("power")); + ui->m_delay_prefix_le->setText(m_properties.prefix("delay")); + ui->m_cross_properties_gb->setDisabled(!ui->m_display_has_cross_rb->isChecked()); } diff --git a/sources/ui/xrefpropertieswidget.ui b/sources/ui/xrefpropertieswidget.ui index 477fcf0ef..1f2572bd9 100644 --- a/sources/ui/xrefpropertieswidget.ui +++ b/sources/ui/xrefpropertieswidget.ui @@ -42,25 +42,58 @@ - - - Afficher les contacts de puissance dans la croix + + + Option d'affichage en croix + + + + + Afficher les contacts de puissance dans la croix + + + + + + + + + + + + + + + Préfixe des contacts de puissance: + + + + + + + Préfixe des contacts temporisés: + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + - - - - Qt::Vertical - - - - 20 - 253 - - - -