summaryrefslogtreecommitdiffstats
path: root/src/gui/image
diff options
context:
space:
mode:
authorABBAPOH <ABBAPOH@nextmail.ru>2013-11-30 21:06:57 +0400
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-12-12 12:02:29 +0100
commit4d8a12904a49d6724098610f7e39b11821377b3a (patch)
tree087882e02cdbcc6cd05dd7a598b85949d7c81b63 /src/gui/image
parent37f99502f9006c5681686e4a43bc80c2b1d0b089 (diff)
Check if device is opened before trying to create image handler.
Change-Id: I60f1f6890fdd73e489da4aab9928370163f55f58 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: aavit <eirik.aavitsland@digia.com>
Diffstat (limited to 'src/gui/image')
-rw-r--r--src/gui/image/qimagewriter.cpp49
1 files changed, 35 insertions, 14 deletions
diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp
index 900093b51b..8dd5fdd111 100644
--- a/src/gui/image/qimagewriter.cpp
+++ b/src/gui/image/qimagewriter.cpp
@@ -245,6 +245,8 @@ class QImageWriterPrivate
public:
QImageWriterPrivate(QImageWriter *qq);
+ bool canWriteHelper();
+
// device
QByteArray format;
QIODevice *device;
@@ -282,6 +284,31 @@ QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq)
q = qq;
}
+bool QImageWriterPrivate::canWriteHelper()
+{
+ if (!device) {
+ imageWriterError = QImageWriter::DeviceError;
+ errorString = QT_TRANSLATE_NOOP(QImageWriter,
+ QLatin1String("Device is not set"));
+ return false;
+ }
+ if (!device->isOpen())
+ device->open(QIODevice::WriteOnly);
+ if (!device->isWritable()) {
+ imageWriterError = QImageWriter::DeviceError;
+ errorString = QT_TRANSLATE_NOOP(QImageWriter,
+ QLatin1String("Device not writable"));
+ return false;
+ }
+ if (!handler && (handler = createWriteHandlerHelper(device, format)) == 0) {
+ imageWriterError = QImageWriter::UnsupportedFormatError;
+ errorString = QT_TRANSLATE_NOOP(QImageWriter,
+ QLatin1String("Unsupported image format"));
+ return false;
+ }
+ return true;
+}
+
/*!
Constructs an empty QImageWriter object. Before writing, you must
call setFormat() to set an image format, then setDevice() or
@@ -561,21 +588,15 @@ void QImageWriter::setText(const QString &key, const QString &text)
*/
bool QImageWriter::canWrite() const
{
- if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
- d->imageWriterError = QImageWriter::UnsupportedFormatError;
- d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
- QLatin1String("Unsupported image format"));
- return false;
+ if (QFile *file = qobject_cast<QFile *>(d->device)) {
+ const bool remove = !file->isOpen() && !file->exists();
+ const bool result = d->canWriteHelper();
+ if (!result && remove)
+ file->remove();
+ return result;
}
- if (d->device && !d->device->isOpen())
- d->device->open(QIODevice::WriteOnly);
- if (!d->device || !d->device->isWritable()) {
- d->imageWriterError = QImageWriter::DeviceError;
- d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
- QLatin1String("Device not writable"));
- return false;
- }
- return true;
+
+ return d->canWriteHelper();
}
/*!