xref: /OK3568_Linux_fs/app/forlinx/forlinx_qt/qopenglwidget/mainwindow.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 "mainwindow.h"
52 
53 #include <QApplication>
54 #include <QMenuBar>
55 #include <QGroupBox>
56 #include <QSlider>
57 #include <QLabel>
58 #include <QCheckBox>
59 #include <QRandomGenerator>
60 #include <QSpinBox>
61 #include <QScrollArea>
62 
63 #include "glwidget.h"
64 
MainWindow()65 MainWindow::MainWindow()
66     : m_nextX(1), m_nextY(1)
67 {
68 	setWindowState(Qt::WindowMaximized);
69 	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
70     GLWidget *glwidget = new GLWidget(this, true, qRgb(20, 20, 50));
71     m_glWidgets << glwidget;
72     QLabel *label = new QLabel(this);
73     m_timer = new QTimer(this);
74     QSlider *slider = new QSlider(this);
75     slider->setOrientation(Qt::Horizontal);
76 
77     QLabel *updateLabel = new QLabel("Update interval");
78     QSpinBox *updateInterval = new QSpinBox(this);
79     updateInterval->setSuffix(" ms");
80     updateInterval->setValue(10);
81     updateInterval->setToolTip("Interval for the timer that calls update().\n"
82                                "Note that on most systems the swap will block to wait for vsync\n"
83                                "and therefore an interval < 16 ms will likely lead to a 60 FPS update rate.");
84     QGroupBox *updateGroupBox = new QGroupBox(this);
85     QCheckBox *timerBased = new QCheckBox("Use timer", this);
86     timerBased->setChecked(false);
87     timerBased->setToolTip("Toggles using a timer to trigger update().\n"
88                            "When not set, each paintGL() schedules the next update immediately,\n"
89                            "expecting the blocking swap to throttle the thread.\n"
90                            "This shows how unnecessary the timer is in most cases.");
91     QCheckBox *transparent = new QCheckBox("Transparent background", this);
92     transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n"
93                             "Note how the button on top stacks incorrectly when enabling this.");
94     QHBoxLayout *updateLayout = new QHBoxLayout;
95     updateLayout->addWidget(updateLabel);
96     updateLayout->addWidget(updateInterval);
97     updateLayout->addWidget(timerBased);
98     updateLayout->addWidget(transparent);
99     updateGroupBox->setLayout(updateLayout);
100 
101     slider->setRange(0, 50);
102     slider->setSliderPosition(30);
103     m_timer->setInterval(10);
104     label->setText("A scrollable QOpenGLWidget");
105     label->setAlignment(Qt::AlignHCenter);
106 
107     QGroupBox * groupBox = new QGroupBox(this);
108     setCentralWidget(groupBox);
109     groupBox->setTitle("QOpenGLWidget Example");
110 
111     m_layout = new QGridLayout(groupBox);
112 
113     QScrollArea *scrollArea = new QScrollArea;
114     scrollArea->setWidget(glwidget);
115 
116 	QPushButton *exitBtn = new QPushButton(this);
117 	QPalette   pal;
118 	pal.setColor(QPalette::Button, Qt::red);
119     exitBtn->setText(tr("Exit"));
120 	exitBtn->resize(100, 100);
121 	exitBtn->setPalette(pal);
122     connect(exitBtn, &QPushButton::clicked, this, [=](){
123     close();
124     });
125 
126     m_layout->addWidget(scrollArea,1,0,8,1);
127     m_layout->addWidget(label,9,0,1,1);
128     m_layout->addWidget(updateGroupBox, 10, 0, 1, 1);
129     m_layout->addWidget(slider, 11,0,1,1);
130 	m_layout->addWidget(exitBtn, 12, 0, 1, 1);
131 
132     groupBox->setLayout(m_layout);
133 
134 
135     QMenu *fileMenu = menuBar()->addMenu("&File");
136     fileMenu->addAction("E&xit", this, &QWidget::close);
137     QMenu *showMenu = menuBar()->addMenu("&Show");
138     showMenu->addAction("Show 3D Logo", glwidget, &GLWidget::setLogo);
139     showMenu->addAction("Show 2D Texture", glwidget, &GLWidget::setTexture);
140     QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
141     showBubbles->setCheckable(true);
142     showBubbles->setChecked(true);
143     QMenu *helpMenu = menuBar()->addMenu("&Help");
144     helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
145 
146     connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
147 
148     connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
149     connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
150     connect(updateInterval, QOverload<int>::of(&QSpinBox::valueChanged),
151             this, &MainWindow::updateIntervalChanged);
152     connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
153     connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
154 
155     if (timerBased->isChecked())
156         m_timer->start();
157     else
158         updateInterval->setEnabled(false);
159 }
160 
updateIntervalChanged(int value)161 void MainWindow::updateIntervalChanged(int value)
162 {
163     m_timer->setInterval(value);
164     if (m_timer->isActive())
165         m_timer->start();
166 }
167 
addNew()168 void MainWindow::addNew()
169 {
170     if (m_nextY == 4)
171         return;
172     GLWidget *w = new GLWidget(this, false, qRgb(QRandomGenerator::global()->bounded(256),
173                                                  QRandomGenerator::global()->bounded(256),
174                                                  QRandomGenerator::global()->bounded(256)));
175     m_glWidgets << w;
176     connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
177     m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
178     if (m_nextX == 3) {
179         m_nextX = 1;
180         ++m_nextY;
181     } else {
182         ++m_nextX;
183     }
184 }
185 
timerUsageChanged(bool enabled)186 void MainWindow::timerUsageChanged(bool enabled)
187 {
188     if (enabled) {
189         m_timer->start();
190     } else {
191         m_timer->stop();
192         foreach (QOpenGLWidget *w, m_glWidgets)
193             w->update();
194     }
195 }
196 
resizeEvent(QResizeEvent *)197 void MainWindow::resizeEvent(QResizeEvent *)
198 {
199      m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
200 }
201