From 3f39c0f76cec121181ab9d6a0cf057c1b1aeb25c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 4 Jun 2014 12:25:56 +0200 Subject: Polish the image viewer example. - Open files passed on the command line. - Point the file dialog to the pictures location and use a filter string for the supported types. - Set the window title according to file name. Task-number: QTBUG-37203 Task-number: QTBUG-39287 Change-Id: I4e5e43875c3a7544c862c054181e75942939c1d5 Reviewed-by: David Faure --- .../widgets/widgets/imageviewer/imageviewer.cpp | 68 ++++++++++++++-------- 1 file changed, 45 insertions(+), 23 deletions(-) (limited to 'examples/widgets/widgets/imageviewer/imageviewer.cpp') diff --git a/examples/widgets/widgets/imageviewer/imageviewer.cpp b/examples/widgets/widgets/imageviewer/imageviewer.cpp index 77ec92d57a..eae94a2499 100644 --- a/examples/widgets/widgets/imageviewer/imageviewer.cpp +++ b/examples/widgets/widgets/imageviewer/imageviewer.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the examples of the Qt Toolkit. @@ -61,39 +61,61 @@ ImageViewer::ImageViewer() createActions(); createMenus(); - setWindowTitle(tr("Image Viewer")); - resize(500, 400); + resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5); } + //! [0] +//! [2] -//! [1] -void ImageViewer::open() -//! [1] //! [2] +bool ImageViewer::loadFile(const QString &fileName) { - QString fileName = QFileDialog::getOpenFileName(this, - tr("Open File"), QDir::currentPath()); - if (!fileName.isEmpty()) { - QImage image(fileName); - if (image.isNull()) { - QMessageBox::information(this, tr("Image Viewer"), - tr("Cannot load %1.").arg(fileName)); - return; - } + QImage image(fileName); + if (image.isNull()) { + QMessageBox::information(this, QGuiApplication::applicationDisplayName(), + tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName))); + setWindowFilePath(QString()); + imageLabel->setPixmap(QPixmap()); + imageLabel->adjustSize(); + return false; + } //! [2] //! [3] - imageLabel->setPixmap(QPixmap::fromImage(image)); + imageLabel->setPixmap(QPixmap::fromImage(image)); //! [3] //! [4] - scaleFactor = 1.0; + scaleFactor = 1.0; + + printAct->setEnabled(true); + fitToWindowAct->setEnabled(true); + updateActions(); - printAct->setEnabled(true); - fitToWindowAct->setEnabled(true); - updateActions(); + if (!fitToWindowAct->isChecked()) + imageLabel->adjustSize(); - if (!fitToWindowAct->isChecked()) - imageLabel->adjustSize(); - } + setWindowFilePath(fileName); + return true; } + //! [4] +//! [2] + +//! [1] +void ImageViewer::open() +{ + QStringList mimeTypeFilters; + foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes()) + mimeTypeFilters.append(mimeTypeName); + mimeTypeFilters.sort(); + const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation); + QFileDialog dialog(this, tr("Open File"), + picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.first()); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + dialog.setMimeTypeFilters(mimeTypeFilters); + dialog.selectMimeTypeFilter("image/jpeg"); + + while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {} +} +//! [1] + //! [5] void ImageViewer::print() //! [5] //! [6] -- cgit v1.2.3