0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "imageviewer.h"
|
|
45 |
|
|
46 |
//! [0]
|
|
47 |
ImageViewer::ImageViewer()
|
|
48 |
{
|
|
49 |
imageLabel = new QLabel;
|
|
50 |
imageLabel->setBackgroundRole(QPalette::Base);
|
|
51 |
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
|
|
52 |
imageLabel->setScaledContents(true);
|
|
53 |
|
|
54 |
scrollArea = new QScrollArea;
|
|
55 |
scrollArea->setBackgroundRole(QPalette::Dark);
|
|
56 |
scrollArea->setWidget(imageLabel);
|
|
57 |
setCentralWidget(scrollArea);
|
|
58 |
|
|
59 |
createActions();
|
|
60 |
createMenus();
|
|
61 |
|
|
62 |
setWindowTitle(tr("Image Viewer"));
|
|
63 |
resize(500, 400);
|
|
64 |
}
|
|
65 |
//! [0]
|
|
66 |
|
|
67 |
//! [1]
|
|
68 |
void ImageViewer::open()
|
|
69 |
//! [1] //! [2]
|
|
70 |
{
|
|
71 |
QString fileName = QFileDialog::getOpenFileName(this,
|
|
72 |
tr("Open File"), QDir::currentPath());
|
|
73 |
if (!fileName.isEmpty()) {
|
|
74 |
QImage image(fileName);
|
|
75 |
if (image.isNull()) {
|
|
76 |
QMessageBox::information(this, tr("Image Viewer"),
|
|
77 |
tr("Cannot load %1.").arg(fileName));
|
|
78 |
return;
|
|
79 |
}
|
|
80 |
//! [2] //! [3]
|
|
81 |
imageLabel->setPixmap(QPixmap::fromImage(image));
|
|
82 |
//! [3] //! [4]
|
|
83 |
scaleFactor = 1.0;
|
|
84 |
|
|
85 |
printAct->setEnabled(true);
|
|
86 |
fitToWindowAct->setEnabled(true);
|
|
87 |
updateActions();
|
|
88 |
|
|
89 |
if (!fitToWindowAct->isChecked())
|
|
90 |
imageLabel->adjustSize();
|
|
91 |
}
|
|
92 |
}
|
|
93 |
//! [4]
|
|
94 |
|
|
95 |
//! [5]
|
|
96 |
void ImageViewer::print()
|
|
97 |
//! [5] //! [6]
|
|
98 |
{
|
|
99 |
Q_ASSERT(imageLabel->pixmap());
|
|
100 |
#ifndef QT_NO_PRINTER
|
|
101 |
//! [6] //! [7]
|
|
102 |
QPrintDialog dialog(&printer, this);
|
|
103 |
//! [7] //! [8]
|
|
104 |
if (dialog.exec()) {
|
|
105 |
QPainter painter(&printer);
|
|
106 |
QRect rect = painter.viewport();
|
|
107 |
QSize size = imageLabel->pixmap()->size();
|
|
108 |
size.scale(rect.size(), Qt::KeepAspectRatio);
|
|
109 |
painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
|
|
110 |
painter.setWindow(imageLabel->pixmap()->rect());
|
|
111 |
painter.drawPixmap(0, 0, *imageLabel->pixmap());
|
|
112 |
}
|
|
113 |
#endif
|
|
114 |
}
|
|
115 |
//! [8]
|
|
116 |
|
|
117 |
//! [9]
|
|
118 |
void ImageViewer::zoomIn()
|
|
119 |
//! [9] //! [10]
|
|
120 |
{
|
|
121 |
scaleImage(1.25);
|
|
122 |
}
|
|
123 |
|
|
124 |
void ImageViewer::zoomOut()
|
|
125 |
{
|
|
126 |
scaleImage(0.8);
|
|
127 |
}
|
|
128 |
|
|
129 |
//! [10] //! [11]
|
|
130 |
void ImageViewer::normalSize()
|
|
131 |
//! [11] //! [12]
|
|
132 |
{
|
|
133 |
imageLabel->adjustSize();
|
|
134 |
scaleFactor = 1.0;
|
|
135 |
}
|
|
136 |
//! [12]
|
|
137 |
|
|
138 |
//! [13]
|
|
139 |
void ImageViewer::fitToWindow()
|
|
140 |
//! [13] //! [14]
|
|
141 |
{
|
|
142 |
bool fitToWindow = fitToWindowAct->isChecked();
|
|
143 |
scrollArea->setWidgetResizable(fitToWindow);
|
|
144 |
if (!fitToWindow) {
|
|
145 |
normalSize();
|
|
146 |
}
|
|
147 |
updateActions();
|
|
148 |
}
|
|
149 |
//! [14]
|
|
150 |
|
|
151 |
|
|
152 |
//! [15]
|
|
153 |
void ImageViewer::about()
|
|
154 |
//! [15] //! [16]
|
|
155 |
{
|
|
156 |
QMessageBox::about(this, tr("About Image Viewer"),
|
|
157 |
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
|
|
158 |
"and QScrollArea to display an image. QLabel is typically used "
|
|
159 |
"for displaying a text, but it can also display an image. "
|
|
160 |
"QScrollArea provides a scrolling view around another widget. "
|
|
161 |
"If the child widget exceeds the size of the frame, QScrollArea "
|
|
162 |
"automatically provides scroll bars. </p><p>The example "
|
|
163 |
"demonstrates how QLabel's ability to scale its contents "
|
|
164 |
"(QLabel::scaledContents), and QScrollArea's ability to "
|
|
165 |
"automatically resize its contents "
|
|
166 |
"(QScrollArea::widgetResizable), can be used to implement "
|
|
167 |
"zooming and scaling features. </p><p>In addition the example "
|
|
168 |
"shows how to use QPainter to print an image.</p>"));
|
|
169 |
}
|
|
170 |
//! [16]
|
|
171 |
|
|
172 |
//! [17]
|
|
173 |
void ImageViewer::createActions()
|
|
174 |
//! [17] //! [18]
|
|
175 |
{
|
|
176 |
openAct = new QAction(tr("&Open..."), this);
|
|
177 |
openAct->setShortcut(tr("Ctrl+O"));
|
|
178 |
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
|
179 |
|
|
180 |
printAct = new QAction(tr("&Print..."), this);
|
|
181 |
printAct->setShortcut(tr("Ctrl+P"));
|
|
182 |
printAct->setEnabled(false);
|
|
183 |
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
|
|
184 |
|
|
185 |
exitAct = new QAction(tr("E&xit"), this);
|
|
186 |
exitAct->setShortcut(tr("Ctrl+Q"));
|
|
187 |
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
|
|
188 |
|
|
189 |
zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
|
|
190 |
zoomInAct->setShortcut(tr("Ctrl++"));
|
|
191 |
zoomInAct->setEnabled(false);
|
|
192 |
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
|
|
193 |
|
|
194 |
zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
|
|
195 |
zoomOutAct->setShortcut(tr("Ctrl+-"));
|
|
196 |
zoomOutAct->setEnabled(false);
|
|
197 |
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
|
|
198 |
|
|
199 |
normalSizeAct = new QAction(tr("&Normal Size"), this);
|
|
200 |
normalSizeAct->setShortcut(tr("Ctrl+S"));
|
|
201 |
normalSizeAct->setEnabled(false);
|
|
202 |
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
|
|
203 |
|
|
204 |
fitToWindowAct = new QAction(tr("&Fit to Window"), this);
|
|
205 |
fitToWindowAct->setEnabled(false);
|
|
206 |
fitToWindowAct->setCheckable(true);
|
|
207 |
fitToWindowAct->setShortcut(tr("Ctrl+F"));
|
|
208 |
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
|
|
209 |
|
|
210 |
aboutAct = new QAction(tr("&About"), this);
|
|
211 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
|
|
212 |
|
|
213 |
aboutQtAct = new QAction(tr("About &Qt"), this);
|
|
214 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
|
|
215 |
}
|
|
216 |
//! [18]
|
|
217 |
|
|
218 |
//! [19]
|
|
219 |
void ImageViewer::createMenus()
|
|
220 |
//! [19] //! [20]
|
|
221 |
{
|
|
222 |
fileMenu = new QMenu(tr("&File"), this);
|
|
223 |
fileMenu->addAction(openAct);
|
|
224 |
fileMenu->addAction(printAct);
|
|
225 |
fileMenu->addSeparator();
|
|
226 |
fileMenu->addAction(exitAct);
|
|
227 |
|
|
228 |
viewMenu = new QMenu(tr("&View"), this);
|
|
229 |
viewMenu->addAction(zoomInAct);
|
|
230 |
viewMenu->addAction(zoomOutAct);
|
|
231 |
viewMenu->addAction(normalSizeAct);
|
|
232 |
viewMenu->addSeparator();
|
|
233 |
viewMenu->addAction(fitToWindowAct);
|
|
234 |
|
|
235 |
helpMenu = new QMenu(tr("&Help"), this);
|
|
236 |
helpMenu->addAction(aboutAct);
|
|
237 |
helpMenu->addAction(aboutQtAct);
|
|
238 |
|
|
239 |
menuBar()->addMenu(fileMenu);
|
|
240 |
menuBar()->addMenu(viewMenu);
|
|
241 |
menuBar()->addMenu(helpMenu);
|
|
242 |
}
|
|
243 |
//! [20]
|
|
244 |
|
|
245 |
//! [21]
|
|
246 |
void ImageViewer::updateActions()
|
|
247 |
//! [21] //! [22]
|
|
248 |
{
|
|
249 |
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
|
|
250 |
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
|
|
251 |
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
|
|
252 |
}
|
|
253 |
//! [22]
|
|
254 |
|
|
255 |
//! [23]
|
|
256 |
void ImageViewer::scaleImage(double factor)
|
|
257 |
//! [23] //! [24]
|
|
258 |
{
|
|
259 |
Q_ASSERT(imageLabel->pixmap());
|
|
260 |
scaleFactor *= factor;
|
|
261 |
imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());
|
|
262 |
|
|
263 |
adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
|
|
264 |
adjustScrollBar(scrollArea->verticalScrollBar(), factor);
|
|
265 |
|
|
266 |
zoomInAct->setEnabled(scaleFactor < 3.0);
|
|
267 |
zoomOutAct->setEnabled(scaleFactor > 0.333);
|
|
268 |
}
|
|
269 |
//! [24]
|
|
270 |
|
|
271 |
//! [25]
|
|
272 |
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
|
|
273 |
//! [25] //! [26]
|
|
274 |
{
|
|
275 |
scrollBar->setValue(int(factor * scrollBar->value()
|
|
276 |
+ ((factor - 1) * scrollBar->pageStep()/2)));
|
|
277 |
}
|
|
278 |
//! [26]
|