src/qt3support/painting/q3picture.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "private/qpicture_p.h"
       
    43 #include "qfile.h"
       
    44 #include "qpainter.h"
       
    45 #include "q3picture.h"
       
    46 #include "q3paintengine_svg_p.h"
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 class Q3SvgDevice : public QPaintDevice
       
    51 {
       
    52 public:
       
    53     Q3SvgDevice() : QPaintDevice() {}
       
    54     bool load(QIODevice *dev) { return svgEngine.load(dev); }
       
    55     bool save(const QString &fileName) { return svgEngine.save(fileName); }
       
    56     bool save(QIODevice *dev) { return svgEngine.save(dev); }
       
    57     void setBoundingRect(const QRect &rect) { svgEngine.setBoundingRect(rect); }
       
    58     QRect boundingRect() const { return svgEngine.boundingRect(); }
       
    59     QPaintEngine *paintEngine() const { return (QPaintEngine *)&svgEngine; }
       
    60     bool play(QPainter *p) { return svgEngine.play(p); }
       
    61     int metric(PaintDeviceMetric m) const;
       
    62 
       
    63 private:
       
    64     Q3SVGPaintEngine svgEngine;
       
    65 };
       
    66 
       
    67 int Q3SvgDevice::metric(PaintDeviceMetric m) const
       
    68 {
       
    69         int val;
       
    70         QRect br = svgEngine.boundingRect();
       
    71         switch (m) {
       
    72         case PdmWidth:
       
    73             val = br.width();
       
    74             break;
       
    75         case PdmHeight:
       
    76             val = br.height();
       
    77             break;
       
    78         case PdmWidthMM:
       
    79             val = int(25.4/72.0*br.width());
       
    80             break;
       
    81         case PdmHeightMM:
       
    82             val = int(25.4/72.0*br.height());
       
    83             break;
       
    84         case PdmDpiX:
       
    85             val = 72;
       
    86             break;
       
    87         case PdmDpiY:
       
    88             val = 72;
       
    89             break;
       
    90         case PdmNumColors:
       
    91             val = 16777216;
       
    92             break;
       
    93         case PdmDepth:
       
    94             val = 24;
       
    95             break;
       
    96         default:
       
    97             val = 0;
       
    98             qWarning("Q3SvgDevice::metric: Invalid metric command");
       
    99         }
       
   100         return val;
       
   101 }
       
   102 
       
   103 /*!
       
   104     \class Q3Picture
       
   105     \brief The Q3Picture class is a paint device that records and
       
   106     replays Q3Painter commands.
       
   107 
       
   108     \compat
       
   109 
       
   110     Q3Picture can also read and write SVG (Scalable Vector Graphics)
       
   111     files, a \l{http://www.w3.org/Graphics/SVG/} {W3C} XML format.
       
   112     Note that when using the load() and save() functions to read and
       
   113     write SVG files, the format must be specified.
       
   114 
       
   115     \sa QPicture
       
   116 */
       
   117 
       
   118 /*!
       
   119     \fn Q3Picture::Q3Picture()
       
   120 
       
   121     Constructs a Q3Picture.
       
   122 */
       
   123 
       
   124 /*!
       
   125     \fn Q3Picture::Q3Picture(const QPicture &other)
       
   126 
       
   127     Constructs a copy of \a other.
       
   128 */
       
   129 
       
   130 /*!
       
   131     \overload
       
   132     Loads the picture in the specified \a format from a file with the
       
   133     given \a fileName. Returns true if the file is loaded successfully;
       
   134     otherwise returns false.
       
   135 */
       
   136 bool Q3Picture::load(const QString &fileName, const char *format)
       
   137 {
       
   138     QFile f(fileName);
       
   139     if (!f.open(QIODevice::ReadOnly))
       
   140         return false;
       
   141     return load(&f, format);
       
   142 }
       
   143 
       
   144 /*!
       
   145     \fn bool Q3Picture::load(QIODevice *device, const char *format)
       
   146 
       
   147     Loads the picture in the specified \a format from the given \a device.
       
   148     Returns true if the file is loaded successfully; otherwise returns false.
       
   149 
       
   150     Note that when using the load() function to read SVG files, the
       
   151     format must be specified. For example:
       
   152 
       
   153     \snippet doc/src/snippets/code/src_qt3support_painting_q3picture.cpp 0
       
   154 
       
   155     \sa save()
       
   156 */
       
   157 bool Q3Picture::load(QIODevice *dev, const char *format)
       
   158 {
       
   159     if (qstrcmp(format, "svg" ) == 0) {
       
   160 	Q3SvgDevice svg;
       
   161 	if (!svg.load(dev))
       
   162 	    return false;
       
   163         QPainter p(this);
       
   164         p.setRenderHint(QPainter::Antialiasing);
       
   165 	bool b = svg.play(&p);
       
   166 	d_func()->brect = svg.boundingRect();
       
   167 	return b;
       
   168     }
       
   169     return QPicture::load(dev, format);
       
   170 }
       
   171 
       
   172 /*!
       
   173     \overload
       
   174     Saves the picture in the specified \a format to the file with the
       
   175     given \a fileName.
       
   176 
       
   177     \sa load()
       
   178 */
       
   179 bool Q3Picture::save(const QString &fileName, const char *format)
       
   180 {
       
   181     if (paintingActive()) {
       
   182         qWarning("Q3Picture::save: still being painted on. "
       
   183                   "Call QPainter::end() first");
       
   184         return false;
       
   185     }
       
   186 
       
   187     // identical to QIODevice* code below but the file name
       
   188     // makes a difference when it comes to saving pixmaps
       
   189     if (qstricmp( format, "svg") == 0) {
       
   190 	Q3SvgDevice svg;
       
   191 	QPainter p(&svg);
       
   192 	if (!play(&p))
       
   193 	    return false;
       
   194 	svg.setBoundingRect(boundingRect());
       
   195 	return svg.save(fileName);
       
   196     }
       
   197 
       
   198     return QPicture::save(fileName, format);
       
   199 }
       
   200 
       
   201 /*!
       
   202     \fn bool Q3Picture::save(QIODevice *device, const char *format)
       
   203 
       
   204     Saves the picture in the specified \a format to the given \a device.
       
   205     Returns true if the save is successful. Returns false if, for
       
   206     example, the picture is still being painted, i.e., QPainter::end()
       
   207     has not yet been called.
       
   208 
       
   209     Note that when using the save() function to save SVG files, the
       
   210     format must be specified. For example:
       
   211 
       
   212     \snippet doc/src/snippets/code/src_qt3support_painting_q3picture.cpp 1
       
   213 
       
   214     \sa load()
       
   215 */
       
   216 bool Q3Picture::save(QIODevice *dev, const char *format)
       
   217 {
       
   218     if (paintingActive()) {
       
   219         qWarning("Q3Picture::save: still being painted on. "
       
   220                   "Call QPainter::end() first");
       
   221         return false;
       
   222     }
       
   223 
       
   224     if (qstricmp(format, "svg") == 0) {
       
   225 	Q3SvgDevice svg;
       
   226 	QPainter p(&svg);
       
   227 	if (!play(&p))
       
   228 	    return false;
       
   229 	svg.setBoundingRect(boundingRect());
       
   230 	return svg.save(dev);
       
   231     }
       
   232     return QPicture::save(dev, format);
       
   233 }
       
   234 
       
   235 QT_END_NAMESPACE