xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/qopenglwidget/mainwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /****************************************************************************
2*4882a593Smuzhiyun **
3*4882a593Smuzhiyun ** Copyright (C) 2016 The Qt Company Ltd.
4*4882a593Smuzhiyun ** Contact: https://www.qt.io/licensing/
5*4882a593Smuzhiyun **
6*4882a593Smuzhiyun ** This file is part of the examples of the Qt Toolkit.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun ** $QT_BEGIN_LICENSE:BSD$
9*4882a593Smuzhiyun ** Commercial License Usage
10*4882a593Smuzhiyun ** Licensees holding valid commercial Qt licenses may use this file in
11*4882a593Smuzhiyun ** accordance with the commercial license agreement provided with the
12*4882a593Smuzhiyun ** Software or, alternatively, in accordance with the terms contained in
13*4882a593Smuzhiyun ** a written agreement between you and The Qt Company. For licensing terms
14*4882a593Smuzhiyun ** and conditions see https://www.qt.io/terms-conditions. For further
15*4882a593Smuzhiyun ** information use the contact form at https://www.qt.io/contact-us.
16*4882a593Smuzhiyun **
17*4882a593Smuzhiyun ** BSD License Usage
18*4882a593Smuzhiyun ** Alternatively, you may use this file under the terms of the BSD license
19*4882a593Smuzhiyun ** as follows:
20*4882a593Smuzhiyun **
21*4882a593Smuzhiyun ** "Redistribution and use in source and binary forms, with or without
22*4882a593Smuzhiyun ** modification, are permitted provided that the following conditions are
23*4882a593Smuzhiyun ** met:
24*4882a593Smuzhiyun **   * Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun **   * Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun **     notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun **     the documentation and/or other materials provided with the
29*4882a593Smuzhiyun **     distribution.
30*4882a593Smuzhiyun **   * Neither the name of The Qt Company Ltd nor the names of its
31*4882a593Smuzhiyun **     contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun **     from this software without specific prior written permission.
33*4882a593Smuzhiyun **
34*4882a593Smuzhiyun **
35*4882a593Smuzhiyun ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38*4882a593Smuzhiyun ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39*4882a593Smuzhiyun ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40*4882a593Smuzhiyun ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41*4882a593Smuzhiyun ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42*4882a593Smuzhiyun ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43*4882a593Smuzhiyun ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44*4882a593Smuzhiyun ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45*4882a593Smuzhiyun ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46*4882a593Smuzhiyun **
47*4882a593Smuzhiyun ** $QT_END_LICENSE$
48*4882a593Smuzhiyun **
49*4882a593Smuzhiyun ****************************************************************************/
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "mainwindow.h"
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #include <QApplication>
54*4882a593Smuzhiyun #include <QMenuBar>
55*4882a593Smuzhiyun #include <QGroupBox>
56*4882a593Smuzhiyun #include <QSlider>
57*4882a593Smuzhiyun #include <QLabel>
58*4882a593Smuzhiyun #include <QCheckBox>
59*4882a593Smuzhiyun #include <QRandomGenerator>
60*4882a593Smuzhiyun #include <QSpinBox>
61*4882a593Smuzhiyun #include <QScrollArea>
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #include "glwidget.h"
64*4882a593Smuzhiyun 
MainWindow()65*4882a593Smuzhiyun MainWindow::MainWindow()
66*4882a593Smuzhiyun     : m_nextX(1), m_nextY(1)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	setWindowState(Qt::WindowMaximized);
69*4882a593Smuzhiyun 	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
70*4882a593Smuzhiyun     GLWidget *glwidget = new GLWidget(this, true, qRgb(20, 20, 50));
71*4882a593Smuzhiyun     m_glWidgets << glwidget;
72*4882a593Smuzhiyun     QLabel *label = new QLabel(this);
73*4882a593Smuzhiyun     m_timer = new QTimer(this);
74*4882a593Smuzhiyun     QSlider *slider = new QSlider(this);
75*4882a593Smuzhiyun     slider->setOrientation(Qt::Horizontal);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun     QLabel *updateLabel = new QLabel("Update interval");
78*4882a593Smuzhiyun     QSpinBox *updateInterval = new QSpinBox(this);
79*4882a593Smuzhiyun     updateInterval->setSuffix(" ms");
80*4882a593Smuzhiyun     updateInterval->setValue(10);
81*4882a593Smuzhiyun     updateInterval->setToolTip("Interval for the timer that calls update().\n"
82*4882a593Smuzhiyun                                "Note that on most systems the swap will block to wait for vsync\n"
83*4882a593Smuzhiyun                                "and therefore an interval < 16 ms will likely lead to a 60 FPS update rate.");
84*4882a593Smuzhiyun     QGroupBox *updateGroupBox = new QGroupBox(this);
85*4882a593Smuzhiyun     QCheckBox *timerBased = new QCheckBox("Use timer", this);
86*4882a593Smuzhiyun     timerBased->setChecked(false);
87*4882a593Smuzhiyun     timerBased->setToolTip("Toggles using a timer to trigger update().\n"
88*4882a593Smuzhiyun                            "When not set, each paintGL() schedules the next update immediately,\n"
89*4882a593Smuzhiyun                            "expecting the blocking swap to throttle the thread.\n"
90*4882a593Smuzhiyun                            "This shows how unnecessary the timer is in most cases.");
91*4882a593Smuzhiyun     QCheckBox *transparent = new QCheckBox("Transparent background", this);
92*4882a593Smuzhiyun     transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n"
93*4882a593Smuzhiyun                             "Note how the button on top stacks incorrectly when enabling this.");
94*4882a593Smuzhiyun     QHBoxLayout *updateLayout = new QHBoxLayout;
95*4882a593Smuzhiyun     updateLayout->addWidget(updateLabel);
96*4882a593Smuzhiyun     updateLayout->addWidget(updateInterval);
97*4882a593Smuzhiyun     updateLayout->addWidget(timerBased);
98*4882a593Smuzhiyun     updateLayout->addWidget(transparent);
99*4882a593Smuzhiyun     updateGroupBox->setLayout(updateLayout);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun     slider->setRange(0, 50);
102*4882a593Smuzhiyun     slider->setSliderPosition(30);
103*4882a593Smuzhiyun     m_timer->setInterval(10);
104*4882a593Smuzhiyun     label->setText("A scrollable QOpenGLWidget");
105*4882a593Smuzhiyun     label->setAlignment(Qt::AlignHCenter);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun     QGroupBox * groupBox = new QGroupBox(this);
108*4882a593Smuzhiyun     setCentralWidget(groupBox);
109*4882a593Smuzhiyun     groupBox->setTitle("QOpenGLWidget Example");
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun     m_layout = new QGridLayout(groupBox);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun     QScrollArea *scrollArea = new QScrollArea;
114*4882a593Smuzhiyun     scrollArea->setWidget(glwidget);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	QPushButton *exitBtn = new QPushButton(this);
117*4882a593Smuzhiyun 	QPalette   pal;
118*4882a593Smuzhiyun 	pal.setColor(QPalette::Button, Qt::red);
119*4882a593Smuzhiyun     exitBtn->setText(tr("Exit"));
120*4882a593Smuzhiyun 	exitBtn->resize(100, 100);
121*4882a593Smuzhiyun 	exitBtn->setPalette(pal);
122*4882a593Smuzhiyun     connect(exitBtn, &QPushButton::clicked, this, [=](){
123*4882a593Smuzhiyun     close();
124*4882a593Smuzhiyun     });
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun     m_layout->addWidget(scrollArea,1,0,8,1);
127*4882a593Smuzhiyun     m_layout->addWidget(label,9,0,1,1);
128*4882a593Smuzhiyun     m_layout->addWidget(updateGroupBox, 10, 0, 1, 1);
129*4882a593Smuzhiyun     m_layout->addWidget(slider, 11,0,1,1);
130*4882a593Smuzhiyun 	m_layout->addWidget(exitBtn, 12, 0, 1, 1);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun     groupBox->setLayout(m_layout);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun     QMenu *fileMenu = menuBar()->addMenu("&File");
136*4882a593Smuzhiyun     fileMenu->addAction("E&xit", this, &QWidget::close);
137*4882a593Smuzhiyun     QMenu *showMenu = menuBar()->addMenu("&Show");
138*4882a593Smuzhiyun     showMenu->addAction("Show 3D Logo", glwidget, &GLWidget::setLogo);
139*4882a593Smuzhiyun     showMenu->addAction("Show 2D Texture", glwidget, &GLWidget::setTexture);
140*4882a593Smuzhiyun     QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
141*4882a593Smuzhiyun     showBubbles->setCheckable(true);
142*4882a593Smuzhiyun     showBubbles->setChecked(true);
143*4882a593Smuzhiyun     QMenu *helpMenu = menuBar()->addMenu("&Help");
144*4882a593Smuzhiyun     helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun     connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun     connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
149*4882a593Smuzhiyun     connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
150*4882a593Smuzhiyun     connect(updateInterval, QOverload<int>::of(&QSpinBox::valueChanged),
151*4882a593Smuzhiyun             this, &MainWindow::updateIntervalChanged);
152*4882a593Smuzhiyun     connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
153*4882a593Smuzhiyun     connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun     if (timerBased->isChecked())
156*4882a593Smuzhiyun         m_timer->start();
157*4882a593Smuzhiyun     else
158*4882a593Smuzhiyun         updateInterval->setEnabled(false);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
updateIntervalChanged(int value)161*4882a593Smuzhiyun void MainWindow::updateIntervalChanged(int value)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun     m_timer->setInterval(value);
164*4882a593Smuzhiyun     if (m_timer->isActive())
165*4882a593Smuzhiyun         m_timer->start();
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun 
addNew()168*4882a593Smuzhiyun void MainWindow::addNew()
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun     if (m_nextY == 4)
171*4882a593Smuzhiyun         return;
172*4882a593Smuzhiyun     GLWidget *w = new GLWidget(this, false, qRgb(QRandomGenerator::global()->bounded(256),
173*4882a593Smuzhiyun                                                  QRandomGenerator::global()->bounded(256),
174*4882a593Smuzhiyun                                                  QRandomGenerator::global()->bounded(256)));
175*4882a593Smuzhiyun     m_glWidgets << w;
176*4882a593Smuzhiyun     connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
177*4882a593Smuzhiyun     m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
178*4882a593Smuzhiyun     if (m_nextX == 3) {
179*4882a593Smuzhiyun         m_nextX = 1;
180*4882a593Smuzhiyun         ++m_nextY;
181*4882a593Smuzhiyun     } else {
182*4882a593Smuzhiyun         ++m_nextX;
183*4882a593Smuzhiyun     }
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
timerUsageChanged(bool enabled)186*4882a593Smuzhiyun void MainWindow::timerUsageChanged(bool enabled)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun     if (enabled) {
189*4882a593Smuzhiyun         m_timer->start();
190*4882a593Smuzhiyun     } else {
191*4882a593Smuzhiyun         m_timer->stop();
192*4882a593Smuzhiyun         foreach (QOpenGLWidget *w, m_glWidgets)
193*4882a593Smuzhiyun             w->update();
194*4882a593Smuzhiyun     }
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
resizeEvent(QResizeEvent *)197*4882a593Smuzhiyun void MainWindow::resizeEvent(QResizeEvent *)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun      m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
200*4882a593Smuzhiyun }
201