9 #include <QStyleOptionGraphicsItem>
10 #include <QSvgGenerator>
13 extern ulong imageLastID;
15 /////////////////////////////////////////////////////////////////
17 /////////////////////////////////////////////////////////////////
20 // qDebug() << "Const ImageObj () this=" << this;
24 ImageObj::ImageObj(QGraphicsItem *parent) : QGraphicsItem(parent)
26 // qDebug() << "Const ImageObj this=" << this << " parent= " << parent ;
32 // qDebug() << "Destr ImageObj this=" << this << " imageType = " <<
36 case ImageObj::ClonedSVG:
40 case ImageObj::Pixmap:
45 qDebug() << "Destr ImgObj: imageType undefined";
52 // qDebug() << "Const ImageObj (scene)";
57 imageID = imageLastID;
59 imageType = ImageObj::Undefined;
65 void ImageObj::copy(ImageObj *other)
67 prepareGeometryChange();
68 if (imageType != ImageObj::Undefined)
69 qWarning() << "ImageObj::copy into existing image of type "
72 switch (other->imageType) {
74 case ImageObj::ClonedSVG:
75 if (!other->svgCachePath.isEmpty()) {
76 load(other->svgCachePath, true);
79 qWarning() << "ImgObj::copy svg: no svgCachePath available.";
81 svgItem->setVisible(isVisible());
83 case ImageObj::Pixmap:
84 pixmapItem = new QGraphicsPixmapItem();
85 pixmapItem->setPixmap(other->pixmapItem->pixmap());
86 pixmapItem->setParentItem(parentItem());
87 pixmapItem->setVisible(isVisible());
88 imageType = ImageObj::Pixmap;
91 qWarning() << "ImgObj::copy other->imageType undefined";
95 setScaleFactor(other->scaleFactor);
98 void ImageObj::setPos(const QPointF &pos)
102 case ImageObj::ClonedSVG:
103 svgItem->setPos(pos);
105 case ImageObj::Pixmap:
106 pixmapItem->setPos(pos);
113 void ImageObj::setPos(const qreal &x, const qreal &y) { setPos(QPointF(x, y)); }
115 void ImageObj::setZValue(qreal z)
119 case ImageObj::ClonedSVG:
120 svgItem->setZValue(z);
122 case ImageObj::Pixmap:
123 pixmapItem->setZValue(z);
130 void ImageObj::setVisibility(bool v)
134 case ImageObj::ClonedSVG:
135 v ? svgItem->show() : svgItem->hide();
137 case ImageObj::Pixmap:
138 v ? pixmapItem->show() : pixmapItem->hide();
145 void ImageObj::setWidth(qreal w)
147 if (boundingRect().width() == 0)
150 setScaleFactor(w / boundingRect().width());
153 void ImageObj::setScaleFactor(qreal f)
158 case ImageObj::ClonedSVG:
159 svgItem->setScale(f);
161 case ImageObj::Pixmap:
163 pixmapItem->setScale(f);
170 qreal ImageObj::getScaleFactor() { return scaleFactor; }
172 QRectF ImageObj::boundingRect() const
176 case ImageObj::ClonedSVG:
177 return svgItem->mapToScene(svgItem->boundingRect()).boundingRect();
178 case ImageObj::Pixmap:
179 return pixmapItem->mapToScene(pixmapItem->boundingRect()).boundingRect();
186 void ImageObj::paint(QPainter *painter, const QStyleOptionGraphicsItem *sogi,
189 // Not really called, but required because paint is pure virtual in
194 case ImageObj::ClonedSVG:
195 svgItem->paint(painter, sogi, widget);
197 case ImageObj::Pixmap:
198 pixmapItem->paint(painter, sogi, widget);
205 bool ImageObj::load(const QString &fn, bool createClone)
207 // createClone == true, if called via copy()
209 if (imageType != ImageObj::Undefined) {
210 qWarning() << "ImageObj::load (" << fn
211 << ") into existing image of type " << imageType;
215 if (fn.toLower().endsWith(".svg")) {
216 svgItem = new QGraphicsSvgItem(fn);
218 scene()->addItem(svgItem);
221 imageType = ImageObj::ClonedSVG;
225 imageType = ImageObj::SVG;
227 // Copy original file to cache
229 QString newPath = cacheDir.path() + "/" + QString().number(imageID) +
231 if (!svgFile.copy(newPath)) {
232 qWarning() << "ImageObj::load (" << fn
233 << ") could not be copied to " << newPath;
236 svgCachePath = newPath;
244 prepareGeometryChange();
247 qWarning() << "ImageObj::load " << fn
248 << "pixmapIteam already exists";
249 pixmapItem = new QGraphicsPixmapItem(this);
250 pixmapItem->setPixmap(pm);
251 pixmapItem->setParentItem(parentItem());
252 imageType = ImageObj::Pixmap;
261 bool ImageObj::save(const QString &fn)
265 case ImageObj::ClonedSVG:
267 QFile svgFile(svgCachePath);
268 if (!QFile(fn).exists() && !svgFile.copy(fn)) {
269 qWarning() << "ImageObj::save failed to copy " << svgCachePath
276 case ImageObj::Pixmap:
277 return pixmapItem->pixmap().save(fn, "PNG", 100);
285 QString ImageObj::getExtension()
290 case ImageObj::ClonedSVG:
293 case ImageObj::Pixmap:
302 ImageObj::ImageType ImageObj::getType() { return imageType; }
304 QIcon ImageObj::getIcon()
308 case ImageObj::ClonedSVG:
309 return QPixmap(svgCachePath);
311 case ImageObj::Pixmap:
312 return QIcon(pixmapItem->pixmap());