2007-12-01 10:47:15 +00:00
|
|
|
/*
|
2012-01-01 22:51:51 +00:00
|
|
|
Copyright 2006-2012 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 "partcircle.h"
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Constructeur
|
|
|
|
@param editor L'editeur d'element concerne
|
|
|
|
@param parent Le QGraphicsItem parent de ce cercle
|
|
|
|
@param scene La scene sur laquelle figure ce cercle
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
PartCircle::PartCircle(QETElementEditor *editor, QGraphicsItem *parent, QGraphicsScene *scene) : QGraphicsEllipseItem(parent, scene), CustomElementGraphicPart(editor) {
|
2007-06-30 17:41:07 +00:00
|
|
|
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
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/// Destructeur
|
|
|
|
PartCircle::~PartCircle() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Dessine le cercle
|
|
|
|
@param painter QPainter a utiliser pour rendre le dessin
|
|
|
|
@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 PartCircle::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) {
|
|
|
|
Q_UNUSED(widget);
|
2007-06-30 17:41:07 +00:00
|
|
|
applyStylesToQPainter(*painter);
|
|
|
|
QPen t = painter -> pen();
|
2009-04-18 18:54:25 +00:00
|
|
|
t.setCosmetic(options && options -> levelOfDetail < 1.0);
|
2007-06-30 17:41:07 +00:00
|
|
|
if (isSelected()) {
|
|
|
|
t.setColor(Qt::red);
|
|
|
|
}
|
2009-04-18 18:54:25 +00:00
|
|
|
painter -> setPen(t);
|
2007-06-30 17:41:07 +00:00
|
|
|
painter -> drawEllipse(rect());
|
|
|
|
if (isSelected()) {
|
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
painter -> setPen((painter -> brush().color() == QColor(Qt::black) && painter -> brush().isOpaque()) ? Qt::yellow : Qt::blue);
|
|
|
|
QPointF center = rect().center();
|
|
|
|
painter -> drawLine(QLineF(center.x() - 2.0, center.y(), center.x() + 2.0, center.y()));
|
|
|
|
painter -> drawLine(QLineF(center.x(), center.y() - 2.0, center.x(), center.y() + 2.0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Exporte le cercle en XML
|
|
|
|
@param xml_document Document XML a utiliser pour creer l'element XML
|
|
|
|
@return un element XML decrivant le cercle
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
const QDomElement PartCircle::toXml(QDomDocument &xml_document) const {
|
|
|
|
QDomElement xml_element = xml_document.createElement("circle");
|
|
|
|
QPointF top_left(sceneTopLeft());
|
2008-08-23 19:27:14 +00:00
|
|
|
xml_element.setAttribute("x", QString("%1").arg(top_left.x()));
|
|
|
|
xml_element.setAttribute("y", QString("%1").arg(top_left.y()));
|
|
|
|
xml_element.setAttribute("diameter", QString("%1").arg(rect().width()));
|
2007-06-30 17:41:07 +00:00
|
|
|
stylesToXml(xml_element);
|
|
|
|
return(xml_element);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Importe les proprietes d'un cercle depuis un element XML
|
|
|
|
@param qde Element XML a lire
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
void PartCircle::fromXml(const QDomElement &qde) {
|
|
|
|
stylesFromXml(qde);
|
|
|
|
qreal diameter = qde.attribute("diameter", "0").toDouble();
|
|
|
|
setRect(
|
|
|
|
QRectF(
|
|
|
|
mapFromScene(
|
|
|
|
qde.attribute("x", "0").toDouble(),
|
|
|
|
qde.attribute("y", "0").toDouble()
|
|
|
|
),
|
|
|
|
QSizeF(
|
|
|
|
diameter,
|
|
|
|
diameter
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Specifie la valeur d'une propriete donnee du cercle
|
|
|
|
@param property propriete a modifier. Valeurs acceptees :
|
|
|
|
* x : abscisse du centre du cercle
|
|
|
|
* y : ordonnee du centre du cercle
|
|
|
|
* diameter : diametre du cercle
|
|
|
|
@param value Valeur a attribuer a la propriete
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
void PartCircle::setProperty(const QString &property, const QVariant &value) {
|
|
|
|
CustomElementGraphicPart::setProperty(property, value);
|
|
|
|
if (!value.canConvert(QVariant::Double)) return;
|
|
|
|
if (property == "x") {
|
|
|
|
QRectF current_rect = rect();
|
|
|
|
QPointF current_pos = mapToScene(current_rect.center());
|
|
|
|
setRect(current_rect.translated(value.toDouble() - current_pos.x(), 0.0));
|
|
|
|
} else if (property == "y") {
|
|
|
|
QRectF current_rect = rect();
|
|
|
|
QPointF current_pos = mapToScene(current_rect.center());
|
|
|
|
setRect(current_rect.translated(0.0, value.toDouble() - current_pos.y()));
|
|
|
|
} else if (property == "diameter") {
|
|
|
|
QRectF current_rect = rect();
|
|
|
|
qreal new_diameter = qAbs(value.toDouble());
|
|
|
|
current_rect.translate(
|
|
|
|
(new_diameter - current_rect.width()) / -2.0,
|
|
|
|
(new_diameter - current_rect.height()) / -2.0
|
|
|
|
);
|
|
|
|
current_rect.setSize(QSizeF(new_diameter, new_diameter));
|
|
|
|
setRect(current_rect);
|
|
|
|
}
|
2009-04-03 19:30:25 +00:00
|
|
|
update();
|
2007-08-25 03:43:05 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
Permet d'acceder a la valeur d'une propriete de style donnee.
|
|
|
|
@param property propriete lue. Valeurs acceptees :
|
|
|
|
* x : abscisse du centre du cercle
|
|
|
|
* y : ordonnee du centre du cercle
|
|
|
|
* diameter : diametre du cercle
|
|
|
|
@return La valeur de la propriete property
|
|
|
|
*/
|
2007-08-25 03:43:05 +00:00
|
|
|
QVariant PartCircle::property(const QString &property) {
|
|
|
|
// appelle la methode property de CustomElementGraphicpart pour les styles
|
|
|
|
QVariant style_property = CustomElementGraphicPart::property(property);
|
|
|
|
if (style_property != QVariant()) return(style_property);
|
|
|
|
|
|
|
|
if (property == "x") {
|
|
|
|
return(mapToScene(rect().center()).x());
|
|
|
|
} else if (property == "y") {
|
|
|
|
return(mapToScene(rect().center()).y());
|
|
|
|
} else if (property == "diameter") {
|
|
|
|
return(rect().width());
|
|
|
|
}
|
|
|
|
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 PartCircle::itemChange(GraphicsItemChange change, const QVariant &value) {
|
|
|
|
if (scene()) {
|
2010-02-18 00:42:41 +00:00
|
|
|
if (change == QGraphicsItem::ItemPositionChange || change == QGraphicsItem::ItemPositionHasChanged) {
|
|
|
|
updateCurrentPartEditor();
|
2007-06-30 17:41:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(QGraphicsEllipseItem::itemChange(change, value));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le coin superieur gauche du rectangle dans lequel s'inscrit
|
|
|
|
le cercle, dans les coordonnees de la scene.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QPointF PartCircle::sceneTopLeft() const {
|
|
|
|
return(mapToScene(rect().topLeft()));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le centre du cercle, dans les coordonnees de la scene.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QPointF PartCircle::sceneCenter() const {
|
|
|
|
return(mapToScene(rect().center()));
|
|
|
|
}
|
|
|
|
|
2007-12-05 21:16:01 +00:00
|
|
|
/**
|
|
|
|
@return le rectangle delimitant cette partie.
|
|
|
|
*/
|
2007-06-30 17:41:07 +00:00
|
|
|
QRectF PartCircle::boundingRect() const {
|
|
|
|
qreal adjust = 1.5;
|
|
|
|
QRectF r(QGraphicsEllipseItem::boundingRect());
|
|
|
|
r.adjust(-adjust, -adjust, adjust, adjust);
|
|
|
|
return(r);
|
|
|
|
}
|
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.
|
|
|
|
Un cercle est pertinent des lors que son rayon n'est pas nul
|
|
|
|
*/
|
|
|
|
bool PartCircle::isUseless() const {
|
|
|
|
return(rect().isNull());
|
|
|
|
}
|