Debian Codegraph @

tests/manual/qopenglwidget/openglwidget/main.cpp

raw
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 test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "openglwidget.h"
30 #include <QApplication>
31 #include <QPushButton>
32 #include <QMdiArea>
33 #include <QMdiSubWindow>
34 #include <QMenu>
35 #include <QMenuBar>
36 #include <QMainWindow>
37 #include <QLCDNumber>
38 #include <QScrollArea>
39 #include <QScrollBar>
40 #include <QTabWidget>
41 #include <QLabel>
42 #include <QTimer>
43 #include <QSurfaceFormat>
44 #include <QDebug>
45 #include <private/qwindow_p.h>
46
47 class Tools : public QObject
48 {
49 Q_OBJECT
50
51 public:
52 Tools(QWidget *root, QWidget *widgetToTurn, const QVector<QWidget *> glwidgets)
53 : m_root(root), m_widgetToTurn(widgetToTurn), m_glWidgets(glwidgets) { }
54 void dump();
55
56 private slots:
57 void turnNative();
58 void hideShowAllGL();
59 void dumpCompositingStatus();
60
61 signals:
62 void aboutToShowGLWidgets();
63
64 private:
65 void dumpWidget(QWidget *w, int indent = 0);
66
67 QWidget *m_root;
68 QWidget *m_widgetToTurn;
69 QVector<QWidget *> m_glWidgets;
70 };
71
72 void Tools::turnNative()
73 {
74 qDebug("Turning into native");
75 m_widgetToTurn->winId();
76 dump();
77 }
78
79 void Tools::hideShowAllGL()
80 {
81 if (m_glWidgets[0]->isVisible()) {
82 qDebug("Hiding all render-to-texture widgets");
83 foreach (QWidget *w, m_glWidgets)
84 w->hide();
85 } else {
86 qDebug("Showing all render-to-texture widgets");
87 emit aboutToShowGLWidgets();
88 foreach (QWidget *w, m_glWidgets)
89 w->show();
90 }
91 }
92
93 void Tools::dump()
94 {
95 qDebug() << "Widget hierarchy";
96 dumpWidget(m_root);
97 qDebug() << "========";
98 }
99
100 void Tools::dumpWidget(QWidget *w, int indent)
101 {
102 QString indentStr;
103 indentStr.fill(' ', indent);
104 qDebug().noquote() << indentStr << w << "winId =" << w->internalWinId();
105 foreach (QObject *obj, w->children()) {
106 if (QWidget *cw = qobject_cast<QWidget *>(obj))
107 dumpWidget(cw, indent + 4);
108 }
109 }
110
111 void Tools::dumpCompositingStatus()
112 {
113 QWindow *w = m_root->window()->windowHandle();
114 qDebug() << "Compositing status for" << w << m_root->window() << "is" << QWindowPrivate::get(w)->compositing;
115 }
116
117 class TabWidgetResetter : public QObject
118 {
119 Q_OBJECT
120 public:
121 TabWidgetResetter(QTabWidget *tw) : m_tw(tw) { }
122 public slots:
123 void reset() { m_tw->setCurrentIndex(0); }
124 private:
125 QTabWidget *m_tw;
126 };
127
128 int main(int argc, char *argv[])
129 {
130 if (argc > 1 && !strcmp(argv[1], "--sharecontext")) {
131 qDebug("Requesting all contexts to share");
132 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
133 }
134
135 QApplication a(argc, argv);
136
137 QSurfaceFormat format;
138 if (QCoreApplication::arguments().contains(QLatin1String("--multisample")))
139 format.setSamples(4);
140 if (QCoreApplication::arguments().contains(QLatin1String("--coreprofile"))) {
141 format.setVersion(3, 2);
142 format.setProfile(QSurfaceFormat::CoreProfile);
143 }
144 qDebug() << "Requesting" << format;
145
146 QMainWindow wnd;
147 wnd.setObjectName("Main Window");
148 wnd.resize(1024, 768);
149
150 QMdiArea *w = new QMdiArea;
151 w->setObjectName("MDI area");
152 w->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
153 wnd.setCentralWidget(w);
154
155 OpenGLWidget *glw = new OpenGLWidget(33, QVector3D(0, 0, 1));
156 glw->setObjectName("First GL Widget with 33 ms timer");
157 glw->setFormat(format);
158 glw->setMinimumSize(100, 100);
159 QMdiSubWindow *sw = w->addSubWindow(glw);
160 sw->setObjectName("First MDI Sub-Window");
161 sw->setWindowTitle("33 ms timer");
162
163 OpenGLWidget *glw2 = new OpenGLWidget(16);
164 glw2->setObjectName("Second GL Widget with 16 ms timer");
165 glw2->setFormat(format);
166 glw2->setMinimumSize(100, 100);
167 QOpenGLWidget *glw22 = new OpenGLWidget(16);
168 glw22->setObjectName("Second #2 GLWidget");
169 glw22->setParent(glw2);
170 glw22->resize(40, 40);
171 sw = w->addSubWindow(glw2);
172 sw->setObjectName("Second MDI Sub-Window");
173 sw->setWindowTitle("16 ms timer");
174
175 OpenGLWidget *glw3 = new OpenGLWidget(0); // trigger updates continuously, no timer
176 glw3->setObjectName("GL widget in scroll area (possibly native)");
177 glw3->setFormat(format);
178 glw3->setFixedSize(600, 600);
179 OpenGLWidget *glw3child = new OpenGLWidget(0);
180 const float glw3ClearColor[] = { 0.5f, 0.2f, 0.8f };
181 glw3child->setClearColor(glw3ClearColor);
182 glw3child->setObjectName("Child widget of GL Widget in scroll area");
183 glw3child->setFormat(format);
184 glw3child->setParent(glw3);
185 glw3child->setGeometry(500, 500, 100, 100); // lower right corner of parent
186 QScrollArea *sa = new QScrollArea;
187 sa->setWidget(glw3);
188 sa->setMinimumSize(100, 100);
189 sa->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
190 sw = w->addSubWindow(sa);
191 sw->setObjectName("MDI Sub-Window for scroll area");
192 sw->setWindowTitle("Cont. update");
193 sw->resize(300, 300);
194 sa->verticalScrollBar()->setValue(300);
195
196 QLCDNumber *lcd = new QLCDNumber;
197 lcd->display(1337);
198 lcd->setMinimumSize(300, 100);
199 sw = w->addSubWindow(lcd);
200 sw->setObjectName("MDI Sub-Window for LCD widget");
201 sw->setWindowTitle("Ordinary widget");
202
203 QTabWidget *tw = new QTabWidget;
204 QOpenGLWidget *glw4 = new OpenGLWidget(16, QVector3D(1, 0, 0));
205 glw4->setObjectName("GL widget in tab widget");
206 tw->addTab(glw4, "OpenGL");
207 QLabel *label = new QLabel("Another tab");
208 tw->addTab(label, "Not OpenGL");
209 tw->setMinimumSize(100, 100);
210 sw = w->addSubWindow(tw);
211 sw->setObjectName("MDI Sub-Window for tab widget");
212 sw->setWindowTitle("Tabs");
213
214 TabWidgetResetter twr(tw);
215 Tools t(&wnd, glw3, QVector<QWidget *>() << glw << glw2 << glw3 << glw4);
216 QObject::connect(&t, SIGNAL(aboutToShowGLWidgets()), &twr, SLOT(reset()));
217 QMenu *toolsMenu = wnd.menuBar()->addMenu("&Tools");
218 toolsMenu->addAction("&Turn widgets (or some parent) into native", &t, SLOT(turnNative()));
219 toolsMenu->addAction("&Hide/show all OpenGL widgets", &t, SLOT(hideShowAllGL()));
220
221 QTimer compStatusDumpTimer;
222 QObject::connect(&compStatusDumpTimer, SIGNAL(timeout()), &t, SLOT(dumpCompositingStatus()));
223 compStatusDumpTimer.start(5000);
224
225 wnd.show();
226
227 if (glw->isValid())
228 qDebug() << "Got" << glw->format();
229
230 t.dump();
231
232 return a.exec();
233 }
234
235 #include "main.moc"