2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2010-01-03 16:25:37 +00:00
|
|
|
Copyright 2006-2010 Xavier Guerrin
|
2007-12-01 10:47:15 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
#include "partterminal.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
#include "terminaleditor.h"
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param editor L'editeur d'element concerne
|
|
|
|
@param parent Le QGraphicsItem parent de cette borne
|
|
|
|
@param scene La scene sur laquelle figure cette borne
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
PartTerminal::PartTerminal(QETElementEditor *editor, QGraphicsItem *parent, QGraphicsScene *scene) :
|
|
|
|
CustomElementPart(editor),
|
2007-06-30 17:41:07 +00:00
|
|
|
QGraphicsItem(parent, scene),
|
|
|
|
_orientation(QET::North)
|
|
|
|
{
|
2007-08-25 03:43:05 +00:00
|
|
|
informations = new TerminalEditor(elementEditor(), this);
|
2007-11-07 20:23:24 +00:00
|
|
|
informations -> setElementTypeName(name());
|
2007-06-30 17:41:07 +00:00
|
|
|
updateSecondPoint();
|
|
|
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
2010-01-20 00:16:55 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
|
|
|
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
#endif
|
2007-06-30 17:41:07 +00:00
|
|
|
setZValue(100000);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/// Destructeur
|
2007-06-30 17:41:07 +00:00
|
|
|
PartTerminal::~PartTerminal() {
|
2009-04-03 19:30:25 +00:00
|
|
|
if (informations -> parentWidget()) return; // le widget sera supprime par son parent
|
2007-06-30 17:41:07 +00:00
|
|
|
delete informations;
|
|
|
|
};
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Importe les proprietes d'une borne depuis un element XML
|
|
|
|
@param xml_elmt Element XML a lire
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
void PartTerminal::fromXml(const QDomElement &xml_elmt) {
|
|
|
|
// lit la position de la borne
|
|
|
|
qreal term_x = 0.0, term_y = 0.0;
|
|
|
|
QET::attributeIsAReal(xml_elmt, "x", &term_x);
|
|
|
|
QET::attributeIsAReal(xml_elmt, "y", &term_y);
|
|
|
|
setPos(QPointF(term_x, term_y));
|
|
|
|
|
|
|
|
// lit l'orientation de la borne
|
|
|
|
_orientation = QET::orientationFromString(xml_elmt.attribute("orientation"));
|
|
|
|
updateSecondPoint();
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Exporte la borne en XML
|
|
|
|
@param xml_document Document XML a utiliser pour creer l'element XML
|
|
|
|
@return un element XML decrivant la borne
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
const QDomElement PartTerminal::toXml(QDomDocument &xml_document) const {
|
|
|
|
QDomElement xml_element = xml_document.createElement("terminal");
|
|
|
|
|
|
|
|
// ecrit la position de la borne
|
2007-08-18 22:59:10 +00:00
|
|
|
xml_element.setAttribute("x", QString("%1").arg(scenePos().x()));
|
|
|
|
xml_element.setAttribute("y", QString("%1").arg(scenePos().y()));
|
2007-06-30 17:41:07 +00:00
|
|
|
|
|
|
|
// ecrit l'orientation de la borne
|
|
|
|
xml_element.setAttribute("orientation", orientationToString(_orientation));
|
|
|
|
|
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return Le widget permettant d'editer cette borne
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QWidget *PartTerminal::elementInformations() {
|
|
|
|
return(informations);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Dessine la borne
|
2009-11-22 16:12:22 +00:00
|
|
|
@param p QPainter a utiliser pour rendre le dessin
|
2007-12-05 21:16:01 +00:00
|
|
|
@param options Options pour affiner le rendu
|
|
|
|
@param widget Widget sur lequel le rendu est effectue
|
|
|
|
*/
|
2009-11-22 16:12:22 +00:00
|
|
|
void PartTerminal::paint(QPainter *p, const QStyleOptionGraphicsItem *options, QWidget *widget) {
|
|
|
|
Q_UNUSED(widget);
|
2007-06-30 17:41:07 +00:00
|
|
|
p -> save();
|
|
|
|
|
|
|
|
// annulation des renderhints
|
|
|
|
p -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
p -> setRenderHint(QPainter::TextAntialiasing, false);
|
|
|
|
p -> setRenderHint(QPainter::SmoothPixmapTransform, false);
|
|
|
|
|
|
|
|
QPen t;
|
|
|
|
t.setWidthF(1.0);
|
2009-04-18 18:54:25 +00:00
|
|
|
t.setCosmetic(options && options -> levelOfDetail < 1.0);
|
2007-06-30 17:41:07 +00:00
|
|
|
|
|
|
|
// dessin de la borne en rouge
|
2007-11-07 20:23:24 +00:00
|
|
|
t.setColor(isSelected() ? Terminal::couleur_neutre : Qt::red);
|
2007-06-30 17:41:07 +00:00
|
|
|
p -> setPen(t);
|
|
|
|
p -> drawLine(QPointF(0.0, 0.0), second_point);
|
|
|
|
|
|
|
|
// dessin du point d'amarrage au conducteur en bleu
|
2007-11-07 20:23:24 +00:00
|
|
|
t.setColor(isSelected() ? Qt::red : Terminal::couleur_neutre);
|
2007-06-30 17:41:07 +00:00
|
|
|
p -> setPen(t);
|
|
|
|
p -> setBrush(Terminal::couleur_neutre);
|
|
|
|
p -> drawPoint(QPointF(0.0, 0.0));
|
|
|
|
p -> restore();
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le rectangle delimitant cette partie.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QRectF PartTerminal::boundingRect() const {
|
|
|
|
QPointF p1, p2;
|
|
|
|
if (second_point.x() <= 0.0 && second_point.y() <= 0.0) {
|
|
|
|
p1 = second_point;
|
|
|
|
p2 = QPointF(0.0, 0.0);
|
|
|
|
} else {
|
|
|
|
p1 = QPointF(0.0, 0.0);
|
|
|
|
p2 = second_point;
|
|
|
|
}
|
|
|
|
QRectF br;
|
|
|
|
br.setTopLeft (p1 - QPointF(2.0, 2.0));
|
|
|
|
br.setBottomRight(p2 + QPointF(2.0, 2.0));
|
|
|
|
return(br);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return L'orientation de la borne
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QET::Orientation PartTerminal::orientation() const {
|
|
|
|
return(_orientation);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Definit l'orientation de la borne
|
|
|
|
@param ori la nouvelle orientation de la borne
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
void PartTerminal::setOrientation(QET::Orientation ori) {
|
|
|
|
prepareGeometryChange();
|
|
|
|
_orientation = ori;
|
|
|
|
updateSecondPoint();
|
|
|
|
informations -> updateForm();
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la valeur d'une propriete donnee de la borne
|
|
|
|
@param property propriete a modifier. Valeurs acceptees :
|
|
|
|
* x : abscisse de la borne
|
|
|
|
* y : ordonnee de la borne
|
|
|
|
* orientation : orientation de la borne
|
|
|
|
@param value Valeur a attribuer a la propriete
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
void PartTerminal::setProperty(const QString &property, const QVariant &value) {
|
|
|
|
if (property == "x") {
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
setPos(value.toDouble(), pos().y());
|
|
|
|
} else if (property == "y") {
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
setPos(pos().x(), value.toDouble());
|
|
|
|
} else if (property == "orientation") {
|
|
|
|
if (!value.canConvert(QVariant::Int)) return;
|
|
|
|
setOrientation(static_cast<QET::Orientation>(value.toInt()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Permet d'acceder a la valeur d'une propriete donnee de la borne
|
|
|
|
@param property propriete lue. Valeurs acceptees :
|
|
|
|
* x : abscisse de la borne
|
|
|
|
* y : ordonnee de la borne
|
|
|
|
* orientation : orientation de la borne
|
|
|
|
@return La valeur de la propriete property
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
QVariant PartTerminal::property(const QString &property) {
|
|
|
|
if (property == "x") {
|
|
|
|
return(scenePos().x());
|
|
|
|
} else if (property == "y") {
|
|
|
|
return(scenePos().y());
|
|
|
|
} else if (property == "orientation") {
|
|
|
|
return(_orientation);
|
|
|
|
}
|
|
|
|
return(QVariant());
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Gere les changements intervenant sur cette partie
|
|
|
|
@param change Type de changement
|
|
|
|
@param value Valeur numerique relative au changement
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QVariant PartTerminal::itemChange(GraphicsItemChange change, const QVariant &value) {
|
|
|
|
if (scene()) {
|
|
|
|
if (change == QGraphicsItem::ItemPositionChange || change == QGraphicsItem::ItemSelectedChange) {
|
|
|
|
informations -> updateForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(QGraphicsItem::itemChange(change, value));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Met a jour la position du second point en fonction de la position et de
|
|
|
|
l'orientation de la borne.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
void PartTerminal::updateSecondPoint() {
|
|
|
|
qreal ts = 4.0; // terminal size
|
|
|
|
switch(_orientation) {
|
|
|
|
case QET::North: second_point = QPointF(0.0, ts); break;
|
|
|
|
case QET::East : second_point = QPointF(-ts, 0.0); break;
|
|
|
|
case QET::South: second_point = QPointF(0.0, -ts); break;
|
|
|
|
case QET::West : second_point = QPointF(ts, 0.0); break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-15 21:57:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
@return true si cette partie n'est pas pertinente et ne merite pas d'etre
|
|
|
|
conservee / enregistree.
|
|
|
|
Une borne est toujours pertinente ; cette fonction renvoie donc
|
|
|
|
toujours false
|
|
|
|
*/
|
|
|
|
bool PartTerminal::isUseless() const {
|
|
|
|
return(false);
|
|
|
|
}
|