Debian Codegraph @

tests/manual/examples/opengl/qopenglwidget/mainwindow.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "mainwindow.h"
5
6 #include <QApplication>
7 #include <QMenuBar>
8 #include <QGroupBox>
9 #include <QSlider>
10 #include <QLabel>
11 #include <QCheckBox>
12 #include <QRandomGenerator>
13 #include <QSpinBox>
14 #include <QScrollArea>
15 #include <QTabWidget>
16 #include <QTabBar>
17 #include <QToolButton>
18
19 #include "glwidget.h"
20
21 MainWindow::MainWindow()
22 : m_nextX(1), m_nextY(1)
23 {
24 GLWidget *glwidget = new GLWidget(this, qRgb(20, 20, 50));
25 m_glWidgets << glwidget;
26 QLabel *label = new QLabel(this);
27 m_timer = new QTimer(this);
28 QSlider *slider = new QSlider(this);
29 slider->setOrientation(Qt::Horizontal);
30
31 QLabel *updateLabel = new QLabel("Update interval");
32 QSpinBox *updateInterval = new QSpinBox(this);
33 updateInterval->setSuffix(" ms");
34 updateInterval->setValue(10);
35 updateInterval->setToolTip("Interval for the timer that calls update().\n"
36 "Note that on most systems the swap will block to wait for vsync\n"
37 "and therefore an interval < 16 ms will likely lead to a 60 FPS update rate.");
38 QGroupBox *updateGroupBox = new QGroupBox(this);
39 QCheckBox *timerBased = new QCheckBox("Use timer", this);
40 timerBased->setChecked(false);
41 timerBased->setToolTip("Toggles using a timer to trigger update().\n"
42 "When not set, each paintGL() schedules the next update immediately,\n"
43 "expecting the blocking swap to throttle the thread.\n"
44 "This shows how unnecessary the timer is in most cases.");
45 QCheckBox *transparent = new QCheckBox("Transparent background", this);
46 transparent->setToolTip("Toggles Qt::WA_AlwaysStackOnTop and transparent clear color for glClear().\n"
47 "Note how the button on top stacks incorrectly when enabling this.");
48 QHBoxLayout *updateLayout = new QHBoxLayout;
49 updateLayout->addWidget(updateLabel);
50 updateLayout->addWidget(updateInterval);
51 updateLayout->addWidget(timerBased);
52 updateLayout->addWidget(transparent);
53 updateGroupBox->setLayout(updateLayout);
54
55 slider->setRange(0, 50);
56 slider->setSliderPosition(30);
57 m_timer->setInterval(10);
58 label->setText("A scrollable QOpenGLWidget");
59 label->setAlignment(Qt::AlignHCenter);
60
61 QGroupBox * groupBox = new QGroupBox(this);
62 setCentralWidget(groupBox);
63 groupBox->setTitle("QOpenGLWidget Example");
64
65 m_layout = new QGridLayout(groupBox);
66
67 QScrollArea *scrollArea = new QScrollArea;
68 scrollArea->setWidget(glwidget);
69
70 m_layout->addWidget(scrollArea,1,0,8,1);
71 m_layout->addWidget(label,9,0,1,1);
72 m_layout->addWidget(updateGroupBox, 10, 0, 1, 1);
73 m_layout->addWidget(slider, 11,0,1,1);
74
75 groupBox->setLayout(m_layout);
76
77
78 QMenu *fileMenu = menuBar()->addMenu("&File");
79 fileMenu->addAction("E&xit", this, &QWidget::close);
80 QMenu *showMenu = menuBar()->addMenu("&Show");
81 showMenu->addAction("Show 3D Logo", glwidget, &GLWidget::setLogo);
82 showMenu->addAction("Show 2D Texture", glwidget, &GLWidget::setTexture);
83 QAction *showBubbles = showMenu->addAction("Show bubbles", glwidget, &GLWidget::setShowBubbles);
84 showBubbles->setCheckable(true);
85 showBubbles->setChecked(true);
86 showMenu->addAction("Open tab window", this, &MainWindow::showNewWindow);
87 QMenu *helpMenu = menuBar()->addMenu("&Help");
88 helpMenu->addAction("About Qt", qApp, &QApplication::aboutQt);
89
90 connect(m_timer, &QTimer::timeout, glwidget, QOverload<>::of(&QWidget::update));
91
92 connect(slider, &QAbstractSlider::valueChanged, glwidget, &GLWidget::setScaling);
93 connect(transparent, &QCheckBox::toggled, glwidget, &GLWidget::setTransparent);
94 connect(updateInterval, &QSpinBox::valueChanged,
95 this, &MainWindow::updateIntervalChanged);
96 connect(timerBased, &QCheckBox::toggled, this, &MainWindow::timerUsageChanged);
97 connect(timerBased, &QCheckBox::toggled, updateInterval, &QWidget::setEnabled);
98
99 if (timerBased->isChecked())
100 m_timer->start();
101 else
102 updateInterval->setEnabled(false);
103 }
104
105 void MainWindow::updateIntervalChanged(int value)
106 {
107 m_timer->setInterval(value);
108 if (m_timer->isActive())
109 m_timer->start();
110 }
111
112 void MainWindow::addNew()
113 {
114 if (m_nextY == 4)
115 return;
116 GLWidget *w = new GLWidget(nullptr, qRgb(QRandomGenerator::global()->bounded(256),
117 QRandomGenerator::global()->bounded(256),
118 QRandomGenerator::global()->bounded(256)));
119 m_glWidgets << w;
120 connect(m_timer, &QTimer::timeout, w, QOverload<>::of(&QWidget::update));
121 m_layout->addWidget(w, m_nextY, m_nextX, 1, 1);
122 if (m_nextX == 3) {
123 m_nextX = 1;
124 ++m_nextY;
125 } else {
126 ++m_nextX;
127 }
128 }
129
130 void MainWindow::timerUsageChanged(bool enabled)
131 {
132 if (enabled) {
133 m_timer->start();
134 } else {
135 m_timer->stop();
136 for (QOpenGLWidget *w : std::as_const(m_glWidgets))
137 w->update();
138 }
139 }
140
141 void MainWindow::resizeEvent(QResizeEvent *)
142 {
143 m_glWidgets[0]->setMinimumSize(size() + QSize(128, 128));
144 }
145
146 void MainWindow::showNewWindow()
147 {
148 QTabWidget *tabs = new QTabWidget;
149 tabs->resize(800, 600);
150
151 QToolButton *tb = new QToolButton;
152 tb->setText(QLatin1String("+"));
153 tabs->addTab(new QLabel(QLatin1String("Add OpenGL widgets with +")), QString());
154 tabs->setTabEnabled(0, false);
155 tabs->tabBar()->setTabButton(0, QTabBar::RightSide, tb);
156 tabs->tabBar()->setTabsClosable(true);
157 QObject::connect(tabs->tabBar(), &QTabBar::tabCloseRequested, tabs, [tabs](int index) {
158 tabs->widget(index)->deleteLater();
159 });
160
161 const QString msgToTopLevel = QLatin1String("Break out to top-level window");
162 const QString msgFromTopLevel = QLatin1String("Move back under tab widget");
163
164 QObject::connect(tb, &QAbstractButton::clicked, tabs, [=] {
165 GLWidget *glwidget = new GLWidget(nullptr, Qt::blue);
166 glwidget->resize(tabs->size());
167 glwidget->setWindowTitle(QString::asprintf("QOpenGLWidget %p", glwidget));
168
169 QPushButton *btn = new QPushButton(msgToTopLevel, glwidget);
170 connect(btn, &QPushButton::clicked, glwidget, [=] {
171 if (glwidget->parent()) {
172 glwidget->setAttribute(Qt::WA_DeleteOnClose, true);
173 glwidget->setParent(nullptr);
174 glwidget->show();
175 btn->setText(msgFromTopLevel);
176 } else {
177 glwidget->setAttribute(Qt::WA_DeleteOnClose, false);
178 tabs->addTab(glwidget, glwidget->windowTitle());
179 btn->setText(msgToTopLevel);
180 }
181 });
182
183 tabs->setCurrentIndex(tabs->addTab(glwidget, glwidget->windowTitle()));
184 });
185
186 tabs->setAttribute(Qt::WA_DeleteOnClose);
187 tabs->show();
188 }