ImageViewer: Handle invalid SVGs and GIFs nicely

Fail opening the file in the image viewer, like we do for other images,
so Qt Creator can offer a different editor (binary editor).

We have separate code paths/backends for SVGs and movies (including
GIFs), which didn't catch invalid images early.

Fixes: QTCREATORBUG-27121
Change-Id: I1bfbca70b7d76ee9cac3e0f35494b62e3856069e
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Robert Löhning <robert.loehning@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2022-02-28 11:10:44 +01:00
parent 2dfa9f0882
commit 897907f401

View File

@@ -111,7 +111,7 @@ Core::IDocument::OpenResult ImageViewerFile::openImpl(QString *errorString,
if (format.startsWith("svg")) { if (format.startsWith("svg")) {
m_tempSvgItem = new QGraphicsSvgItem(fileName); m_tempSvgItem = new QGraphicsSvgItem(fileName);
QRectF bound = m_tempSvgItem->boundingRect(); QRectF bound = m_tempSvgItem->boundingRect();
if (qFuzzyIsNull(bound.width()) && qFuzzyIsNull(bound.height())) { if (!bound.isValid() || (qFuzzyIsNull(bound.width()) && qFuzzyIsNull(bound.height()))) {
delete m_tempSvgItem; delete m_tempSvgItem;
m_tempSvgItem = nullptr; m_tempSvgItem = nullptr;
if (errorString) if (errorString)
@@ -123,8 +123,17 @@ Core::IDocument::OpenResult ImageViewerFile::openImpl(QString *errorString,
} else } else
#endif #endif
if (QMovie::supportedFormats().contains(format)) { if (QMovie::supportedFormats().contains(format)) {
m_type = TypeMovie;
m_movie = new QMovie(fileName, QByteArray(), this); m_movie = new QMovie(fileName, QByteArray(), this);
// force reading movie/image data, so we can catch completely invalid movies/images early:
m_movie->jumpToNextFrame();
if (!m_movie->isValid()) {
if (errorString)
*errorString = tr("Failed to read image.");
delete m_movie;
m_movie = nullptr;
return OpenResult::CannotHandle;
}
m_type = TypeMovie;
m_movie->setCacheMode(QMovie::CacheAll); m_movie->setCacheMode(QMovie::CacheAll);
connect( connect(
m_movie, m_movie,