diff --git a/customelement.cpp b/customelement.cpp index e772a0752..12371bfb9 100644 --- a/customelement.cpp +++ b/customelement.cpp @@ -1,4 +1,6 @@ #include "customelement.h" +#include "elementtextitem.h" +#include "diagram.h" /** Constructeur de la classe ElementPerso. Permet d'instancier un element @@ -150,7 +152,6 @@ CustomElement::CustomElement(QString &nom_fichier, QGraphicsItem *qgi, Diagram * // fermeture du fichier fichier.close(); - //(new QGraphicsTextItem("plop", this, scene())) -> setTextInteractionFlags(Qt::TextEditorInteraction); if (etat != NULL) *etat = 0; elmt_etat = 0; } @@ -192,6 +193,7 @@ bool CustomElement::parseElement(QDomElement &e, QPainter &qp, Diagram *s) { else if (e.tagName() == "arc") return(parseArc(e, qp)); else if (e.tagName() == "polygon") return(parsePolygon(e, qp)); else if (e.tagName() == "text") return(parseText(e, qp)); + else if (e.tagName() == "input") return(parseInput(e, s)); else return(true); // on n'est pas chiant, on ignore l'element inconnu } @@ -365,6 +367,34 @@ bool CustomElement::parseText(QDomElement &e, QPainter &qp) { return(true); } +/** + Analyse un element XML suppose representer un champ de texte editable par + l'utilisateur. Si l'analyse reussit, le champ est ajoute au dessin. + Le texte est defini par : + - une position + - une chaine de caractères facultative utilisee comme valeur par defaut + - une taille + - le fait de subir les rotations de l'element ou non + @param e L'element XML a analyser + @param s Le schema sur lequel l'element perso sera affiche + @return true si l'analyse reussit, false sinon +*/ +bool CustomElement::parseInput(QDomElement &e, Diagram *s) { + qreal pos_x, pos_y; + int size; + if ( + !attributeIsAReal(e, "x", &pos_x) ||\ + !attributeIsAReal(e, "y", &pos_y) ||\ + !attributeIsAnInteger(e, "size", &size) + ) return(false); + + ElementTextItem *eti = new ElementTextItem(e.attribute("text"), this, s); + eti -> setFont(QFont("Sans Serif", size)); + eti -> setPos(pos_x, pos_y); + if (e.attribute("rotate") == "true") eti -> setFollowParentRotations(true); + return(true); +} + /** Analyse un element XML suppose representer une borne. Si l'analyse reussit, la borne est ajoutee a l'element. diff --git a/customelement.h b/customelement.h index 4896f92aa..c34976087 100644 --- a/customelement.h +++ b/customelement.h @@ -25,6 +25,7 @@ bool parseArc(QDomElement &, QPainter &); bool parsePolygon(QDomElement &, QPainter &); bool parseText(QDomElement &, QPainter &); + bool parseInput(QDomElement &, Diagram *); bool parseTerminal(QDomElement &, Diagram *); void setQPainterAntiAliasing(QPainter &, bool); bool attributeIsAnInteger(QDomElement &, QString, int * = NULL); diff --git a/element.cpp b/element.cpp index d9cf84d77..62c04f88a 100644 --- a/element.cpp +++ b/element.cpp @@ -1,5 +1,6 @@ #include "element.h" #include "diagram.h" +#include "elementtextitem.h" #include /*** Methodes publiques ***/ @@ -122,11 +123,24 @@ bool Element::setOrientation(Terminal::Orientation o) { if (!acceptOrientation(o)) return(false); prepareGeometryChange(); // rotation en consequence et rafraichissement de l'element graphique - rotate(90.0 * (o - ori)); + qreal rotation_value = 90.0 * (o - ori); + rotate(rotation_value); ori = o; update(); foreach(QGraphicsItem *qgi, children()) { if (Terminal *p = qgraphicsitem_cast(qgi)) p -> updateConducer(); + else if (ElementTextItem *eti = qgraphicsitem_cast(qgi)) { + // applique une rotation contraire si besoin + if (!eti -> followParentRotations()) { + QMatrix new_matrix = eti -> matrix(); + qreal dx = eti -> boundingRect().width() / 2.0; + qreal dy = eti -> boundingRect().height() / 2.0; + new_matrix.translate(dx, dy); + new_matrix.rotate(-rotation_value); + new_matrix.translate(-dx, -dy); + eti -> setMatrix(new_matrix); + } + } } return(true); } diff --git a/elementtextitem.cpp b/elementtextitem.cpp new file mode 100644 index 000000000..ddb716b99 --- /dev/null +++ b/elementtextitem.cpp @@ -0,0 +1,27 @@ +#include "elementtextitem.h" + +ElementTextItem::ElementTextItem(QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(parent, scene) { + follow_parent_rotations = false; + setTextInteractionFlags(Qt::TextEditorInteraction); +} + +ElementTextItem::ElementTextItem(const QString &text, QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsTextItem(text, parent, scene) { + follow_parent_rotations = false; + setTextInteractionFlags(Qt::TextEditorInteraction); +} + +void ElementTextItem::setPos(const QPointF &pos) { + QPointF actual_pos = pos; + actual_pos -= QPointF(0.0, boundingRect().height() / 2.0); + QGraphicsItem::setPos(actual_pos); +} + +void ElementTextItem::setPos(qreal x, qreal y) { + setPos(QPointF(x, y)); +} + +QPointF ElementTextItem::pos() const { + QPointF actual_pos = pos(); + actual_pos += QPointF(0.0, boundingRect().height() / 2.0); + return(actual_pos); +} diff --git a/elementtextitem.h b/elementtextitem.h new file mode 100644 index 000000000..56e197d66 --- /dev/null +++ b/elementtextitem.h @@ -0,0 +1,24 @@ +#ifndef ELEMENT_TEXT_ITEM_H + #define ELEMENT_TEXT_ITEM_H + #include + class ElementTextItem : public QGraphicsTextItem { + // constructeurs + public: + ElementTextItem(QGraphicsItem * = 0, QGraphicsScene * = 0); + ElementTextItem(const QString &, QGraphicsItem * = 0, QGraphicsScene * = 0); + + // attributs + private: + bool follow_parent_rotations; + + // methodes + public: + enum { Type = UserType + 1003 }; + virtual int type() const { return Type; } + inline bool followParentRotations() const { return(follow_parent_rotations); } + inline void setFollowParentRotations(bool frp) { follow_parent_rotations = frp; } + void setPos(const QPointF &); + void setPos(qreal, qreal); + QPointF pos() const; + }; +#endif diff --git a/qelectrotech.pro b/qelectrotech.pro index 94be790bf..c0036c57a 100644 --- a/qelectrotech.pro +++ b/qelectrotech.pro @@ -20,7 +20,8 @@ HEADERS += aboutqet.h \ fixedelement.h \ qetapp.h \ terminal.h \ - conducersegment.h + conducersegment.h \ + elementtextitem.h SOURCES += aboutqet.cpp \ borderinset.cpp \ conducer.cpp \ @@ -34,7 +35,8 @@ SOURCES += aboutqet.cpp \ main.cpp \ qetapp.cpp \ terminal.cpp \ - conducersegment.cpp + conducersegment.cpp \ + elementtextitem.cpp RESOURCES += qelectrotech.qrc TRANSLATIONS += lang/qet_en.ts lang/qt_fr.ts QT += xml