diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-06-26 14:38:12 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-06-29 21:15:54 +0000 |
commit | f61c42491929695997e4f3bd8d02e3ac9c57f9a7 (patch) | |
tree | f0b22e1c5ff3a8b8cfcbd92b102c1b0d8928ceeb /examples | |
parent | d6bf951b76d0a14d0975fb05e71364159c56fce6 (diff) |
Move image viewer example to manual test
Change-Id: I4b64033f1a075681ce5b918fdf2e018ad05a7869
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit b8f588bea74aae0a890e1af18b936b0bfbf8c237)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/widgets/doc/images/imageviewer-fit_to_window_1.png | bin | 84584 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/doc/images/imageviewer-fit_to_window_2.png | bin | 145998 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/doc/images/imageviewer-original_size.png | bin | 61567 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/doc/images/imageviewer-zoom_in_1.png | bin | 84559 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/doc/images/imageviewer-zoom_in_2.png | bin | 85537 -> 0 bytes | |||
-rw-r--r-- | examples/widgets/doc/src/imageviewer.qdoc | 320 | ||||
-rw-r--r-- | examples/widgets/widgets/CMakeLists.txt | 1 | ||||
-rw-r--r-- | examples/widgets/widgets/imageviewer/CMakeLists.txt | 44 | ||||
-rw-r--r-- | examples/widgets/widgets/imageviewer/imageviewer.cpp | 363 | ||||
-rw-r--r-- | examples/widgets/widgets/imageviewer/imageviewer.h | 74 | ||||
-rw-r--r-- | examples/widgets/widgets/imageviewer/imageviewer.pro | 11 | ||||
-rw-r--r-- | examples/widgets/widgets/imageviewer/main.cpp | 24 | ||||
-rw-r--r-- | examples/widgets/widgets/widgets.pro | 1 |
13 files changed, 0 insertions, 838 deletions
diff --git a/examples/widgets/doc/images/imageviewer-fit_to_window_1.png b/examples/widgets/doc/images/imageviewer-fit_to_window_1.png Binary files differdeleted file mode 100644 index 0fe1ba1c62..0000000000 --- a/examples/widgets/doc/images/imageviewer-fit_to_window_1.png +++ /dev/null diff --git a/examples/widgets/doc/images/imageviewer-fit_to_window_2.png b/examples/widgets/doc/images/imageviewer-fit_to_window_2.png Binary files differdeleted file mode 100644 index 29e3a93bc1..0000000000 --- a/examples/widgets/doc/images/imageviewer-fit_to_window_2.png +++ /dev/null diff --git a/examples/widgets/doc/images/imageviewer-original_size.png b/examples/widgets/doc/images/imageviewer-original_size.png Binary files differdeleted file mode 100644 index c0443eba25..0000000000 --- a/examples/widgets/doc/images/imageviewer-original_size.png +++ /dev/null diff --git a/examples/widgets/doc/images/imageviewer-zoom_in_1.png b/examples/widgets/doc/images/imageviewer-zoom_in_1.png Binary files differdeleted file mode 100644 index 05b0fffecb..0000000000 --- a/examples/widgets/doc/images/imageviewer-zoom_in_1.png +++ /dev/null diff --git a/examples/widgets/doc/images/imageviewer-zoom_in_2.png b/examples/widgets/doc/images/imageviewer-zoom_in_2.png Binary files differdeleted file mode 100644 index 0c36111c03..0000000000 --- a/examples/widgets/doc/images/imageviewer-zoom_in_2.png +++ /dev/null diff --git a/examples/widgets/doc/src/imageviewer.qdoc b/examples/widgets/doc/src/imageviewer.qdoc deleted file mode 100644 index aa6eec46d8..0000000000 --- a/examples/widgets/doc/src/imageviewer.qdoc +++ /dev/null @@ -1,320 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only - -/*! - \example widgets/imageviewer - \title Image Viewer Example - \ingroup examples-widgets - \brief The example shows how to combine QLabel and QScrollArea to - display an image. - - QLabel is typically used for displaying text, - but it can also display an image. QScrollArea provides a - scrolling view around another widget. If the child widget exceeds - the size of the frame, QScrollArea automatically provides scroll - bars. - - The example demonstrates how QLabel's ability to scale its - contents (QLabel::scaledContents), and QScrollArea's ability to - automatically resize its contents (QScrollArea::widgetResizable), - can be used to implement zooming and scaling features. In - addition the example shows how to use QPainter to print an image. - - \borderedimage imageviewer-example.png - \caption Screenshot of the Image Viewer example - - With the Image Viewer application, the users can view an image of - their choice. The \uicontrol File menu gives the user the possibility - to: - - \list - \li \uicontrol{Open...} - Open an image file - \li \uicontrol{Print...} - Print an image - \li \uicontrol{Exit} - Exit the application - \endlist - - Once an image is loaded, the \uicontrol View menu allows the users to: - - \list - \li \uicontrol{Zoom In} - Scale the image up by 25% - \li \uicontrol{Zoom Out} - Scale the image down by 25% - \li \uicontrol{Normal Size} - Show the image at its original size - \li \uicontrol{Fit to Window} - Stretch the image to occupy the entire window - \endlist - - In addition the \uicontrol Help menu provides the users with information - about the Image Viewer example in particular, and about Qt in - general. - - \section1 ImageViewer Class Definition - - \snippet widgets/imageviewer/imageviewer.h 0 - - The \c ImageViewer class inherits from QMainWindow. We reimplement - the constructor, and create several private slots to facilitate - the menu entries. In addition we create four private functions. - - We use \c createActions() and \c createMenus() when constructing - the \c ImageViewer widget. We use the \c updateActions() function - to update the menu entries when a new image is loaded, or when - the \uicontrol {Fit to Window} option is toggled. The zoom slots use \c - scaleImage() to perform the zooming. In turn, \c - scaleImage() uses \c adjustScrollBar() to preserve the focal point after - scaling an image. - - \section1 ImageViewer Class Implementation - - \snippet widgets/imageviewer/imageviewer.cpp 0 - - In the constructor we first create the label and the scroll area. - - We set \c {imageLabel}'s size policy to \l - {QSizePolicy::Ignored}{ignored}, making the users able to scale - the image to whatever size they want when the \uicontrol {Fit to Window} - option is turned on. Otherwise, the default size polizy (\l - {QSizePolicy::Preferred}{preferred}) will make scroll bars appear - when the scroll area becomes smaller than the label's minimum size - hint. - - We ensure that the label will scale its contents to fill all - available space, to enable the image to scale properly when - zooming. If we omitted to set the \c {imageLabel}'s \l - {QLabel::scaledContents}{scaledContents} property, zooming in - would enlarge the QLabel, but leave the pixmap at - its original size, exposing the QLabel's background. - - We make \c imageLabel the scroll area's child widget, and we make - \c scrollArea the central widget of the QMainWindow. At the end - we create the associated actions and menus, and customize the \c - {ImageViewer}'s appearance. - - \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 - - In the \c loadFile() function, we instantiate a QImageReader - and enable automatic transformations by calling - QImageReader::setAutoTransform(). For files in JPEG format, - this ensures that portrait mode images of digital cameras are shown - correctly by applying the appropriate orientation read from the - EXIF meta data stored in the image file. - - We then load the image using QImageReader::read(). If this returns - a null image, indicating that the file is not an image file, - we use a QMessageBox to alert the user. - - The QMessageBox class provides a modal dialog with a short - message, an icon, and some buttons. As with QFileDialog the - easiest way to create a QMessageBox is to use its static - convenience functions. QMessageBox provides a range of different - messages arranged along two axes: severity (question, - information, warning and critical) and complexity (the number of - necessary response buttons). In this particular example an - information message with an \uicontrol OK button (the default) is - sufficient, since the message is part of a normal operation. - - \snippet widgets/imageviewer/imageviewer.cpp 4 - - If the format is supported, we display the image in \c imageLabel - by setting the label's \l {QLabel::pixmap}{pixmap}. Then we enable - the \uicontrol Print and \uicontrol {Fit to Window} menu entries and update - the rest of the view menu entries. The \uicontrol Open and \uicontrol Exit - entries are enabled by default. - - If the \uicontrol {Fit to Window} option is turned off, the - QScrollArea::widgetResizable property is \c false and it is - our responsibility (not QScrollArea's) to give the QLabel a - reasonable size based on its contents. We call - \{QWidget::adjustSize()}{adjustSize()} to achieve this, which is - essentially the same as - - \code - imageLabel->resize(imageLabel->pixmap()->size()); - \endcode - - In the \c print() slot, we first make sure that an image has been - loaded into the application: - - \snippet widgets/imageviewer/imageviewer.cpp 5 - \snippet widgets/imageviewer/imageviewer.cpp 6 - - If the application is built in debug mode, the \c Q_ASSERT() macro - will expand to - - \code - if (imageLabel->pixmap().isNull()) - qFatal("ASSERT: "imageLabel->pixmap().isNull()" in file ..."); - \endcode - - In release mode, the macro simply disappear. The mode can be set - in the application's \c .pro file. One way to do so is to add an - option to \uicontrol qmake when building the application: - - \code - qmake "CONFIG += debug" foo.pro - \endcode - - or - - \code - qmake "CONFIG += release" foo.pro - \endcode - - Another approach is to add this line directly to the \c .pro - file. - - \snippet widgets/imageviewer/imageviewer.cpp 7 - \snippet widgets/imageviewer/imageviewer.cpp 8 - - Then we present a print dialog allowing the user to choose a - printer and to set a few options. We construct a painter with a - QPrinter as the paint device. We set the painter's window - and viewport in such a way that the image is as large as possible - on the paper, but without altering its - \l{Qt::KeepAspectRatio}{aspect ratio}. - - In the end we draw the pixmap at position (0, 0). - - \snippet widgets/imageviewer/imageviewer.cpp 9 - \snippet widgets/imageviewer/imageviewer.cpp 10 - - We implement the zooming slots using the private \c scaleImage() - function. We set the scaling factors to 1.25 and 0.8, - respectively. These factor values ensure that a \uicontrol {Zoom In} - action and a \uicontrol {Zoom Out} action will cancel each other (since - 1.25 * 0.8 == 1), and in that way the normal image size can be - restored using the zooming features. - - The screenshots below show an image in its normal size, and the - same image after zooming in: - - \table - \row - \li \inlineimage imageviewer-original_size.png - \li \inlineimage imageviewer-zoom_in_1.png - \li \inlineimage imageviewer-zoom_in_2.png - \endtable - - \snippet widgets/imageviewer/imageviewer.cpp 11 - \snippet widgets/imageviewer/imageviewer.cpp 12 - - When zooming, we use the QLabel's ability to scale its contents. - Such scaling doesn't change the actual size hint of the contents. - And since the \l {QLabel::adjustSize()}{adjustSize()} function - use those size hint, the only thing we need to do to restore the - normal size of the currently displayed image is to call \c - adjustSize() and reset the scale factor to 1.0. - - \snippet widgets/imageviewer/imageviewer.cpp 13 - \snippet widgets/imageviewer/imageviewer.cpp 14 - - The \c fitToWindow() slot is called each time the user toggled - the \uicontrol {Fit to Window} option. If the slot is called to turn on - the option, we tell the scroll area to resize its child widget - with the QScrollArea::setWidgetResizable() function. Then we - disable the \uicontrol {Zoom In}, \uicontrol {Zoom Out} and \uicontrol {Normal - Size} menu entries using the private \c updateActions() function. - - If the \l {QScrollArea::widgetResizable} property is set to \c - false (the default), the scroll area honors the size of its child - widget. If this property is set to \c true, the scroll area will - automatically resize the widget in order to avoid scroll bars - where they can be avoided, or to take advantage of extra space. - But the scroll area will honor the minimum size hint of its child - widget independent of the widget resizable property. So in this - example we set \c {imageLabel}'s size policy to \l - {QSizePolicy::Ignored}{ignored} in the constructor, to avoid that - scroll bars appear when the scroll area becomes smaller than the - label's minimum size hint. - - The screenshots below shows an image in its normal size, and the - same image with the \uicontrol {Fit to window} option turned on. - Enlarging the window will stretch the image further, as shown in - the third screenshot. - - \table - \row - \li \inlineimage imageviewer-original_size.png - \li \inlineimage imageviewer-fit_to_window_1.png - \li \inlineimage imageviewer-fit_to_window_2.png - \endtable - - If the slot is called to turn off the option, the - {QScrollArea::setWidgetResizable} property is set to \c false. We - also restore the image pixmap to its normal size by adjusting the - label's size to its content. And in the end we update the view - menu entries. - - \snippet widgets/imageviewer/imageviewer.cpp 15 - \snippet widgets/imageviewer/imageviewer.cpp 16 - - We implement the \c about() slot to create a message box - describing what the example is designed to show. - - \snippet widgets/imageviewer/imageviewer.cpp 17 - \snippet widgets/imageviewer/imageviewer.cpp 18 - - In the private \c createAction() function, we create the - actions providing the application features and populate - a menu with them. - - We assign a short-cut key to each action and connect them to the - appropriate slots. We only enable the \c openAct and \c exitAct at - the time of creation, the others are updated once an image has - been loaded into the application. In addition we make the \c - fitToWindowAct \l {QAction::checkable}{checkable}. - - The QMenu class provides a menu widget for use in menu bars, - context menus, and other popup menus. The QMenuBar class provides - a horizontal menu bar that consists of a list of pull-down menu - items. So we put the menus in the \c {ImageViewer}'s - menu bar which we retrieve with the QMainWindow::menuBar() - function. - - \snippet widgets/imageviewer/imageviewer.cpp 21 - \snippet widgets/imageviewer/imageviewer.cpp 22 - - The private \c updateActions() function enables or disables the - \uicontrol {Zoom In}, \uicontrol {Zoom Out} and \uicontrol {Normal Size} menu - entries depending on whether the \uicontrol {Fit to Window} option is - turned on or off. - - \snippet widgets/imageviewer/imageviewer.cpp 23 - \snippet widgets/imageviewer/imageviewer.cpp 24 - - In \c scaleImage(), we use the \c factor parameter to calculate - the new scaling factor for the displayed image, and resize \c - imageLabel. Since we set the - \l{QLabel::scaledContents}{scaledContents} property to \c true in - the constructor, the call to QWidget::resize() will scale the - image displayed in the label. We also adjust the scroll bars to - preserve the focal point of the image. - - At the end, if the scale factor is less than 33.3% or greater - than 300%, we disable the respective menu entry to prevent the - image pixmap from becoming too large, consuming too much - resources in the window system. - - \snippet widgets/imageviewer/imageviewer.cpp 25 - \snippet widgets/imageviewer/imageviewer.cpp 26 - - Whenever we zoom in or out, we need to adjust the scroll bars in - consequence. It would have been tempting to simply call - - \code - scrollBar->setValue(int(factor * scrollBar->value())); - \endcode - - but this would make the top-left corner the focal point, not the - center. Therefore we need to take into account the scroll bar - handle's size (the \l{QScrollBar::pageStep}{page step}). -*/ diff --git a/examples/widgets/widgets/CMakeLists.txt b/examples/widgets/widgets/CMakeLists.txt index 64b26558b2..fe5841951c 100644 --- a/examples/widgets/widgets/CMakeLists.txt +++ b/examples/widgets/widgets/CMakeLists.txt @@ -5,7 +5,6 @@ qt_internal_add_example(analogclock) qt_internal_add_example(calculator) qt_internal_add_example(calendarwidget) qt_internal_add_example(groupbox) -qt_internal_add_example(imageviewer) qt_internal_add_example(lineedits) if(QT_FEATURE_movie) qt_internal_add_example(movie) diff --git a/examples/widgets/widgets/imageviewer/CMakeLists.txt b/examples/widgets/widgets/imageviewer/CMakeLists.txt deleted file mode 100644 index 34f524e1f7..0000000000 --- a/examples/widgets/widgets/imageviewer/CMakeLists.txt +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(imageviewer LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/widgets/imageviewer") - -find_package(Qt6 - REQUIRED COMPONENTS Core Gui Widgets - OPTIONAL_COMPONENTS PrintSupport -) - -qt_standard_project_setup() - -qt_add_executable(imageviewer - imageviewer.cpp imageviewer.h - main.cpp -) - -set_target_properties(imageviewer PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(imageviewer PRIVATE - Qt6::Core - Qt6::Gui - Qt6::Widgets -) - -if (TARGET Qt6::PrintSupport) - target_link_libraries(imageviewer PRIVATE Qt6::PrintSupport) -endif() - -install(TARGETS imageviewer - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/widgets/widgets/imageviewer/imageviewer.cpp b/examples/widgets/widgets/imageviewer/imageviewer.cpp deleted file mode 100644 index fd8fbaf2b8..0000000000 --- a/examples/widgets/widgets/imageviewer/imageviewer.cpp +++ /dev/null @@ -1,363 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "imageviewer.h" - -#include <QApplication> -#include <QClipboard> -#include <QColorSpace> -#include <QDir> -#include <QFileDialog> -#include <QImageReader> -#include <QImageWriter> -#include <QLabel> -#include <QMenuBar> -#include <QMessageBox> -#include <QMimeData> -#include <QPainter> -#include <QScreen> -#include <QScrollArea> -#include <QScrollBar> -#include <QStandardPaths> -#include <QStatusBar> - -#if defined(QT_PRINTSUPPORT_LIB) -# include <QtPrintSupport/qtprintsupportglobal.h> - -# if QT_CONFIG(printdialog) -# include <QPrintDialog> -# endif -#endif - -//! [0] -ImageViewer::ImageViewer(QWidget *parent) - : QMainWindow(parent), imageLabel(new QLabel) - , scrollArea(new QScrollArea) -{ - imageLabel->setBackgroundRole(QPalette::Base); - imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); - imageLabel->setScaledContents(true); - - scrollArea->setBackgroundRole(QPalette::Dark); - scrollArea->setWidget(imageLabel); - scrollArea->setVisible(false); - setCentralWidget(scrollArea); - - createActions(); - - resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5); -} - -//! [0] -//! [2] - -bool ImageViewer::loadFile(const QString &fileName) -{ - QImageReader reader(fileName); - reader.setAutoTransform(true); - const QImage newImage = reader.read(); - if (newImage.isNull()) { - QMessageBox::information(this, QGuiApplication::applicationDisplayName(), - tr("Cannot load %1: %2") - .arg(QDir::toNativeSeparators(fileName), reader.errorString())); - return false; - } -//! [2] - - setImage(newImage); - - setWindowFilePath(fileName); - - const QString description = image.colorSpace().isValid() - ? image.colorSpace().description() : tr("unknown"); - const QString message = tr("Opened \"%1\", %2x%3, Depth: %4 (%5)") - .arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()) - .arg(image.depth()).arg(description); - statusBar()->showMessage(message); - return true; -} - -void ImageViewer::setImage(const QImage &newImage) -{ - image = newImage; - if (image.colorSpace().isValid()) - image.convertToColorSpace(QColorSpace::SRgb); - 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] - -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] - -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; - const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen - ? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes(); - for (const QByteArray &mimeTypeName : supportedMimeTypes) - mimeTypeFilters.append(mimeTypeName); - mimeTypeFilters.sort(); - dialog.setMimeTypeFilters(mimeTypeFilters); - dialog.selectMimeTypeFilter("image/jpeg"); - dialog.setAcceptMode(acceptMode); - 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().constFirst())) {} -} -//! [1] - -void ImageViewer::saveAs() -{ - QFileDialog dialog(this, tr("Save File As")); - initializeImageFileDialog(dialog, QFileDialog::AcceptSave); - - while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().constFirst())) {} -} - -//! [5] -void ImageViewer::print() -//! [5] //! [6] -{ - Q_ASSERT(!imageLabel->pixmap().isNull()); -#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) -//! [6] //! [7] - QPrintDialog dialog(&printer, this); -//! [7] //! [8] - if (dialog.exec()) { - QPainter painter(&printer); - QPixmap pixmap = imageLabel->pixmap(); - QRect rect = painter.viewport(); - QSize size = pixmap.size(); - size.scale(rect.size(), Qt::KeepAspectRatio); - painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); - painter.setWindow(pixmap.rect()); - painter.drawPixmap(0, 0, pixmap); - } -#endif -} -//! [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] -void ImageViewer::zoomIn() -//! [9] //! [10] -{ - scaleImage(1.25); -} - -void ImageViewer::zoomOut() -{ - scaleImage(0.8); -} - -//! [10] //! [11] -void ImageViewer::normalSize() -//! [11] //! [12] -{ - imageLabel->adjustSize(); - scaleFactor = 1.0; -} -//! [12] - -//! [13] -void ImageViewer::fitToWindow() -//! [13] //! [14] -{ - bool fitToWindow = fitToWindowAct->isChecked(); - scrollArea->setWidgetResizable(fitToWindow); - if (!fitToWindow) - normalSize(); - updateActions(); -} -//! [14] - - -//! [15] -void ImageViewer::about() -//! [15] //! [16] -{ - QMessageBox::about(this, tr("About Image Viewer"), - tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel " - "and QScrollArea to display an image. QLabel is typically used " - "for displaying a text, but it can also display an image. " - "QScrollArea provides a scrolling view around another widget. " - "If the child widget exceeds the size of the frame, QScrollArea " - "automatically provides scroll bars. </p><p>The example " - "demonstrates how QLabel's ability to scale its contents " - "(QLabel::scaledContents), and QScrollArea's ability to " - "automatically resize its contents " - "(QScrollArea::widgetResizable), can be used to implement " - "zooming and scaling features. </p><p>In addition the example " - "shows how to use QPainter to print an image.</p>")); -} -//! [16] - -//! [17] -void ImageViewer::createActions() -//! [17] //! [18] -{ - QMenu *fileMenu = menuBar()->addMenu(tr("&File")); - - QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::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->setShortcut(QKeySequence::Print); - printAct->setEnabled(false); - - fileMenu->addSeparator(); - - QAction *exitAct = fileMenu->addAction(tr("E&xit"), this, &QWidget::close); - 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")); - - zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &ImageViewer::zoomIn); - zoomInAct->setShortcut(QKeySequence::ZoomIn); - zoomInAct->setEnabled(false); - - zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &ImageViewer::zoomOut); - zoomOutAct->setShortcut(QKeySequence::ZoomOut); - zoomOutAct->setEnabled(false); - - normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &ImageViewer::normalSize); - normalSizeAct->setShortcut(tr("Ctrl+S")); - normalSizeAct->setEnabled(false); - - viewMenu->addSeparator(); - - fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &ImageViewer::fitToWindow); - fitToWindowAct->setEnabled(false); - fitToWindowAct->setCheckable(true); - fitToWindowAct->setShortcut(tr("Ctrl+F")); - - QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); - - helpMenu->addAction(tr("&About"), this, &ImageViewer::about); - helpMenu->addAction(tr("About &Qt"), this, &QApplication::aboutQt); -} -//! [18] - -//! [21] -void ImageViewer::updateActions() -//! [21] //! [22] -{ - saveAsAct->setEnabled(!image.isNull()); - copyAct->setEnabled(!image.isNull()); - zoomInAct->setEnabled(!fitToWindowAct->isChecked()); - zoomOutAct->setEnabled(!fitToWindowAct->isChecked()); - normalSizeAct->setEnabled(!fitToWindowAct->isChecked()); -} -//! [22] - -//! [23] -void ImageViewer::scaleImage(double factor) -//! [23] //! [24] -{ - scaleFactor *= factor; - imageLabel->resize(scaleFactor * imageLabel->pixmap().size()); - - adjustScrollBar(scrollArea->horizontalScrollBar(), factor); - adjustScrollBar(scrollArea->verticalScrollBar(), factor); - - zoomInAct->setEnabled(scaleFactor < 3.0); - zoomOutAct->setEnabled(scaleFactor > 0.333); -} -//! [24] - -//! [25] -void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor) -//! [25] //! [26] -{ - scrollBar->setValue(int(factor * scrollBar->value() - + ((factor - 1) * scrollBar->pageStep()/2))); -} -//! [26] diff --git a/examples/widgets/widgets/imageviewer/imageviewer.h b/examples/widgets/widgets/imageviewer/imageviewer.h deleted file mode 100644 index e9e6858c74..0000000000 --- a/examples/widgets/widgets/imageviewer/imageviewer.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef IMAGEVIEWER_H -#define IMAGEVIEWER_H - -#include <QMainWindow> -#include <QImage> -#if defined(QT_PRINTSUPPORT_LIB) -# include <QtPrintSupport/qtprintsupportglobal.h> - -# if QT_CONFIG(printer) -# include <QPrinter> -# endif -#endif - -QT_BEGIN_NAMESPACE -class QAction; -class QLabel; -class QMenu; -class QScrollArea; -class QScrollBar; -QT_END_NAMESPACE - -//! [0] -class ImageViewer : public QMainWindow -{ - Q_OBJECT - -public: - ImageViewer(QWidget *parent = nullptr); - bool loadFile(const QString &); - -private slots: - void open(); - void saveAs(); - void print(); - void copy(); - void paste(); - void zoomIn(); - void zoomOut(); - void normalSize(); - void fitToWindow(); - void about(); - -private: - void createActions(); - void createMenus(); - void updateActions(); - bool saveFile(const QString &fileName); - void setImage(const QImage &newImage); - void scaleImage(double factor); - void adjustScrollBar(QScrollBar *scrollBar, double factor); - - QImage image; - QLabel *imageLabel; - QScrollArea *scrollArea; - double scaleFactor = 1; - -#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer) - QPrinter printer; -#endif - - QAction *saveAsAct; - QAction *printAct; - QAction *copyAct; - QAction *zoomInAct; - QAction *zoomOutAct; - QAction *normalSizeAct; - QAction *fitToWindowAct; -}; -//! [0] - -#endif diff --git a/examples/widgets/widgets/imageviewer/imageviewer.pro b/examples/widgets/widgets/imageviewer/imageviewer.pro deleted file mode 100644 index d3db63ec20..0000000000 --- a/examples/widgets/widgets/imageviewer/imageviewer.pro +++ /dev/null @@ -1,11 +0,0 @@ -QT += widgets -requires(qtConfig(filedialog)) -qtHaveModule(printsupport): QT += printsupport - -HEADERS = imageviewer.h -SOURCES = imageviewer.cpp \ - main.cpp - -# install -target.path = $$[QT_INSTALL_EXAMPLES]/widgets/widgets/imageviewer -INSTALLS += target diff --git a/examples/widgets/widgets/imageviewer/main.cpp b/examples/widgets/widgets/imageviewer/main.cpp deleted file mode 100644 index 013f7d8689..0000000000 --- a/examples/widgets/widgets/imageviewer/main.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include <QApplication> -#include <QCommandLineParser> - -#include "imageviewer.h" - -int main(int argc, char *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; - if (!commandLineParser.positionalArguments().isEmpty() - && !imageViewer.loadFile(commandLineParser.positionalArguments().constFirst())) { - return -1; - } - imageViewer.show(); - return app.exec(); -} diff --git a/examples/widgets/widgets/widgets.pro b/examples/widgets/widgets/widgets.pro index 972c099af4..7d71aeeb41 100644 --- a/examples/widgets/widgets/widgets.pro +++ b/examples/widgets/widgets/widgets.pro @@ -3,7 +3,6 @@ SUBDIRS = analogclock \ calculator \ calendarwidget \ groupbox \ - imageviewer \ lineedits \ movie \ scribble \ |