qdnd.cpp: Extract Method imageMimeFormats

imageReadMimeFormats and imageWriteMimeFormats had
duplicated source code.

While touching the code, do some optimizations:
- use QStringBuilder more
- replace index based loop with 'range for'

Change-Id: I174456bf4e55e030930b9b5a4aaef57c722d8f05
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Anton Kudryavtsev 2016-08-18 12:31:26 +03:00
parent 8ff2955f99
commit 25dd9c521f

View File

@ -149,17 +149,12 @@ Qt::DropAction QDragManager::drag(QDrag *o)
#if !(defined(QT_NO_DRAGANDDROP) && defined(QT_NO_CLIPBOARD)) #if !(defined(QT_NO_DRAGANDDROP) && defined(QT_NO_CLIPBOARD))
static QStringList imageReadMimeFormats() static QStringList imageMimeFormats(const QList<QByteArray> &imageFormats)
{ {
QStringList formats; QStringList formats;
QList<QByteArray> imageFormats = QImageReader::supportedImageFormats(); formats.reserve(imageFormats.size());
const int numImageFormats = imageFormats.size(); for (const auto &format : imageFormats)
formats.reserve(numImageFormats); formats.append(QLatin1String("image/") + QLatin1String(format.toLower()));
for (int i = 0; i < numImageFormats; ++i) {
QString format = QLatin1String("image/");
format += QString::fromLatin1(imageFormats.at(i).toLower());
formats.append(format);
}
//put png at the front because it is best //put png at the front because it is best
int pngIndex = formats.indexOf(QLatin1String("image/png")); int pngIndex = formats.indexOf(QLatin1String("image/png"));
@ -169,25 +164,15 @@ static QStringList imageReadMimeFormats()
return formats; return formats;
} }
static inline QStringList imageReadMimeFormats()
static QStringList imageWriteMimeFormats()
{ {
QStringList formats; return imageMimeFormats(QImageReader::supportedImageFormats());
QList<QByteArray> imageFormats = QImageWriter::supportedImageFormats(); }
const int numImageFormats = imageFormats.size();
formats.reserve(numImageFormats);
for (int i = 0; i < numImageFormats; ++i) {
QString format = QLatin1String("image/");
format += QString::fromLatin1(imageFormats.at(i).toLower());
formats.append(format);
}
//put png at the front because it is best
int pngIndex = formats.indexOf(QLatin1String("image/png"));
if (pngIndex != -1 && pngIndex != 0)
formats.move(pngIndex, 0);
return formats; static inline QStringList imageWriteMimeFormats()
{
return imageMimeFormats(QImageWriter::supportedImageFormats());
} }
QInternalMimeData::QInternalMimeData() QInternalMimeData::QInternalMimeData()