1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
42
#include "mainwindow.h"
46
MainWindow::MainWindow()
48
centralWidget = new QWidget;
49
setCentralWidget(centralWidget);
51
glWidget = new GLWidget;
52
pixmapLabel = new QLabel;
54
glWidgetArea = new QScrollArea;
55
glWidgetArea->setWidget(glWidget);
56
glWidgetArea->setWidgetResizable(true);
57
glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58
glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
59
glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
60
glWidgetArea->setMinimumSize(50, 50);
62
pixmapLabelArea = new QScrollArea;
63
pixmapLabelArea->setWidget(pixmapLabel);
64
pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
65
pixmapLabelArea->setMinimumSize(50, 50);
67
xSlider = createSlider(SIGNAL(xRotationChanged(int)),
68
SLOT(setXRotation(int)));
69
ySlider = createSlider(SIGNAL(yRotationChanged(int)),
70
SLOT(setYRotation(int)));
71
zSlider = createSlider(SIGNAL(zRotationChanged(int)),
72
SLOT(setZRotation(int)));
77
QGridLayout *centralLayout = new QGridLayout;
78
centralLayout->addWidget(glWidgetArea, 0, 0);
79
centralLayout->addWidget(pixmapLabelArea, 0, 1);
80
centralLayout->addWidget(xSlider, 1, 0, 1, 2);
81
centralLayout->addWidget(ySlider, 2, 0, 1, 2);
82
centralLayout->addWidget(zSlider, 3, 0, 1, 2);
83
centralWidget->setLayout(centralLayout);
85
xSlider->setValue(15 * 16);
86
ySlider->setValue(345 * 16);
87
zSlider->setValue(0 * 16);
89
setWindowTitle(tr("Grabber"));
93
void MainWindow::renderIntoPixmap()
95
QSize size = getSize();
97
QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height());
102
void MainWindow::grabFrameBuffer()
104
QImage image = glWidget->grabFrameBuffer();
105
setPixmap(QPixmap::fromImage(image));
108
void MainWindow::clearPixmap()
110
setPixmap(QPixmap());
113
void MainWindow::about()
115
QMessageBox::about(this, tr("About Grabber"),
116
tr("The <b>Grabber</b> example demonstrates two approaches for "
117
"rendering OpenGL into a Qt pixmap."));
120
void MainWindow::createActions()
122
renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this);
123
renderIntoPixmapAct->setShortcut(tr("Ctrl+R"));
124
connect(renderIntoPixmapAct, SIGNAL(triggered()),
125
this, SLOT(renderIntoPixmap()));
127
grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this);
128
grabFrameBufferAct->setShortcut(tr("Ctrl+G"));
129
connect(grabFrameBufferAct, SIGNAL(triggered()),
130
this, SLOT(grabFrameBuffer()));
132
clearPixmapAct = new QAction(tr("&Clear Pixmap"), this);
133
clearPixmapAct->setShortcut(tr("Ctrl+L"));
134
connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap()));
136
exitAct = new QAction(tr("E&xit"), this);
137
exitAct->setShortcuts(QKeySequence::Quit);
138
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
140
aboutAct = new QAction(tr("&About"), this);
141
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
143
aboutQtAct = new QAction(tr("About &Qt"), this);
144
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
147
void MainWindow::createMenus()
149
fileMenu = menuBar()->addMenu(tr("&File"));
150
fileMenu->addAction(renderIntoPixmapAct);
151
fileMenu->addAction(grabFrameBufferAct);
152
fileMenu->addAction(clearPixmapAct);
153
fileMenu->addSeparator();
154
fileMenu->addAction(exitAct);
156
helpMenu = menuBar()->addMenu(tr("&Help"));
157
helpMenu->addAction(aboutAct);
158
helpMenu->addAction(aboutQtAct);
161
QSlider *MainWindow::createSlider(const char *changedSignal,
162
const char *setterSlot)
164
QSlider *slider = new QSlider(Qt::Horizontal);
165
slider->setRange(0, 360 * 16);
166
slider->setSingleStep(16);
167
slider->setPageStep(15 * 16);
168
slider->setTickInterval(15 * 16);
169
slider->setTickPosition(QSlider::TicksRight);
170
connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot);
171
connect(glWidget, changedSignal, slider, SLOT(setValue(int)));
175
void MainWindow::setPixmap(const QPixmap &pixmap)
177
pixmapLabel->setPixmap(pixmap);
178
QSize size = pixmap.size();
179
if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize())
181
pixmapLabel->resize(size);
184
QSize MainWindow::getSize()
187
QString text = QInputDialog::getText(this, tr("Grabber"),
188
tr("Enter pixmap size:"),
190
tr("%1 x %2").arg(glWidget->width())
191
.arg(glWidget->height()),
196
QRegExp regExp(tr("([0-9]+) *x *([0-9]+)"));
197
if (regExp.exactMatch(text)) {
198
int width = regExp.cap(1).toInt();
199
int height = regExp.cap(2).toInt();
200
if (width > 0 && width < 2048 && height > 0 && height < 2048)
201
return QSize(width, height);
204
return glWidget->size();