| 1 |
// Copyright (C) 2021 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QApplication> |
| 5 |
#include <QVBoxLayout> |
| 6 |
#include <QHBoxLayout> |
| 7 |
#include <QSlider> |
| 8 |
#include <QLineEdit> |
| 9 |
#include <QPushButton> |
| 10 |
#include <QLabel> |
| 11 |
#include <QCheckBox> |
| 12 |
#include <QFileDialog> |
| 13 |
#include "examplewidget.h" |
| 14 |
|
| 15 |
static const bool TEST_OFFSCREEN_GRAB = false; |
| 16 |
|
| 17 |
int main(int argc, char **argv) |
| 18 |
{ |
| 19 |
qputenv("QSG_INFO", "1"); |
| 20 |
QApplication app(argc, argv); |
| 21 |
|
| 22 |
QVBoxLayout *layout = new QVBoxLayout; |
| 23 |
|
| 24 |
QLineEdit *edit = new QLineEdit(QLatin1String("Text on cube")); |
| 25 |
QSlider *slider = new QSlider(Qt::Horizontal); |
| 26 |
ExampleRhiWidget *rw = new ExampleRhiWidget; |
| 27 |
|
| 28 |
QObject::connect(edit, &QLineEdit::textChanged, edit, [edit, rw] { |
| 29 |
rw->setCubeTextureText(edit->text()); |
| 30 |
}); |
| 31 |
|
| 32 |
slider->setMinimum(0); |
| 33 |
slider->setMaximum(360); |
| 34 |
QObject::connect(slider, &QSlider::valueChanged, slider, [slider, rw] { |
| 35 |
rw->setCubeRotation(slider->value()); |
| 36 |
}); |
| 37 |
|
| 38 |
QPushButton *btn = new QPushButton(QLatin1String("Grab to image")); |
| 39 |
QObject::connect(btn, &QPushButton::clicked, btn, [rw] { |
| 40 |
QImage image = rw->grabTexture(); |
| 41 |
qDebug() << image; |
| 42 |
if (!image.isNull()) { |
| 43 |
QFileDialog fd(rw->parentWidget()); |
| 44 |
fd.setAcceptMode(QFileDialog::AcceptSave); |
| 45 |
fd.setDefaultSuffix("png"); |
| 46 |
fd.selectFile("test.png"); |
| 47 |
if (fd.exec() == QDialog::Accepted) |
| 48 |
image.save(fd.selectedFiles().first()); |
| 49 |
} |
| 50 |
}); |
| 51 |
QHBoxLayout *btnLayout = new QHBoxLayout; |
| 52 |
btnLayout->addWidget(btn); |
| 53 |
QCheckBox *cbExplicitSize = new QCheckBox(QLatin1String("Use explicit size")); |
| 54 |
QObject::connect(cbExplicitSize, &QCheckBox::stateChanged, cbExplicitSize, [cbExplicitSize, rw] { |
| 55 |
if (cbExplicitSize->isChecked()) |
| 56 |
rw->setExplicitSize(QSize(128, 128)); |
| 57 |
else |
| 58 |
rw->setExplicitSize(QSize()); |
| 59 |
}); |
| 60 |
btnLayout->addWidget(cbExplicitSize); |
| 61 |
QPushButton *btnMakeWindow = new QPushButton(QLatin1String("Make top-level window")); |
| 62 |
QObject::connect(btnMakeWindow, &QPushButton::clicked, btnMakeWindow, [rw, btnMakeWindow, layout] { |
| 63 |
if (rw->parentWidget()) { |
| 64 |
rw->setParent(nullptr); |
| 65 |
rw->setAttribute(Qt::WA_DeleteOnClose, true); |
| 66 |
rw->show(); |
| 67 |
btnMakeWindow->setText(QLatin1String("Make child widget")); |
| 68 |
} else { |
| 69 |
rw->setAttribute(Qt::WA_DeleteOnClose, false); |
| 70 |
layout->addWidget(rw); |
| 71 |
btnMakeWindow->setText(QLatin1String("Make top-level window")); |
| 72 |
} |
| 73 |
}); |
| 74 |
btnLayout->addWidget(btnMakeWindow); |
| 75 |
|
| 76 |
layout->addWidget(edit); |
| 77 |
QHBoxLayout *sliderLayout = new QHBoxLayout; |
| 78 |
sliderLayout->addWidget(new QLabel(QLatin1String("Cube rotation"))); |
| 79 |
sliderLayout->addWidget(slider); |
| 80 |
layout->addLayout(sliderLayout); |
| 81 |
layout->addLayout(btnLayout); |
| 82 |
layout->addWidget(rw); |
| 83 |
|
| 84 |
rw->setCubeTextureText(edit->text()); |
| 85 |
|
| 86 |
if (TEST_OFFSCREEN_GRAB) { |
| 87 |
rw->resize(320, 200); |
| 88 |
rw->grabTexture().save("offscreen_grab.png"); |
| 89 |
} |
| 90 |
|
| 91 |
QWidget w; |
| 92 |
w.setLayout(layout); |
| 93 |
w.resize(1280, 720); |
| 94 |
w.show(); |
| 95 |
|
| 96 |
return app.exec(); |
| 97 |
} |