~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/widgets-imageviewer-imageviewer-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-10-12 23:14:14 UTC
  • mto: (15.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20061012231414-y2oqbom5dy389os0
Tags: upstream-4.2.0
ImportĀ upstreamĀ versionĀ 4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
5
<head>
6
 
    <title>Qt 4.1: imageviewer.cpp Example File (widgets/imageviewer/imageviewer.cpp)</title>
7
 
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
 
a:link { color: #004faf; text-decoration: none }
9
 
a:visited { color: #672967; text-decoration: none }
10
 
td.postheader { font-family: sans-serif }
11
 
tr.address { font-family: sans-serif }
12
 
body { background: #ffffff; color: black; }</style>
 
6
  <title>Qt 4.2: imageviewer.cpp Example File (widgets/imageviewer/imageviewer.cpp)</title>
 
7
  <link href="classic.css" rel="stylesheet" type="text/css" />
13
8
</head>
14
9
<body>
15
10
<table border="0" cellpadding="0" cellspacing="0" width="100%">
16
11
<tr>
17
 
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
12
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
18
13
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
 
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">imageviewer.cpp Example File<br /><sup><sup>widgets/imageviewer/imageviewer.cpp</sup></sup></h1>
20
 
<pre>&nbsp;   /****************************************************************************
21
 
    **
22
 
    ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
23
 
    **
24
 
    ** This file is part of the documentation of the Qt Toolkit.
25
 
    **
26
 
    ** This file may be used under the terms of the GNU General Public
27
 
    ** License version 2.0 as published by the Free Software Foundation
28
 
    ** and appearing in the file LICENSE.GPL included in the packaging of
29
 
    ** this file.  Please review the following information to ensure GNU
30
 
    ** General Public Licensing requirements will be met:
31
 
    ** http://www.trolltech.com/products/qt/opensource.html
32
 
    **
33
 
    ** If you are unsure which license is appropriate for your use, please
34
 
    ** review the following information:
35
 
    ** http://www.trolltech.com/products/qt/licensing.html or contact the
36
 
    ** sales department at sales@trolltech.com.
37
 
    **
38
 
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
39
 
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
40
 
    **
41
 
    ****************************************************************************/
42
 
 
43
 
    #include &lt;QtGui&gt;
44
 
 
45
 
    #include &quot;imageviewer.h&quot;
46
 
 
47
 
    ImageViewer::ImageViewer()
48
 
    {
49
 
        imageLabel = new QLabel;
50
 
        imageLabel-&gt;setBackgroundRole(QPalette::Base);
51
 
        imageLabel-&gt;setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
52
 
        imageLabel-&gt;setScaledContents(true);
53
 
 
54
 
        scrollArea = new QScrollArea;
55
 
        scrollArea-&gt;setBackgroundRole(QPalette::Dark);
56
 
        scrollArea-&gt;setWidget(imageLabel);
57
 
        setCentralWidget(scrollArea);
58
 
 
59
 
        createActions();
60
 
        createMenus();
61
 
 
62
 
        setWindowTitle(tr(&quot;Image Viewer&quot;));
63
 
        resize(500, 400);
64
 
    }
65
 
 
66
 
    void ImageViewer::open()
67
 
    {
68
 
        QString fileName = QFileDialog::getOpenFileName(this,
69
 
                                        tr(&quot;Open File&quot;), QDir::currentPath());
70
 
        if (!fileName.isEmpty()) {
71
 
            QImage image(fileName);
72
 
            if (image.isNull()) {
73
 
                QMessageBox::information(this, tr(&quot;Image Viewer&quot;),
74
 
                                         tr(&quot;Cannot load %1.&quot;).arg(fileName));
75
 
                return;
76
 
            }
77
 
            imageLabel-&gt;setPixmap(QPixmap::fromImage(image));
78
 
            scaleFactor = 1.0;
79
 
 
80
 
            printAct-&gt;setEnabled(true);
81
 
            fitToWindowAct-&gt;setEnabled(true);
82
 
            updateActions();
83
 
 
84
 
            if (!fitToWindowAct-&gt;isChecked())
85
 
                imageLabel-&gt;adjustSize();
86
 
        }
87
 
    }
88
 
 
89
 
    void ImageViewer::print()
90
 
    {
91
 
        Q_ASSERT(imageLabel-&gt;pixmap());
92
 
        QPrintDialog dialog(&amp;printer, this);
93
 
        if (dialog.exec()) {
94
 
            QPainter painter(&amp;printer);
95
 
            QRect rect = painter.viewport();
96
 
            QSize size = imageLabel-&gt;pixmap()-&gt;size();
97
 
            size.scale(rect.size(), Qt::KeepAspectRatio);
98
 
            painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
99
 
            painter.setWindow(imageLabel-&gt;pixmap()-&gt;rect());
100
 
            painter.drawPixmap(0, 0, *imageLabel-&gt;pixmap());
101
 
        }
102
 
    }
103
 
 
104
 
    void ImageViewer::zoomIn()
105
 
    {
106
 
        scaleImage(1.25);
107
 
    }
108
 
 
109
 
    void ImageViewer::zoomOut()
110
 
    {
111
 
        scaleImage(0.8);
112
 
    }
113
 
 
114
 
    void ImageViewer::normalSize()
115
 
    {
116
 
        imageLabel-&gt;adjustSize();
117
 
        scaleFactor = 1.0;
118
 
    }
119
 
 
120
 
    void ImageViewer::fitToWindow()
121
 
    {
122
 
        bool fitToWindow = fitToWindowAct-&gt;isChecked();
123
 
        scrollArea-&gt;setWidgetResizable(fitToWindow);
124
 
        if (!fitToWindow) {
125
 
            normalSize();
126
 
        }
127
 
        updateActions();
128
 
    }
129
 
 
130
 
    void ImageViewer::about()
131
 
    {
132
 
        QMessageBox::about(this, tr(&quot;About Image Viewer&quot;),
133
 
                tr(&quot;&lt;p&gt;The &lt;b&gt;Image Viewer&lt;/b&gt; example shows how to combine QLabel &quot;
134
 
                   &quot;and QScrollArea to display an image. QLabel is typically used &quot;
135
 
                   &quot;for displaying a text, but it can also display an image. &quot;
136
 
                   &quot;QScrollArea provides a scrolling view around another widget. &quot;
137
 
                   &quot;If the child widget exceeds the size of the frame, QScrollArea &quot;
138
 
                   &quot;automatically provides scroll bars. &lt;/p&gt;&lt;p&gt;The example &quot;
139
 
                   &quot;demonstrates how QLabel's ability to scale its contents &quot;
140
 
                   &quot;(QLabel::scaledContents), and QScrollArea's ability to &quot;
141
 
                   &quot;automatically resize its contents &quot;
142
 
                   &quot;(QScrollArea::widgetResizable), can be used to implement &quot;
143
 
                   &quot;zooming and scaling features. &lt;/p&gt;&lt;p&gt;In addition the example &quot;
144
 
                   &quot;shows how to use QPainter to print an image.&lt;/p&gt;&quot;));
145
 
    }
146
 
 
147
 
    void ImageViewer::createActions()
148
 
    {
149
 
        openAct = new QAction(tr(&quot;&amp;Open...&quot;), this);
150
 
        openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
151
 
        connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
152
 
 
153
 
        printAct = new QAction(tr(&quot;&amp;Print...&quot;), this);
154
 
        printAct-&gt;setShortcut(tr(&quot;Ctrl+P&quot;));
155
 
        printAct-&gt;setEnabled(false);
156
 
        connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
157
 
 
158
 
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
159
 
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
160
 
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
161
 
 
162
 
        zoomInAct = new QAction(tr(&quot;Zoom &amp;In (25%)&quot;), this);
163
 
        zoomInAct-&gt;setShortcut(tr(&quot;Ctrl++&quot;));
164
 
        zoomInAct-&gt;setEnabled(false);
165
 
        connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
166
 
 
167
 
        zoomOutAct = new QAction(tr(&quot;Zoom &amp;Out (25%)&quot;), this);
168
 
        zoomOutAct-&gt;setShortcut(tr(&quot;Ctrl+-&quot;));
169
 
        zoomOutAct-&gt;setEnabled(false);
170
 
        connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
171
 
 
172
 
        normalSizeAct = new QAction(tr(&quot;&amp;Normal Size&quot;), this);
173
 
        normalSizeAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
174
 
        normalSizeAct-&gt;setEnabled(false);
175
 
        connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
176
 
 
177
 
        fitToWindowAct = new QAction(tr(&quot;&amp;Fit to Window&quot;), this);
178
 
        fitToWindowAct-&gt;setEnabled(false);
179
 
        fitToWindowAct-&gt;setCheckable(true);
180
 
        fitToWindowAct-&gt;setShortcut(tr(&quot;Ctrl+F&quot;));
181
 
        connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
182
 
 
183
 
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
184
 
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
185
 
 
186
 
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
187
 
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
188
 
    }
189
 
 
190
 
    void ImageViewer::createMenus()
191
 
    {
192
 
        fileMenu = new QMenu(tr(&quot;&amp;File&quot;), this);
193
 
        fileMenu-&gt;addAction(openAct);
194
 
        fileMenu-&gt;addAction(printAct);
195
 
        fileMenu-&gt;addSeparator();
196
 
        fileMenu-&gt;addAction(exitAct);
197
 
 
198
 
        viewMenu = new QMenu(tr(&quot;&amp;View&quot;), this);
199
 
        viewMenu-&gt;addAction(zoomInAct);
200
 
        viewMenu-&gt;addAction(zoomOutAct);
201
 
        viewMenu-&gt;addAction(normalSizeAct);
202
 
        viewMenu-&gt;addSeparator();
203
 
        viewMenu-&gt;addAction(fitToWindowAct);
204
 
 
205
 
        helpMenu = new QMenu(tr(&quot;&amp;Help&quot;), this);
206
 
        helpMenu-&gt;addAction(aboutAct);
207
 
        helpMenu-&gt;addAction(aboutQtAct);
208
 
 
209
 
        menuBar()-&gt;addMenu(fileMenu);
210
 
        menuBar()-&gt;addMenu(viewMenu);
211
 
        menuBar()-&gt;addMenu(helpMenu);
212
 
    }
213
 
 
214
 
    void ImageViewer::updateActions()
215
 
    {
216
 
        zoomInAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
217
 
        zoomOutAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
218
 
        normalSizeAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
219
 
    }
220
 
 
221
 
    void ImageViewer::scaleImage(double factor)
222
 
    {
223
 
        Q_ASSERT(imageLabel-&gt;pixmap());
224
 
        scaleFactor *= factor;
225
 
        imageLabel-&gt;resize(scaleFactor * imageLabel-&gt;pixmap()-&gt;size());
226
 
 
227
 
        adjustScrollBar(scrollArea-&gt;horizontalScrollBar(), factor);
228
 
        adjustScrollBar(scrollArea-&gt;verticalScrollBar(), factor);
229
 
 
230
 
        zoomInAct-&gt;setEnabled(scaleFactor &lt; 3.0);
231
 
        zoomOutAct-&gt;setEnabled(scaleFactor &gt; 0.333);
232
 
    }
233
 
 
234
 
    void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
235
 
    {
236
 
        scrollBar-&gt;setValue(int(factor * scrollBar-&gt;value()
237
 
                                + ((factor - 1) * scrollBar-&gt;pageStep()/2)));
238
 
    }</pre>
 
14
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">imageviewer.cpp Example File<br /><sup><sup>widgets/imageviewer/imageviewer.cpp</sup></sup></h1>
 
15
<pre> /****************************************************************************
 
16
 **
 
17
 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
 
18
 **
 
19
 ** This file is part of the example classes of the Qt Toolkit.
 
20
 **
 
21
 ** This file may be used under the terms of the GNU General Public
 
22
 ** License version 2.0 as published by the Free Software Foundation
 
23
 ** and appearing in the file LICENSE.GPL included in the packaging of
 
24
 ** this file.  Please review the following information to ensure GNU
 
25
 ** General Public Licensing requirements will be met:
 
26
 ** http:<span class="comment">//www.trolltech.com/products/qt/opensource.html</span>
 
27
 **
 
28
 ** If you are unsure which license is appropriate for your use, please
 
29
 ** review the following information:
 
30
 ** http:<span class="comment">//www.trolltech.com/products/qt/licensing.html or contact the</span>
 
31
 ** sales department at sales@trolltech.com.
 
32
 **
 
33
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
34
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
35
 **
 
36
 ****************************************************************************/
 
37
 
 
38
 #include &lt;QtGui&gt;
 
39
 
 
40
 #include &quot;imageviewer.h&quot;
 
41
 
 
42
 ImageViewer::ImageViewer()
 
43
 {
 
44
     imageLabel = new QLabel;
 
45
     imageLabel-&gt;setBackgroundRole(QPalette::Base);
 
46
     imageLabel-&gt;setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
47
     imageLabel-&gt;setScaledContents(true);
 
48
 
 
49
     scrollArea = new QScrollArea;
 
50
     scrollArea-&gt;setBackgroundRole(QPalette::Dark);
 
51
     scrollArea-&gt;setWidget(imageLabel);
 
52
     setCentralWidget(scrollArea);
 
53
 
 
54
     createActions();
 
55
     createMenus();
 
56
 
 
57
     setWindowTitle(tr(&quot;Image Viewer&quot;));
 
58
     resize(500, 400);
 
59
 }
 
60
 
 
61
 void ImageViewer::open()
 
62
 {
 
63
     QString fileName = QFileDialog::getOpenFileName(this,
 
64
                                     tr(&quot;Open File&quot;), QDir::currentPath());
 
65
     if (!fileName.isEmpty()) {
 
66
         QImage image(fileName);
 
67
         if (image.isNull()) {
 
68
             QMessageBox::information(this, tr(&quot;Image Viewer&quot;),
 
69
                                      tr(&quot;Cannot load %1.&quot;).arg(fileName));
 
70
             return;
 
71
         }
 
72
         imageLabel-&gt;setPixmap(QPixmap::fromImage(image));
 
73
         scaleFactor = 1.0;
 
74
 
 
75
         printAct-&gt;setEnabled(true);
 
76
         fitToWindowAct-&gt;setEnabled(true);
 
77
         updateActions();
 
78
 
 
79
         if (!fitToWindowAct-&gt;isChecked())
 
80
             imageLabel-&gt;adjustSize();
 
81
     }
 
82
 }
 
83
 
 
84
 void ImageViewer::print()
 
85
 {
 
86
     Q_ASSERT(imageLabel-&gt;pixmap());
 
87
     QPrintDialog dialog(&amp;printer, this);
 
88
     if (dialog.exec()) {
 
89
         QPainter painter(&amp;printer);
 
90
         QRect rect = painter.viewport();
 
91
         QSize size = imageLabel-&gt;pixmap()-&gt;size();
 
92
         size.scale(rect.size(), Qt::KeepAspectRatio);
 
93
         painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
 
94
         painter.setWindow(imageLabel-&gt;pixmap()-&gt;rect());
 
95
         painter.drawPixmap(0, 0, *imageLabel-&gt;pixmap());
 
96
     }
 
97
 }
 
98
 
 
99
 void ImageViewer::zoomIn()
 
100
 {
 
101
     scaleImage(1.25);
 
102
 }
 
103
 
 
104
 void ImageViewer::zoomOut()
 
105
 {
 
106
     scaleImage(0.8);
 
107
 }
 
108
 
 
109
 void ImageViewer::normalSize()
 
110
 {
 
111
     imageLabel-&gt;adjustSize();
 
112
     scaleFactor = 1.0;
 
113
 }
 
114
 
 
115
 void ImageViewer::fitToWindow()
 
116
 {
 
117
     bool fitToWindow = fitToWindowAct-&gt;isChecked();
 
118
     scrollArea-&gt;setWidgetResizable(fitToWindow);
 
119
     if (!fitToWindow) {
 
120
         normalSize();
 
121
     }
 
122
     updateActions();
 
123
 }
 
124
 
 
125
 void ImageViewer::about()
 
126
 {
 
127
     QMessageBox::about(this, tr(&quot;About Image Viewer&quot;),
 
128
             tr(&quot;&lt;p&gt;The &lt;b&gt;Image Viewer&lt;/b&gt; example shows how to combine QLabel &quot;
 
129
                &quot;and QScrollArea to display an image. QLabel is typically used &quot;
 
130
                &quot;for displaying a text, but it can also display an image. &quot;
 
131
                &quot;QScrollArea provides a scrolling view around another widget. &quot;
 
132
                &quot;If the child widget exceeds the size of the frame, QScrollArea &quot;
 
133
                &quot;automatically provides scroll bars. &lt;/p&gt;&lt;p&gt;The example &quot;
 
134
                &quot;demonstrates how QLabel's ability to scale its contents &quot;
 
135
                &quot;(QLabel::scaledContents), and QScrollArea's ability to &quot;
 
136
                &quot;automatically resize its contents &quot;
 
137
                &quot;(QScrollArea::widgetResizable), can be used to implement &quot;
 
138
                &quot;zooming and scaling features. &lt;/p&gt;&lt;p&gt;In addition the example &quot;
 
139
                &quot;shows how to use QPainter to print an image.&lt;/p&gt;&quot;));
 
140
 }
 
141
 
 
142
 void ImageViewer::createActions()
 
143
 {
 
144
     openAct = new QAction(tr(&quot;&amp;Open...&quot;), this);
 
145
     openAct-&gt;setShortcut(tr(&quot;Ctrl+O&quot;));
 
146
     connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
 
147
 
 
148
     printAct = new QAction(tr(&quot;&amp;Print...&quot;), this);
 
149
     printAct-&gt;setShortcut(tr(&quot;Ctrl+P&quot;));
 
150
     printAct-&gt;setEnabled(false);
 
151
     connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
 
152
 
 
153
     exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
154
     exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
155
     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
156
 
 
157
     zoomInAct = new QAction(tr(&quot;Zoom &amp;In (25%)&quot;), this);
 
158
     zoomInAct-&gt;setShortcut(tr(&quot;Ctrl++&quot;));
 
159
     zoomInAct-&gt;setEnabled(false);
 
160
     connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
 
161
 
 
162
     zoomOutAct = new QAction(tr(&quot;Zoom &amp;Out (25%)&quot;), this);
 
163
     zoomOutAct-&gt;setShortcut(tr(&quot;Ctrl+-&quot;));
 
164
     zoomOutAct-&gt;setEnabled(false);
 
165
     connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
 
166
 
 
167
     normalSizeAct = new QAction(tr(&quot;&amp;Normal Size&quot;), this);
 
168
     normalSizeAct-&gt;setShortcut(tr(&quot;Ctrl+S&quot;));
 
169
     normalSizeAct-&gt;setEnabled(false);
 
170
     connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
 
171
 
 
172
     fitToWindowAct = new QAction(tr(&quot;&amp;Fit to Window&quot;), this);
 
173
     fitToWindowAct-&gt;setEnabled(false);
 
174
     fitToWindowAct-&gt;setCheckable(true);
 
175
     fitToWindowAct-&gt;setShortcut(tr(&quot;Ctrl+F&quot;));
 
176
     connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
 
177
 
 
178
     aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
179
     connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
180
 
 
181
     aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
182
     connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
183
 }
 
184
 
 
185
 void ImageViewer::createMenus()
 
186
 {
 
187
     fileMenu = new QMenu(tr(&quot;&amp;File&quot;), this);
 
188
     fileMenu-&gt;addAction(openAct);
 
189
     fileMenu-&gt;addAction(printAct);
 
190
     fileMenu-&gt;addSeparator();
 
191
     fileMenu-&gt;addAction(exitAct);
 
192
 
 
193
     viewMenu = new QMenu(tr(&quot;&amp;View&quot;), this);
 
194
     viewMenu-&gt;addAction(zoomInAct);
 
195
     viewMenu-&gt;addAction(zoomOutAct);
 
196
     viewMenu-&gt;addAction(normalSizeAct);
 
197
     viewMenu-&gt;addSeparator();
 
198
     viewMenu-&gt;addAction(fitToWindowAct);
 
199
 
 
200
     helpMenu = new QMenu(tr(&quot;&amp;Help&quot;), this);
 
201
     helpMenu-&gt;addAction(aboutAct);
 
202
     helpMenu-&gt;addAction(aboutQtAct);
 
203
 
 
204
     menuBar()-&gt;addMenu(fileMenu);
 
205
     menuBar()-&gt;addMenu(viewMenu);
 
206
     menuBar()-&gt;addMenu(helpMenu);
 
207
 }
 
208
 
 
209
 void ImageViewer::updateActions()
 
210
 {
 
211
     zoomInAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
 
212
     zoomOutAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
 
213
     normalSizeAct-&gt;setEnabled(!fitToWindowAct-&gt;isChecked());
 
214
 }
 
215
 
 
216
 void ImageViewer::scaleImage(double factor)
 
217
 {
 
218
     Q_ASSERT(imageLabel-&gt;pixmap());
 
219
     scaleFactor *= factor;
 
220
     imageLabel-&gt;resize(scaleFactor * imageLabel-&gt;pixmap()-&gt;size());
 
221
 
 
222
     adjustScrollBar(scrollArea-&gt;horizontalScrollBar(), factor);
 
223
     adjustScrollBar(scrollArea-&gt;verticalScrollBar(), factor);
 
224
 
 
225
     zoomInAct-&gt;setEnabled(scaleFactor &lt; 3.0);
 
226
     zoomOutAct-&gt;setEnabled(scaleFactor &gt; 0.333);
 
227
 }
 
228
 
 
229
 void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
 
230
 {
 
231
     scrollBar-&gt;setValue(int(factor * scrollBar-&gt;value()
 
232
                             + ((factor - 1) * scrollBar-&gt;pageStep()/2)));
 
233
 }</pre>
239
234
<p /><address><hr /><div align="center">
240
235
<table width="100%" cellspacing="0" border="0"><tr class="address">
241
236
<td width="30%">Copyright &copy; 2006 <a href="trolltech.html">Trolltech</a></td>
242
237
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
243
 
<td width="30%" align="right"><div align="right">Qt 4.1.4</div></td>
 
238
<td width="30%" align="right"><div align="right">Qt 4.2.0</div></td>
244
239
</tr></table></div></address></body>
245
240
</html>