summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets/imageviewer
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/widgets/imageviewer')
-rw-r--r--examples/widgets/widgets/imageviewer/imageviewer.cpp280
-rw-r--r--examples/widgets/widgets/imageviewer/imageviewer.desktop11
-rw-r--r--examples/widgets/widgets/imageviewer/imageviewer.h105
-rw-r--r--examples/widgets/widgets/imageviewer/imageviewer.pro18
-rw-r--r--examples/widgets/widgets/imageviewer/main.cpp51
5 files changed, 465 insertions, 0 deletions
diff --git a/examples/widgets/widgets/imageviewer/imageviewer.cpp b/examples/widgets/widgets/imageviewer/imageviewer.cpp
new file mode 100644
index 0000000000..97ab1d77f8
--- /dev/null
+++ b/examples/widgets/widgets/imageviewer/imageviewer.cpp
@@ -0,0 +1,280 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtWidgets>
+#ifndef QT_NO_PRINTER
+#include <QPrintDialog>
+#endif
+
+#include "imageviewer.h"
+
+//! [0]
+ImageViewer::ImageViewer()
+{
+ imageLabel = new QLabel;
+ imageLabel->setBackgroundRole(QPalette::Base);
+ imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+ imageLabel->setScaledContents(true);
+
+ scrollArea = new QScrollArea;
+ scrollArea->setBackgroundRole(QPalette::Dark);
+ scrollArea->setWidget(imageLabel);
+ setCentralWidget(scrollArea);
+
+ createActions();
+ createMenus();
+
+ setWindowTitle(tr("Image Viewer"));
+ resize(500, 400);
+}
+//! [0]
+
+//! [1]
+void ImageViewer::open()
+//! [1] //! [2]
+{
+ 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;
+ }
+//! [2] //! [3]
+ imageLabel->setPixmap(QPixmap::fromImage(image));
+//! [3] //! [4]
+ scaleFactor = 1.0;
+
+ printAct->setEnabled(true);
+ fitToWindowAct->setEnabled(true);
+ updateActions();
+
+ if (!fitToWindowAct->isChecked())
+ imageLabel->adjustSize();
+ }
+}
+//! [4]
+
+//! [5]
+void ImageViewer::print()
+//! [5] //! [6]
+{
+ Q_ASSERT(imageLabel->pixmap());
+#ifndef QT_NO_PRINTER
+//! [6] //! [7]
+ QPrintDialog dialog(&printer, this);
+//! [7] //! [8]
+ if (dialog.exec()) {
+ QPainter painter(&printer);
+ QRect rect = painter.viewport();
+ QSize size = imageLabel->pixmap()->size();
+ size.scale(rect.size(), Qt::KeepAspectRatio);
+ painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
+ painter.setWindow(imageLabel->pixmap()->rect());
+ painter.drawPixmap(0, 0, *imageLabel->pixmap());
+ }
+#endif
+}
+//! [8]
+
+//! [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]
+{
+ openAct = new QAction(tr("&Open..."), this);
+ openAct->setShortcut(tr("Ctrl+O"));
+ connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
+
+ printAct = new QAction(tr("&Print..."), this);
+ printAct->setShortcut(tr("Ctrl+P"));
+ printAct->setEnabled(false);
+ connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
+
+ exitAct = new QAction(tr("E&xit"), this);
+ exitAct->setShortcut(tr("Ctrl+Q"));
+ connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
+
+ zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
+ zoomInAct->setShortcut(tr("Ctrl++"));
+ zoomInAct->setEnabled(false);
+ connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
+
+ zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
+ zoomOutAct->setShortcut(tr("Ctrl+-"));
+ zoomOutAct->setEnabled(false);
+ connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
+
+ normalSizeAct = new QAction(tr("&Normal Size"), this);
+ normalSizeAct->setShortcut(tr("Ctrl+S"));
+ normalSizeAct->setEnabled(false);
+ connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
+
+ fitToWindowAct = new QAction(tr("&Fit to Window"), this);
+ fitToWindowAct->setEnabled(false);
+ fitToWindowAct->setCheckable(true);
+ fitToWindowAct->setShortcut(tr("Ctrl+F"));
+ connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
+
+ aboutAct = new QAction(tr("&About"), this);
+ connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
+
+ aboutQtAct = new QAction(tr("About &Qt"), this);
+ connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
+}
+//! [18]
+
+//! [19]
+void ImageViewer::createMenus()
+//! [19] //! [20]
+{
+ fileMenu = new QMenu(tr("&File"), this);
+ fileMenu->addAction(openAct);
+ fileMenu->addAction(printAct);
+ fileMenu->addSeparator();
+ fileMenu->addAction(exitAct);
+
+ viewMenu = new QMenu(tr("&View"), this);
+ viewMenu->addAction(zoomInAct);
+ viewMenu->addAction(zoomOutAct);
+ viewMenu->addAction(normalSizeAct);
+ viewMenu->addSeparator();
+ viewMenu->addAction(fitToWindowAct);
+
+ helpMenu = new QMenu(tr("&Help"), this);
+ helpMenu->addAction(aboutAct);
+ helpMenu->addAction(aboutQtAct);
+
+ menuBar()->addMenu(fileMenu);
+ menuBar()->addMenu(viewMenu);
+ menuBar()->addMenu(helpMenu);
+}
+//! [20]
+
+//! [21]
+void ImageViewer::updateActions()
+//! [21] //! [22]
+{
+ zoomInAct->setEnabled(!fitToWindowAct->isChecked());
+ zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
+ normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
+}
+//! [22]
+
+//! [23]
+void ImageViewer::scaleImage(double factor)
+//! [23] //! [24]
+{
+ Q_ASSERT(imageLabel->pixmap());
+ 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.desktop b/examples/widgets/widgets/imageviewer/imageviewer.desktop
new file mode 100644
index 0000000000..63f2408c38
--- /dev/null
+++ b/examples/widgets/widgets/imageviewer/imageviewer.desktop
@@ -0,0 +1,11 @@
+[Desktop Entry]
+Encoding=UTF-8
+Version=1.0
+Type=Application
+Terminal=false
+Name=Image Viewer
+Exec=/opt/usr/bin/imageviewer
+Icon=imageviewer
+X-Window-Icon=
+X-HildonDesk-ShowInToolbar=true
+X-Osso-Type=application/x-executable
diff --git a/examples/widgets/widgets/imageviewer/imageviewer.h b/examples/widgets/widgets/imageviewer/imageviewer.h
new file mode 100644
index 0000000000..98300794a2
--- /dev/null
+++ b/examples/widgets/widgets/imageviewer/imageviewer.h
@@ -0,0 +1,105 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef IMAGEVIEWER_H
+#define IMAGEVIEWER_H
+
+#include <QMainWindow>
+#ifndef QT_NO_PRINTER
+#include <QPrinter>
+#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();
+
+private slots:
+ void open();
+ void print();
+ void zoomIn();
+ void zoomOut();
+ void normalSize();
+ void fitToWindow();
+ void about();
+
+private:
+ void createActions();
+ void createMenus();
+ void updateActions();
+ void scaleImage(double factor);
+ void adjustScrollBar(QScrollBar *scrollBar, double factor);
+
+ QLabel *imageLabel;
+ QScrollArea *scrollArea;
+ double scaleFactor;
+
+#ifndef QT_NO_PRINTER
+ QPrinter printer;
+#endif
+
+ QAction *openAct;
+ QAction *printAct;
+ QAction *exitAct;
+ QAction *zoomInAct;
+ QAction *zoomOutAct;
+ QAction *normalSizeAct;
+ QAction *fitToWindowAct;
+ QAction *aboutAct;
+ QAction *aboutQtAct;
+
+ QMenu *fileMenu;
+ QMenu *viewMenu;
+ QMenu *helpMenu;
+};
+//! [0]
+
+#endif
diff --git a/examples/widgets/widgets/imageviewer/imageviewer.pro b/examples/widgets/widgets/imageviewer/imageviewer.pro
new file mode 100644
index 0000000000..15751ff3d9
--- /dev/null
+++ b/examples/widgets/widgets/imageviewer/imageviewer.pro
@@ -0,0 +1,18 @@
+HEADERS = imageviewer.h
+SOURCES = imageviewer.cpp \
+ main.cpp
+
+# install
+target.path = $$[QT_INSTALL_EXAMPLES]/qtbase/widgets/imageviewer
+sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS imageviewer.pro
+sources.path = $$[QT_INSTALL_EXAMPLES]/qtbase/widgets/imageviewer
+INSTALLS += target sources
+
+
+wince*: {
+ DEPLOYMENT_PLUGIN += qjpeg qgif
+}
+QT += widgets
+!isEmpty(QT.printsupport.name): QT += printsupport
+
+simulator: warning(This example might not fully work on Simulator platform)
diff --git a/examples/widgets/widgets/imageviewer/main.cpp b/examples/widgets/widgets/imageviewer/main.cpp
new file mode 100644
index 0000000000..4d735d4c46
--- /dev/null
+++ b/examples/widgets/widgets/imageviewer/main.cpp
@@ -0,0 +1,51 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the examples of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
+** the names of its contributors may be used to endorse or promote
+** products derived from this software without specific prior written
+** permission.
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QApplication>
+
+#include "imageviewer.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+ ImageViewer imageViewer;
+ imageViewer.show();
+ return app.exec();
+}