source: trunk/examples/widgets/imageviewer/imageviewer.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 8.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42
43#include "imageviewer.h"
44
45//! [0]
46ImageViewer::ImageViewer()
47{
48 imageLabel = new QLabel;
49 imageLabel->setBackgroundRole(QPalette::Base);
50 imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
51 imageLabel->setScaledContents(true);
52
53 scrollArea = new QScrollArea;
54 scrollArea->setBackgroundRole(QPalette::Dark);
55 scrollArea->setWidget(imageLabel);
56 setCentralWidget(scrollArea);
57
58 createActions();
59 createMenus();
60
61 setWindowTitle(tr("Image Viewer"));
62 resize(500, 400);
63}
64//! [0]
65
66//! [1]
67void ImageViewer::open()
68//! [1] //! [2]
69{
70 QString fileName = QFileDialog::getOpenFileName(this,
71 tr("Open File"), QDir::currentPath());
72 if (!fileName.isEmpty()) {
73 QImage image(fileName);
74 if (image.isNull()) {
75 QMessageBox::information(this, tr("Image Viewer"),
76 tr("Cannot load %1.").arg(fileName));
77 return;
78 }
79//! [2] //! [3]
80 imageLabel->setPixmap(QPixmap::fromImage(image));
81//! [3] //! [4]
82 scaleFactor = 1.0;
83
84 printAct->setEnabled(true);
85 fitToWindowAct->setEnabled(true);
86 updateActions();
87
88 if (!fitToWindowAct->isChecked())
89 imageLabel->adjustSize();
90 }
91}
92//! [4]
93
94//! [5]
95void ImageViewer::print()
96//! [5] //! [6]
97{
98 Q_ASSERT(imageLabel->pixmap());
99#ifndef QT_NO_PRINTER
100//! [6] //! [7]
101 QPrintDialog dialog(&printer, this);
102//! [7] //! [8]
103 if (dialog.exec()) {
104 QPainter painter(&printer);
105 QRect rect = painter.viewport();
106 QSize size = imageLabel->pixmap()->size();
107 size.scale(rect.size(), Qt::KeepAspectRatio);
108 painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
109 painter.setWindow(imageLabel->pixmap()->rect());
110 painter.drawPixmap(0, 0, *imageLabel->pixmap());
111 }
112#endif
113}
114//! [8]
115
116//! [9]
117void ImageViewer::zoomIn()
118//! [9] //! [10]
119{
120 scaleImage(1.25);
121}
122
123void ImageViewer::zoomOut()
124{
125 scaleImage(0.8);
126}
127
128//! [10] //! [11]
129void ImageViewer::normalSize()
130//! [11] //! [12]
131{
132 imageLabel->adjustSize();
133 scaleFactor = 1.0;
134}
135//! [12]
136
137//! [13]
138void ImageViewer::fitToWindow()
139//! [13] //! [14]
140{
141 bool fitToWindow = fitToWindowAct->isChecked();
142 scrollArea->setWidgetResizable(fitToWindow);
143 if (!fitToWindow) {
144 normalSize();
145 }
146 updateActions();
147}
148//! [14]
149
150
151//! [15]
152void ImageViewer::about()
153//! [15] //! [16]
154{
155 QMessageBox::about(this, tr("About Image Viewer"),
156 tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
157 "and QScrollArea to display an image. QLabel is typically used "
158 "for displaying a text, but it can also display an image. "
159 "QScrollArea provides a scrolling view around another widget. "
160 "If the child widget exceeds the size of the frame, QScrollArea "
161 "automatically provides scroll bars. </p><p>The example "
162 "demonstrates how QLabel's ability to scale its contents "
163 "(QLabel::scaledContents), and QScrollArea's ability to "
164 "automatically resize its contents "
165 "(QScrollArea::widgetResizable), can be used to implement "
166 "zooming and scaling features. </p><p>In addition the example "
167 "shows how to use QPainter to print an image.</p>"));
168}
169//! [16]
170
171//! [17]
172void ImageViewer::createActions()
173//! [17] //! [18]
174{
175 openAct = new QAction(tr("&Open..."), this);
176 openAct->setShortcut(tr("Ctrl+O"));
177 connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
178
179 printAct = new QAction(tr("&Print..."), this);
180 printAct->setShortcut(tr("Ctrl+P"));
181 printAct->setEnabled(false);
182 connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
183
184 exitAct = new QAction(tr("E&xit"), this);
185 exitAct->setShortcut(tr("Ctrl+Q"));
186 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
187
188 zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
189 zoomInAct->setShortcut(tr("Ctrl++"));
190 zoomInAct->setEnabled(false);
191 connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
192
193 zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
194 zoomOutAct->setShortcut(tr("Ctrl+-"));
195 zoomOutAct->setEnabled(false);
196 connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
197
198 normalSizeAct = new QAction(tr("&Normal Size"), this);
199 normalSizeAct->setShortcut(tr("Ctrl+S"));
200 normalSizeAct->setEnabled(false);
201 connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
202
203 fitToWindowAct = new QAction(tr("&Fit to Window"), this);
204 fitToWindowAct->setEnabled(false);
205 fitToWindowAct->setCheckable(true);
206 fitToWindowAct->setShortcut(tr("Ctrl+F"));
207 connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
208
209 aboutAct = new QAction(tr("&About"), this);
210 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
211
212 aboutQtAct = new QAction(tr("About &Qt"), this);
213 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
214}
215//! [18]
216
217//! [19]
218void ImageViewer::createMenus()
219//! [19] //! [20]
220{
221 fileMenu = new QMenu(tr("&File"), this);
222 fileMenu->addAction(openAct);
223 fileMenu->addAction(printAct);
224 fileMenu->addSeparator();
225 fileMenu->addAction(exitAct);
226
227 viewMenu = new QMenu(tr("&View"), this);
228 viewMenu->addAction(zoomInAct);
229 viewMenu->addAction(zoomOutAct);
230 viewMenu->addAction(normalSizeAct);
231 viewMenu->addSeparator();
232 viewMenu->addAction(fitToWindowAct);
233
234 helpMenu = new QMenu(tr("&Help"), this);
235 helpMenu->addAction(aboutAct);
236 helpMenu->addAction(aboutQtAct);
237
238 menuBar()->addMenu(fileMenu);
239 menuBar()->addMenu(viewMenu);
240 menuBar()->addMenu(helpMenu);
241}
242//! [20]
243
244//! [21]
245void ImageViewer::updateActions()
246//! [21] //! [22]
247{
248 zoomInAct->setEnabled(!fitToWindowAct->isChecked());
249 zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
250 normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
251}
252//! [22]
253
254//! [23]
255void ImageViewer::scaleImage(double factor)
256//! [23] //! [24]
257{
258 Q_ASSERT(imageLabel->pixmap());
259 scaleFactor *= factor;
260 imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
261
262 adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
263 adjustScrollBar(scrollArea->verticalScrollBar(), factor);
264
265 zoomInAct->setEnabled(scaleFactor < 3.0);
266 zoomOutAct->setEnabled(scaleFactor > 0.333);
267}
268//! [24]
269
270//! [25]
271void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
272//! [25] //! [26]
273{
274 scrollBar->setValue(int(factor * scrollBar->value()
275 + ((factor - 1) * scrollBar->pageStep()/2)));
276}
277//! [26]
Note: See TracBrowser for help on using the repository browser.