2014-06-20 09:58:34 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2014-06-20 09:58:34 +00:00
|
|
|
**
|
|
|
|
** This file is part of the examples of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2016-01-22 12:24:00 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
**
|
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2014-06-20 09:58:34 +00:00
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2015-02-13 11:15:33 +00:00
|
|
|
** * Neither the name of The Qt Company Ltd nor the names of its
|
|
|
|
** contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
2014-06-20 09:58:34 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QSlider>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QSpinBox>
|
2015-06-01 09:44:37 +00:00
|
|
|
#include <QScrollArea>
|
2014-06-20 09:58:34 +00:00
|
|
|
|
|
|
|
#include "glwidget.h"
|
|
|
|
|
2015-08-28 13:16:57 +00:00
|
|
|
typedef void (QWidget::*QWidgetVoidSlot)();
|
|
|
|
|
2014-06-20 09:58:34 +00:00
|
|
|
MainWindow::MainWindow()
|
|
|
|
: m_nextX(1), m_nextY(1)
|
|
|
|
{
|
|
|
|
GLWidget *glwidget = new GLWidget(this, true, qRgb(20, 20, 50));
|
2014-09-28 16:58:46 +00:00
|
|
|
m_glWidgets << glwidget;
|
2014-06-20 09:58:34 +00:00
|
|
|
QLabel *label = new QLabel(this);
|
|
|
|
m_timer = new QTimer(this);
|
|
|
|
QSlider *slider = new QSlider(this);
|
|
|
|
slider->setOrientation(Qt::Horizontal);
|
|
|
|
|
|
|
|
QLabel *updateLabel = new QLabel("Update interval");
|
|
|
|
QSpinBox *updateInterval = new QSpinBox(this);
|
|
|
|
updateInterval->setSuffix(" ms");
|
|
|
|
updateInterval->setValue(10);
|
|
|
|
updateInterval->setToolTip("Interval for the timer that calls update().\n"
|
|
|
|
"Note that on most systems the swap will block to wait for vsync\n"
|
|
|
|
"and therefore an interval < 16 ms will likely lead to a 60 FPS update rate.");
|
|
|
|
QGroupBox *updateGroupBox = new QGroupBox(this);
|
2014-09-28 16:58:46 +00:00
|
|
|
QCheckBox *timerBased = new QCheckBox("Use timer", this);
|
2015-06-01 09:44:37 +00:00
|
|
|
timerBased->setChecked(false);
|
2014-09-28 16:58:46 +00:00
|
|
|
timerBased->setToolTip("Toggles using a timer to trigger update().\n"
|
|
|
|
"When not set, each paintGL() schedules the next update immediately,\n"
|
|
|
|
"expecting the blocking swap to throttle the thread.\n"
|
|
|
|
"This shows how unnecessary the timer is in most cases.");
|
2014-06-20 09:58:34 +00:00
|
|
|
QCheckBox *transparent = new QCheckBox("Transparent background", this);
|
|
|
|
transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n"
|
|
|
|
"Note how the button on top stacks incorrectly when enabling this.");
|
|
|
|
QHBoxLayout *updateLayout = new QHBoxLayout;
|
|
|
|
updateLayout->addWidget(updateLabel);
|
|
|
|
updateLayout->addWidget(updateInterval);
|
2014-09-28 16:58:46 +00:00
|
|
|
updateLayout->addWidget(timerBased);
|
2014-06-20 09:58:34 +00:00
|
|
|
updateLayout->addWidget(transparent);
|
|
|
|
updateGroupBox->setLayout(updateLayout);
|
|
|
|
|
|
|
|
slider->setRange(0, 50);
|
|
|
|
slider->setSliderPosition(30);
|
|
|
|
m_timer->setInterval(10);
|
2015-06-01 09:44:37 +00:00
|
|
|
label->setText("A scrollable QOpenGLWidget");
|
2014-06-20 09:58:34 +00:00
|
|
|
label->setAlignment(Qt::AlignHCenter);
|
|
|
|
|
|
|
|
QGroupBox * groupBox = new QGroupBox(this);
|
|
|
|
setCentralWidget(groupBox);
|
|
|
|
groupBox->setTitle("QOpenGLWidget Example");
|
|
|
|
|
|
|
|
m_layout = new QGridLayout(groupBox);
|
|
|
|
|
2015-06-01 09:44:37 +00:00
|
|
|
QScrollArea *scrollArea = new QScrollArea;
|
|
|
|
scrollArea->setWidget(glwidget);
|
|
|
|
|
|
|
|
m_layout->addWidget(scrollArea,1,0,8,1);
|
2014-06-20 09:58:34 +00:00
|
|
|
m_layout->addWidget(label,9,0,1,1);
|
|
|
|
m_layout->addWidget(updateGroupBox, 10, 0, 1, 1);
|
|
|
|
m_layout->addWidget(slider, 11,0,1,1);
|
|
|
|
|
|
|
|
groupBox->setLayout(m_layout);
|
|
|
|
|
2015-08-28 13:16:57 +00:00
|
|
|
|
2014-08-22 14:45:59 +00:00
|
|
|
QMenu *fileMenu = menuBar()->addMenu("&File");
|
2015-08-28 13:16:57 +00:00
|
|
|
fileMenu->addAction("E&xit", this, &QWidget::close);
|
2014-08-22 14:45:59 +00:00
|
|
|
QMenu *showMenu = menuBar()->addMenu("&Show");
|
2015-08-28 13:16:57 +00:00
|
|
|
showMenu->addAction("Show 3D Logo", glwidget, &GLWidget::setLogo);
|
|
|
|
showMenu->addAction("Show 2D Texture", glwidget, &GLWidget::setTexture);
|
|
|
|
QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
|
2014-06-20 09:58:34 +00:00
|
|
|
showBubbles->setCheckable(true);
|
|
|
|
showBubbles->setChecked(true);
|
2015-08-28 13:16:57 +00:00
|
|
|
QMenu *helpMenu = menuBar()->addMenu("&Help");
|
|
|
|
helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
|
2014-06-20 09:58:34 +00:00
|
|
|
|
2015-08-28 13:16:57 +00:00
|
|
|
connect(m_timer, &QTimer::timeout,
|
|
|
|
glwidget, static_cast<QWidgetVoidSlot>(&QWidget::update));
|
2014-06-20 09:58:34 +00:00
|
|
|
|
2015-08-28 13:16:57 +00:00
|
|
|
connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
|
2014-06-20 09:58:34 +00:00
|
|
|
connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
|
|
|
|
|
2015-08-28 13:16:57 +00:00
|
|
|
typedef void (QSpinBox::*QSpinBoxIntSignal)(int);
|
|
|
|
connect(updateInterval, static_cast<QSpinBoxIntSignal>(&QSpinBox::valueChanged),
|
|
|
|
this, &MainWindow::updateIntervalChanged);
|
2014-09-28 16:58:46 +00:00
|
|
|
connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
|
|
|
|
connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
|
2014-06-20 09:58:34 +00:00
|
|
|
|
2015-06-01 09:44:37 +00:00
|
|
|
if (timerBased->isChecked())
|
|
|
|
m_timer->start();
|
|
|
|
else
|
|
|
|
updateInterval->setEnabled(false);
|
2014-06-20 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::updateIntervalChanged(int value)
|
|
|
|
{
|
|
|
|
m_timer->setInterval(value);
|
2014-09-28 16:58:46 +00:00
|
|
|
if (m_timer->isActive())
|
|
|
|
m_timer->start();
|
2014-06-20 09:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::addNew()
|
|
|
|
{
|
|
|
|
if (m_nextY == 4)
|
|
|
|
return;
|
|
|
|
GLWidget *w = new GLWidget(this, false, qRgb(qrand() % 256, qrand() % 256, qrand() % 256));
|
2014-09-28 16:58:46 +00:00
|
|
|
m_glWidgets << w;
|
2015-08-28 13:16:57 +00:00
|
|
|
connect(m_timer, &QTimer::timeout, w, static_cast<QWidgetVoidSlot>(&QWidget::update));
|
2014-06-20 09:58:34 +00:00
|
|
|
m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
|
|
|
|
if (m_nextX == 3) {
|
|
|
|
m_nextX = 1;
|
|
|
|
++m_nextY;
|
|
|
|
} else {
|
|
|
|
++m_nextX;
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 16:58:46 +00:00
|
|
|
|
|
|
|
void MainWindow::timerUsageChanged(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled) {
|
|
|
|
m_timer->start();
|
|
|
|
} else {
|
|
|
|
m_timer->stop();
|
|
|
|
foreach (QOpenGLWidget *w, m_glWidgets)
|
|
|
|
w->update();
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 09:44:37 +00:00
|
|
|
|
|
|
|
void MainWindow::resizeEvent(QResizeEvent *)
|
|
|
|
{
|
|
|
|
m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
|
|
|
|
}
|