File indexing completed on 2024-05-12 16:34:24

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2011 Silvio Heinrich <plassy@web.de>
0004  * Copyright (C) 2012 Inge Wallin <inge@lysator.liu.se>
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 #ifndef PICTURESHAPE_H
0023 #define PICTURESHAPE_H
0024 
0025 #include <QPainterPath>
0026 #include <QPixmap>
0027 #include <QImage>
0028 #include <QRunnable>
0029 
0030 #include <KoTosContainer.h>
0031 #include <KoFrameShape.h>
0032 #include <SvgShape.h>
0033 
0034 #include "ClippingRect.h"
0035 
0036 #define PICTURESHAPEID "PictureShape"
0037 
0038 class KoImageData;
0039 class KoImageCollection;
0040 class PictureShape;
0041 class KoClipPath;
0042 
0043 namespace _Private
0044 {
0045     /**
0046      * This class acts as a proxy for the PictureShape class
0047      * since it is not possible to add slots to it
0048      * (MOC always complains)
0049      */
0050     class PictureShapeProxy: public QObject
0051     {
0052         Q_OBJECT
0053     public:
0054         explicit PictureShapeProxy(PictureShape *p):
0055             m_pictureShape(p) { }
0056 
0057     public Q_SLOTS:
0058         void setImage(const QString& key, const QImage& image);
0059 
0060     private:
0061         PictureShape *m_pictureShape;
0062     };
0063 
0064     /**
0065      * This class will scale an image to a given size.
0066      * Instances of this class can be executed in a thread pool
0067      * therefore the scaling process can be done in the background
0068      */
0069     class PixmapScaler: public QObject, public QRunnable
0070     {
0071         Q_OBJECT
0072     public:
0073         PixmapScaler(PictureShape *pictureShape, const QSize &pixmapSize);
0074         void run() override;
0075 
0076     Q_SIGNALS:
0077         void finished(const QString &, const QImage &);
0078 
0079     private:
0080         QSize m_size;
0081         QImage m_image;
0082         quint64 m_imageKey;
0083     };
0084 
0085     /**
0086      * This method will create an outline path out of the image
0087      */
0088     QPainterPath generateOutline(const QImage &imageIn, int threshold = 20);
0089 }
0090 
0091 
0092 class PictureShape : public KoTosContainer, public KoFrameShape, public SvgShape
0093 {
0094     friend class _Private::PixmapScaler;
0095     friend class _Private::PictureShapeProxy;
0096 
0097 public:
0098     // Odf 1.2: 20.313  style:mirror
0099     // The value could be 0, or a combination of one of the Horizontal* and/or Vertical
0100     // separated by whitespace.
0101     enum MirrorMode {
0102         MirrorNone             = 0x00,
0103         MirrorHorizontal       = 0x01,
0104         MirrorHorizontalOnEven = 0x02,
0105         MirrorHorizontalOnOdd  = 0x04,
0106         MirrorVertical         = 0x08,
0107 
0108         MirrorMask = 0x0f      // Only used as a mask, never as a value.
0109     };
0110 
0111     enum ColorMode {
0112         Standard,
0113         Greyscale,
0114         Mono,
0115         Watermark
0116     };
0117 
0118     PictureShape();
0119 
0120     // reimplemented
0121     void paint(QPainter &painter, const KoViewConverter &converter, KoShapePaintingContext &paintcontext) override;
0122     // reimplemented
0123     QPainterPath shadowOutline() const override;
0124     // reimplemented
0125     void saveOdf(KoShapeSavingContext &context) const override;
0126     // reimplemented
0127     bool loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context) override;
0128     // reimplemented
0129     void waitUntilReady(const KoViewConverter &converter, bool asynchronous) const override;
0130     // reimplemented from SvgShape
0131     bool saveSvg(SvgSavingContext &context) override;
0132     // reimplemented from SvgShape
0133     bool loadSvg(const KoXmlElement &element, SvgLoadingContext &context) override;
0134     /**
0135      * Get the collection used in the shape.
0136      */
0137     KoImageCollection *imageCollection() const;
0138     KoImageData *imageData() const;
0139     QFlags<MirrorMode> mirrorMode() const;
0140     ColorMode colorMode() const;
0141     QRectF cropRect() const;
0142     bool isPictureInProportion() const;
0143 
0144     void setImageCollection(KoImageCollection *collection) { m_imageCollection = collection; }
0145     void setCropRect(const QRectF& rect);
0146     void setMirrorMode(QFlags<MirrorMode> mode);
0147     void setColorMode(ColorMode mode);
0148     void setColoring(qreal red, qreal green, qreal blue, qreal luminance, qreal contrast);
0149     void setGamma(qreal gamma);
0150     KoClipPath *generateClipPath();
0151 
0152 
0153 protected:
0154     bool loadOdfFrameElement(const KoXmlElement &element, KoShapeLoadingContext &context) override;
0155     QString saveStyle(KoGenStyle &style, KoShapeSavingContext &context) const override;
0156     void loadStyle(const KoXmlElement &element, KoShapeLoadingContext &context) override;
0157 
0158 private:
0159     QSize calcOptimalPixmapSize(const QSizeF &shapeSize, const QSizeF &imageSize) const;
0160     ClippingRect parseClippingRectString(const QString &string) const;
0161 
0162 private:
0163     KoImageCollection *m_imageCollection;
0164     mutable QImage m_printQualityImage;
0165     mutable QSizeF m_printQualityRequestedSize;
0166 
0167     QFlags<MirrorMode> m_mirrorMode;
0168     ColorMode m_colorMode;
0169     ClippingRect m_clippingRect;
0170 
0171     _Private::PictureShapeProxy m_proxy;
0172 };
0173 
0174 #endif