src/gui/image/qimagewriter.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   115 #include <private/qpnghandler_p.h>
   115 #include <private/qpnghandler_p.h>
   116 #endif
   116 #endif
   117 
   117 
   118 QT_BEGIN_NAMESPACE
   118 QT_BEGIN_NAMESPACE
   119 
   119 
   120 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   120 #ifndef QT_NO_LIBRARY
   121 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   121 Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
   122                           (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
   122                           (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
   123 #endif
   123 #endif
   124 
   124 
   125 static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,
   125 static QImageIOHandler *createWriteHandlerHelper(QIODevice *device,
   127 {
   127 {
   128     QByteArray form = format.toLower();
   128     QByteArray form = format.toLower();
   129     QByteArray suffix;
   129     QByteArray suffix;
   130     QImageIOHandler *handler = 0;
   130     QImageIOHandler *handler = 0;
   131 
   131 
   132 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   132 #ifndef QT_NO_LIBRARY
   133     // check if any plugins can write the image
   133     // check if any plugins can write the image
   134     QFactoryLoader *l = loader();
   134     QFactoryLoader *l = loader();
   135     QStringList keys = l->keys();
   135     QStringList keys = l->keys();
   136     int suffixPluginIndex = -1;
   136     int suffixPluginIndex = -1;
   137 #endif
   137 #endif
   140         // if there's no format, see if \a device is a file, and if so, find
   140         // if there's no format, see if \a device is a file, and if so, find
   141         // the file suffix and find support for that format among our plugins.
   141         // the file suffix and find support for that format among our plugins.
   142         // this allows plugins to override our built-in handlers.
   142         // this allows plugins to override our built-in handlers.
   143         if (QFile *file = qobject_cast<QFile *>(device)) {
   143         if (QFile *file = qobject_cast<QFile *>(device)) {
   144             if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
   144             if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) {
   145 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   145 #ifndef QT_NO_LIBRARY
   146                 int index = keys.indexOf(QString::fromLatin1(suffix));
   146                 int index = keys.indexOf(QString::fromLatin1(suffix));
   147                 if (index != -1)
   147                 if (index != -1)
   148                     suffixPluginIndex = index;
   148                     suffixPluginIndex = index;
   149 #endif
   149 #endif
   150             }
   150             }
   151         }
   151         }
   152     }
   152     }
   153 
   153 
   154     QByteArray testFormat = !form.isEmpty() ? form : suffix;
   154     QByteArray testFormat = !form.isEmpty() ? form : suffix;
   155 
   155 
   156 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   156 #ifndef QT_NO_LIBRARY
   157     if (suffixPluginIndex != -1) {
   157     if (suffixPluginIndex != -1) {
   158         // when format is missing, check if we can find a plugin for the
   158         // when format is missing, check if we can find a plugin for the
   159         // suffix.
   159         // suffix.
   160         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix)));
   160         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(QString::fromLatin1(suffix)));
   161         if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))
   161         if (plugin && (plugin->capabilities(device, suffix) & QImageIOPlugin::CanWrite))
   162             handler = plugin->create(device, suffix);
   162             handler = plugin->create(device, suffix);
   163     }
   163     }
   164 #endif // Q_NO_LIBRARY
   164 #endif // QT_NO_LIBRARY
   165 
   165 
   166     // check if any built-in handlers can write the image
   166     // check if any built-in handlers can write the image
   167     if (!handler && !testFormat.isEmpty()) {
   167     if (!handler && !testFormat.isEmpty()) {
   168         if (false) {
   168         if (false) {
   169 #ifndef QT_NO_IMAGEFORMAT_PNG
   169 #ifndef QT_NO_IMAGEFORMAT_PNG
   190             handler->setOption(QImageIOHandler::SubType, testFormat);
   190             handler->setOption(QImageIOHandler::SubType, testFormat);
   191 #endif
   191 #endif
   192         }
   192         }
   193     }
   193     }
   194 
   194 
   195 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   195 #ifndef QT_NO_LIBRARY
   196     if (!testFormat.isEmpty()) {
   196     if (!testFormat.isEmpty()) {
   197         for (int i = 0; i < keys.size(); ++i) {
   197         for (int i = 0; i < keys.size(); ++i) {
   198             QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
   198             QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
   199             if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
   199             if (plugin && (plugin->capabilities(device, testFormat) & QImageIOPlugin::CanWrite)) {
   200                 delete handler;
   200                 delete handler;
   201                 handler = plugin->create(device, testFormat);
   201                 handler = plugin->create(device, testFormat);
   202                 break;
   202                 break;
   203             }
   203             }
   204         }
   204         }
   205     }
   205     }
   206 #endif
   206 #endif // QT_NO_LIBRARY
   207 
   207 
   208     if (!handler)
   208     if (!handler)
   209         return 0;
   209         return 0;
   210 
   210 
   211     handler->setDevice(device);
   211     handler->setDevice(device);
   668 #endif
   668 #endif
   669 #ifndef QT_NO_IMAGEFORMAT_PNG
   669 #ifndef QT_NO_IMAGEFORMAT_PNG
   670     formats << "png";
   670     formats << "png";
   671 #endif
   671 #endif
   672 
   672 
   673 #if !defined (QT_NO_LIBRARY) && !defined(QT_NO_SETTINGS)
   673 #ifndef QT_NO_LIBRARY
   674     QFactoryLoader *l = loader();
   674     QFactoryLoader *l = loader();
   675     QStringList keys = l->keys();
   675     QStringList keys = l->keys();
   676     for (int i = 0; i < keys.count(); ++i) {
   676     for (int i = 0; i < keys.count(); ++i) {
   677         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
   677         QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));
   678         if (plugin && (plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanWrite) != 0)
   678         if (plugin && (plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanWrite) != 0)