]> git.sven.stormbind.net Git - sven/vym.git/blob - src/imageobj.cpp
New upstream version 2.9.22
[sven/vym.git] / src / imageobj.cpp
1 #include "imageobj.h"
2
3 #include "file.h"
4 #include "mapobj.h"
5
6 #include <QDebug>
7 #include <QDir>
8 #include <QPainter>
9 #include <QStyleOptionGraphicsItem>
10 #include <QSvgGenerator>
11
12 extern QDir cacheDir;
13 extern ulong imageLastID;
14
15 /////////////////////////////////////////////////////////////////
16 // ImageObj
17 /////////////////////////////////////////////////////////////////
18 ImageObj::ImageObj()
19 {
20     // qDebug() << "Const ImageObj ()  this=" << this;
21     init();
22 }
23
24 ImageObj::ImageObj(QGraphicsItem *parent) : QGraphicsItem(parent)
25 {
26     // qDebug() << "Const ImageObj  this=" << this << "  parent= " << parent ;
27     init();
28 }
29
30 ImageObj::~ImageObj()
31 {
32     // qDebug() << "Destr ImageObj  this=" << this << "  imageType = " <<
33     // imageType ;
34     switch (imageType) {
35     case ImageObj::SVG:
36     case ImageObj::ClonedSVG:
37         if (svgItem)
38             delete (svgItem);
39         break;
40     case ImageObj::Pixmap:
41         if (pixmapItem)
42             delete (pixmapItem);
43         break;
44     default:
45         qDebug() << "Destr ImgObj: imageType undefined";
46         break;
47     }
48 }
49
50 void ImageObj::init()
51 {
52     // qDebug() << "Const ImageObj (scene)";
53     hide();
54
55     // Assign ID
56     imageLastID++;
57     imageID = imageLastID;
58
59     imageType = ImageObj::Undefined;
60     svgItem = NULL;
61     pixmapItem = NULL;
62     scaleFactor = 1;
63 }
64
65 void ImageObj::copy(ImageObj *other)
66 {
67     prepareGeometryChange();
68     if (imageType != ImageObj::Undefined)
69         qWarning() << "ImageObj::copy into existing image of type "
70                    << imageType;
71
72     switch (other->imageType) {
73     case ImageObj::SVG:
74     case ImageObj::ClonedSVG:
75         if (!other->svgCachePath.isEmpty()) {
76             load(other->svgCachePath, true);
77         }
78         else
79             qWarning() << "ImgObj::copy svg: no svgCachePath available.";
80
81         svgItem->setVisible(isVisible());
82         break;
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;
89         break;
90     default:
91         qWarning() << "ImgObj::copy other->imageType undefined";
92         return;
93         break;
94     }
95     setScaleFactor(other->scaleFactor);
96 }
97
98 void ImageObj::setPos(const QPointF &pos)
99 {
100     switch (imageType) {
101     case ImageObj::SVG:
102     case ImageObj::ClonedSVG:
103         svgItem->setPos(pos);
104         break;
105     case ImageObj::Pixmap:
106         pixmapItem->setPos(pos);
107         break;
108     default:
109         break;
110     }
111 }
112
113 void ImageObj::setPos(const qreal &x, const qreal &y) { setPos(QPointF(x, y)); }
114
115 void ImageObj::setZValue(qreal z)
116 {
117     switch (imageType) {
118     case ImageObj::SVG:
119     case ImageObj::ClonedSVG:
120         svgItem->setZValue(z);
121         break;
122     case ImageObj::Pixmap:
123         pixmapItem->setZValue(z);
124         break;
125     default:
126         break;
127     }
128 }
129
130 void ImageObj::setVisibility(bool v)
131 {
132     switch (imageType) {
133     case ImageObj::SVG:
134     case ImageObj::ClonedSVG:
135         v ? svgItem->show() : svgItem->hide();
136         break;
137     case ImageObj::Pixmap:
138         v ? pixmapItem->show() : pixmapItem->hide();
139         break;
140     default:
141         break;
142     }
143 }
144
145 void ImageObj::setWidth(qreal w)
146 {
147     if (boundingRect().width() == 0)
148         return;
149
150     setScaleFactor(w / boundingRect().width());
151 }
152
153 void ImageObj::setScaleFactor(qreal f)
154 {
155     scaleFactor = f;
156     switch (imageType) {
157     case ImageObj::SVG:
158     case ImageObj::ClonedSVG:
159         svgItem->setScale(f);
160         break;
161     case ImageObj::Pixmap:
162         if (f != 1)
163             pixmapItem->setScale(f);
164         break;
165     default:
166         break;
167     }
168 }
169
170 qreal ImageObj::getScaleFactor() { return scaleFactor; }
171
172 QRectF ImageObj::boundingRect() const
173 {
174     switch (imageType) {
175     case ImageObj::SVG:
176     case ImageObj::ClonedSVG:
177         return svgItem->mapToScene(svgItem->boundingRect()).boundingRect();
178     case ImageObj::Pixmap:
179         return pixmapItem->mapToScene(pixmapItem->boundingRect()).boundingRect();
180     default:
181         break;
182     }
183     return QRectF();
184 }
185
186 void ImageObj::paint(QPainter *painter, const QStyleOptionGraphicsItem *sogi,
187                      QWidget *widget)
188 {
189     // Not really called, but required because paint is pure virtual in
190     // QGraphicsItem
191
192     switch (imageType) {
193     case ImageObj::SVG:
194     case ImageObj::ClonedSVG:
195         svgItem->paint(painter, sogi, widget);
196         break;
197     case ImageObj::Pixmap:
198         pixmapItem->paint(painter, sogi, widget);
199         break;
200     default:
201         break;
202     }
203 }
204
205 bool ImageObj::load(const QString &fn, bool createClone)
206 {
207     // createClone == true, if called via copy()
208
209     if (imageType != ImageObj::Undefined) {
210         qWarning() << "ImageObj::load (" << fn
211                    << ") into existing image of type " << imageType;
212         return false;
213     }
214
215     if (fn.toLower().endsWith(".svg")) {
216         svgItem = new QGraphicsSvgItem(fn);
217         if (scene())
218             scene()->addItem(svgItem);
219
220         if (createClone) {
221             imageType = ImageObj::ClonedSVG;
222             svgCachePath = fn;
223         }
224         else {
225             imageType = ImageObj::SVG;
226
227             // Copy original file to cache
228             QFile svgFile(fn);
229             QString newPath = cacheDir.path() + "/" + QString().number(imageID) +
230                               "-" + basename(fn);
231             if (!svgFile.copy(newPath)) {
232                 qWarning() << "ImageObj::load (" << fn
233                            << ") could not be copied to " << newPath;
234             }
235
236             svgCachePath = newPath;
237         }
238
239         return true;
240     }
241     else {
242         QPixmap pm;
243         if (pm.load(fn)) {
244             prepareGeometryChange();
245
246             if (pixmapItem)
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;
253
254             return true;
255         }
256     }
257
258     return false;
259 }
260
261 bool ImageObj::save(const QString &fn)
262 {
263     switch (imageType) {
264     case ImageObj::SVG:
265     case ImageObj::ClonedSVG:
266         if (svgItem) {
267             QFile svgFile(svgCachePath);
268             if (!QFile(fn).exists() && !svgFile.copy(fn)) {
269                 qWarning() << "ImageObj::save  failed to copy " << svgCachePath
270                            << " to " << fn;
271                 return false;
272             }
273         }
274         return true;
275         break;
276     case ImageObj::Pixmap:
277         return pixmapItem->pixmap().save(fn, "PNG", 100);
278         break;
279     default:
280         break;
281     }
282     return false;
283 }
284
285 QString ImageObj::getExtension()
286 {
287     QString s;
288     switch (imageType) {
289     case ImageObj::SVG:
290     case ImageObj::ClonedSVG:
291         s = ".svg";
292         break;
293     case ImageObj::Pixmap:
294         s = ".png";
295         break;
296     default:
297         break;
298     }
299     return s;
300 }
301
302 ImageObj::ImageType ImageObj::getType() { return imageType; }
303
304 QIcon ImageObj::getIcon()
305 {
306     switch (imageType) {
307     case ImageObj::SVG:
308     case ImageObj::ClonedSVG:
309         return QPixmap(svgCachePath);
310         break;
311     case ImageObj::Pixmap:
312         return QIcon(pixmapItem->pixmap());
313         break;
314     default:
315         break;
316     }
317     return QIcon();
318 }