diff options
Diffstat (limited to 'examples/opengl/hellogl2/window.cpp')
-rw-r--r-- | examples/opengl/hellogl2/window.cpp | 131 |
1 files changed, 50 insertions, 81 deletions
diff --git a/examples/opengl/hellogl2/window.cpp b/examples/opengl/hellogl2/window.cpp index 59570c83ca..5a7ddf826f 100644 --- a/examples/opengl/hellogl2/window.cpp +++ b/examples/opengl/hellogl2/window.cpp @@ -1,56 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** 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: -** -** "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. -** * 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. -** -** -** 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -#include "glwidget.h" #include "window.h" -#include "mainwindow.h" +#include "glwidget.h" #include <QSlider> #include <QVBoxLayout> #include <QHBoxLayout> @@ -58,9 +10,18 @@ #include <QPushButton> #include <QApplication> #include <QMessageBox> +#include <QMainWindow> -Window::Window(MainWindow *mw) - : mainWindow(mw) +static QMainWindow *findMainWindow() +{ + for (auto *w : QApplication::topLevelWidgets()) { + if (auto *mw = qobject_cast<QMainWindow *>(w)) + return mw; + } + return nullptr; +} + +Window::Window() { glWidget = new GLWidget; @@ -75,22 +36,19 @@ Window::Window(MainWindow *mw) connect(zSlider, &QSlider::valueChanged, glWidget, &GLWidget::setZRotation); connect(glWidget, &GLWidget::zRotationChanged, zSlider, &QSlider::setValue); - QVBoxLayout *mainLayout = new QVBoxLayout; - QHBoxLayout *container = new QHBoxLayout; + QVBoxLayout *mainLayout = new QVBoxLayout(this); + QWidget *w = new QWidget; + QHBoxLayout *container = new QHBoxLayout(w); container->addWidget(glWidget); container->addWidget(xSlider); container->addWidget(ySlider); container->addWidget(zSlider); - QWidget *w = new QWidget; - w->setLayout(container); mainLayout->addWidget(w); dockBtn = new QPushButton(tr("Undock"), this); connect(dockBtn, &QPushButton::clicked, this, &Window::dockUndock); mainLayout->addWidget(dockBtn); - setLayout(mainLayout); - xSlider->setValue(15 * 16); ySlider->setValue(345 * 16); zSlider->setValue(0 * 16); @@ -111,7 +69,7 @@ QSlider *Window::createSlider() void Window::keyPressEvent(QKeyEvent *e) { - if (e->key() == Qt::Key_Escape) + if (isWindow() && e->key() == Qt::Key_Escape) close(); else QWidget::keyPressEvent(e); @@ -119,26 +77,37 @@ void Window::keyPressEvent(QKeyEvent *e) void Window::dockUndock() { - if (parent()) { - setParent(nullptr); - setAttribute(Qt::WA_DeleteOnClose); - move(QGuiApplication::primaryScreen()->size().width() / 2 - width() / 2, - QGuiApplication::primaryScreen()->size().height() / 2 - height() / 2); - dockBtn->setText(tr("Dock")); - show(); - } else { - if (!mainWindow->centralWidget()) { - if (mainWindow->isVisible()) { - setAttribute(Qt::WA_DeleteOnClose, false); - dockBtn->setText(tr("Undock")); - mainWindow->setCentralWidget(this); - } else { - QMessageBox::information(nullptr, tr("Cannot dock"), - tr("Main window already closed")); - } - } else { - QMessageBox::information(nullptr, tr("Cannot dock"), - tr("Main window already occupied")); - } + if (parent()) + undock(); + else + dock(); +} + +void Window::dock() +{ + auto *mainWindow = findMainWindow(); + if (mainWindow == nullptr || !mainWindow->isVisible()) { + QMessageBox::information(this, tr("Cannot Dock"), + tr("Main window already closed")); + return; } + if (mainWindow->centralWidget()) { + QMessageBox::information(this, tr("Cannot Dock"), + tr("Main window already occupied")); + return; + } + setAttribute(Qt::WA_DeleteOnClose, false); + dockBtn->setText(tr("Undock")); + mainWindow->setCentralWidget(this); +} + +void Window::undock() +{ + setParent(nullptr); + setAttribute(Qt::WA_DeleteOnClose); + const auto geometry = screen()->availableGeometry(); + move(geometry.x() + (geometry.width() - width()) / 2, + geometry.y() + (geometry.height() - height()) / 2); + dockBtn->setText(tr("Dock")); + show(); } |