| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
#include "glwidget.h" |
| 52 |
#include "mainwindow.h" |
| 53 |
|
| 54 |
#include <QtWidgets> |
| 55 |
|
| 56 |
MainWindow::MainWindow() |
| 57 |
{ |
| 58 |
centralWidget = new QWidget; |
| 59 |
setCentralWidget(centralWidget); |
| 60 |
|
| 61 |
glWidget = new GLWidget; |
| 62 |
pixmapLabel = new QLabel; |
| 63 |
|
| 64 |
glWidgetArea = new QScrollArea; |
| 65 |
glWidgetArea->setWidget(glWidget); |
| 66 |
glWidgetArea->setWidgetResizable(true); |
| 67 |
glWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 68 |
glWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 69 |
glWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 70 |
glWidgetArea->setMinimumSize(50, 50); |
| 71 |
|
| 72 |
pixmapLabelArea = new QScrollArea; |
| 73 |
pixmapLabelArea->setWidget(pixmapLabel); |
| 74 |
pixmapLabelArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 75 |
pixmapLabelArea->setMinimumSize(50, 50); |
| 76 |
|
| 77 |
xSlider = createSlider(SIGNAL(xRotationChanged(int)), |
| 78 |
SLOT(setXRotation(int))); |
| 79 |
ySlider = createSlider(SIGNAL(yRotationChanged(int)), |
| 80 |
SLOT(setYRotation(int))); |
| 81 |
zSlider = createSlider(SIGNAL(zRotationChanged(int)), |
| 82 |
SLOT(setZRotation(int))); |
| 83 |
|
| 84 |
createActions(); |
| 85 |
createMenus(); |
| 86 |
|
| 87 |
QGridLayout *centralLayout = new QGridLayout; |
| 88 |
centralLayout->addWidget(glWidgetArea, 0, 0); |
| 89 |
centralLayout->addWidget(pixmapLabelArea, 0, 1); |
| 90 |
centralLayout->addWidget(xSlider, 1, 0, 1, 2); |
| 91 |
centralLayout->addWidget(ySlider, 2, 0, 1, 2); |
| 92 |
centralLayout->addWidget(zSlider, 3, 0, 1, 2); |
| 93 |
centralWidget->setLayout(centralLayout); |
| 94 |
|
| 95 |
xSlider->setValue(15 * 16); |
| 96 |
ySlider->setValue(345 * 16); |
| 97 |
zSlider->setValue(0 * 16); |
| 98 |
|
| 99 |
setWindowTitle(tr("Grabber")); |
| 100 |
resize(400, 300); |
| 101 |
} |
| 102 |
|
| 103 |
void MainWindow::renderIntoPixmap() |
| 104 |
{ |
| 105 |
QSize size = getSize(); |
| 106 |
if (size.isValid()) { |
| 107 |
QPixmap pixmap = glWidget->renderPixmap(size.width(), size.height()); |
| 108 |
setPixmap(pixmap); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void MainWindow::grabFrameBuffer() |
| 113 |
{ |
| 114 |
QImage image = glWidget->grabFrameBuffer(); |
| 115 |
setPixmap(QPixmap::fromImage(image)); |
| 116 |
} |
| 117 |
|
| 118 |
void MainWindow::clearPixmap() |
| 119 |
{ |
| 120 |
setPixmap(QPixmap()); |
| 121 |
} |
| 122 |
|
| 123 |
void MainWindow::about() |
| 124 |
{ |
| 125 |
QMessageBox::about(this, tr("About Grabber"), |
| 126 |
tr("The <b>Grabber</b> example demonstrates two approaches for " |
| 127 |
"rendering OpenGL into a Qt pixmap.")); |
| 128 |
} |
| 129 |
|
| 130 |
void MainWindow::createActions() |
| 131 |
{ |
| 132 |
renderIntoPixmapAct = new QAction(tr("&Render into Pixmap..."), this); |
| 133 |
renderIntoPixmapAct->setShortcut(tr("Ctrl+R")); |
| 134 |
connect(renderIntoPixmapAct, SIGNAL(triggered()), |
| 135 |
this, SLOT(renderIntoPixmap())); |
| 136 |
|
| 137 |
grabFrameBufferAct = new QAction(tr("&Grab Frame Buffer"), this); |
| 138 |
grabFrameBufferAct->setShortcut(tr("Ctrl+G")); |
| 139 |
connect(grabFrameBufferAct, SIGNAL(triggered()), |
| 140 |
this, SLOT(grabFrameBuffer())); |
| 141 |
|
| 142 |
clearPixmapAct = new QAction(tr("&Clear Pixmap"), this); |
| 143 |
clearPixmapAct->setShortcut(tr("Ctrl+L")); |
| 144 |
connect(clearPixmapAct, SIGNAL(triggered()), this, SLOT(clearPixmap())); |
| 145 |
|
| 146 |
exitAct = new QAction(tr("E&xit"), this); |
| 147 |
exitAct->setShortcuts(QKeySequence::Quit); |
| 148 |
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); |
| 149 |
|
| 150 |
aboutAct = new QAction(tr("&About"), this); |
| 151 |
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); |
| 152 |
|
| 153 |
aboutQtAct = new QAction(tr("About &Qt"), this); |
| 154 |
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); |
| 155 |
} |
| 156 |
|
| 157 |
void MainWindow::createMenus() |
| 158 |
{ |
| 159 |
fileMenu = menuBar()->addMenu(tr("&File")); |
| 160 |
fileMenu->addAction(renderIntoPixmapAct); |
| 161 |
fileMenu->addAction(grabFrameBufferAct); |
| 162 |
fileMenu->addAction(clearPixmapAct); |
| 163 |
fileMenu->addSeparator(); |
| 164 |
fileMenu->addAction(exitAct); |
| 165 |
|
| 166 |
helpMenu = menuBar()->addMenu(tr("&Help")); |
| 167 |
helpMenu->addAction(aboutAct); |
| 168 |
helpMenu->addAction(aboutQtAct); |
| 169 |
} |
| 170 |
|
| 171 |
QSlider *MainWindow::createSlider(const char *changedSignal, |
| 172 |
const char *setterSlot) |
| 173 |
{ |
| 174 |
QSlider *slider = new QSlider(Qt::Horizontal); |
| 175 |
slider->setRange(0, 360 * 16); |
| 176 |
slider->setSingleStep(16); |
| 177 |
slider->setPageStep(15 * 16); |
| 178 |
slider->setTickInterval(15 * 16); |
| 179 |
slider->setTickPosition(QSlider::TicksRight); |
| 180 |
connect(slider, SIGNAL(valueChanged(int)), glWidget, setterSlot); |
| 181 |
connect(glWidget, changedSignal, slider, SLOT(setValue(int))); |
| 182 |
return slider; |
| 183 |
} |
| 184 |
|
| 185 |
void MainWindow::setPixmap(const QPixmap &pixmap) |
| 186 |
{ |
| 187 |
pixmapLabel->setPixmap(pixmap); |
| 188 |
QSize size = pixmap.size() / pixmap.devicePixelRatio(); |
| 189 |
if (size - QSize(1, 0) == pixmapLabelArea->maximumViewportSize()) |
| 190 |
size -= QSize(1, 0); |
| 191 |
pixmapLabel->resize(size); |
| 192 |
} |
| 193 |
|
| 194 |
QSize MainWindow::getSize() |
| 195 |
{ |
| 196 |
bool ok; |
| 197 |
QString text = QInputDialog::getText(this, tr("Grabber"), |
| 198 |
tr("Enter pixmap size:"), |
| 199 |
QLineEdit::Normal, |
| 200 |
tr("%1 x %2").arg(glWidget->width()) |
| 201 |
.arg(glWidget->height()), |
| 202 |
&ok); |
| 203 |
if (!ok) |
| 204 |
return QSize(); |
| 205 |
|
| 206 |
QRegularExpression regExp(tr("^([0-9]+) *x *([0-9]+)$")); |
| 207 |
QRegularExpressionMatch match = regExp.match(text); |
| 208 |
if (match.hasMatch()) { |
| 209 |
int width = match.captured(1).toInt(); |
| 210 |
int height = match.captured(2).toInt(); |
| 211 |
if (width > 0 && width < 2048 && height > 0 && height < 2048) |
| 212 |
return QSize(width, height); |
| 213 |
} |
| 214 |
|
| 215 |
return glWidget->size(); |
| 216 |
} |