source: trunk/examples/opengl/grabber/mainwindow.cpp@ 983

Last change on this file since 983 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: 7.2 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#include <QtOpenGL>
43
44#include "glwidget.h"
45#include "mainwindow.h"
46
47MainWindow::MainWindow()
48{
49 centralWidget = new QWidget;
50 setCentralWidget(centralWidget);
51
52 glWidget = new GLWidget;
53 pixmapLabel = new QLabel;
54
55 glWidgetArea = new QScrollArea;
56 glWidgetArea->setWidget(glWidget);
57 glWidgetArea->setWidgetResizable(true);
58 glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59 glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
61 glWidgetArea->setMinimumSize(50, 50);
62
63 pixmapLabelArea = new QScrollArea;
64 pixmapLabelArea->setWidget(pixmapLabel);
65 pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
66 pixmapLabelArea->setMinimumSize(50, 50);
67
68 xSlider = createSlider(SIGNAL(xRotationChanged(int)),
69 SLOT(setXRotation(int)));
70 ySlider = createSlider(SIGNAL(yRotationChanged(int)),
71 SLOT(setYRotation(int)));
72 zSlider = createSlider(SIGNAL(zRotationChanged(int)),
73 SLOT(setZRotation(int)));
74
75 createActions();
76 createMenus();
77
78 QGridLayout *centralLayout = new QGridLayout;
79 centralLayout->addWidget(glWidgetArea, 0, 0);
80 centralLayout->addWidget(pixmapLabelArea, 0, 1);
81 centralLayout->addWidget(xSlider, 1, 0, 1, 2);
82 centralLayout->addWidget(ySlider, 2, 0, 1, 2);
83 centralLayout->addWidget(zSlider, 3, 0, 1, 2);
84 centralWidget->setLayout(centralLayout);
85
86 xSlider->setValue(15 * 16);
87 ySlider->setValue(345 * 16);
88 zSlider->setValue(0 * 16);
89
90 setWindowTitle(tr("Grabber"));
91 resize(400, 300);
92}
93
94void MainWindow::renderIntoPixmap()
95{
96 QSize size = getSize();
97 if (size.isValid()) {
98 QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height());
99 setPixmap(pixmap);
100 }
101}
102
103void MainWindow::grabFrameBuffer()
104{
105 QImage image = glWidget->grabFrameBuffer();
106 setPixmap(QPixmap::fromImage(image));
107}
108
109void MainWindow::clearPixmap()
110{
111 setPixmap(QPixmap());
112}
113
114void MainWindow::about()
115{
116 QMessageBox::about(this, tr("About Grabber"),
117 tr("The <b>Grabber</b> example demonstrates two approaches for "
118 "rendering OpenGL into a Qt pixmap."));
119}
120
121void MainWindow::createActions()
122{
123 renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this);
124 renderIntoPixmapAct->setShortcut(tr("Ctrl+R"));
125 connect(renderIntoPixmapAct, SIGNAL(triggered()),
126 this, SLOT(renderIntoPixmap()));
127
128 grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this);
129 grabFrameBufferAct->setShortcut(tr("Ctrl+G"));
130 connect(grabFrameBufferAct, SIGNAL(triggered()),
131 this, SLOT(grabFrameBuffer()));
132
133 clearPixmapAct = new QAction(tr("&Clear Pixmap"), this);
134 clearPixmapAct->setShortcut(tr("Ctrl+L"));
135 connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
136
137 exitAct = new QAction(tr("E&xit"), this);
138 exitAct->setShortcuts(QKeySequence::Quit);
139 connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
140
141 aboutAct = new QAction(tr("&About"), this);
142 connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
143
144 aboutQtAct = new QAction(tr("About &Qt"), this);
145 connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
146}
147
148void MainWindow::createMenus()
149{
150 fileMenu = menuBar()->addMenu(tr("&File"));
151 fileMenu->addAction(renderIntoPixmapAct);
152 fileMenu->addAction(grabFrameBufferAct);
153 fileMenu->addAction(clearPixmapAct);
154 fileMenu->addSeparator();
155 fileMenu->addAction(exitAct);
156
157 helpMenu = menuBar()->addMenu(tr("&Help"));
158 helpMenu->addAction(aboutAct);
159 helpMenu->addAction(aboutQtAct);
160}
161
162QSlider *MainWindow::createSlider(const char *changedSignal,
163 const char *setterSlot)
164{
165 QSlider *slider = new QSlider(Qt::Horizontal);
166 slider->setRange(0, 360 * 16);
167 slider->setSingleStep(16);
168 slider->setPageStep(15 * 16);
169 slider->setTickInterval(15 * 16);
170 slider->setTickPosition(QSlider::TicksRight);
171 connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
172 connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
173 return slider;
174}
175
176void MainWindow::setPixmap(const QPixmap &pixmap)
177{
178 pixmapLabel->setPixmap(pixmap);
179 QSize size = pixmap.size();
180 if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize())
181 size -= QSize(1, 0);
182 pixmapLabel->resize(size);
183}
184
185QSize MainWindow::getSize()
186{
187 bool ok;
188 QString text = QInputDialog::getText(this, tr("Grabber"),
189 tr("Enter pixmap size:"),
190 QLineEdit::Normal,
191 tr("%1 x %2").arg(glWidget->width())
192 .arg(glWidget->height()),
193 &ok);
194 if (!ok)
195 return QSize();
196
197 QRegExp regExp(tr("([0-9]+) *x *([0-9]+)"));
198 if (regExp.exactMatch(text)) {
199 int width = regExp.cap(1).toInt();
200 int height = regExp.cap(2).toInt();
201 if (width > 0 && width < 2048 && height > 0 && height < 2048)
202 return QSize(width, height);
203 }
204
205 return glWidget->size();
206}
Note: See TracBrowser for help on using the repository browser.