| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include <QApplication> |
| 5 |
#include <QVBoxLayout> |
| 6 |
#include <QHBoxLayout> |
| 7 |
#include <QSlider> |
| 8 |
#include <QTextEdit> |
| 9 |
#include <QPushButton> |
| 10 |
#include <QLabel> |
| 11 |
#include <QCheckBox> |
| 12 |
#include <QFileDialog> |
| 13 |
#include <QFontInfo> |
| 14 |
#include <QMouseEvent> |
| 15 |
#include <QDrag> |
| 16 |
#include <QMimeData> |
| 17 |
#include "examplewidget.h" |
| 18 |
|
| 19 |
int main(int argc, char **argv) |
| 20 |
{ |
| 21 |
QApplication app(argc, argv); |
| 22 |
|
| 23 |
QVBoxLayout *layout = new QVBoxLayout; |
| 24 |
|
| 25 |
ExampleRhiWidget *rhiWidget = new ExampleRhiWidget; |
| 26 |
QLabel *overlayLabel = new QLabel(rhiWidget); |
| 27 |
overlayLabel->setText(QObject::tr("This is a\nsemi-transparent\n overlay widget\n" |
| 28 |
"placed on top of\nthe QRhiWidget.")); |
| 29 |
overlayLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); |
| 30 |
overlayLabel->setAutoFillBackground(true); |
| 31 |
QPalette semiTransparent(QColor(255, 0, 0, 64)); |
| 32 |
semiTransparent.setBrush(QPalette::Text, Qt::white); |
| 33 |
semiTransparent.setBrush(QPalette::WindowText, Qt::white); |
| 34 |
overlayLabel->setPalette(semiTransparent); |
| 35 |
QFont f = overlayLabel->font(); |
| 36 |
f.setPixelSize(QFontInfo(f).pixelSize() * 2); |
| 37 |
f.setWeight(QFont::Bold); |
| 38 |
overlayLabel->setFont(f); |
| 39 |
overlayLabel->resize(320, 320); |
| 40 |
overlayLabel->hide(); |
| 41 |
QObject::connect(rhiWidget, &ExampleRhiWidget::resized, rhiWidget, [rhiWidget, overlayLabel] { |
| 42 |
const int w = overlayLabel->width(); |
| 43 |
const int h = overlayLabel->height(); |
| 44 |
overlayLabel->setGeometry(rhiWidget->width() / 2 - w / 2, rhiWidget->height() / 2 - h / 2, w, h); |
| 45 |
}); |
| 46 |
|
| 47 |
QTextEdit *edit = new QTextEdit(QObject::tr("QRhiWidget!<br><br>" |
| 48 |
"The cube is textured with QPainter-generated content.<br><br>" |
| 49 |
"Regular, non-native widgets on top work just fine.")); |
| 50 |
QObject::connect(edit, &QTextEdit::textChanged, edit, [edit, rhiWidget] { |
| 51 |
rhiWidget->setCubeTextureText(edit->toPlainText()); |
| 52 |
}); |
| 53 |
edit->setMaximumHeight(100); |
| 54 |
layout->addWidget(edit); |
| 55 |
|
| 56 |
QSlider *slider = new QSlider(Qt::Horizontal); |
| 57 |
slider->setMinimum(0); |
| 58 |
slider->setMaximum(360); |
| 59 |
QObject::connect(slider, &QSlider::valueChanged, slider, [slider, rhiWidget] { |
| 60 |
rhiWidget->setCubeRotation(slider->value()); |
| 61 |
}); |
| 62 |
|
| 63 |
QHBoxLayout *sliderLayout = new QHBoxLayout; |
| 64 |
sliderLayout->addWidget(new QLabel(QObject::tr("Cube rotation"))); |
| 65 |
sliderLayout->addWidget(slider); |
| 66 |
layout->addLayout(sliderLayout); |
| 67 |
|
| 68 |
QHBoxLayout *btnLayout = new QHBoxLayout; |
| 69 |
|
| 70 |
QLabel *apiLabel = new QLabel; |
| 71 |
btnLayout->addWidget(apiLabel); |
| 72 |
QObject::connect(rhiWidget, &ExampleRhiWidget::rhiChanged, rhiWidget, [apiLabel](const QString &apiName) { |
| 73 |
apiLabel->setText(QObject::tr("Using QRhi on ") + apiName); |
| 74 |
}); |
| 75 |
|
| 76 |
QPushButton *btnMakeWindow = new QPushButton(QObject::tr("Make top-level window")); |
| 77 |
QObject::connect(btnMakeWindow, &QPushButton::clicked, btnMakeWindow, [rhiWidget, btnMakeWindow, layout] { |
| 78 |
if (rhiWidget->parentWidget()) { |
| 79 |
rhiWidget->setParent(nullptr); |
| 80 |
rhiWidget->setAttribute(Qt::WA_DeleteOnClose, true); |
| 81 |
rhiWidget->show(); |
| 82 |
btnMakeWindow->setText(QObject::tr("Make child widget")); |
| 83 |
} else { |
| 84 |
rhiWidget->setAttribute(Qt::WA_DeleteOnClose, false); |
| 85 |
layout->addWidget(rhiWidget); |
| 86 |
btnMakeWindow->setText(QObject::tr("Make top-level window")); |
| 87 |
} |
| 88 |
}); |
| 89 |
btnLayout->addWidget(btnMakeWindow); |
| 90 |
|
| 91 |
QPushButton *btn = new QPushButton(QObject::tr("Grab to image")); |
| 92 |
QObject::connect(btn, &QPushButton::clicked, btn, [rhiWidget] { |
| 93 |
QImage image = rhiWidget->grabFramebuffer(); |
| 94 |
qDebug() << "Got image" << image; |
| 95 |
if (!image.isNull()) { |
| 96 |
QFileDialog fd(rhiWidget->parentWidget()); |
| 97 |
fd.setAcceptMode(QFileDialog::AcceptSave); |
| 98 |
fd.setDefaultSuffix("png"); |
| 99 |
fd.selectFile("test.png"); |
| 100 |
if (fd.exec() == QDialog::Accepted) |
| 101 |
image.save(fd.selectedFiles().first()); |
| 102 |
} |
| 103 |
}); |
| 104 |
btnLayout->addWidget(btn); |
| 105 |
|
| 106 |
QCheckBox *cbMsaa = new QCheckBox(QObject::tr("Use 4x MSAA")); |
| 107 |
QObject::connect(cbMsaa, &QCheckBox::checkStateChanged, cbMsaa, [cbMsaa, rhiWidget] { |
| 108 |
if (cbMsaa->isChecked()) |
| 109 |
rhiWidget->setSampleCount(4); |
| 110 |
else |
| 111 |
rhiWidget->setSampleCount(1); |
| 112 |
}); |
| 113 |
btnLayout->addWidget(cbMsaa); |
| 114 |
|
| 115 |
QCheckBox *cbOvberlay = new QCheckBox(QObject::tr("Show overlay widget")); |
| 116 |
QObject::connect(cbOvberlay, &QCheckBox::checkStateChanged, cbOvberlay, [cbOvberlay, overlayLabel] { |
| 117 |
if (cbOvberlay->isChecked()) |
| 118 |
overlayLabel->setVisible(true); |
| 119 |
else |
| 120 |
overlayLabel->setVisible(false); |
| 121 |
}); |
| 122 |
btnLayout->addWidget(cbOvberlay); |
| 123 |
|
| 124 |
QCheckBox *cbFlip = new QCheckBox(QObject::tr("Flip")); |
| 125 |
QObject::connect(cbFlip, &QCheckBox::checkStateChanged, cbOvberlay, [cbFlip, rhiWidget] { |
| 126 |
rhiWidget->setMirrorVertically(cbFlip->isChecked()); |
| 127 |
}); |
| 128 |
btnLayout->addWidget(cbFlip); |
| 129 |
|
| 130 |
QCheckBox *cbFixedSize = new QCheckBox(QObject::tr("Use fixed color buffer size")); |
| 131 |
btnLayout->addWidget(cbFixedSize); |
| 132 |
QSlider *fixedSizeSlider = new QSlider(Qt::Horizontal); |
| 133 |
fixedSizeSlider->setMinimum(16); |
| 134 |
fixedSizeSlider->setMaximum(512); |
| 135 |
btnLayout->addWidget(fixedSizeSlider); |
| 136 |
|
| 137 |
QObject::connect(cbFixedSize, &QCheckBox::checkStateChanged, cbFixedSize, [cbFixedSize, fixedSizeSlider, rhiWidget] { |
| 138 |
if (cbFixedSize->isChecked()) |
| 139 |
rhiWidget->setFixedColorBufferSize(QSize(fixedSizeSlider->value(), fixedSizeSlider->value())); |
| 140 |
else |
| 141 |
rhiWidget->setFixedColorBufferSize(QSize()); |
| 142 |
}); |
| 143 |
QObject::connect(fixedSizeSlider, &QSlider::valueChanged, fixedSizeSlider, [fixedSizeSlider, cbFixedSize, rhiWidget] { |
| 144 |
if (cbFixedSize->isChecked()) |
| 145 |
rhiWidget->setFixedColorBufferSize(QSize(fixedSizeSlider->value(), fixedSizeSlider->value())); |
| 146 |
}); |
| 147 |
|
| 148 |
// Exit when the detached window is closed; there is not much we can do |
| 149 |
// with the controls in the main window then. |
| 150 |
QObject::connect(rhiWidget, &QObject::destroyed, rhiWidget, [rhiWidget] { |
| 151 |
if (!rhiWidget->parentWidget()) |
| 152 |
qGuiApp->quit(); |
| 153 |
}); |
| 154 |
|
| 155 |
layout->addLayout(btnLayout); |
| 156 |
layout->addWidget(rhiWidget); |
| 157 |
|
| 158 |
rhiWidget->setCubeTextureText(edit->toPlainText()); |
| 159 |
|
| 160 |
QWidget w; |
| 161 |
w.setLayout(layout); |
| 162 |
w.resize(1280, 720); |
| 163 |
w.show(); |
| 164 |
|
| 165 |
return app.exec(); |
| 166 |
} |