📄 qpicture.cpp
字号:
Detaches from shared picture data and makes sure that this picture is the only one referring to the data. If multiple pictures share common data, this picture makes a copy of the data and detaches itself from the sharing mechanism. Nothing is done if there is just a single reference.*//*! \fn bool QPicture::isDetached() const\internal*/void QPicture::detach_helper(){ Q_D(QPicture); QPicturePrivate *x = new QPicturePrivate; int pictsize = size(); x->pictb.setData(data(), pictsize); if (d->pictb.isOpen()) { x->pictb.open(d->pictb.openMode()); x->pictb.seek(d->pictb.pos()); } x->trecs = d->trecs; x->formatOk = d->formatOk; x->formatMinor = d->formatMinor; x->brect = d->brect; x->override_rect = d->override_rect; x = qAtomicSetPtr(&d_ptr, x); if (!x->ref.deref()) delete x;}/*! Assigns picture \a p to this picture and returns a reference to this picture.*/QPicture& QPicture::operator=(const QPicture &p){ qAtomicAssign<QPicturePrivate>(d_ptr, p.d_ptr); return *this;}/*! \internal Sets formatOk to false and resets the format version numbers to default*/void QPicturePrivate::resetFormat(){ formatOk = false; formatMajor = mfhdr_maj; formatMinor = mfhdr_min;}/*! \internal Checks data integrity and format version number. Set formatOk to true on success, to false otherwise. Returns the resulting formatOk value.*/bool QPicturePrivate::checkFormat(){ resetFormat(); // can't check anything in an empty buffer if (pictb.size() == 0 || pictb.isOpen()) return false; pictb.open(QIODevice::ReadOnly); // open buffer device QDataStream s; s.setDevice(&pictb); // attach data stream to buffer char mf_id[4]; // picture header tag s.readRawData(mf_id, 4); // read actual tag if (memcmp(mf_id, qt_mfhdr_tag, 4) != 0) { // wrong header id qWarning("QPicturePaintEngine::checkFormat: Incorrect header"); pictb.close(); return false; } int cs_start = sizeof(quint32); // pos of checksum word int data_start = cs_start + sizeof(quint16); quint16 cs,ccs; QByteArray buf = pictb.buffer(); // pointer to data s >> cs; // read checksum ccs = (quint16) qChecksum(buf.constData() + data_start, buf.size() - data_start); if (ccs != cs) { qWarning("QPicturePaintEngine::checkFormat: Invalid checksum %x, %x expected", ccs, cs); pictb.close(); return false; } quint16 major, minor; s >> major >> minor; // read version number if (major > mfhdr_maj) { // new, incompatible version qWarning("QPicturePaintEngine::checkFormat: Incompatible version %d.%d", major, minor); pictb.close(); return false; } s.setVersion(major != 4 ? major : 3); quint8 c, clen; s >> c >> clen; if (c == QPicturePrivate::PdcBegin) { if (!(major >= 1 && major <= 3)) { qint32 l, t, w, h; s >> l >> t >> w >> h; brect = QRect(l, t, w, h); } } else { qWarning("QPicturePaintEngine::checkFormat: Format error"); pictb.close(); return false; } pictb.close(); formatOk = true; // picture seems to be ok formatMajor = major; formatMinor = minor; return true;}/*! \internal */QPaintEngine *QPicture::paintEngine() const{ if (!d_func()->paintEngine) const_cast<QPicture*>(this)->d_func()->paintEngine = new QPicturePaintEngine; return d_func()->paintEngine;}/***************************************************************************** QPicture stream functions *****************************************************************************//*! \relates QPicture Writes picture \a r to the stream \a s and returns a reference to the stream.*/QDataStream &operator<<(QDataStream &s, const QPicture &r){ quint32 size = r.d_func()->pictb.buffer().size(); s << size; // null picture ? if (size == 0) return s; // just write the whole buffer to the stream s.writeRawData (r.d_func()->pictb.buffer(), r.d_func()->pictb.buffer().size()); return s;}/*! \relates QPicture Reads a picture from the stream \a s into picture \a r and returns a reference to the stream.*/QDataStream &operator>>(QDataStream &s, QPicture &r){ QDataStream sr; // "init"; this code is similar to the beginning of QPicture::cmd() sr.setDevice(&r.d_func()->pictb); sr.setVersion(r.d_func()->formatMajor); quint32 len; s >> len; QByteArray data; if (len > 0) { data.resize(len); s.readRawData(data.data(), len); } r.d_func()->pictb.setData(data); r.d_func()->resetFormat(); return s;}#ifndef QT_NO_PICTUREIO#include "qregexp.h"#include "qapplication.h"#include "qpictureformatplugin.h"/*! \obsolete Returns a string that specifies the picture format of the file \a fileName, or 0 if the file cannot be read or if the format is not recognized. \sa load() save()*/const char* QPicture::pictureFormat(const QString &fileName){ return QPictureIO::pictureFormat(fileName);}/*! \obsolete Returns a list of picture formats that are supported for picture input. \sa outputFormats() inputFormatList() QPictureIO*/QList<QByteArray> QPicture::inputFormats(){ return QPictureIO::inputFormats();}static QStringList qToStringList(const QList<QByteArray> arr){ QStringList list; for (int i = 0; i < arr.count(); ++i) list.append(QString::fromLatin1(arr.at(i))); return list;}/*! \obsolete Returns a list of picture formats that are supported for picture input. Note that if you want to iterate over the list, you should iterate over a copy, e.g. \quotefromfile snippets/picture/picture.cpp \skipto FORMATS \skipto QStringList \printuntil myProcessing \sa outputFormatList() inputFormats() QPictureIO*/QStringList QPicture::inputFormatList(){ return qToStringList(QPictureIO::inputFormats());}/*! \obsolete Returns a list of picture formats that are supported for picture output. Note that if you want to iterate over the list, you should iterate over a copy, e.g. \quotefromfile snippets/picture/picture.cpp \skipto OUTPUT \skipto QStringList \printuntil myProcessing \sa inputFormatList() outputFormats() QPictureIO*/QStringList QPicture::outputFormatList(){ return qToStringList(QPictureIO::outputFormats());}/*! \obsolete Returns a list of picture formats that are supported for picture output. \sa inputFormats() outputFormatList() QPictureIO*/QList<QByteArray> QPicture::outputFormats(){ return QPictureIO::outputFormats();}/***************************************************************************** QPictureIO member functions *****************************************************************************//*! \obsolete \class QPictureIO qpicture.h \brief The QPictureIO class contains parameters for loading and saving pictures. \ingroup multimedia \ingroup io QPictureIO contains a QIODevice object that is used for picture data I/O. The programmer can install new picture file formats in addition to those that Qt provides. You don't normally need to use this class; QPicture::load(), QPicture::save(). \sa QPicture QPixmap QFile*/struct QPictureIOData{ QPicture pi; // picture int iostat; // IO status QByteArray frmt; // picture format QIODevice *iodev; // IO device QString fname; // file name QString descr; // picture description const char *parameters; int quality; float gamma;};/*! Constructs a QPictureIO object with all parameters set to zero.*/QPictureIO::QPictureIO(){ init();}/*! Constructs a QPictureIO object with the I/O device \a ioDevice and a \a format tag.*/QPictureIO::QPictureIO(QIODevice *ioDevice, const char *format){ init(); d->iodev = ioDevice; d->frmt = format;}/*! Constructs a QPictureIO object with the file name \a fileName and a \a format tag.*/QPictureIO::QPictureIO(const QString &fileName, const char* format){ init(); d->frmt = format; d->fname = fileName;}/*! Contains initialization common to all QPictureIO constructors.*/void QPictureIO::init(){ d = new QPictureIOData(); d->parameters = 0; d->quality = -1; // default quality of the current format d->gamma=0.0f; d->iostat = 0; d->iodev = 0;}/*! Destroys the object and all related data.*/QPictureIO::~QPictureIO(){ if (d->parameters) delete [] (char*)d->parameters; delete d;}/***************************************************************************** QPictureIO picture handler functions *****************************************************************************/class QPictureHandler{public: QPictureHandler(const char *f, const char *h, const QByteArray& fl, picture_io_handler r, picture_io_handler w); QByteArray format; // picture format QRegExp header; // picture header pattern enum TMode { Untranslated=0, TranslateIn, TranslateInOut } text_mode; picture_io_handler read_picture; // picture read function picture_io_handler write_picture; // picture write function bool obsolete; // support not "published"};QPictureHandler::QPictureHandler(const char *f, const char *h, const QByteArray& fl, picture_io_handler r, picture_io_handler w) : format(f), header(QString::fromLatin1(h)){ text_mode = Untranslated; if (fl.contains('t')) text_mode = TranslateIn; else if (fl.contains('T')) text_mode = TranslateInOut; obsolete = fl.contains('O'); read_picture = r; write_picture = w;}typedef QList<QPictureHandler *> QPHList;static QPHList pictureHandlers;#ifndef QT_NO_LIBRARYQ_GLOBAL_STATIC(QMutex, mutex)Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, (QPictureFormatInterface_iid, QCoreApplication::libraryPaths(), QLatin1String("/pictureformats")))#endifvoid qt_init_picture_plugins(){#ifndef QT_NO_LIBRARY QMutexLocker locker(mutex()); QFactoryLoader *loader = ::loader(); QStringList keys = loader->keys(); for (int i = 0; i < keys.count(); ++i) if (QPictureFormatInterface *format = qobject_cast<QPictureFormatInterface*>(loader->instance(keys.at(i)))) format->installIOHandler(keys.at(i));#endif}static void cleanup(){ // make sure that picture handlers are delete before plugin manager while (!pictureHandlers.isEmpty()) delete pictureHandlers.takeFirst();}void qt_init_picture_handlers() // initialize picture handlers{ static bool done = false; if (done) return; done = true; qAddPostRoutine(cleanup);}static QPictureHandler *get_picture_handler(const char *format){ // get pointer to handler qt_init_picture_handlers(); qt_init_picture_plugins(); for (int i = 0; i < pictureHandlers.size(); ++i) { if (pictureHandlers.at(i)->format == format) return pictureHandlers.at(i); } return 0; // no such handler}/*! Defines a picture I/O handler for the picture format called \a format, which is recognized using the regular expression defined in \a header, read using \a readPicture and written using \a writePicture. \a flags is a string of single-character flags for this format. The only flag defined currently is T (upper case), so the only legal value for \a flags are "T" and the empty string. The "T" flag means that the picture file is a text file, and Qt should treat all newline conventions as equivalent. (XPM files and some PPM files are text files for example.) \a format is used to select a handler to write a QPicture; \a header is used to select a handler to read an picture file. If \a readPicture is a null pointer, the QPictureIO will not be able to read pictures in \a format. If \a writePicture is a null pointer, the QPictureIO will not be able to write pictures in \a format. If both are null, the QPictureIO object is valid but useless. Example: \quotefromfile snippets/picture/picture.cpp \skipto SVG READ \skipto readSVG \printuntil } \skipto SVG WRITE \skipto writeSVG \printuntil }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -