2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2015-02-20 14:56:22 +00:00
|
|
|
Copyright 2006-2015 The QElectroTech Team
|
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/>.
|
|
|
|
*/
|
2006-10-27 15:47:22 +00:00
|
|
|
#include "element.h"
|
2007-01-29 00:41:12 +00:00
|
|
|
#include "diagram.h"
|
2007-10-03 17:02:39 +00:00
|
|
|
#include "conductor.h"
|
2007-02-24 00:26:04 +00:00
|
|
|
#include "elementtextitem.h"
|
2007-09-26 17:14:09 +00:00
|
|
|
#include "diagramcommands.h"
|
2006-10-27 15:47:22 +00:00
|
|
|
#include <QtDebug>
|
2013-12-31 14:39:34 +00:00
|
|
|
#include "elementprovider.h"
|
2014-03-18 12:35:36 +00:00
|
|
|
#include "diagramposition.h"
|
2014-07-21 20:44:32 +00:00
|
|
|
#include "terminal.h"
|
2015-06-23 20:40:05 +00:00
|
|
|
#include "PropertiesEditor/propertieseditordialog.h"
|
|
|
|
#include "elementpropertieswidget.h"
|
2006-10-27 15:47:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Constructeur pour un element sans scene ni parent
|
|
|
|
*/
|
2014-12-14 13:06:21 +00:00
|
|
|
Element::Element(QGraphicsItem *parent) :
|
2013-11-14 10:11:22 +00:00
|
|
|
QetGraphicsItem(parent),
|
2014-11-16 14:15:32 +00:00
|
|
|
must_highlight_(false),
|
2014-12-14 18:51:49 +00:00
|
|
|
m_mouse_over(false)
|
2007-12-09 10:30:35 +00:00
|
|
|
{
|
2014-02-22 17:24:20 +00:00
|
|
|
link_type_ = Simple;
|
2013-12-31 14:39:34 +00:00
|
|
|
uuid_ = QUuid::createUuid();
|
2006-12-03 12:48:33 +00:00
|
|
|
setZValue(10);
|
2014-10-10 08:58:44 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
2015-03-02 20:14:56 +00:00
|
|
|
setAcceptHoverEvents(true);
|
2006-10-27 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 03:13:13 +00:00
|
|
|
/**
|
|
|
|
Destructeur
|
|
|
|
*/
|
|
|
|
Element::~Element() {
|
|
|
|
}
|
|
|
|
|
2015-06-23 20:40:05 +00:00
|
|
|
void Element::editProperty()
|
|
|
|
{
|
|
|
|
if (diagram() && !diagram()->isReadOnly())
|
|
|
|
{
|
|
|
|
ElementPropertiesWidget *epw = new ElementPropertiesWidget(this);
|
|
|
|
PropertiesEditorDialog dialog(epw, QApplication::activeWindow());
|
|
|
|
connect(epw, &ElementPropertiesWidget::findEditClicked, &dialog, &QDialog::reject);
|
|
|
|
dialog.exec();
|
|
|
|
}
|
2013-11-29 14:42:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 12:35:36 +00:00
|
|
|
|
|
|
|
|
2010-04-25 12:12:59 +00:00
|
|
|
/**
|
|
|
|
@return true si l'element est mis en evidence
|
|
|
|
*/
|
|
|
|
bool Element::isHighlighted() const {
|
|
|
|
return(must_highlight_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param hl true pour mettre l'element en evidence, false sinon
|
|
|
|
*/
|
|
|
|
void Element::setHighlighted(bool hl) {
|
|
|
|
must_highlight_ = hl;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:13:13 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::displayHelpLine
|
|
|
|
* Display the help line of each terminal if b is true
|
|
|
|
* @param b
|
|
|
|
*/
|
|
|
|
void Element::displayHelpLine(bool b)
|
|
|
|
{
|
|
|
|
foreach (Terminal *t, terminals())
|
|
|
|
t->drawHelpLine(b);
|
|
|
|
}
|
|
|
|
|
2006-10-27 15:47:22 +00:00
|
|
|
/**
|
2015-09-16 15:11:13 +00:00
|
|
|
* @brief Element::paint
|
|
|
|
* @param painter
|
|
|
|
* @param options
|
|
|
|
* @param widget
|
|
|
|
*/
|
|
|
|
void Element::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
|
|
|
{
|
2014-11-16 20:32:55 +00:00
|
|
|
|
2015-03-02 20:14:56 +00:00
|
|
|
#ifndef Q_OS_WIN
|
2015-09-16 15:11:13 +00:00
|
|
|
//Fix visual bug on QGraphicsScene that occur only on X11 with default zoom.
|
|
|
|
QSettings settings;
|
|
|
|
static bool must_correct_rendering_bug = settings.value("correct-rendering", false).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
|
2013-11-14 10:11:22 +00:00
|
|
|
qreal applied_rotation = 90.0 * orientation();
|
2007-10-11 12:34:53 +00:00
|
|
|
if (applied_rotation == 90.0) painter -> translate(1.0, -1.0);
|
|
|
|
else if (applied_rotation == 180.0) painter -> translate(-1.0, -1.0);
|
|
|
|
else if (applied_rotation == 270.0) painter -> translate(-1.0, 1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-04-25 12:12:59 +00:00
|
|
|
if (must_highlight_) drawHighlight(painter, options);
|
|
|
|
|
2014-12-14 18:51:49 +00:00
|
|
|
//Draw the element himself
|
2006-10-27 15:47:22 +00:00
|
|
|
paint(painter, options);
|
|
|
|
|
2014-12-14 18:51:49 +00:00
|
|
|
//Draw the selection rectangle
|
|
|
|
if ( isSelected() || m_mouse_over ) drawSelection(painter, options);
|
2006-10-27 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return Le rectangle delimitant le contour de l'element
|
|
|
|
*/
|
|
|
|
QRectF Element::boundingRect() const {
|
|
|
|
return(QRectF(QPointF(-hotspot_coord.x(), -hotspot_coord.y()), dimensions));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Definit la taille de l'element sur le schema. Les tailles doivent etre
|
|
|
|
des multiples de 10 ; si ce n'est pas le cas, les dimensions indiquees
|
|
|
|
seront arrrondies aux dizaines superieures.
|
|
|
|
@param wid Largeur de l'element
|
|
|
|
@param hei Hauteur de l'element
|
|
|
|
@return La taille finale de l'element
|
|
|
|
*/
|
|
|
|
QSize Element::setSize(int wid, int hei) {
|
|
|
|
prepareGeometryChange();
|
|
|
|
// chaque dimension indiquee est arrondie a la dizaine superieure
|
|
|
|
while (wid % 10) ++ wid;
|
|
|
|
while (hei % 10) ++ hei;
|
|
|
|
// les dimensions finales sont conservees et retournees
|
|
|
|
return(dimensions = QSize(wid, hei));
|
|
|
|
}
|
|
|
|
|
2009-05-17 02:13:40 +00:00
|
|
|
/**
|
|
|
|
@return la taille de l'element sur le schema
|
|
|
|
*/
|
|
|
|
QSize Element::size() const {
|
|
|
|
return(dimensions);
|
|
|
|
}
|
|
|
|
|
2006-10-27 15:47:22 +00:00
|
|
|
/**
|
|
|
|
Definit le hotspot de l'element par rapport au coin superieur gauche de son rectangle delimitant.
|
|
|
|
Necessite que la taille ait deja ete definie
|
2007-10-21 16:10:21 +00:00
|
|
|
@param hs Coordonnees du hotspot
|
2006-10-27 15:47:22 +00:00
|
|
|
*/
|
|
|
|
QPoint Element::setHotspot(QPoint hs) {
|
|
|
|
// la taille doit avoir ete definie
|
|
|
|
prepareGeometryChange();
|
|
|
|
if (dimensions.isNull()) hotspot_coord = QPoint(0, 0);
|
|
|
|
else {
|
|
|
|
// les coordonnees indiquees ne doivent pas depasser les dimensions de l'element
|
2007-01-20 18:11:42 +00:00
|
|
|
int hsx = qMin(hs.x(), dimensions.width());
|
|
|
|
int hsy = qMin(hs.y(), dimensions.height());
|
2006-10-27 15:47:22 +00:00
|
|
|
hotspot_coord = QPoint(hsx, hsy);
|
|
|
|
}
|
|
|
|
return(hotspot_coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return Le hotspot courant de l'element
|
|
|
|
*/
|
|
|
|
QPoint Element::hotspot() const {
|
|
|
|
return(hotspot_coord);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Selectionne l'element
|
|
|
|
*/
|
|
|
|
void Element::select() {
|
|
|
|
setSelected(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Deselectionne l'element
|
|
|
|
*/
|
|
|
|
void Element::deselect() {
|
|
|
|
setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@return La pixmap de l'element
|
|
|
|
*/
|
|
|
|
QPixmap Element::pixmap() {
|
2009-04-03 19:30:25 +00:00
|
|
|
if (preview.isNull()) updatePixmap(); // on genere la pixmap si ce n'est deja fait
|
|
|
|
return(preview);
|
2006-10-27 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
2007-04-09 02:56:47 +00:00
|
|
|
/**
|
2013-11-14 10:11:22 +00:00
|
|
|
* @brief Element::rotateBy
|
|
|
|
* this methode is redefined for handle child item
|
|
|
|
* @param angle
|
|
|
|
*/
|
|
|
|
void Element::rotateBy(const qreal &angle) {
|
|
|
|
qreal applied_angle = QET::correctAngle(angle);
|
|
|
|
applyRotation(applied_angle + rotation());
|
|
|
|
|
|
|
|
//update the path of conductor
|
2008-07-09 21:14:30 +00:00
|
|
|
foreach(QGraphicsItem *qgi, childItems()) {
|
2010-04-11 16:03:40 +00:00
|
|
|
if (Terminal *p = qgraphicsitem_cast<Terminal *>(qgi)) {
|
|
|
|
p -> updateConductor();
|
2007-02-24 00:26:04 +00:00
|
|
|
}
|
2006-12-02 23:42:59 +00:00
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
// repositionne les textes de l'element qui ne comportent pas l'option "FollowParentRotations"
|
|
|
|
foreach(ElementTextItem *eti, texts()) {
|
|
|
|
if (!eti -> followParentRotations()) {
|
|
|
|
// on souhaite pivoter le champ de texte par rapport a son centre
|
|
|
|
QPointF eti_center = eti -> boundingRect().center();
|
|
|
|
// pour ce faire, on repere la position de son centre par rapport a son parent
|
|
|
|
QPointF parent_eti_center_before = eti -> mapToParent(eti_center);
|
|
|
|
// on applique ensuite une simple rotation contraire, qui sera donc appliquee sur le milieu du cote gauche du champ de texte
|
|
|
|
eti -> rotateBy(-applied_angle);
|
|
|
|
// on regarde ensuite la nouvelle position du centre du champ de texte par rapport a son parent
|
|
|
|
QPointF parent_eti_center_after = eti -> mapToParent(eti_center);
|
|
|
|
// on determine la translation a appliquer
|
|
|
|
QPointF eti_translation = parent_eti_center_before - parent_eti_center_after;
|
|
|
|
// on applique cette translation
|
|
|
|
eti -> setPos(eti -> pos() + eti_translation);
|
|
|
|
}
|
|
|
|
}
|
2006-10-27 15:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*** Methodes protegees ***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Dessine un petit repere (axes x et y) relatif a l'element
|
|
|
|
@param painter Le QPainter a utiliser pour dessiner les axes
|
|
|
|
@param options Les options de style a prendre en compte
|
|
|
|
*/
|
2009-11-22 16:12:22 +00:00
|
|
|
void Element::drawAxes(QPainter *painter, const QStyleOptionGraphicsItem *options) {
|
|
|
|
Q_UNUSED(options);
|
2006-10-27 15:47:22 +00:00
|
|
|
painter -> setPen(Qt::blue);
|
|
|
|
painter -> drawLine(0, 0, 10, 0);
|
|
|
|
painter -> drawLine(7,-3, 10, 0);
|
|
|
|
painter -> drawLine(7, 3, 10, 0);
|
|
|
|
painter -> setPen(Qt::red);
|
|
|
|
painter -> drawLine(0, 0, 0, 10);
|
|
|
|
painter -> drawLine(0, 10,-3, 7);
|
|
|
|
painter -> drawLine(0, 10, 3, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** Methodes privees ***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Dessine le cadre de selection de l'element de maniere systematiquement non antialiasee.
|
2009-11-22 16:12:22 +00:00
|
|
|
@param painter Le QPainter a utiliser pour dessiner les bornes.
|
2006-10-27 15:47:22 +00:00
|
|
|
@param options Les options de style a prendre en compte
|
|
|
|
*/
|
2009-11-22 16:12:22 +00:00
|
|
|
void Element::drawSelection(QPainter *painter, const QStyleOptionGraphicsItem *options) {
|
|
|
|
Q_UNUSED(options);
|
2006-10-27 15:47:22 +00:00
|
|
|
painter -> save();
|
|
|
|
// Annulation des renderhints
|
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
painter -> setRenderHint(QPainter::TextAntialiasing, false);
|
|
|
|
painter -> setRenderHint(QPainter::SmoothPixmapTransform, false);
|
|
|
|
// Dessin du cadre de selection en gris
|
|
|
|
QPen t;
|
|
|
|
t.setColor(Qt::gray);
|
|
|
|
t.setStyle(Qt::DashDotLine);
|
2015-03-02 20:14:56 +00:00
|
|
|
t.setCosmetic(true);
|
2006-10-27 15:47:22 +00:00
|
|
|
painter -> setPen(t);
|
|
|
|
// Le dessin se fait a partir du rectangle delimitant
|
2014-11-16 20:32:55 +00:00
|
|
|
painter -> drawRoundRect(boundingRect().adjusted(1, 1, -1, -1), 10, 10);
|
2010-04-25 12:12:59 +00:00
|
|
|
painter -> restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Dessine le cadre de selection de l'element de maniere systematiquement non antialiasee.
|
|
|
|
@param painter Le QPainter a utiliser pour dessiner les bornes.
|
|
|
|
@param options Les options de style a prendre en compte
|
|
|
|
*/
|
|
|
|
void Element::drawHighlight(QPainter *painter, const QStyleOptionGraphicsItem *options) {
|
|
|
|
Q_UNUSED(options);
|
|
|
|
painter -> save();
|
|
|
|
|
|
|
|
qreal gradient_radius = qMin(boundingRect().width(), boundingRect().height()) / 2.0;
|
|
|
|
QRadialGradient gradient(
|
|
|
|
boundingRect().center(),
|
|
|
|
gradient_radius,
|
|
|
|
boundingRect().center()
|
|
|
|
);
|
|
|
|
gradient.setColorAt(0.0, QColor::fromRgb(69, 137, 255, 255));
|
|
|
|
gradient.setColorAt(1.0, QColor::fromRgb(69, 137, 255, 0));
|
|
|
|
QBrush brush(gradient);
|
|
|
|
|
|
|
|
painter -> setPen(Qt::NoPen);
|
|
|
|
painter -> setBrush(brush);
|
|
|
|
// Le dessin se fait a partir du rectangle delimitant
|
2014-11-16 20:32:55 +00:00
|
|
|
painter -> drawRoundRect(boundingRect().adjusted(1, 1, -1, -1), 10, 10);
|
2006-10-27 15:47:22 +00:00
|
|
|
painter -> restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Fonction initialisant et dessinant la pixmap de l'element.
|
|
|
|
*/
|
|
|
|
void Element::updatePixmap() {
|
|
|
|
// Pixmap transparente faisant la taille de base de l'element
|
2009-04-03 19:30:25 +00:00
|
|
|
preview = QPixmap(dimensions);
|
|
|
|
preview.fill(QColor(255, 255, 255, 0));
|
2006-10-27 15:47:22 +00:00
|
|
|
// QPainter sur la pixmap, avec antialiasing
|
2009-04-03 19:30:25 +00:00
|
|
|
QPainter p(&preview);
|
2006-10-27 15:47:22 +00:00
|
|
|
p.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
p.setRenderHint(QPainter::SmoothPixmapTransform, true);
|
|
|
|
// Translation de l'origine du repere de la pixmap
|
|
|
|
p.translate(hotspot_coord);
|
|
|
|
// L'element se dessine sur la pixmap
|
|
|
|
paint(&p, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Permet de savoir si un element XML (QDomElement) represente bien un element
|
|
|
|
@param e Le QDomElement a valide
|
|
|
|
@return true si l'element XML est un Element, false sinon
|
|
|
|
*/
|
|
|
|
bool Element::valideXml(QDomElement &e) {
|
|
|
|
// verifie le nom du tag
|
|
|
|
if (e.tagName() != "element") return(false);
|
|
|
|
|
|
|
|
// verifie la presence des attributs minimaux
|
|
|
|
if (!e.hasAttribute("type")) return(false);
|
|
|
|
if (!e.hasAttribute("x")) return(false);
|
|
|
|
if (!e.hasAttribute("y")) return(false);
|
|
|
|
|
|
|
|
bool conv_ok;
|
|
|
|
// parse l'abscisse
|
|
|
|
e.attribute("x").toDouble(&conv_ok);
|
|
|
|
if (!conv_ok) return(false);
|
|
|
|
|
|
|
|
// parse l'ordonnee
|
|
|
|
e.attribute("y").toDouble(&conv_ok);
|
|
|
|
if (!conv_ok) return(false);
|
|
|
|
return(true);
|
|
|
|
}
|
2007-02-24 18:37:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Methode d'import XML. Cette methode est appelee lors de l'import de contenu
|
|
|
|
XML (coller, import, ouverture de fichier...) afin que l'element puisse
|
|
|
|
gerer lui-meme l'importation de ses bornes. Ici, comme cette classe est
|
|
|
|
caracterisee par un nombre fixe de bornes, l'implementation exige de
|
|
|
|
retrouver exactement ses bornes dans le fichier XML.
|
|
|
|
@param e L'element XML a analyser.
|
|
|
|
@param table_id_adr Reference vers la table de correspondance entre les IDs
|
|
|
|
du fichier XML et les adresses en memoire. Si l'import reussit, il faut y
|
|
|
|
ajouter les bons couples (id, adresse).
|
|
|
|
@return true si l'import a reussi, false sinon
|
|
|
|
|
|
|
|
*/
|
2010-07-18 19:16:52 +00:00
|
|
|
bool Element::fromXml(QDomElement &e, QHash<int, Terminal *> &table_id_adr, bool handle_inputs_rotation) {
|
2007-02-24 18:37:07 +00:00
|
|
|
/*
|
2007-12-05 21:16:01 +00:00
|
|
|
les bornes vont maintenant etre recensees pour associer leurs id a leur adresse reelle
|
2007-02-24 18:37:07 +00:00
|
|
|
ce recensement servira lors de la mise en place des fils
|
|
|
|
*/
|
|
|
|
QList<QDomElement> liste_terminals;
|
2007-10-27 13:18:17 +00:00
|
|
|
foreach(QDomElement qde, QET::findInDomElement(e, "terminals", "terminal")) {
|
2007-02-24 19:56:29 +00:00
|
|
|
if (Terminal::valideXml(qde)) liste_terminals << qde;
|
2007-02-24 18:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, Terminal *> priv_id_adr;
|
|
|
|
int terminals_non_trouvees = 0;
|
2008-07-09 21:14:30 +00:00
|
|
|
foreach(QGraphicsItem *qgi, childItems()) {
|
2007-02-24 18:37:07 +00:00
|
|
|
if (Terminal *p = qgraphicsitem_cast<Terminal *>(qgi)) {
|
|
|
|
bool terminal_trouvee = false;
|
|
|
|
foreach(QDomElement qde, liste_terminals) {
|
|
|
|
if (p -> fromXml(qde)) {
|
|
|
|
priv_id_adr.insert(qde.attribute("id").toInt(), p);
|
|
|
|
terminal_trouvee = true;
|
2011-12-29 17:37:24 +00:00
|
|
|
// We used to break here, because we did not expect
|
|
|
|
// several terminals to share the same position.
|
|
|
|
// Of course, it finally happened.
|
2007-02-24 18:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!terminal_trouvee) ++ terminals_non_trouvees;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (terminals_non_trouvees > 0) {
|
|
|
|
return(false);
|
|
|
|
} else {
|
|
|
|
// verifie que les associations id / adr n'entrent pas en conflit avec table_id_adr
|
|
|
|
foreach(int id_trouve, priv_id_adr.keys()) {
|
|
|
|
if (table_id_adr.contains(id_trouve)) {
|
|
|
|
// cet element possede un id qui est deja reference (= conflit)
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// copie des associations id / adr
|
|
|
|
foreach(int id_trouve, priv_id_adr.keys()) {
|
|
|
|
table_id_adr.insert(id_trouve, priv_id_adr.value(id_trouve));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-24 19:56:29 +00:00
|
|
|
// importe les valeurs des champs de texte
|
2007-10-27 13:18:17 +00:00
|
|
|
QList<QDomElement> inputs = QET::findInDomElement(e, "inputs", "input");
|
2008-07-09 21:14:30 +00:00
|
|
|
foreach(QGraphicsItem *qgi, childItems()) {
|
2007-02-24 19:56:29 +00:00
|
|
|
if (ElementTextItem *eti = qgraphicsitem_cast<ElementTextItem *>(qgi)) {
|
|
|
|
foreach(QDomElement input, inputs) eti -> fromXml(input);
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 14:39:34 +00:00
|
|
|
|
|
|
|
//load uuid of connected elements
|
|
|
|
QList <QDomElement> uuid_list = QET::findInDomElement(e, "links_uuids", "link_uuid");
|
|
|
|
foreach (QDomElement qdo, uuid_list) tmp_uuids_link << qdo.attribute("uuid");
|
2007-02-24 19:56:29 +00:00
|
|
|
|
2013-12-31 14:39:34 +00:00
|
|
|
//uuid of this element
|
|
|
|
uuid_= QUuid(e.attribute("uuid", QUuid::createUuid().toString()));
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
//load informations
|
2015-05-31 15:02:41 +00:00
|
|
|
m_element_informations.fromXml(e.firstChildElement("elementInformations"), "elementInformation");
|
2014-02-12 17:36:35 +00:00
|
|
|
|
2015-05-17 17:47:50 +00:00
|
|
|
//Position and selection.
|
|
|
|
//We directly call setPos from QGraphicsObject, because QetGraphicsItem will snap to grid
|
|
|
|
QGraphicsObject::setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
|
2007-02-24 18:37:07 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
2010-07-18 19:16:52 +00:00
|
|
|
|
|
|
|
// orientation
|
2007-02-24 18:37:07 +00:00
|
|
|
bool conv_ok;
|
2007-03-09 19:18:55 +00:00
|
|
|
int read_ori = e.attribute("orientation").toInt(&conv_ok);
|
2013-11-14 10:11:22 +00:00
|
|
|
if (!conv_ok || read_ori < 0 || read_ori > 3) read_ori = 0;
|
2010-07-18 19:16:52 +00:00
|
|
|
if (handle_inputs_rotation) {
|
2013-11-14 10:11:22 +00:00
|
|
|
rotateBy(90*read_ori);
|
2010-07-18 19:16:52 +00:00
|
|
|
} else {
|
2013-11-14 10:11:22 +00:00
|
|
|
applyRotation(90*read_ori);
|
2010-07-18 19:16:52 +00:00
|
|
|
}
|
2007-02-24 18:37:07 +00:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Permet d'exporter l'element en XML
|
|
|
|
@param document Document XML a utiliser
|
|
|
|
@param table_adr_id Table de correspondance entre les adresses des bornes
|
|
|
|
et leur id dans la representation XML ; cette table completee par cette
|
|
|
|
methode
|
|
|
|
@return L'element XML representant cet element electrique
|
|
|
|
*/
|
|
|
|
QDomElement Element::toXml(QDomDocument &document, QHash<Terminal *, int> &table_adr_id) const {
|
|
|
|
QDomElement element = document.createElement("element");
|
|
|
|
|
|
|
|
// type
|
2009-04-03 19:30:25 +00:00
|
|
|
element.setAttribute("type", typeId());
|
2013-12-31 14:39:34 +00:00
|
|
|
// uuid
|
|
|
|
element.setAttribute("uuid", uuid().toString());
|
2007-02-24 18:37:07 +00:00
|
|
|
|
|
|
|
// position, selection et orientation
|
2008-08-23 19:27:14 +00:00
|
|
|
element.setAttribute("x", QString("%1").arg(pos().x()));
|
|
|
|
element.setAttribute("y", QString("%1").arg(pos().y()));
|
2013-11-14 10:11:22 +00:00
|
|
|
element.setAttribute("orientation", QString("%1").arg(orientation()));
|
2007-02-24 18:37:07 +00:00
|
|
|
|
|
|
|
/* recupere le premier id a utiliser pour les bornes de cet element */
|
|
|
|
int id_terminal = 0;
|
|
|
|
if (!table_adr_id.isEmpty()) {
|
|
|
|
// trouve le plus grand id
|
|
|
|
int max_id_t = -1;
|
|
|
|
foreach (int id_t, table_adr_id.values()) {
|
|
|
|
if (id_t > max_id_t) max_id_t = id_t;
|
|
|
|
}
|
|
|
|
id_terminal = max_id_t + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// enregistrement des bornes de l'appareil
|
2009-04-04 21:47:07 +00:00
|
|
|
QDomElement xml_terminals = document.createElement("terminals");
|
2007-02-24 18:37:07 +00:00
|
|
|
// pour chaque enfant de l'element
|
2009-04-04 21:47:07 +00:00
|
|
|
foreach(Terminal *t, terminals()) {
|
|
|
|
// alors on enregistre la borne
|
|
|
|
QDomElement terminal = t -> toXml(document);
|
|
|
|
terminal.setAttribute("id", id_terminal);
|
|
|
|
table_adr_id.insert(t, id_terminal ++);
|
|
|
|
xml_terminals.appendChild(terminal);
|
2007-02-24 18:37:07 +00:00
|
|
|
}
|
2009-04-04 21:47:07 +00:00
|
|
|
element.appendChild(xml_terminals);
|
2007-02-24 19:56:29 +00:00
|
|
|
|
|
|
|
// enregistrement des champ de texte de l'appareil
|
|
|
|
QDomElement inputs = document.createElement("inputs");
|
2009-04-04 21:47:07 +00:00
|
|
|
foreach(ElementTextItem *eti, texts()) {
|
|
|
|
inputs.appendChild(eti -> toXml(document));
|
2007-02-24 19:56:29 +00:00
|
|
|
}
|
|
|
|
element.appendChild(inputs);
|
2013-12-31 14:39:34 +00:00
|
|
|
|
|
|
|
//if this element is linked to other elements,
|
|
|
|
//save the uuid of each other elements
|
|
|
|
if (! isFree()) {
|
|
|
|
QDomElement links_uuids = document.createElement("links_uuids");
|
|
|
|
foreach (Element *elmt, connected_elements) {
|
|
|
|
QDomElement link_uuid = document.createElement("link_uuid");
|
|
|
|
link_uuid.setAttribute("uuid", elmt->uuid().toString());
|
|
|
|
links_uuids.appendChild(link_uuid);
|
|
|
|
}
|
|
|
|
element.appendChild(links_uuids);
|
|
|
|
}
|
2014-02-12 17:36:35 +00:00
|
|
|
|
|
|
|
//save information of this element
|
2015-05-31 15:02:41 +00:00
|
|
|
if (! m_element_informations.keys().isEmpty()) {
|
2014-02-22 17:24:20 +00:00
|
|
|
QDomElement infos = document.createElement("elementInformations");
|
2015-05-31 15:02:41 +00:00
|
|
|
m_element_informations.toXml(infos, "elementInformation");
|
2014-02-22 17:24:20 +00:00
|
|
|
element.appendChild(infos);
|
|
|
|
}
|
|
|
|
|
2007-02-24 18:37:07 +00:00
|
|
|
return(element);
|
|
|
|
}
|
2013-12-31 14:39:34 +00:00
|
|
|
|
2015-01-09 17:18:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::AlignedFreeTerminals
|
|
|
|
* @return a list of terminal (owned by this element) aligned to other terminal (from other element)
|
|
|
|
* The first Terminal of QPair is a Terminal owned by this element,
|
|
|
|
* this terminal haven't got any conductor docked.
|
|
|
|
* The second Terminal of QPair is a Terminal owned by an other element,
|
|
|
|
* which is aligned with the first Terminal. The second Terminal can have or not docked conductors.
|
|
|
|
*/
|
|
|
|
QList <QPair <Terminal *, Terminal *> > Element::AlignedFreeTerminals() const
|
|
|
|
{
|
|
|
|
QList <QPair <Terminal *, Terminal *> > list;
|
|
|
|
|
|
|
|
foreach (Terminal *terminal, terminals())
|
|
|
|
{
|
|
|
|
if (terminal->conductors().isEmpty())
|
|
|
|
{
|
|
|
|
Terminal *other_terminal = terminal -> alignedWithTerminal();
|
|
|
|
if (other_terminal)
|
|
|
|
list << qMakePair(terminal, other_terminal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2015-01-07 19:35:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::initLink
|
|
|
|
* Initialise the link between this element and other elements.
|
|
|
|
* This method can be call once because init the link according to
|
|
|
|
* uuid store in a private list, after link, the list is clear, so
|
|
|
|
* call another time do nothing.
|
|
|
|
*
|
|
|
|
* @param prj, ownership project of this element and other element to be linked
|
|
|
|
*/
|
|
|
|
void Element::initLink(QETProject *prj)
|
|
|
|
{
|
|
|
|
// if nothing to link return now
|
2013-12-31 14:39:34 +00:00
|
|
|
if (tmp_uuids_link.isEmpty()) return;
|
|
|
|
|
|
|
|
ElementProvider ep(prj);
|
|
|
|
foreach (Element *elmt, ep.fromUuids(tmp_uuids_link)) {
|
|
|
|
elmt->linkToElement(this);
|
|
|
|
}
|
|
|
|
tmp_uuids_link.clear();
|
|
|
|
}
|
2014-03-18 12:35:36 +00:00
|
|
|
|
2014-03-21 14:40:33 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::setElementInformations
|
|
|
|
* Set new information for this element.
|
2015-05-31 15:02:41 +00:00
|
|
|
* If new information is different of current infotmation emit @elementInfoChange
|
2014-03-21 14:40:33 +00:00
|
|
|
* @param dc
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
void Element::setElementInformations(DiagramContext dc)
|
|
|
|
{
|
|
|
|
if (m_element_informations == dc) return;
|
|
|
|
DiagramContext old_info = m_element_informations;
|
|
|
|
m_element_informations = dc;
|
|
|
|
emit elementInfoChange(old_info, m_element_informations);
|
2014-03-21 14:40:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 12:35:36 +00:00
|
|
|
/**
|
|
|
|
* @brief comparPos
|
|
|
|
* Compare position of the two elements. Compare 3 points:
|
|
|
|
* 1 folio - 2 row - 3 line
|
|
|
|
* returns a response when a comparison is found.
|
|
|
|
* @return true if elmt1 is at lower position than elmt 2, else false
|
|
|
|
*/
|
|
|
|
bool comparPos(const Element *elmt1, const Element *elmt2) {
|
|
|
|
//Compare folio first
|
|
|
|
if (elmt1->diagram()->folioIndex() != elmt2->diagram()->folioIndex())
|
|
|
|
return elmt1->diagram()->folioIndex() < elmt2->diagram()->folioIndex();
|
2014-03-26 10:41:06 +00:00
|
|
|
//Compare the row(in letter pos) in second
|
2014-03-18 12:35:36 +00:00
|
|
|
QString a = elmt1->diagram()->convertPosition(elmt1->scenePos()).letter();
|
|
|
|
QString b = elmt2->diagram()->convertPosition(elmt2->scenePos()).letter();
|
|
|
|
if (a != b)
|
|
|
|
return a<b;
|
2014-03-26 10:41:06 +00:00
|
|
|
//In last compare the line, if line is egal, return sorted by row in real pos
|
|
|
|
if (elmt1->pos().x() == elmt2->pos().x())
|
|
|
|
return elmt1->y() <= elmt2->pos().y();
|
2014-03-18 12:35:36 +00:00
|
|
|
return elmt1->pos().x() <= elmt2->pos().x();
|
|
|
|
}
|
2014-11-16 14:15:32 +00:00
|
|
|
|
2015-01-09 17:18:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::mouseMoveEvent
|
|
|
|
* @param event
|
|
|
|
*/
|
2015-01-07 19:35:42 +00:00
|
|
|
void Element::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
QetGraphicsItem::mouseMoveEvent(event);
|
|
|
|
foreach (Terminal *t, terminals())
|
|
|
|
{
|
|
|
|
t -> drawHelpLine(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 17:18:16 +00:00
|
|
|
/**
|
|
|
|
* @brief Element::mouseReleaseEvent
|
|
|
|
* @param event
|
|
|
|
*/
|
2015-01-07 19:35:42 +00:00
|
|
|
void Element::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
QetGraphicsItem::mouseReleaseEvent(event);
|
|
|
|
foreach (Terminal *t, terminals())
|
|
|
|
{
|
|
|
|
t -> drawHelpLine(false);
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 14:15:32 +00:00
|
|
|
|
|
|
|
/**
|
2014-12-14 18:51:49 +00:00
|
|
|
* When mouse over element
|
|
|
|
* change m_mouse_over to true (used in paint() function )
|
|
|
|
* Also highlight linked elements
|
|
|
|
* @param e QGraphicsSceneHoverEvent
|
2014-11-16 14:15:32 +00:00
|
|
|
*/
|
|
|
|
void Element::hoverEnterEvent(QGraphicsSceneHoverEvent *e) {
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2014-12-14 18:51:49 +00:00
|
|
|
foreach (Element *elmt, linkedElements())
|
|
|
|
elmt -> setHighlighted(true);
|
|
|
|
|
|
|
|
m_mouse_over = true;
|
|
|
|
setToolTip( name() );
|
2014-11-16 14:15:32 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-14 18:51:49 +00:00
|
|
|
* When mouse over element leave the position
|
|
|
|
* change m_mouse_over to false(used in paint() function )
|
|
|
|
* Also un-highlight linked elements
|
|
|
|
* @param e QGraphicsSceneHoverEvent
|
2014-11-16 14:15:32 +00:00
|
|
|
*/
|
|
|
|
void Element::hoverLeaveEvent(QGraphicsSceneHoverEvent *e) {
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2014-12-14 18:51:49 +00:00
|
|
|
foreach (Element *elmt, linkedElements())
|
|
|
|
elmt -> setHighlighted(false);
|
|
|
|
|
|
|
|
m_mouse_over = false;
|
|
|
|
update();
|
2014-11-16 14:15:32 +00:00
|
|
|
}
|