2727
2828#include < QFileInfo>
2929#include < QImage>
30- #include < QPixmap>
3130#include < QFile>
3231#include < QByteArray>
33- #include < QGraphicsItem>
3432#include < QPainter>
3533#include < QDebug>
3634
3735#include " ../IDecoder.h"
36+ #include " Internal/GraphicsItems/RasterizedImageGraphicsItem.h"
3837#include " Internal/DecoderAutoRegistrator.h"
3938#include " Internal/Utils/ZLibUtils.h"
4039
@@ -75,18 +74,17 @@ const char *wmfErrorToString(wmf_error_t error)
7574
7675// ====================================================================================================
7776
78- class WmfGraphicsItem : public QGraphicsItem
77+ class WmfPixmapProvider : public RasterizedImageGraphicsItem ::IRasterizedImageProvider
7978{
8079public:
81- WmfGraphicsItem (const QString &filePath)
80+ WmfPixmapProvider (const QString &filePath)
8281 : m_isValid(false )
8382 , m_API(NULL )
8483 , m_ddata(NULL )
8584 , m_width(0 )
8685 , m_height(0 )
8786 , m_minScaleFactor(1 )
8887 , m_maxScaleFactor(1 )
89- , m_cachedScaleFactor(0 )
9088 {
9189 memset (&m_bbox, 0 , sizeof (wmfD_Rect));
9290
@@ -164,7 +162,7 @@ class WmfGraphicsItem : public QGraphicsItem
164162 m_maxScaleFactor = std::min (MAX_IMAGE_DIMENSION / m_width, MAX_IMAGE_DIMENSION / m_height);
165163 }
166164
167- ~WmfGraphicsItem ()
165+ ~WmfPixmapProvider ()
168166 {
169167 if (!isValid ())
170168 return ;
@@ -182,37 +180,7 @@ class WmfGraphicsItem : public QGraphicsItem
182180 return QRectF (0 , 0 , m_width, m_height);
183181 }
184182
185- void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = NULL )
186- {
187- Q_UNUSED (option);
188- Q_UNUSED (widget);
189- if (!isValid ())
190- return ;
191-
192- const QRectF identityRect = QRectF (0 , 0 , 1 , 1 );
193- const QRectF deviceTransformedRect = painter->deviceTransform ().mapRect (identityRect);
194- const qreal deviceScaleFactor = std::max (deviceTransformedRect.width (), deviceTransformedRect.height ());
195- const qreal actualScaleFactor = std::min (std::max (m_minScaleFactor, deviceScaleFactor), m_maxScaleFactor);
196- if (std::abs (actualScaleFactor - m_cachedScaleFactor) / std::max (actualScaleFactor, m_cachedScaleFactor) > 1e-2 )
197- {
198- const bool previousPixmapIsValid = !m_cachedPixmap.isNull ();
199- m_cachedPixmap = QPixmap ();
200- m_cachedPixmap = getPixmap (actualScaleFactor);
201- if (m_cachedPixmap.isNull () && previousPixmapIsValid)
202- {
203- m_maxScaleFactor = m_cachedScaleFactor;
204- m_cachedPixmap = getPixmap (m_cachedScaleFactor);
205- }
206- else
207- {
208- m_cachedScaleFactor = actualScaleFactor;
209- }
210- }
211- painter->drawPixmap (boundingRect (), m_cachedPixmap, QRectF (0 , 0 , m_width * m_cachedScaleFactor, m_height * m_cachedScaleFactor));
212- }
213-
214- protected:
215- QPixmap getPixmap (const qreal scaleFactor)
183+ QImage image (const qreal scaleFactor)
216184 {
217185 m_ddata->type = wmf_gd_image;
218186 m_ddata->bbox .TL .x = m_bbox.TL .x ;
@@ -226,14 +194,14 @@ class WmfGraphicsItem : public QGraphicsItem
226194 if (error != wmf_E_None)
227195 {
228196 qWarning () << " Couldn't decode WMF file into pixbuf:" << wmfErrorToString (error);
229- return QPixmap ();
197+ return QImage ();
230198 }
231199
232200 int *gdPixels = NULL ;
233201 if (m_ddata->gd_image == NULL || (gdPixels = wmf_gd_image_pixels (m_ddata->gd_image )) == NULL )
234202 {
235203 qWarning () << " Couldn't decode WMF file - no output (huh?)" << wmfErrorToString (error);
236- return QPixmap ();
204+ return QImage ();
237205 }
238206
239207 QImage image (static_cast <int >(m_ddata->width ), static_cast <int >(m_ddata->height ), QImage::Format_ARGB32);
@@ -254,11 +222,21 @@ class WmfGraphicsItem : public QGraphicsItem
254222 *(imagePtr++) = qRgba (r, g, b, a);
255223 }
256224 }
257- return QPixmap::fromImage (image);
225+ return image;
226+ }
227+
228+ qreal minScaleFactor () const
229+ {
230+ return m_minScaleFactor;
231+ }
232+
233+ qreal maxScaleFactor () const
234+ {
235+ return m_maxScaleFactor;
258236 }
259237
260238private:
261- Q_DISABLE_COPY (WmfGraphicsItem )
239+ Q_DISABLE_COPY (WmfPixmapProvider )
262240
263241 bool m_isValid;
264242 QByteArray m_inBuffer;
@@ -269,9 +247,6 @@ class WmfGraphicsItem : public QGraphicsItem
269247 unsigned int m_height;
270248 qreal m_minScaleFactor;
271249 qreal m_maxScaleFactor;
272-
273- QPixmap m_cachedPixmap;
274- qreal m_cachedScaleFactor;
275250};
276251
277252// ====================================================================================================
@@ -302,13 +277,11 @@ class DecoderLibWmf : public IDecoder
302277 if (!fileInfo.exists () || !fileInfo.isReadable ())
303278 return NULL ;
304279
305- WmfGraphicsItem *item = new WmfGraphicsItem (filePath);
306- if (!item->isValid ())
307- {
308- delete item;
280+ QSharedPointer<WmfPixmapProvider> provider (new WmfPixmapProvider (filePath));
281+ if (!provider->isValid ())
309282 return NULL ;
310- }
311- return item ;
283+
284+ return new RasterizedImageGraphicsItem (provider) ;
312285 }
313286};
314287
0 commit comments