📄 qpicture.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpicture.h"#include <private/qpicture_p.h>#ifndef QT_NO_PICTURE#include <private/qfactoryloader_p.h>#include <private/qpaintengine_pic_p.h>#include "qdatastream.h"#include "qfile.h"#include "qimage.h"#include "qmutex.h"#include "qpainter.h"#include "qpainterpath.h"#include "qpixmap.h"#include "qregion.h"#include "qdebug.h"/*! \class QPicture qpicture.h \brief The QPicture class is a paint device that records and replays QPainter commands. \ingroup multimedia \ingroup shared \mainclass A picture serializes painter commands to an IO device in a platform-independent format. They are sometimes referred to as meta-files. Qt pictures use a proprietary binary format. Unlike native picture (meta-file) formats on many window systems, Qt pictures have no limitations regarding their contents. Everything that can be painted on a widget or pixmap (e.g., fonts, pixmaps, regions, transformed graphics, etc.) can also be stored in a picture. QPicture is resolution independent, i.e. a QPicture can be displayed on different devices (for example svg, pdf, ps, printer and screen) looking the same. This is, for instance, needed for WYSIWYG print preview. QPicture runs in the default system dpi, and scales the painter to match differences in resolution depending on the window system. QPicture is an \link shclass.html implicitly shared\endlink class. Example of how to record a picture: \quotefromfile snippets/picture/picture.cpp \skipto RECORD \skipto QPicture \printuntil save Example of how to replay a picture: \quotefromfile snippets/picture/picture.cpp \skipto REPLAY \skipto QPicture \printuntil end(); Pictures can also be drawn using play(). Some basic data about a picture is available, for example, size(), isNull() and boundingRect(). \sa QMovie*/const char *qt_mfhdr_tag = "QPIC"; // header tagstatic const quint16 mfhdr_maj = 8; // major version #static const quint16 mfhdr_min = 0; // minor version #extern int qt_defaultDpi();/*! Constructs an empty picture. The \a formatVersion parameter may be used to \e create a QPicture that can be read by applications that are compiled with earlier versions of Qt. Note that the default formatVersion is -1 which signifies the current release, i.e. for Qt 4.0 a formatVersion of 7 is the same as the default formatVersion of -1. Reading pictures generated by earlier versions of Qt is not supported in Qt 4.0.*/QPicture::QPicture(int formatVersion) : QPaintDevice(), d_ptr(new QPicturePrivate){ Q_D(QPicture); d_ptr->q_ptr = this; d->paintEngine = 0; if (formatVersion == 0) qWarning("QPicture: invalid format version 0"); // still accept the 0 default from before Qt 3.0. if (formatVersion > 0 && formatVersion != (int)mfhdr_maj) { d->formatMajor = formatVersion; d->formatMinor = 0; d->formatOk = false; } else { d->resetFormat(); }}/*! Constructs a \link shclass.html shallow copy\endlink of \a pic.*/QPicture::QPicture(const QPicture &pic) : QPaintDevice(), d_ptr(pic.d_ptr){ d_func()->ref.ref();}/*! \internal */QPicture::QPicture(QPicturePrivate &dptr) : QPaintDevice(), d_ptr(&dptr){ d_ptr->q_ptr = this;}/*! Destroys the picture.*/QPicture::~QPicture(){ if (!d_func()->ref.deref()) { delete d_func()->paintEngine; delete d_func(); }}/*! \internal*/int QPicture::devType() const{ return QInternal::Picture;}/*! \fn bool QPicture::isNull() const Returns true if the picture contains no data; otherwise returns false.*//*! \fn uint QPicture::size() const Returns the size of the picture data. \sa data()*//*! \fn const char* QPicture::data() const Returns a pointer to the picture data. The pointer is only valid until the next non-const function is called on this picture. The returned pointer is 0 if the picture contains no data. \sa size(), isNull()*/bool QPicture::isNull() const{ return d_func()->pictb.buffer().isNull();}uint QPicture::size() const{ return d_func()->pictb.buffer().size();}const char* QPicture::data() const{ return d_func()->pictb.buffer();}void QPicture::detach(){ if (d_func()->ref != 1) detach_helper();}bool QPicture::isDetached() const{ return d_func()->ref == 1;}/*! Sets the picture data directly from \a data and \a size. This function copies the input data. \sa data(), size()*/void QPicture::setData(const char* data, uint size){ detach(); d_func()->pictb.setData(data, size); d_func()->resetFormat(); // we'll have to check}/*! Loads a picture from the file specified by \a fileName and returns true if successful; otherwise returns false. By default, the file will be interpreted as being in the native QPicture format. Specifying the \a format string is optional and is only needed for importing picture data stored in a different format. \sa save()*/bool QPicture::load(const QString &fileName, const char *format){ QFile f(fileName); if (!f.open(QIODevice::ReadOnly)) return false; return load(&f, format);}/*! \overload \a dev is the device to use for loading.*/bool QPicture::load(QIODevice *dev, const char *format){ if(format) {#ifndef QT_NO_PICTUREIO QPictureIO io(dev, format); bool result = io.read(); if (result) { operator=(io.picture()); } else if (format)#else bool result = false;#endif { qWarning("QPicture::load: No such picture format: %s", format); } return result; } detach(); QByteArray a = dev->readAll(); d_func()->pictb.setData(a); // set byte array in buffer return d_func()->checkFormat();}/*! Saves a picture to the file specified by \a fileName and returns true if successful; otherwise returns false. Specifying the file \a format string is optional. By default the data will be saved in the native QPicture file format. \sa load()*/bool QPicture::save(const QString &fileName, const char *format){ if (paintingActive()) { qWarning("QPicture::save: still being painted on. " "Call QPainter::end() first"); return false; } if(format) {#ifndef QT_NO_PICTUREIO QPictureIO io(fileName, format); bool result = io.write(); if (result) { operator=(io.picture()); } else if (format)#else bool result = false;#endif { qWarning("QPicture::save: No such picture format: %s", format); } return result; } QFile f(fileName); if (!f.open(QIODevice::WriteOnly)) return false; return save(&f, format);}/*! \overload \a dev is the device to use for saving.*/bool QPicture::save(QIODevice *dev, const char *format){ if (paintingActive()) { qWarning("QPicture::save: still being painted on. " "Call QPainter::end() first"); return false; } if(format) {#ifndef QT_NO_PICTUREIO QPictureIO io(dev, format); bool result = io.write(); if (result) { operator=(io.picture()); } else if (format)#else bool result = false;#endif { qWarning("QPicture::save: No such picture format: %s", format); } return result; } dev->write(d_func()->pictb.buffer(), d_func()->pictb.buffer().size()); return true;}/*! Returns the picture's bounding rectangle or an invalid rectangle if the picture contains no data.*/QRect QPicture::boundingRect() const{ Q_D(const QPicture); // Use override rect where possible. if (!d->override_rect.isEmpty()) return d->override_rect; if (!d->formatOk) d_ptr->checkFormat(); return d->brect;}/*! Sets the picture's bounding rectangle to \a r. The automatically calculated value is overridden.*/void QPicture::setBoundingRect(const QRect &r){ d_func()->override_rect = r;}/*! Replays the picture using \a painter, and returns true if successful; otherwise returns false. This function does exactly the same as QPainter::drawPicture() with (x, y) = (0, 0).*/bool QPicture::play(QPainter *painter){ Q_D(QPicture); if (d->pictb.size() == 0) // nothing recorded return true; if (!d->formatOk && !d->checkFormat()) return false; d->pictb.open(QIODevice::ReadOnly); // open buffer device QDataStream s; s.setDevice(&d->pictb); // attach data stream to buffer s.device()->seek(10); // go directly to the data s.setVersion(d->formatMajor == 4 ? 3 : d->formatMajor); quint8 c, clen; quint32 nrecords; s >> c >> clen; Q_ASSERT(c == QPicturePrivate::PdcBegin); // bounding rect was introduced in ver 4. Read in checkFormat(). if (d->formatMajor >= 4) { qint32 dummy; s >> dummy >> dummy >> dummy >> dummy; } s >> nrecords; if (!exec(painter, s, nrecords)) { qWarning("QPicture::play: Format error"); d->pictb.close(); return false; } d->pictb.close(); return true; // no end-command}/*! \internal Iterates over the internal picture data and draws the picture using \a painter.*/bool QPicture::exec(QPainter *painter, QDataStream &s, int nrecords)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -