QElectroTech  0.70
diagramimageitem.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2013 QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "diagramimageitem.h"
19 #include "diagram.h"
21 #include "imagepropertieswidget.h"
22 
29  QetGraphicsItem(parent_item)
30 {
31  setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
32 }
33 
40 DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, QetGraphicsItem *parent_item):
41  QetGraphicsItem(parent_item),
42  pixmap_(pixmap)
43 {
44  setTransformOriginPoint(boundingRect().center());
45  setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemSendsGeometryChanges);
46 }
47 
53 }
54 
62 void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
63  painter -> drawPixmap(pixmap_.rect(),pixmap_);
64 
65  Q_UNUSED(option); Q_UNUSED(widget);
66 
67  if (isSelected()) {
68  painter -> save();
69  // Annulation des renderhints
70  painter -> setRenderHint(QPainter::Antialiasing, false);
71  painter -> setRenderHint(QPainter::TextAntialiasing, false);
72  painter -> setRenderHint(QPainter::SmoothPixmapTransform, false);
73  // Dessin du cadre de selection en noir à partir du boundingrect
74  QPen t(Qt::black);
75  t.setStyle(Qt::DashLine);
76  painter -> setPen(t);
77  painter -> drawRect(boundingRect());
78  painter -> restore();
79  }
80 }
81 
87 {
88  if (diagram() -> isReadOnly()) return;
89  PropertiesEditorDialog dialog(new ImagePropertiesWidget(this), QApplication::activeWindow());
90  dialog.exec();
91 }
92 
98 void DiagramImageItem::setPixmap(const QPixmap &pixmap) {
99  pixmap_ = pixmap;
100  setTransformOriginPoint(boundingRect().center());
101 }
102 
110  if (!pixmap_.isNull()) {
111  return (QRectF(pixmap_.rect()));
112  } else {
113  QRectF bound;
114  return (bound);
115  }
116 }
117 
122 QString DiagramImageItem::name() const {
123  return tr("une image");
124 }
125 
132 bool DiagramImageItem::fromXml(const QDomElement &e)
133 {
134  if (e.tagName() != "image") {
135  return (false);
136  }
137 
138  QDomNode image_node = e.firstChild();
139  if (!image_node.isText()) {
140  return (false);
141  }
142 
143  //load xml image to QByteArray
144  QByteArray array;
145  array = QByteArray::fromBase64(e.text().toLatin1());
146 
147  //Set QPixmap from the @array
148  QPixmap pixmap;
149  pixmap.loadFromData(array);
150  setPixmap(pixmap);
151 
152  setScale(e.attribute("size").toDouble());
153  setRotation(e.attribute("rotation").toDouble());
154  //We directly call setPos from QGraphicsObject, because QetGraphicsItem will snap to grid
155  QGraphicsObject::setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
156  setZValue(e.attribute("z", QString::number(this->zValue())).toDouble());
157  is_movable_ = (e.attribute("is_movable").toInt());
158 
159  return (true);
160 }
161 
166 QDomElement DiagramImageItem::toXml(QDomDocument &document) const {
167  QDomElement result = document.createElement("image");
168  //write some attribute
169  result.setAttribute("x", QString::number(pos().x()));
170  result.setAttribute("y", QString::number(pos().y()));
171  result.setAttribute("z", QString::number(this->zValue()));
172  result.setAttribute("rotation", QString::number(QET::correctAngle(rotation())));
173  result.setAttribute("size", QString::number(scale()));
174  result.setAttribute("is_movable", bool(is_movable_));
175 
176  //write the pixmap in the xml element after he was been transformed to base64
177  QByteArray array;
178  QBuffer buffer(&array);
179  buffer.open(QIODevice::ReadWrite);
180  pixmap_.save(&buffer, "PNG");
181  QDomText base64 = document.createTextNode(array.toBase64());
182  result.appendChild(base64);
183 
184  return(result);
185 }
The ImagePropertiesWidget class This class provide a widget to edit the propertie of a DiagramImageIt...
~DiagramImageItem() override
DiagramImageItem::~DiagramImageItem Destructor.
QRectF boundingRect() const override
DiagramImageItem::boundingRect the outer bounds of the item as a rectangle, if no pixmap are set...
DiagramImageItem(QetGraphicsItem *=nullptr)
DiagramImageItem::DiagramImageItem Constructor without pixmap.
Diagram * diagram() const
QetGraphicsItem::diagram return the diagram of this item.
virtual bool fromXml(const QDomElement &)
DiagramImageItem::fromXml Load this image fro xml elemebt .
virtual QDomElement toXml(QDomDocument &) const
QString name() const override
DiagramImageItem::name.
void setPixmap(const QPixmap &pixmap)
DiagramImageItem::setPixmap Set the new pixmap to be draw.
The PropertiesEditorDialog class Create a dialog to edit some properties of a thing. Only create a instance of this class and call exec, all is done for you in this class. The first argument (a template) must be a subclass of QWidget and provide the 3 methods bellow : QString::title() void::apply() void::reset() You can subclass the interface PropertiesEditorWidget who provide all this methods. This dialog take ownership of the editor, so the editor will be deleted by this dialog.
qreal correctAngle(const qreal &)
Definition: qet.cpp:505
void editProperty() override
DiagramImageItem::editProperty Open the approriate dialog to edit this image.
QIcon tr
Definition: qeticons.cpp:204
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override
DiagramImageItem::paint Draw the pixmap.