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.cpp38
1 files changed, 28 insertions, 10 deletions
diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp
index e17bf3c0c0..189281cc11 100644
--- a/examples/widgets/gestures/imagegestures/imagewidget.cpp
+++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp
@@ -41,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();
@@ -144,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();
@@ -187,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();
}
@@ -204,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();
}
@@ -234,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();