File indexing completed on 2024-05-12 16:35:04

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2009-2011 Inge Wallin <inge@lysator.liu.se>
0004  * Copyright (C) 2011 Boudewijn Rempt <boud@valdyas.org>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 
0022 
0023 #ifndef VECTORSHAPE_H
0024 #define VECTORSHAPE_H
0025 
0026 
0027 // Qt
0028 #include <QByteArray>
0029 #include <QCache>
0030 #include <QSize>
0031 #include <QRunnable>
0032 #include <QMutex>
0033 
0034 // Calligra
0035 #include <KoShape.h>
0036 #include <KoFrameShape.h>
0037 
0038 
0039 #define DEBUG_VECTORSHAPE 0
0040 
0041 
0042 class QPainter;
0043 class VectorShape;
0044 
0045 #define VectorShape_SHAPEID "VectorShapeID"
0046 
0047 class VectorShape : public QObject, public KoShape, public KoFrameShape {
0048     Q_OBJECT
0049 public:
0050     // Type of vector file. Add here when we get support for more.
0051     enum VectorType {
0052         VectorTypeNone,         // Uninitialized
0053         VectorTypeWmf,          // Windows MetaFile
0054         VectorTypeEmf,          // Extended MetaFile
0055         VectorTypeSvm,          // StarView Metafile
0056         VectorTypeSvg           // Scalable Vector Graphics
0057         // ... more here later
0058     };
0059 
0060     VectorShape();
0061     ~VectorShape() override;
0062 
0063     // reimplemented methods.
0064 
0065     /// reimplemented from KoShape
0066     void paint(QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &paintcontext) override;
0067     /// reimplemented from KoShape
0068     void saveOdf(KoShapeSavingContext & context) const override;
0069     /// reimplemented from KoShape
0070     bool loadOdf( const KoXmlElement & element, KoShapeLoadingContext &context ) override;
0071     /// Load the real contents of the frame shape.  reimplemented  from KoFrameShape
0072     bool loadOdfFrameElement(const KoXmlElement& frameElement,
0073                                      KoShapeLoadingContext& context) override;
0074     /// reimplemented from KoShape
0075     void waitUntilReady(const KoViewConverter &converter, bool asynchronous = true) const override;
0076 
0077     // Methods specific to the vector shape.
0078     QByteArray  compressedContents() const;
0079     VectorType vectorType() const;
0080     void  setCompressedContents(const QByteArray &newContents, VectorType vectorType);
0081 
0082     static VectorShape::VectorType vectorType(const QByteArray &contents);
0083 
0084 private Q_SLOTS:
0085     void renderFinished(const QSize &boundingSize, QImage *image);
0086 
0087 private:
0088     static bool isWmf(const QByteArray &bytes);
0089     static bool isEmf(const QByteArray &bytes);
0090     static bool isSvm(const QByteArray &bytes);
0091     static bool isSvg(const QByteArray &bytes);
0092 
0093     // Member variables
0094     mutable VectorType  m_type;
0095     mutable QByteArray  m_contents;
0096     mutable bool m_isRendering;
0097     mutable QMutex m_mutex;
0098     QCache<int, QImage> m_cache;
0099 
0100     QImage* render(const KoViewConverter &converter, bool asynchronous, bool useCache) const;
0101 };
0102 
0103 
0104 class RenderThread : public QObject, public QRunnable
0105 {
0106     Q_OBJECT
0107 public:
0108     RenderThread(const QByteArray &contents, VectorShape::VectorType type,
0109                  const QSizeF &size, const QSize &boundingSize, qreal zoomX, qreal zoomY);
0110     ~RenderThread() override;
0111     void run() override;
0112 Q_SIGNALS:
0113     void finished(const QSize &boundingSize, QImage *image);
0114 private:
0115     void draw(QPainter &painter);
0116     void drawNull(QPainter &painter) const;
0117     void drawWmf(QPainter &painter) const;
0118     void drawEmf(QPainter &painter) const;
0119     void drawSvm(QPainter &painter) const;
0120     void drawSvg(QPainter &painter) const;
0121 private:
0122     const QByteArray m_contents;
0123     VectorShape::VectorType m_type;
0124     QSizeF m_size;
0125     QSize m_boundingSize;
0126     qreal m_zoomX, m_zoomY;
0127 };
0128 
0129 #endif