summaryrefslogtreecommitdiffstats
path: root/examples/widgets/gestures/imagegestures/imagewidget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/gestures/imagegestures/imagewidget.cpp')
-rw-r--r--examples/widgets/gestures/imagegestures/imagewidget.cpp89
1 files changed, 30 insertions, 59 deletions
diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp
index 072ec2b216..189281cc11 100644
--- a/examples/widgets/gestures/imagegestures/imagewidget.cpp
+++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp
@@ -1,52 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "imagewidget.h"
@@ -88,6 +41,12 @@ void ImageWidget::paintEvent(QPaintEvent*)
{
QPainter p(this);
+ if (files.isEmpty() && !path.isEmpty()) {
+ p.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap,
+ tr("No supported image formats found"));
+ return;
+ }
+
const qreal iw = currentImage.width();
const qreal ih = currentImage.height();
const qreal wh = height();
@@ -191,20 +150,32 @@ void ImageWidget::resizeEvent(QResizeEvent*)
update();
}
-void ImageWidget::openDirectory(const QString &path)
+void ImageWidget::openDirectory(const QString &url)
{
- this->path = path;
+ path = url;
QDir dir(path);
- const QStringList nameFilters{"*.jpg", "*.png"};
- files = dir.entryList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
+
+ QStringList nameFilters;
+ const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
+ for (const QByteArray &format : supportedFormats)
+ nameFilters.append(QLatin1String("*.") + QString::fromLatin1(format));
+ files = dir.entryInfoList(nameFilters, QDir::Files|QDir::Readable, QDir::Name);
position = 0;
goToImage(0);
update();
}
-QImage ImageWidget::loadImage(const QString &fileName) const
+/*
+ With Android's content scheme paths, it might not be possible to simply
+ append a file name to the chosen directory path to be able to open the image,
+ because usually paths are returned by an Android file provider and handling those
+ paths manually is not guaranteed to work. For that reason, it's better to keep
+ around QFileInfo objects and use absoluteFilePath().
+*/
+QImage ImageWidget::loadImage(const QFileInfo &fileInfo) const
{
+ const QString fileName = fileInfo.absoluteFilePath();
QImageReader reader(fileName);
reader.setAutoTransform(true);
qCDebug(lcExample) << "loading" << QDir::toNativeSeparators(fileName) << position << '/' << files.size();
@@ -234,7 +205,7 @@ void ImageWidget::goNextImage()
prevImage = currentImage;
currentImage = nextImage;
if (position+1 < files.size())
- nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1));
+ nextImage = loadImage(files.at(position + 1));
else
nextImage = QImage();
}
@@ -251,7 +222,7 @@ void ImageWidget::goPrevImage()
nextImage = currentImage;
currentImage = prevImage;
if (position > 0)
- prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1));
+ prevImage = loadImage(files.at(position - 1));
else
prevImage = QImage();
}
@@ -281,12 +252,12 @@ void ImageWidget::goToImage(int index)
position = index;
if (index > 0)
- prevImage = loadImage(path + QLatin1Char('/') + files.at(position-1));
+ prevImage = loadImage(files.at(position - 1));
else
prevImage = QImage();
- currentImage = loadImage(path + QLatin1Char('/') + files.at(position));
+ currentImage = loadImage(files.at(position));
if (position+1 < files.size())
- nextImage = loadImage(path + QLatin1Char('/') + files.at(position+1));
+ nextImage = loadImage(files.at(position + 1));
else
nextImage = QImage();
update();