Extend the image viewer example.

Add:
- Save As
- Copy
- Paste from clipboard into new image

Change-Id: Ibbc308e9bbd2ce407119cd9358874f5c22a6bb83
Reviewed-by: Topi Reiniö <topi.reinio@digia.com>
This commit is contained in:
Friedemann Kleint 2015-08-21 10:17:58 +02:00
parent 08de828a11
commit bc2dabf8ff
2 changed files with 123 additions and 20 deletions

View File

@ -72,25 +72,16 @@ bool ImageViewer::loadFile(const QString &fileName)
{ {
QImageReader reader(fileName); QImageReader reader(fileName);
reader.setAutoTransform(true); reader.setAutoTransform(true);
const QImage image = reader.read(); const QImage newImage = reader.read();
if (image.isNull()) { if (newImage.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(), QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1: %2") tr("Cannot load %1: %2")
.arg(QDir::toNativeSeparators(fileName)), reader.errorString()); .arg(QDir::toNativeSeparators(fileName)), reader.errorString());
return false; return false;
} }
//! [2] //! [3] //! [2]
imageLabel->setPixmap(QPixmap::fromImage(image));
//! [3] //! [4]
scaleFactor = 1.0;
scrollArea->setVisible(true); setImage(newImage);
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
setWindowFilePath(fileName); setWindowFilePath(fileName);
@ -100,28 +91,80 @@ bool ImageViewer::loadFile(const QString &fileName)
return true; return true;
} }
void ImageViewer::setImage(const QImage &newImage)
{
image = newImage;
imageLabel->setPixmap(QPixmap::fromImage(image));
//! [4]
scaleFactor = 1.0;
scrollArea->setVisible(true);
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
imageLabel->adjustSize();
}
//! [4] //! [4]
//! [2] bool ImageViewer::saveFile(const QString &fileName)
{
QImageWriter writer(fileName);
if (!writer.write(image)) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot write %1: %2")
.arg(QDir::toNativeSeparators(fileName)), writer.errorString());
return false;
}
const QString message = tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName));
statusBar()->showMessage(message);
return true;
}
//! [1] //! [1]
void ImageViewer::open()
static void initializeImageFileDialog(QFileDialog &dialog, QFileDialog::AcceptMode acceptMode)
{ {
static bool firstDialog = true;
if (firstDialog) {
firstDialog = false;
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
}
QStringList mimeTypeFilters; QStringList mimeTypeFilters;
foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes()) const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen
? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes();
foreach (const QByteArray &mimeTypeName, supportedMimeTypes)
mimeTypeFilters.append(mimeTypeName); mimeTypeFilters.append(mimeTypeName);
mimeTypeFilters.sort(); mimeTypeFilters.sort();
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
QFileDialog dialog(this, tr("Open File"),
picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setMimeTypeFilters(mimeTypeFilters); dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.selectMimeTypeFilter("image/jpeg"); dialog.selectMimeTypeFilter("image/jpeg");
if (acceptMode == QFileDialog::AcceptSave)
dialog.setDefaultSuffix("jpg");
}
void ImageViewer::open()
{
QFileDialog dialog(this, tr("Open File"));
initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {} while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
} }
//! [1] //! [1]
void ImageViewer::saveAs()
{
QFileDialog dialog(this, tr("Save File As"));
initializeImageFileDialog(dialog, QFileDialog::AcceptSave);
while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
}
//! [5] //! [5]
void ImageViewer::print() void ImageViewer::print()
//! [5] //! [6] //! [5] //! [6]
@ -144,6 +187,43 @@ void ImageViewer::print()
} }
//! [8] //! [8]
void ImageViewer::copy()
{
#ifndef QT_NO_CLIPBOARD
QGuiApplication::clipboard()->setImage(image);
#endif // !QT_NO_CLIPBOARD
}
#ifndef QT_NO_CLIPBOARD
static QImage clipboardImage()
{
if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) {
if (mimeData->hasImage()) {
const QImage image = qvariant_cast<QImage>(mimeData->imageData());
if (!image.isNull())
return image;
}
}
return QImage();
}
#endif // !QT_NO_CLIPBOARD
void ImageViewer::paste()
{
#ifndef QT_NO_CLIPBOARD
const QImage newImage = clipboardImage();
if (newImage.isNull()) {
statusBar()->showMessage(tr("No image in clipboard"));
} else {
setImage(newImage);
setWindowFilePath(QString());
const QString message = tr("Obtained image from clipboard, %1x%2, Depth: %3")
.arg(newImage.width()).arg(newImage.height()).arg(newImage.depth());
statusBar()->showMessage(message);
}
#endif // !QT_NO_CLIPBOARD
}
//! [9] //! [9]
void ImageViewer::zoomIn() void ImageViewer::zoomIn()
//! [9] //! [10] //! [9] //! [10]
@ -207,6 +287,9 @@ void ImageViewer::createActions()
QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open); QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
openAct->setShortcut(QKeySequence::Open); openAct->setShortcut(QKeySequence::Open);
saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &ImageViewer::saveAs);
saveAsAct->setEnabled(false);
printAct = fileMenu->addAction(tr("&Print..."), this, &ImageViewer::print); printAct = fileMenu->addAction(tr("&Print..."), this, &ImageViewer::print);
printAct->setShortcut(QKeySequence::Print); printAct->setShortcut(QKeySequence::Print);
printAct->setEnabled(false); printAct->setEnabled(false);
@ -216,6 +299,15 @@ void ImageViewer::createActions()
QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close);
exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setShortcut(tr("Ctrl+Q"));
QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));
copyAct = editMenu->addAction(tr("&Copy"), this, &ImageViewer::copy);
copyAct->setShortcut(QKeySequence::Copy);
copyAct->setEnabled(false);
QAction *pasteAct = editMenu->addAction(tr("&Paste"), this, &ImageViewer::paste);
pasteAct->setShortcut(QKeySequence::Paste);
QMenu *viewMenu = menuBar()->addMenu(tr("&View")); QMenu *viewMenu = menuBar()->addMenu(tr("&View"));
zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn); zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn);
@ -248,6 +340,8 @@ void ImageViewer::createActions()
void ImageViewer::updateActions() void ImageViewer::updateActions()
//! [21] //! [22] //! [21] //! [22]
{ {
saveAsAct->setEnabled(!image.isNull());
copyAct->setEnabled(!image.isNull());
zoomInAct->setEnabled(!fitToWindowAct->isChecked()); zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked()); zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked()); normalSizeAct->setEnabled(!fitToWindowAct->isChecked());

View File

@ -42,6 +42,7 @@
#define IMAGEVIEWER_H #define IMAGEVIEWER_H
#include <QMainWindow> #include <QMainWindow>
#include <QImage>
#ifndef QT_NO_PRINTER #ifndef QT_NO_PRINTER
#include <QPrinter> #include <QPrinter>
#endif #endif
@ -65,7 +66,10 @@ public:
private slots: private slots:
void open(); void open();
void saveAs();
void print(); void print();
void copy();
void paste();
void zoomIn(); void zoomIn();
void zoomOut(); void zoomOut();
void normalSize(); void normalSize();
@ -76,9 +80,12 @@ private:
void createActions(); void createActions();
void createMenus(); void createMenus();
void updateActions(); void updateActions();
bool saveFile(const QString &fileName);
void setImage(const QImage &newImage);
void scaleImage(double factor); void scaleImage(double factor);
void adjustScrollBar(QScrollBar *scrollBar, double factor); void adjustScrollBar(QScrollBar *scrollBar, double factor);
QImage image;
QLabel *imageLabel; QLabel *imageLabel;
QScrollArea *scrollArea; QScrollArea *scrollArea;
double scaleFactor; double scaleFactor;
@ -87,7 +94,9 @@ private:
QPrinter printer; QPrinter printer;
#endif #endif
QAction *saveAsAct;
QAction *printAct; QAction *printAct;
QAction *copyAct;
QAction *zoomInAct; QAction *zoomInAct;
QAction *zoomOutAct; QAction *zoomOutAct;
QAction *normalSizeAct; QAction *normalSizeAct;