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 <david.faure@kdab.com>
This commit is contained in:
parent
81ba16cad9
commit
3f39c0f76c
@ -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
|
** Contact: http://www.qt-project.org/legal
|
||||||
**
|
**
|
||||||
** This file is part of the documentation of the Qt Toolkit.
|
** This file is part of the documentation of the Qt Toolkit.
|
||||||
@ -112,15 +112,19 @@
|
|||||||
{ImageViewer}'s appearance.
|
{ImageViewer}'s appearance.
|
||||||
|
|
||||||
\snippet widgets/imageviewer/imageviewer.cpp 1
|
\snippet widgets/imageviewer/imageviewer.cpp 1
|
||||||
|
|
||||||
|
In the \c open() slot, we show a file dialog to the user. We compile
|
||||||
|
a list of mime types for use as a filter by querying QImageReader
|
||||||
|
for the available mime type names.
|
||||||
|
|
||||||
|
We show the file dialog until a valid file name is entered or
|
||||||
|
the user cancels.
|
||||||
|
|
||||||
|
The function \c loadFile() is used to load the image.
|
||||||
|
|
||||||
\snippet widgets/imageviewer/imageviewer.cpp 2
|
\snippet widgets/imageviewer/imageviewer.cpp 2
|
||||||
|
|
||||||
In the \c open() slot, we show a file dialog to the user. The
|
In the \c loadFile() function, we check if the file's
|
||||||
easiest way to create a QFileDialog is to use the static
|
|
||||||
convenience functions. QFileDialog::getOpenFileName() returns an
|
|
||||||
existing file selected by the user. If the user presses \uicontrol
|
|
||||||
Cancel, QFileDialog returns an empty string.
|
|
||||||
|
|
||||||
Unless the file name is a empty string, we check if the file's
|
|
||||||
format is an image format by constructing a QImage which tries to
|
format is an image format by constructing a QImage which tries to
|
||||||
load the image from the file. If the constructor returns a null
|
load the image from the file. If the constructor returns a null
|
||||||
image, we use a QMessageBox to alert the user.
|
image, we use a QMessageBox to alert the user.
|
||||||
@ -135,7 +139,6 @@
|
|||||||
information message with an \uicontrol OK button (the default) is
|
information message with an \uicontrol OK button (the default) is
|
||||||
sufficient, since the message is part of a normal operation.
|
sufficient, since the message is part of a normal operation.
|
||||||
|
|
||||||
\snippet widgets/imageviewer/imageviewer.cpp 3
|
|
||||||
\snippet widgets/imageviewer/imageviewer.cpp 4
|
\snippet widgets/imageviewer/imageviewer.cpp 4
|
||||||
|
|
||||||
If the format is supported, we display the image in \c imageLabel
|
If the format is supported, we display the image in \c imageLabel
|
||||||
|
@ -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
|
** Contact: http://www.qt-project.org/legal
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -61,38 +61,60 @@ ImageViewer::ImageViewer()
|
|||||||
createActions();
|
createActions();
|
||||||
createMenus();
|
createMenus();
|
||||||
|
|
||||||
setWindowTitle(tr("Image Viewer"));
|
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
|
||||||
resize(500, 400);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//! [0]
|
//! [0]
|
||||||
|
//! [2]
|
||||||
|
|
||||||
|
bool ImageViewer::loadFile(const QString &fileName)
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
//! [3] //! [4]
|
||||||
|
scaleFactor = 1.0;
|
||||||
|
|
||||||
|
printAct->setEnabled(true);
|
||||||
|
fitToWindowAct->setEnabled(true);
|
||||||
|
updateActions();
|
||||||
|
|
||||||
|
if (!fitToWindowAct->isChecked())
|
||||||
|
imageLabel->adjustSize();
|
||||||
|
|
||||||
|
setWindowFilePath(fileName);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! [4]
|
||||||
|
|
||||||
|
//! [2]
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
void ImageViewer::open()
|
void ImageViewer::open()
|
||||||
//! [1] //! [2]
|
|
||||||
{
|
{
|
||||||
QString fileName = QFileDialog::getOpenFileName(this,
|
QStringList mimeTypeFilters;
|
||||||
tr("Open File"), QDir::currentPath());
|
foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
|
||||||
if (!fileName.isEmpty()) {
|
mimeTypeFilters.append(mimeTypeName);
|
||||||
QImage image(fileName);
|
mimeTypeFilters.sort();
|
||||||
if (image.isNull()) {
|
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
|
||||||
QMessageBox::information(this, tr("Image Viewer"),
|
QFileDialog dialog(this, tr("Open File"),
|
||||||
tr("Cannot load %1.").arg(fileName));
|
picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.first());
|
||||||
return;
|
dialog.setAcceptMode(QFileDialog::AcceptOpen);
|
||||||
}
|
dialog.setMimeTypeFilters(mimeTypeFilters);
|
||||||
//! [2] //! [3]
|
dialog.selectMimeTypeFilter("image/jpeg");
|
||||||
imageLabel->setPixmap(QPixmap::fromImage(image));
|
|
||||||
//! [3] //! [4]
|
|
||||||
scaleFactor = 1.0;
|
|
||||||
|
|
||||||
printAct->setEnabled(true);
|
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
|
||||||
fitToWindowAct->setEnabled(true);
|
|
||||||
updateActions();
|
|
||||||
|
|
||||||
if (!fitToWindowAct->isChecked())
|
|
||||||
imageLabel->adjustSize();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//! [4]
|
//! [1]
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
void ImageViewer::print()
|
void ImageViewer::print()
|
||||||
|
@ -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
|
** Contact: http://www.qt-project.org/legal
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -61,6 +61,7 @@ class ImageViewer : public QMainWindow
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ImageViewer();
|
ImageViewer();
|
||||||
|
bool loadFile(const QString &);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void open();
|
void open();
|
||||||
|
@ -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
|
** Contact: http://www.qt-project.org/legal
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -39,13 +39,23 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QCommandLineParser>
|
||||||
|
|
||||||
#include "imageviewer.h"
|
#include "imageviewer.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
QGuiApplication::setApplicationDisplayName(ImageViewer::tr("Image Viewer"));
|
||||||
|
QCommandLineParser commandLineParser;
|
||||||
|
commandLineParser.addHelpOption();
|
||||||
|
commandLineParser.addPositionalArgument(ImageViewer::tr("[file]"), ImageViewer::tr("Image file to open."));
|
||||||
|
commandLineParser.process(QCoreApplication::arguments());
|
||||||
ImageViewer imageViewer;
|
ImageViewer imageViewer;
|
||||||
|
if (!commandLineParser.positionalArguments().isEmpty()
|
||||||
|
&& !imageViewer.loadFile(commandLineParser.positionalArguments().front())) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
imageViewer.show();
|
imageViewer.show();
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user