2013-11-14 10:11:22 +00:00
|
|
|
/*
|
2025-01-04 13:37:40 +01:00
|
|
|
Copyright 2006-2025 The QElectroTech Team
|
2013-11-14 10:11:22 +00:00
|
|
|
This file is part of QElectroTech.
|
2020-08-14 22:00:42 +02:00
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
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.
|
2020-08-14 22:00:42 +02:00
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
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.
|
2020-08-14 22:00:42 +02:00
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "diagramimageitem.h"
|
2020-12-08 19:57:35 +01:00
|
|
|
|
|
|
|
#include "../PropertiesEditor/propertieseditordialog.h"
|
|
|
|
#include "../diagram.h"
|
2020-12-10 18:44:03 +01:00
|
|
|
#include "../ui/imagepropertieswidget.h"
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::DiagramImageItem
|
|
|
|
Constructor without pixmap
|
|
|
|
@param parent_item the parent graphics item
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
DiagramImageItem::DiagramImageItem(QetGraphicsItem *parent_item):
|
|
|
|
QetGraphicsItem(parent_item)
|
|
|
|
{
|
2015-05-08 17:49:29 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::DiagramImageItem
|
|
|
|
Constructor with pixmap
|
|
|
|
@param pixmap the pixmap to be draw
|
|
|
|
@param parent_item the parent graphic item
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, QetGraphicsItem *parent_item):
|
|
|
|
QetGraphicsItem(parent_item),
|
|
|
|
pixmap_(pixmap)
|
|
|
|
{
|
|
|
|
setTransformOriginPoint(boundingRect().center());
|
2015-05-08 17:49:29 +00:00
|
|
|
setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::~DiagramImageItem
|
|
|
|
Destructor
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
DiagramImageItem::~DiagramImageItem()
|
|
|
|
{
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::paint
|
|
|
|
Draw the pixmap.
|
|
|
|
@param painter the Qpainter to use for draw the pixmap
|
|
|
|
@param option the style option
|
|
|
|
@param widget the QWidget where we draw the pixmap
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
|
|
|
painter -> drawPixmap(pixmap_.rect(),pixmap_);
|
|
|
|
|
2014-10-19 11:25:03 +00:00
|
|
|
Q_UNUSED(option); Q_UNUSED(widget);
|
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
if (isSelected()) {
|
|
|
|
painter -> save();
|
|
|
|
// Annulation des renderhints
|
|
|
|
painter -> setRenderHint(QPainter::Antialiasing, false);
|
|
|
|
painter -> setRenderHint(QPainter::TextAntialiasing, false);
|
|
|
|
painter -> setRenderHint(QPainter::SmoothPixmapTransform, false);
|
2020-08-16 14:24:51 +02:00
|
|
|
// Dessin du cadre de selection en noir à partir du boundingrect
|
2013-11-14 10:11:22 +00:00
|
|
|
QPen t(Qt::black);
|
|
|
|
t.setStyle(Qt::DashLine);
|
|
|
|
painter -> setPen(t);
|
|
|
|
painter -> drawRect(boundingRect());
|
|
|
|
painter -> restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::editProperty
|
2022-12-04 08:21:12 -05:00
|
|
|
Open the appropriate dialog to edit this image
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2015-05-08 17:49:29 +00:00
|
|
|
void DiagramImageItem::editProperty()
|
|
|
|
{
|
2013-11-14 10:11:22 +00:00
|
|
|
if (diagram() -> isReadOnly()) return;
|
2015-06-23 20:40:05 +00:00
|
|
|
PropertiesEditorDialog dialog(new ImagePropertiesWidget(this), QApplication::activeWindow());
|
2015-05-08 17:49:29 +00:00
|
|
|
dialog.exec();
|
2013-11-14 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::setPixmap
|
|
|
|
Set the new pixmap to be draw
|
|
|
|
@param pixmap the new pixmap
|
|
|
|
*/
|
2013-11-14 10:11:22 +00:00
|
|
|
void DiagramImageItem::setPixmap(const QPixmap &pixmap) {
|
|
|
|
pixmap_ = pixmap;
|
|
|
|
setTransformOriginPoint(boundingRect().center());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::boundingRect
|
|
|
|
the outer bounds of the item as a rectangle,
|
|
|
|
if no pixmap are set, return a default QRectF
|
|
|
|
@return a QRectF represent the bounding rectangle
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QRectF DiagramImageItem::boundingRect() const
|
|
|
|
{
|
2013-11-14 10:11:22 +00:00
|
|
|
if (!pixmap_.isNull()) {
|
|
|
|
return (QRectF(pixmap_.rect()));
|
|
|
|
} else {
|
|
|
|
QRectF bound;
|
|
|
|
return (bound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 17:45:11 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::name
|
|
|
|
@return the generic name of this item (picture)
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QString DiagramImageItem::name() const
|
|
|
|
{
|
2014-10-11 17:45:11 +00:00
|
|
|
return tr("une image");
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief DiagramImageItem::fromXml
|
2022-12-04 08:21:12 -05:00
|
|
|
Load this image from xml element e
|
2020-08-16 11:19:36 +02:00
|
|
|
@param e
|
2022-12-04 08:21:12 -05:00
|
|
|
@return true if successfully loaded.
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2018-06-17 18:21:56 +00:00
|
|
|
bool DiagramImageItem::fromXml(const QDomElement &e)
|
|
|
|
{
|
|
|
|
if (e.tagName() != "image") {
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:11:22 +00:00
|
|
|
QDomNode image_node = e.firstChild();
|
2018-06-17 18:21:56 +00:00
|
|
|
if (!image_node.isText()) {
|
|
|
|
return (false);
|
|
|
|
}
|
2013-11-14 10:11:22 +00:00
|
|
|
|
2020-08-18 21:28:52 +02:00
|
|
|
//load xml image to QByteArray
|
2013-11-14 10:11:22 +00:00
|
|
|
QByteArray array;
|
2015-03-02 20:14:56 +00:00
|
|
|
array = QByteArray::fromBase64(e.text().toLatin1());
|
2013-11-14 10:11:22 +00:00
|
|
|
|
2020-08-18 21:28:52 +02:00
|
|
|
//Set QPixmap from the array
|
2013-11-14 10:11:22 +00:00
|
|
|
QPixmap pixmap;
|
|
|
|
pixmap.loadFromData(array);
|
|
|
|
setPixmap(pixmap);
|
|
|
|
|
|
|
|
setScale(e.attribute("size").toDouble());
|
2018-03-27 19:04:43 +00:00
|
|
|
setRotation(e.attribute("rotation").toDouble());
|
2015-05-17 17:47:50 +00:00
|
|
|
//We directly call setPos from QGraphicsObject, because QetGraphicsItem will snap to grid
|
|
|
|
QGraphicsObject::setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
|
2018-06-17 18:21:56 +00:00
|
|
|
setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
|
2014-11-13 12:12:53 +00:00
|
|
|
is_movable_ = (e.attribute("is_movable").toInt());
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@param document Le document XML a utiliser
|
|
|
|
@return L'element XML representant l'image
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
QDomElement DiagramImageItem::toXml(QDomDocument &document) const
|
|
|
|
{
|
2013-11-14 10:11:22 +00:00
|
|
|
QDomElement result = document.createElement("image");
|
|
|
|
//write some attribute
|
2018-06-17 18:21:56 +00:00
|
|
|
result.setAttribute("x", QString::number(pos().x()));
|
|
|
|
result.setAttribute("y", QString::number(pos().y()));
|
|
|
|
result.setAttribute("z", QString::number(this->zValue()));
|
|
|
|
result.setAttribute("rotation", QString::number(QET::correctAngle(rotation())));
|
|
|
|
result.setAttribute("size", QString::number(scale()));
|
2014-11-13 11:04:34 +00:00
|
|
|
result.setAttribute("is_movable", bool(is_movable_));
|
2013-11-14 10:11:22 +00:00
|
|
|
|
|
|
|
//write the pixmap in the xml element after he was been transformed to base64
|
|
|
|
QByteArray array;
|
|
|
|
QBuffer buffer(&array);
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
|
|
|
pixmap_.save(&buffer, "PNG");
|
|
|
|
QDomText base64 = document.createTextNode(array.toBase64());
|
|
|
|
result.appendChild(base64);
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|