2007-12-01 10:47:15 +00:00
/*
2008-02-06 19:40:45 +00:00
Copyright 2006 - 2008 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-01-29 20:14:26 +00:00
# include "terminal.h"
2007-01-29 00:41:12 +00:00
# include "diagram.h"
2006-10-27 15:47:22 +00:00
# include "element.h"
2007-10-03 17:02:39 +00:00
# include "conductor.h"
2007-09-25 23:24:36 +00:00
# include "diagramcommands.h"
2008-02-25 18:28:19 +00:00
# include "qetapp.h"
2006-10-27 15:47:22 +00:00
2007-06-30 17:41:07 +00:00
QColor Terminal : : couleur_neutre = QColor ( Qt : : blue ) ;
QColor Terminal : : couleur_autorise = QColor ( Qt : : darkGreen ) ;
QColor Terminal : : couleur_prudence = QColor ( " #ff8000 " ) ;
QColor Terminal : : couleur_interdit = QColor ( Qt : : red ) ;
2007-10-10 22:35:32 +00:00
const qreal Terminal : : terminalSize = 4.0 ;
2007-06-30 17:41:07 +00:00
2006-10-27 15:47:22 +00:00
/**
Fonction privee pour initialiser la borne .
@ param pf position du point d ' amarrage pour un conducteur
@ param o orientation de la borne : Qt : : Horizontal ou Qt : : Vertical
*/
2007-06-30 17:41:07 +00:00
void Terminal : : initialise ( QPointF pf , QET : : Orientation o ) {
2006-10-27 15:47:22 +00:00
// definition du pount d'amarrage pour un conducteur
2007-10-03 17:02:39 +00:00
amarrage_conductor = pf ;
2006-10-27 15:47:22 +00:00
2007-01-29 20:14:26 +00:00
// definition de l'orientation de la terminal (par defaut : sud)
2007-06-30 17:41:07 +00:00
if ( o < QET : : North | | o > QET : : West ) sens = QET : : South ;
2006-10-27 15:47:22 +00:00
else sens = o ;
// calcul de la position du point d'amarrage a l'element
2007-10-03 17:02:39 +00:00
amarrage_elmt = amarrage_conductor ;
2006-10-27 15:47:22 +00:00
switch ( sens ) {
2007-10-10 22:35:32 +00:00
case QET : : North : amarrage_elmt + = QPointF ( 0 , Terminal : : terminalSize ) ; break ;
case QET : : East : amarrage_elmt + = QPointF ( - Terminal : : terminalSize , 0 ) ; break ;
case QET : : West : amarrage_elmt + = QPointF ( Terminal : : terminalSize , 0 ) ; break ;
2007-06-30 17:41:07 +00:00
case QET : : South :
2007-10-10 22:35:32 +00:00
default : amarrage_elmt + = QPointF ( 0 , - Terminal : : terminalSize ) ;
2006-10-27 15:47:22 +00:00
}
// par defaut : pas de conducteur
// QRectF null
br = new QRectF ( ) ;
2007-01-29 20:14:26 +00:00
terminal_precedente = NULL ;
2006-10-27 15:47:22 +00:00
// divers
setAcceptsHoverEvents ( true ) ;
setAcceptedMouseButtons ( Qt : : LeftButton ) ;
hovered = false ;
2007-09-15 22:14:23 +00:00
setToolTip ( QObject : : tr ( " Borne " ) ) ;
2006-10-27 15:47:22 +00:00
}
/**
Constructeur par defaut
*/
2007-06-30 17:41:07 +00:00
Terminal : : Terminal ( ) :
QGraphicsItem ( 0 , 0 ) ,
couleur_hovered ( Terminal : : couleur_neutre )
{
initialise ( QPointF ( 0.0 , 0.0 ) , QET : : South ) ;
2006-10-27 15:47:22 +00:00
}
/**
initialise une borne
@ param pf position du point d ' amarrage pour un conducteur
@ param o orientation de la borne : Qt : : Horizontal ou Qt : : Vertical
@ param e Element auquel cette borne appartient
@ param s Scene sur laquelle figure cette borne
*/
2007-06-30 17:41:07 +00:00
Terminal : : Terminal ( QPointF pf , QET : : Orientation o , Element * e , Diagram * s ) :
QGraphicsItem ( e , s ) ,
couleur_hovered ( Terminal : : couleur_neutre )
{
2006-10-27 15:47:22 +00:00
initialise ( pf , o ) ;
}
/**
initialise une borne
@ param pf_x Abscisse du point d ' amarrage pour un conducteur
@ param pf_y Ordonnee du point d ' amarrage pour un conducteur
@ param o orientation de la borne : Qt : : Horizontal ou Qt : : Vertical
@ param e Element auquel cette borne appartient
@ param s Scene sur laquelle figure cette borne
*/
2007-06-30 17:41:07 +00:00
Terminal : : Terminal ( qreal pf_x , qreal pf_y , QET : : Orientation o , Element * e , Diagram * s ) :
QGraphicsItem ( e , s ) ,
couleur_hovered ( Terminal : : couleur_neutre )
{
2006-10-27 15:47:22 +00:00
initialise ( QPointF ( pf_x , pf_y ) , o ) ;
}
/**
Destructeur
2007-04-12 03:13:13 +00:00
La destruction de la borne entraine la destruction des conducteurs
associes .
2006-10-27 15:47:22 +00:00
*/
2007-01-29 20:14:26 +00:00
Terminal : : ~ Terminal ( ) {
2007-04-12 03:13:13 +00:00
//qDebug() << "Terminal::~Terminal" << (void *)this;
2007-10-03 17:02:39 +00:00
foreach ( Conductor * c , liste_conductors ) delete c ;
2006-10-27 15:47:22 +00:00
delete br ;
}
/**
Permet de connaitre l ' orientation de la borne . Si le parent de la borne
est bien un Element , cette fonction renvoie l ' orientation par rapport a
la scene de la borne , en tenant compte du fait que l ' element ait pu etre
pivote . Sinon elle renvoie son sens normal .
2007-01-29 20:14:26 +00:00
@ return L ' orientation actuelle de la Terminal .
2006-10-27 15:47:22 +00:00
*/
2007-06-30 17:41:07 +00:00
QET : : Orientation Terminal : : orientation ( ) const {
2006-10-27 15:47:22 +00:00
if ( Element * elt = qgraphicsitem_cast < Element * > ( parentItem ( ) ) ) {
2006-11-09 19:19:51 +00:00
// orientations actuelle et par defaut de l'element
2007-06-30 17:41:07 +00:00
QET : : Orientation ori_cur = elt - > orientation ( ) . current ( ) ;
QET : : Orientation ori_def = elt - > orientation ( ) . defaultOrientation ( ) ;
2006-11-09 19:19:51 +00:00
if ( ori_cur = = ori_def ) return ( sens ) ;
2006-10-27 15:47:22 +00:00
else {
2007-01-20 18:11:42 +00:00
// calcul l'angle de rotation implique par l'orientation de l'element parent
2006-11-09 19:19:51 +00:00
// angle de rotation de la borne sur la scene, divise par 90
int angle = ori_cur - ori_def + sens ;
while ( angle > = 4 ) angle - = 4 ;
2007-06-30 17:41:07 +00:00
return ( ( QET : : Orientation ) angle ) ;
2006-10-27 15:47:22 +00:00
}
} else return ( sens ) ;
}
/**
2007-10-03 17:02:39 +00:00
Attribue un conductor a la borne
2006-10-27 15:47:22 +00:00
@ param f Le conducteur a rattacher a cette borne
*/
2007-10-03 17:02:39 +00:00
bool Terminal : : addConductor ( Conductor * f ) {
2006-10-27 15:47:22 +00:00
// pointeur 0 refuse
if ( ! f ) return ( false ) ;
// une seule des deux bornes du conducteur doit etre this
2007-10-03 17:02:39 +00:00
Q_ASSERT_X ( ( f - > terminal1 = = this ^ f - > terminal2 = = this ) , " Terminal::addConductor " , " Le conductor devrait etre relie exactement une fois a la terminal en cours " ) ;
2006-10-27 15:47:22 +00:00
// determine l'autre borne a laquelle cette borne va etre relie grace au conducteur
2007-01-29 20:14:26 +00:00
Terminal * autre_terminal = ( f - > terminal1 = = this ) ? f - > terminal2 : f - > terminal1 ;
2006-10-27 15:47:22 +00:00
// verifie que la borne n'est pas deja reliee avec l'autre borne
bool deja_liees = false ;
2007-10-03 17:02:39 +00:00
foreach ( Conductor * conductor , liste_conductors ) {
if ( conductor - > terminal1 = = autre_terminal | | conductor - > terminal2 = = autre_terminal ) deja_liees = true ;
2006-10-27 15:47:22 +00:00
}
// si les deux bornes sont deja reliees, on refuse d'ajouter le conducteur
if ( deja_liees ) return ( false ) ;
// sinon on ajoute le conducteur
2007-10-03 17:02:39 +00:00
liste_conductors . append ( f ) ;
2006-10-27 15:47:22 +00:00
return ( true ) ;
}
2007-04-09 02:56:47 +00:00
/**
Enleve un conducteur donne a la borne
@ param f Conducteur a enlever
*/
2007-10-03 17:02:39 +00:00
void Terminal : : removeConductor ( Conductor * f ) {
//qDebug() << "Terminal::removeConductor" << (void *)this;
int index = liste_conductors . indexOf ( f ) ;
2006-10-27 15:47:22 +00:00
if ( index = = - 1 ) return ;
2007-10-03 17:02:39 +00:00
liste_conductors . removeAt ( index ) ;
2006-10-27 15:47:22 +00:00
}
/**
Fonction de dessin des bornes
@ param p Le QPainter a utiliser
@ param options Les options de dessin
@ param widget Le widget sur lequel on dessine
*/
2007-10-11 12:34:53 +00:00
void Terminal : : paint ( QPainter * p , const QStyleOptionGraphicsItem * options , QWidget * widget ) {
2006-10-27 15:47:22 +00:00
p - > save ( ) ;
2007-10-11 12:34:53 +00:00
# ifdef Q_WS_X11
// corrige un bug de rendu ne se produisant que lors du rendu sur QGraphicsScene sous X11 au zoom par defaut
2008-02-25 18:28:19 +00:00
static bool must_correct_rendering_bug = QETApp : : settings ( ) . value ( " correct-rendering " , true ) . toBool ( ) ;
2007-10-11 12:34:53 +00:00
if ( must_correct_rendering_bug ) {
Diagram * dia = diagram ( ) ;
if ( dia & & options - > levelOfDetail = = 1.0 & & widget ) {
// calcule la rotation qu'a subi l'element
qreal applied_rotation = 0.0 ;
if ( Element * elt = qgraphicsitem_cast < Element * > ( parentItem ( ) ) ) {
// orientations actuelle et par defaut de l'element
QET : : Orientation ori_cur = elt - > orientation ( ) . current ( ) ;
QET : : Orientation ori_def = elt - > orientation ( ) . defaultOrientation ( ) ;
applied_rotation = 90.0 * ( ori_cur - ori_def ) ;
while ( applied_rotation < 360.0 ) applied_rotation + = 360.0 ;
while ( applied_rotation > 360.0 ) applied_rotation - = 360.0 ;
}
if ( applied_rotation = = 90.0 ) p - > translate ( 1.0 , - 1.0 ) ;
else if ( applied_rotation = = 180.0 ) p - > translate ( - 1.0 , - 1.0 ) ;
else if ( applied_rotation = = 270.0 ) p - > translate ( - 1.0 , 1.0 ) ;
}
}
# endif
2006-10-27 15:47:22 +00:00
//annulation des renderhints
p - > setRenderHint ( QPainter : : Antialiasing , false ) ;
p - > setRenderHint ( QPainter : : TextAntialiasing , false ) ;
p - > setRenderHint ( QPainter : : SmoothPixmapTransform , false ) ;
// on travaille avec les coordonnees de l'element parent
2007-10-03 17:02:39 +00:00
QPointF f = mapFromParent ( amarrage_conductor ) ;
2006-10-27 15:47:22 +00:00
QPointF e = mapFromParent ( amarrage_elmt ) ;
QPen t ;
t . setWidthF ( 1.0 ) ;
// dessin de la borne en rouge
t . setColor ( Qt : : red ) ;
p - > setPen ( t ) ;
p - > drawLine ( f , e ) ;
// dessin du point d'amarrage au conducteur en bleu
t . setColor ( couleur_hovered ) ;
p - > setPen ( t ) ;
p - > setBrush ( couleur_hovered ) ;
2007-02-14 01:08:29 +00:00
if ( hovered ) {
p - > setRenderHint ( QPainter : : Antialiasing , true ) ;
2007-10-04 14:30:52 +00:00
p - > drawEllipse ( QRectF ( f . x ( ) - 2.5 , f . y ( ) - 2.5 , 5.0 , 5.0 ) ) ;
2007-02-14 01:08:29 +00:00
} else p - > drawPoint ( f ) ;
2006-10-27 15:47:22 +00:00
p - > restore ( ) ;
}
/**
@ return Le rectangle ( en precision flottante ) delimitant la borne et ses alentours .
*/
2007-01-29 20:14:26 +00:00
QRectF Terminal : : boundingRect ( ) const {
2006-10-27 15:47:22 +00:00
if ( br - > isNull ( ) ) {
2007-10-03 17:02:39 +00:00
qreal afx = amarrage_conductor . x ( ) ;
qreal afy = amarrage_conductor . y ( ) ;
2006-10-27 15:47:22 +00:00
qreal aex = amarrage_elmt . x ( ) ;
qreal aey = amarrage_elmt . y ( ) ;
QPointF origine ;
2007-10-03 17:02:39 +00:00
origine = ( afx < = aex & & afy < = aey ? amarrage_conductor : amarrage_elmt ) ;
2006-10-27 15:47:22 +00:00
origine + = QPointF ( - 3.0 , - 3.0 ) ;
qreal w = qAbs ( ( int ) ( afx - aex ) ) + 7 ;
qreal h = qAbs ( ( int ) ( afy - aey ) ) + 7 ;
* br = QRectF ( origine , QSizeF ( w , h ) ) ;
}
return ( * br ) ;
}
/**
Gere l ' entree de la souris sur la zone de la Borne .
*/
2007-01-29 20:14:26 +00:00
void Terminal : : hoverEnterEvent ( QGraphicsSceneHoverEvent * ) {
2006-10-27 15:47:22 +00:00
hovered = true ;
update ( ) ;
}
/**
Gere les mouvements de la souris sur la zone de la Borne .
*/
2007-01-29 20:14:26 +00:00
void Terminal : : hoverMoveEvent ( QGraphicsSceneHoverEvent * ) {
2006-10-27 15:47:22 +00:00
}
/**
Gere le fait que la souris sorte de la zone de la Borne .
*/
2007-01-29 20:14:26 +00:00
void Terminal : : hoverLeaveEvent ( QGraphicsSceneHoverEvent * ) {
2006-10-27 15:47:22 +00:00
hovered = false ;
update ( ) ;
}
/**
Gere le fait qu ' on enfonce un bouton de la souris sur la Borne .
@ param e L ' evenement souris correspondant
*/
2007-01-29 20:14:26 +00:00
void Terminal : : mousePressEvent ( QGraphicsSceneMouseEvent * e ) {
2007-09-25 23:24:36 +00:00
if ( Diagram * s = diagram ( ) ) {
2007-10-03 17:02:39 +00:00
s - > setConductorStart ( mapToScene ( QPointF ( amarrage_conductor ) ) ) ;
s - > setConductorStop ( e - > scenePos ( ) ) ;
s - > setConductor ( true ) ;
2007-04-15 19:22:54 +00:00
//setCursor(Qt::CrossCursor);
2006-10-27 15:47:22 +00:00
}
}
/**
Gere le fait qu ' on bouge la souris sur la Borne .
@ param e L ' evenement souris correspondant
*/
2007-01-29 20:14:26 +00:00
void Terminal : : mouseMoveEvent ( QGraphicsSceneMouseEvent * e ) {
2006-10-27 15:47:22 +00:00
// pendant la pose d'un conducteur, on adopte un autre curseur
2007-04-15 19:22:54 +00:00
//setCursor(Qt::CrossCursor);
2006-10-27 15:47:22 +00:00
// d'un mouvement a l'autre, il faut retirer l'effet hover de la borne precedente
2007-01-29 20:14:26 +00:00
if ( terminal_precedente ! = NULL ) {
if ( terminal_precedente = = this ) hovered = true ;
else terminal_precedente - > hovered = false ;
terminal_precedente - > couleur_hovered = terminal_precedente - > couleur_neutre ;
terminal_precedente - > update ( ) ;
2006-10-27 15:47:22 +00:00
}
2007-09-25 23:24:36 +00:00
Diagram * s = diagram ( ) ;
if ( ! s ) return ;
2007-01-29 00:41:12 +00:00
// si la scene est un Diagram, on actualise le poseur de conducteur
2007-10-03 17:02:39 +00:00
s - > setConductorStop ( e - > scenePos ( ) ) ;
2006-10-27 15:47:22 +00:00
// on recupere la liste des qgi sous le pointeur
2007-09-25 23:24:36 +00:00
QList < QGraphicsItem * > qgis = s - > items ( e - > scenePos ( ) ) ;
2006-10-27 15:47:22 +00:00
/* le qgi le plus haut
2007-10-03 17:02:39 +00:00
= le poseur de conductor
2006-10-27 15:47:22 +00:00
= le premier element de la liste
= la liste ne peut etre vide
= on prend le deuxieme element de la liste
*/
2007-01-29 20:14:26 +00:00
Q_ASSERT_X ( ! ( qgis . isEmpty ( ) ) , " Terminal::mouseMoveEvent " , " La liste d'items ne devrait pas etre vide " ) ;
2006-10-27 15:47:22 +00:00
// s'il y a autre chose que le poseur de conducteur dans la liste
if ( qgis . size ( ) > 1 ) {
// on prend le deuxieme element de la liste
QGraphicsItem * qgi = qgis . at ( 1 ) ;
// si le qgi est une borne...
2007-01-29 20:14:26 +00:00
if ( Terminal * p = qgraphicsitem_cast < Terminal * > ( qgi ) ) {
2006-10-27 15:47:22 +00:00
// ...on lui applique l'effet hover approprie
if ( p = = this ) {
// effet si l'on hover sur la borne de depart
couleur_hovered = couleur_interdit ;
} else if ( p - > parentItem ( ) = = parentItem ( ) ) {
// effet si l'on hover sur une borne du meme appareil
2007-12-09 10:30:35 +00:00
if ( ( ( Element * ) parentItem ( ) ) - > internalConnections ( ) )
2006-10-27 15:47:22 +00:00
p - > couleur_hovered = p - > couleur_autorise ;
else p - > couleur_hovered = p - > couleur_interdit ;
2007-10-03 17:02:39 +00:00
} else if ( p - > nbConductors ( ) ) {
2006-10-27 15:47:22 +00:00
// si la borne a deja un conducteur
// verifie que cette borne n'est pas deja reliee a l'autre borne
bool deja_reliee = false ;
2007-10-03 17:02:39 +00:00
foreach ( Conductor * f , liste_conductors ) {
2007-01-29 20:14:26 +00:00
if ( f - > terminal1 = = p | | f - > terminal2 = = p ) {
2006-10-27 15:47:22 +00:00
deja_reliee = true ;
break ;
}
}
// interdit si les bornes sont deja reliees, prudence sinon
p - > couleur_hovered = deja_reliee ? p - > couleur_interdit : p - > couleur_prudence ;
} else {
// effet si on peut poser le conducteur
p - > couleur_hovered = p - > couleur_autorise ;
}
2007-01-29 20:14:26 +00:00
terminal_precedente = p ;
2006-10-27 15:47:22 +00:00
p - > hovered = true ;
p - > update ( ) ;
}
}
}
/**
Gere le fait qu ' on relache la souris sur la Borne .
@ param e L ' evenement souris correspondant
*/
2007-01-29 20:14:26 +00:00
void Terminal : : mouseReleaseEvent ( QGraphicsSceneMouseEvent * e ) {
2007-04-15 19:22:54 +00:00
//setCursor(Qt::ArrowCursor);
2007-01-29 20:14:26 +00:00
terminal_precedente = NULL ;
2006-10-27 15:47:22 +00:00
couleur_hovered = couleur_neutre ;
2007-01-29 00:41:12 +00:00
// verifie que la scene est bien un Diagram
2007-09-25 23:24:36 +00:00
if ( Diagram * s = diagram ( ) ) {
2006-10-27 15:47:22 +00:00
// on arrete de dessiner l'apercu du conducteur
2007-10-03 17:02:39 +00:00
s - > setConductor ( false ) ;
2006-10-27 15:47:22 +00:00
// on recupere l'element sous le pointeur lors du MouseReleaseEvent
QGraphicsItem * qgi = s - > itemAt ( e - > scenePos ( ) ) ;
// s'il n'y a rien, on arrete la
if ( ! qgi ) return ;
// idem si l'element obtenu n'est pas une borne
2007-01-29 20:14:26 +00:00
Terminal * p = qgraphicsitem_cast < Terminal * > ( qgi ) ;
2006-10-27 15:47:22 +00:00
if ( ! p ) return ;
// on remet la couleur de hover a sa valeur par defaut
p - > couleur_hovered = p - > couleur_neutre ;
// idem s'il s'agit de la borne actuelle
if ( p = = this ) return ;
// idem s'il s'agit d'une borne de l'element actuel et que l'element n'a pas le droit de relier ses propres bornes
2007-12-09 10:30:35 +00:00
bool cia = ( ( Element * ) parentItem ( ) ) - > internalConnections ( ) ;
2006-10-27 15:47:22 +00:00
if ( ! cia ) foreach ( QGraphicsItem * item , parentItem ( ) - > children ( ) ) if ( item = = p ) return ;
// derniere verification : verifier que cette borne n'est pas deja reliee a l'autre borne
2007-10-03 17:02:39 +00:00
foreach ( Conductor * f , liste_conductors ) if ( f - > terminal1 = = p | | f - > terminal2 = = p ) return ;
2006-10-27 15:47:22 +00:00
// autrement, on pose un conducteur
2007-10-14 15:16:37 +00:00
Conductor * new_conductor = new Conductor ( this , p ) ;
new_conductor - > setProperties ( s - > defaultConductorProperties ) ;
s - > undoStack ( ) . push ( new AddConductorCommand ( s , new_conductor ) ) ;
2006-10-27 15:47:22 +00:00
}
}
/**
2007-01-29 20:14:26 +00:00
Met a jour l ' eventuel conducteur relie a la Terminal .
2006-11-30 18:05:02 +00:00
@ param newpos Position de l ' element parent a prendre en compte
2006-10-27 15:47:22 +00:00
*/
2007-10-03 17:02:39 +00:00
void Terminal : : updateConductor ( QPointF newpos ) {
2006-11-30 18:05:02 +00:00
if ( ! scene ( ) | | ! parentItem ( ) ) return ;
2007-10-03 17:02:39 +00:00
foreach ( Conductor * conductor , liste_conductors ) {
if ( conductor - > isDestroyed ( ) ) continue ;
if ( newpos = = QPointF ( ) ) conductor - > update ( QRectF ( ) ) ;
2006-11-30 18:05:02 +00:00
else {
// determine la translation subie par l'element parent
QPointF translation = newpos - parentItem ( ) - > pos ( ) ;
// rafraichit le conducteur en tenant compte de la translation
2007-10-03 17:02:39 +00:00
conductor - > updateWithNewPos ( QRectF ( ) , this , amarrageConductor ( ) + translation ) ;
2006-11-30 18:05:02 +00:00
}
2006-10-27 15:47:22 +00:00
}
}
/**
@ return La liste des conducteurs lies a cette borne
*/
2007-10-03 17:02:39 +00:00
QList < Conductor * > Terminal : : conductors ( ) const {
return ( liste_conductors ) ;
2006-10-27 15:47:22 +00:00
}
/**
Methode d ' export en XML
@ param doc Le Document XML a utiliser pour creer l ' element XML
@ return un QDomElement representant cette borne
*/
2007-01-29 20:14:26 +00:00
QDomElement Terminal : : toXml ( QDomDocument & doc ) const {
2007-03-09 19:18:55 +00:00
QDomElement qdo = doc . createElement ( " terminal " ) ;
2006-10-27 15:47:22 +00:00
qdo . setAttribute ( " x " , amarrage_elmt . x ( ) ) ;
qdo . setAttribute ( " y " , amarrage_elmt . y ( ) ) ;
qdo . setAttribute ( " orientation " , sens ) ;
return ( qdo ) ;
}
/**
Permet de savoir si un element XML represente une borne
2007-10-21 16:10:21 +00:00
@ param terminal Le QDomElement a analyser
2006-10-27 15:47:22 +00:00
@ return true si le QDomElement passe en parametre est une borne , false sinon
*/
2007-01-29 20:14:26 +00:00
bool Terminal : : valideXml ( QDomElement & terminal ) {
2006-10-27 15:47:22 +00:00
// verifie le nom du tag
2007-03-09 19:18:55 +00:00
if ( terminal . tagName ( ) ! = " terminal " ) return ( false ) ;
2006-10-27 15:47:22 +00:00
// verifie la presence des attributs minimaux
2007-01-29 20:14:26 +00:00
if ( ! terminal . hasAttribute ( " x " ) ) return ( false ) ;
if ( ! terminal . hasAttribute ( " y " ) ) return ( false ) ;
if ( ! terminal . hasAttribute ( " orientation " ) ) return ( false ) ;
2006-10-27 15:47:22 +00:00
bool conv_ok ;
// parse l'abscisse
2007-01-29 20:14:26 +00:00
terminal . attribute ( " x " ) . toDouble ( & conv_ok ) ;
2006-10-27 15:47:22 +00:00
if ( ! conv_ok ) return ( false ) ;
// parse l'ordonnee
2007-01-29 20:14:26 +00:00
terminal . attribute ( " y " ) . toDouble ( & conv_ok ) ;
2006-10-27 15:47:22 +00:00
if ( ! conv_ok ) return ( false ) ;
// parse l'id
2007-01-29 20:14:26 +00:00
terminal . attribute ( " id " ) . toInt ( & conv_ok ) ;
2006-10-27 15:47:22 +00:00
if ( ! conv_ok ) return ( false ) ;
// parse l'orientation
2007-01-29 20:14:26 +00:00
int terminal_or = terminal . attribute ( " orientation " ) . toInt ( & conv_ok ) ;
2006-10-27 15:47:22 +00:00
if ( ! conv_ok ) return ( false ) ;
2007-06-30 17:41:07 +00:00
if ( terminal_or ! = QET : : North & & terminal_or ! = QET : : South & & terminal_or ! = QET : : East & & terminal_or ! = QET : : West ) return ( false ) ;
2006-10-27 15:47:22 +00:00
// a ce stade, la borne est syntaxiquement correcte
return ( true ) ;
}
/**
Permet de savoir si un element XML represente cette borne . Attention , l ' element XML n ' est pas verifie
2007-10-21 16:10:21 +00:00
@ param terminal Le QDomElement a analyser
2006-10-27 15:47:22 +00:00
@ return true si la borne " se reconnait " ( memes coordonnes , meme orientation ) , false sinon
*/
2007-01-29 20:14:26 +00:00
bool Terminal : : fromXml ( QDomElement & terminal ) {
2006-10-27 15:47:22 +00:00
return (
2007-01-29 20:14:26 +00:00
terminal . attribute ( " x " ) . toDouble ( ) = = amarrage_elmt . x ( ) & & \
terminal . attribute ( " y " ) . toDouble ( ) = = amarrage_elmt . y ( ) & & \
terminal . attribute ( " orientation " ) . toInt ( ) = = sens
2006-10-27 15:47:22 +00:00
) ;
}
2007-09-25 23:24:36 +00:00
/// @return le Diagram auquel cette borne appartient, ou 0 si cette borne est independant
Diagram * Terminal : : diagram ( ) const {
return ( qobject_cast < Diagram * > ( scene ( ) ) ) ;
}