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

« back to all changes in this revision

Viewing changes to doc/html/opengl-grabber-mainwindow-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<head>
 
6
    <title>Qt 4.0: mainwindow.cpp Example File (opengl/grabber/mainwindow.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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
18
<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="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</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">mainwindow.cpp Example File<br /><small><small>opengl/grabber/mainwindow.cpp</small></small></h1>
 
20
<pre>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
26
    ** This file may be distributed under the terms of the Q Public License
 
27
** as defined by Trolltech AS of Norway and appearing in the file
 
28
** LICENSE.QPL included in the packaging of this file.
 
29
**
 
30
** This file may be distributed and/or modified under the terms of the
 
31
** GNU General Public License version 2 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.
 
34
**
 
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
36
**   information about Qt Commercial License Agreements.
 
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
43
    ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
44
    ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
    #include &lt;QtOpenGL&gt;
 
50
 
 
51
    #include &quot;glwidget.h&quot;
 
52
    #include &quot;mainwindow.h&quot;
 
53
 
 
54
    MainWindow::MainWindow()
 
55
    {
 
56
        centralWidget = new QWidget;
 
57
        setCentralWidget(centralWidget);
 
58
 
 
59
        glWidget = new GLWidget;
 
60
        pixmapLabel = new QLabel;
 
61
 
 
62
        glWidgetArea = new QScrollArea;
 
63
        glWidgetArea-&gt;setWidget(glWidget);
 
64
        glWidgetArea-&gt;setWidgetResizable(true);
 
65
        glWidgetArea-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
66
        glWidgetArea-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
 
67
        glWidgetArea-&gt;setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
68
        glWidgetArea-&gt;setMinimumSize(50, 50);
 
69
 
 
70
        pixmapLabelArea = new QScrollArea;
 
71
        pixmapLabelArea-&gt;setWidget(pixmapLabel);
 
72
        pixmapLabelArea-&gt;setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
 
73
        pixmapLabelArea-&gt;setMinimumSize(50, 50);
 
74
 
 
75
        xSlider = createSlider(SIGNAL(xRotationChanged(int)),
 
76
                               SLOT(setXRotation(int)));
 
77
        ySlider = createSlider(SIGNAL(yRotationChanged(int)),
 
78
                               SLOT(setYRotation(int)));
 
79
        zSlider = createSlider(SIGNAL(zRotationChanged(int)),
 
80
                               SLOT(setZRotation(int)));
 
81
 
 
82
        createActions();
 
83
        createMenus();
 
84
 
 
85
        QGridLayout *centralLayout = new QGridLayout;
 
86
        centralLayout-&gt;addWidget(glWidgetArea, 0, 0);
 
87
        centralLayout-&gt;addWidget(pixmapLabelArea, 0, 1);
 
88
        centralLayout-&gt;addWidget(xSlider, 1, 0, 1, 2);
 
89
        centralLayout-&gt;addWidget(ySlider, 2, 0, 1, 2);
 
90
        centralLayout-&gt;addWidget(zSlider, 3, 0, 1, 2);
 
91
        centralWidget-&gt;setLayout(centralLayout);
 
92
 
 
93
        xSlider-&gt;setValue(15 * 16);
 
94
        ySlider-&gt;setValue(345 * 16);
 
95
        zSlider-&gt;setValue(0 * 16);
 
96
 
 
97
        setWindowTitle(tr(&quot;Grabber&quot;));
 
98
        resize(400, 300);
 
99
    }
 
100
 
 
101
    void MainWindow::renderIntoPixmap()
 
102
    {
 
103
        QSize size = getSize();
 
104
        if (size.isValid()) {
 
105
            QPixmap pixmap = glWidget-&gt;renderPixmap(size.width(), size.height());
 
106
            setPixmap(pixmap);
 
107
        }
 
108
    }
 
109
 
 
110
    void MainWindow::grabFrameBuffer()
 
111
    {
 
112
        QImage image = glWidget-&gt;grabFrameBuffer();
 
113
        setPixmap(QPixmap::fromImage(image));
 
114
    }
 
115
 
 
116
    void MainWindow::clearPixmap()
 
117
    {
 
118
        setPixmap(QPixmap());
 
119
    }
 
120
 
 
121
    void MainWindow::about()
 
122
    {
 
123
        QMessageBox::about(this, tr(&quot;About Grabber&quot;),
 
124
                tr(&quot;The &lt;b&gt;Grabber&lt;/b&gt; example demonstrates two approaches for &quot;
 
125
                   &quot;rendering OpenGL into a Qt pixmap.&quot;));
 
126
    }
 
127
 
 
128
    void MainWindow::createActions()
 
129
    {
 
130
        renderIntoPixmapAct = new QAction(tr(&quot;&amp;Render into Pixmap...&quot;), this);
 
131
        renderIntoPixmapAct-&gt;setShortcut(tr(&quot;Ctrl+R&quot;));
 
132
        connect(renderIntoPixmapAct, SIGNAL(triggered()),
 
133
                this, SLOT(renderIntoPixmap()));
 
134
 
 
135
        grabFrameBufferAct = new QAction(tr(&quot;&amp;Grab Frame Buffer&quot;), this);
 
136
        grabFrameBufferAct-&gt;setShortcut(tr(&quot;Ctrl+G&quot;));
 
137
        connect(grabFrameBufferAct, SIGNAL(triggered()),
 
138
                this, SLOT(grabFrameBuffer()));
 
139
 
 
140
        clearPixmapAct = new QAction(tr(&quot;&amp;Clear Pixmap&quot;), this);
 
141
        clearPixmapAct-&gt;setShortcut(tr(&quot;Ctrl+L&quot;));
 
142
        connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
 
143
 
 
144
        exitAct = new QAction(tr(&quot;E&amp;xit&quot;), this);
 
145
        exitAct-&gt;setShortcut(tr(&quot;Ctrl+Q&quot;));
 
146
        connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
 
147
 
 
148
        aboutAct = new QAction(tr(&quot;&amp;About&quot;), this);
 
149
        connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
 
150
 
 
151
        aboutQtAct = new QAction(tr(&quot;About &amp;Qt&quot;), this);
 
152
        connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
153
    }
 
154
 
 
155
    void MainWindow::createMenus()
 
156
    {
 
157
        fileMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;File&quot;));
 
158
        fileMenu-&gt;addAction(renderIntoPixmapAct);
 
159
        fileMenu-&gt;addAction(grabFrameBufferAct);
 
160
        fileMenu-&gt;addAction(clearPixmapAct);
 
161
        fileMenu-&gt;addSeparator();
 
162
        fileMenu-&gt;addAction(exitAct);
 
163
 
 
164
        helpMenu = menuBar()-&gt;addMenu(tr(&quot;&amp;Help&quot;));
 
165
        helpMenu-&gt;addAction(aboutAct);
 
166
        helpMenu-&gt;addAction(aboutQtAct);
 
167
    }
 
168
 
 
169
    QSlider *MainWindow::createSlider(const char *changedSignal,
 
170
                                      const char *setterSlot)
 
171
    {
 
172
        QSlider *slider = new QSlider(Qt::Horizontal);
 
173
        slider-&gt;setRange(0, 360 * 16);
 
174
        slider-&gt;setSingleStep(16);
 
175
        slider-&gt;setPageStep(15 * 16);
 
176
        slider-&gt;setTickInterval(15 * 16);
 
177
        slider-&gt;setTickPosition(QSlider::TicksRight);
 
178
        connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
 
179
        connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
 
180
        return slider;
 
181
    }
 
182
 
 
183
    void MainWindow::setPixmap(const QPixmap &amp;pixmap)
 
184
    {
 
185
        pixmapLabel-&gt;setPixmap(pixmap);
 
186
        QSize size = pixmap.size();
 
187
        if (size - QSize(1, 0) == pixmapLabelArea-&gt;maximumViewportSize())
 
188
            size -= QSize(1, 0);
 
189
        pixmapLabel-&gt;resize(size);
 
190
    }
 
191
 
 
192
    QSize MainWindow::getSize()
 
193
    {
 
194
        bool ok;
 
195
        QString text = QInputDialog::getText(this, tr(&quot;Grabber&quot;),
 
196
                                             tr(&quot;Enter pixmap size:&quot;),
 
197
                                             QLineEdit::Normal,
 
198
                                             tr(&quot;%1 x %2&quot;).arg(glWidget-&gt;width())
 
199
                                                          .arg(glWidget-&gt;height()),
 
200
                                             &amp;ok);
 
201
        if (!ok)
 
202
            return QSize();
 
203
 
 
204
        QRegExp regExp(tr(&quot;([0-9]+) *x *([0-9]+)&quot;));
 
205
        if (regExp.exactMatch(text)) {
 
206
            int width = regExp.cap(0).toInt();
 
207
            int height = regExp.cap(1).toInt();
 
208
            if (width &gt; 0 &amp;&amp; width &lt; 2048 &amp;&amp; height &gt; 0 &amp;&amp; height &lt; 2048)
 
209
                return QSize(width, height);
 
210
        }
 
211
 
 
212
        return glWidget-&gt;size();
 
213
    }</pre>
 
214
<p /><address><hr /><div align="center">
 
215
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
216
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
217
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
218
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
219
</tr></table></div></address></body>
 
220
</html>