diff options
Diffstat (limited to 'examples/widgets/gestures/imagegestures')
4 files changed, 50 insertions, 29 deletions
diff --git a/examples/widgets/gestures/imagegestures/CMakeLists.txt b/examples/widgets/gestures/imagegestures/CMakeLists.txt index e24b5b58bf..1e173dc63f 100644 --- a/examples/widgets/gestures/imagegestures/CMakeLists.txt +++ b/examples/widgets/gestures/imagegestures/CMakeLists.txt @@ -4,16 +4,10 @@ cmake_minimum_required(VERSION 3.16) project(imagegestures LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/gestures/imagegestures") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) +qt_standard_project_setup() + qt_add_executable(imagegestures imagewidget.cpp imagewidget.h main.cpp @@ -25,14 +19,21 @@ set_target_properties(imagegestures PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(imagegestures PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets +target_link_libraries(imagegestures PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets ) install(TARGETS imagegestures - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET imagegestures + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc index 7722da680a..46053566ab 100644 --- a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc +++ b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc @@ -4,12 +4,13 @@ /*! \example gestures/imagegestures \title Image Gestures Example + \examplecategory {User Interface Components} \brief Demonstrates the use of simple gestures in a widget. This example shows how to enable gestures for a widget and use gesture input to perform actions. - \image imagegestures-example.jpg + \image imagegestures-example.png We use two classes to create the user interface for the application: \c MainWidget and \c ImageWidget. The \c MainWidget class is simply used as a container for the 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(); diff --git a/examples/widgets/gestures/imagegestures/imagewidget.h b/examples/widgets/gestures/imagegestures/imagewidget.h index 7e8142442a..1f4010baac 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.h +++ b/examples/widgets/gestures/imagegestures/imagewidget.h @@ -4,6 +4,7 @@ #ifndef IMAGEWIDGET_H #define IMAGEWIDGET_H +#include <QFileInfo> #include <QImage> #include <QLoggingCategory> #include <QWidget> @@ -24,7 +25,7 @@ class ImageWidget : public QWidget public: ImageWidget(QWidget *parent = nullptr); - void openDirectory(const QString &path); + void openDirectory(const QString &url); void grabGestures(const QList<Qt::GestureType> &gestures); protected: @@ -40,14 +41,14 @@ private: void swipeTriggered(QSwipeGesture*); //! [class definition begin] - QImage loadImage(const QString &fileName) const; + QImage loadImage(const QFileInfo &fileInfo) const; void loadImage(); void goNextImage(); void goPrevImage(); void goToImage(int index); QString path; - QStringList files; + QFileInfoList files; int position; QImage prevImage, nextImage; |